|
| 1 | +/** |
| 2 | + * TypeScript type definitions for safe-template-parser |
| 3 | + * A safe template string parser for JavaScript |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Token types used by the Lexer |
| 8 | + */ |
| 9 | +export const TokenType: { |
| 10 | + readonly NUMBER: symbol; |
| 11 | + readonly IDENTIFIER: symbol; |
| 12 | + readonly OPERATOR: symbol; |
| 13 | + readonly LEFT_PAREN: symbol; |
| 14 | + readonly RIGHT_PAREN: symbol; |
| 15 | + readonly LEFT_BRACKET: symbol; |
| 16 | + readonly RIGHT_BRACKET: symbol; |
| 17 | + readonly LEFT_BRACE: symbol; |
| 18 | + readonly RIGHT_BRACE: symbol; |
| 19 | + readonly COLON: symbol; |
| 20 | + readonly COMMA: symbol; |
| 21 | + readonly DOT: symbol; |
| 22 | + readonly STRING: symbol; |
| 23 | + readonly BOOLEAN: symbol; |
| 24 | + readonly NULL: symbol; |
| 25 | + readonly EOF: symbol; |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * AST node types used by the Parser |
| 30 | + */ |
| 31 | +export const ASTNodeType: { |
| 32 | + readonly NUMBER: symbol; |
| 33 | + readonly VARIABLE: symbol; |
| 34 | + readonly BINARY_OP: symbol; |
| 35 | + readonly UNARY_OP: symbol; |
| 36 | + readonly FUNCTION_CALL: symbol; |
| 37 | + readonly ARRAY_ACCESS: symbol; |
| 38 | + readonly PROPERTY_ACCESS: symbol; |
| 39 | + readonly TERNARY_OP: symbol; |
| 40 | + readonly ARRAY_LITERAL: symbol; |
| 41 | + readonly OBJECT_LITERAL: symbol; |
| 42 | + readonly NULL: symbol; |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * Represents a single token produced by the Lexer |
| 47 | + */ |
| 48 | +export class Token { |
| 49 | + type: symbol; |
| 50 | + value: string | number | boolean | null; |
| 51 | + constructor(type: symbol, value: string | number | boolean | null); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * AST Node interface representing parsed expression tree nodes |
| 56 | + */ |
| 57 | +export interface ASTNode { |
| 58 | + type: symbol; |
| 59 | + value?: string | number | boolean | null; |
| 60 | + left?: ASTNode; |
| 61 | + right?: ASTNode; |
| 62 | + operator?: string; |
| 63 | + name?: string; |
| 64 | + args?: ASTNode[]; |
| 65 | + object?: ASTNode; |
| 66 | + property?: string | ASTNode; |
| 67 | + condition?: ASTNode; |
| 68 | + trueExpr?: ASTNode; |
| 69 | + falseExpr?: ASTNode; |
| 70 | + elements?: ASTNode[]; |
| 71 | + properties?: Array<{ key: string; value: ASTNode }>; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Lexer class that tokenizes input strings into tokens |
| 76 | + */ |
| 77 | +export class Lexer { |
| 78 | + /** |
| 79 | + * Creates a new Lexer instance |
| 80 | + * @param input - The input string to tokenize |
| 81 | + */ |
| 82 | + constructor(input: string); |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns the next token from the input |
| 86 | + */ |
| 87 | + nextToken(): Token; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Parser class that converts tokens into an Abstract Syntax Tree (AST) |
| 92 | + */ |
| 93 | +export class Parser { |
| 94 | + /** |
| 95 | + * Creates a new Parser instance |
| 96 | + * @param lexer - The Lexer instance to read tokens from |
| 97 | + */ |
| 98 | + constructor(lexer: Lexer); |
| 99 | + |
| 100 | + /** |
| 101 | + * Parses the token stream and returns the root AST node |
| 102 | + */ |
| 103 | + parse(): ASTNode; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Type for allowed functions that can be used in templates |
| 108 | + */ |
| 109 | +export type AllowedFunctions = Record<string, (...args: unknown[]) => unknown>; |
| 110 | + |
| 111 | +/** |
| 112 | + * Type for template data object |
| 113 | + */ |
| 114 | +export type TemplateData = Record<string, unknown>; |
| 115 | + |
| 116 | +/** |
| 117 | + * Interpreter class that evaluates an AST with given data |
| 118 | + */ |
| 119 | +export class Interpreter { |
| 120 | + /** |
| 121 | + * Creates a new Interpreter instance |
| 122 | + * @param tree - The AST to interpret |
| 123 | + * @param data - The data object containing variable values |
| 124 | + * @param allowedFunctions - Optional map of allowed functions |
| 125 | + */ |
| 126 | + constructor( |
| 127 | + tree: ASTNode, |
| 128 | + data: TemplateData, |
| 129 | + allowedFunctions?: AllowedFunctions |
| 130 | + ); |
| 131 | + |
| 132 | + /** |
| 133 | + * Interprets the AST and returns the result |
| 134 | + */ |
| 135 | + interpret(): unknown; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Parses a template string and replaces expressions with evaluated values |
| 140 | + * |
| 141 | + * @param templateString - The template string containing {{expression}} placeholders |
| 142 | + * @param data - The data object containing variable values |
| 143 | + * @param allowedFunctions - Optional map of user-defined functions |
| 144 | + * @returns The template string with all expressions evaluated and replaced |
| 145 | + * |
| 146 | + * @example |
| 147 | + * ```typescript |
| 148 | + * import { parseTemplateString } from 'safe-template-parser'; |
| 149 | + * |
| 150 | + * const template = "Hello, {{name}}! You have {{count}} messages."; |
| 151 | + * const data = { name: "Alice", count: 5 }; |
| 152 | + * const result = parseTemplateString(template, data); |
| 153 | + * // Result: "Hello, Alice! You have 5 messages." |
| 154 | + * ``` |
| 155 | + * |
| 156 | + * @example |
| 157 | + * ```typescript |
| 158 | + * // With expressions |
| 159 | + * const template = "Total: {{price * quantity}}"; |
| 160 | + * const data = { price: 100, quantity: 3 }; |
| 161 | + * const result = parseTemplateString(template, data); |
| 162 | + * // Result: "Total: 300" |
| 163 | + * ``` |
| 164 | + * |
| 165 | + * @example |
| 166 | + * ```typescript |
| 167 | + * // With custom functions |
| 168 | + * const template = "Formatted: {{formatCurrency(amount)}}"; |
| 169 | + * const data = { amount: 1234.56 }; |
| 170 | + * const functions = { |
| 171 | + * formatCurrency: (n: number) => `$${n.toFixed(2)}` |
| 172 | + * }; |
| 173 | + * const result = parseTemplateString(template, data, functions); |
| 174 | + * // Result: "Formatted: $1234.56" |
| 175 | + * ``` |
| 176 | + */ |
| 177 | +export function parseTemplateString( |
| 178 | + templateString: string, |
| 179 | + data: TemplateData, |
| 180 | + allowedFunctions?: AllowedFunctions |
| 181 | +): string; |
| 182 | + |
| 183 | +export default parseTemplateString; |
0 commit comments