(lexs: ILexemeResult[])
| 17 | } |
| 18 | |
| 19 | const parser = (lexs: ILexemeResult[]): ISyntaxTree => { |
| 20 | let nesting = 0; |
| 21 | |
| 22 | const nestedLexs = lexs.map(lex => { |
| 23 | const res = Object.assign({}, lex, {nesting}); |
| 24 | |
| 25 | nesting += lex.lexeme.nesting || 0; |
| 26 | |
| 27 | return res; |
| 28 | }); |
| 29 | |
| 30 | // find lowest priority 0-nesting lex |
| 31 | const pivot = nestedLexs |
| 32 | .filter( |
| 33 | lex => lex.nesting === 0 && typeof lex.lexeme.priority === 'number' |
| 34 | ) |
| 35 | .sort( |
| 36 | (a, b) => (b.lexeme.priority || -1) - (a.lexeme.priority || -1) |
| 37 | )[0]; |
| 38 | |
| 39 | Logger.trace('parser -> pivot', pivot, lexs); |
| 40 | |
| 41 | const pivotIndex = nestedLexs.indexOf(pivot); |
| 42 | |
| 43 | if (pivot.lexeme.syntaxer) { |
| 44 | const tree = pivot.lexeme.syntaxer(lexs, pivot, pivotIndex); |
| 45 | |
| 46 | if (Array.isArray(tree.left)) { |
| 47 | tree.left = parser(tree.left); |
| 48 | } |
| 49 | |
| 50 | if (Array.isArray(tree.right)) { |
| 51 | tree.right = parser(tree.right); |
| 52 | } |
| 53 | |
| 54 | if (Array.isArray(tree.block)) { |
| 55 | tree.block = parser(tree.block); |
| 56 | } |
| 57 | |
| 58 | return tree; |
| 59 | } else { |
| 60 | throw new Error(pivot.lexeme.type); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | export default (lexerResult: ILexerResult): ISyntaxerResult => { |
| 65 | const {lexemes} = lexerResult; |
no test coverage detected
searching dependent graphs…