(input: string, options?: ParserOptions)
| 21 | }; |
| 22 | |
| 23 | export function parse(input: string, options?: ParserOptions): ASTNode { |
| 24 | let token: Token; |
| 25 | const lexer = createLexer(input, options); |
| 26 | const tokens: Array<Token> = []; |
| 27 | const tokenChunk: Array<Token> = []; |
| 28 | |
| 29 | // 允许的变量名字空间 |
| 30 | let variableNamespaces: Array<string> = options?.variableNamespaces ?? [ |
| 31 | 'window', |
| 32 | 'cookie', |
| 33 | 'ls', |
| 34 | 'ss' |
| 35 | ]; |
| 36 | if (!Array.isArray(variableNamespaces)) { |
| 37 | variableNamespaces = []; |
| 38 | } |
| 39 | |
| 40 | function next() { |
| 41 | token = tokenChunk.length ? tokenChunk.shift()! : lexer.next(); |
| 42 | |
| 43 | if (!token) { |
| 44 | throw new TypeError('next token is undefined'); |
| 45 | } |
| 46 | tokens.push(token); |
| 47 | } |
| 48 | |
| 49 | function back() { |
| 50 | tokenChunk.unshift(tokens.pop()!); |
| 51 | token = tokens[tokens.length - 1]; |
| 52 | } |
| 53 | |
| 54 | function matchPunctuator(operator: string | Array<string>) { |
| 55 | return ( |
| 56 | token.type === TokenName[TokenEnum.Punctuator] && |
| 57 | (Array.isArray(operator) |
| 58 | ? ~operator.indexOf(token.value!) |
| 59 | : token.value === operator) |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | function fatal() { |
| 64 | throw TypeError( |
| 65 | `Unexpected token ${token!.value || token.type} in ${token!.start.line}:${ |
| 66 | token!.start.column |
| 67 | }` |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | function assert(result: any) { |
| 72 | if (!result) { |
| 73 | fatal(); |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | function expression(): ASTNodeOrNull { |
| 79 | return assignmentExpression(); |
| 80 | } |
no test coverage detected