|
| 1 | +// import {Codegen} from '@jsonjoy.com/util/lib/codegen' |
| 2 | +// import {dynamicFunction} from '@jsonjoy.com/util/lib/codegen/dynamicFunction' |
| 3 | +// import {emitStringMatch} from '@jsonjoy.com/util/lib/codegen/util/helpers'; |
| 4 | +// import {RegExpTerminalMatch, StringTerminalMatch} from '../matches'; |
| 5 | +// import {scrub} from '../util'; |
| 6 | +// import type {Grammar, Rule, MatchParser} from '../types'; |
| 7 | + |
| 8 | +// export class CodegenGrammar { |
| 9 | +// public static readonly compile = (grammar: Grammar): MatchParser => { |
| 10 | +// const codegen = new CodegenGrammar(grammar); |
| 11 | +// codegen.generate(); |
| 12 | +// return codegen.compile(); |
| 13 | +// }; |
| 14 | + |
| 15 | +// public readonly codegen: Codegen<MatchParser>; |
| 16 | +// protected readonly rules = new Map<string, MatchParser>(); |
| 17 | + |
| 18 | +// constructor(public readonly grammar: Grammar) { |
| 19 | +// this.codegen = new Codegen({ |
| 20 | +// args: ['str', 'pos'], |
| 21 | +// }); |
| 22 | +// } |
| 23 | + |
| 24 | +// protected compileRule(name: string, rule: Rule): MatchParser { |
| 25 | +// const {codegen} = this; |
| 26 | +// const {match} = rule; |
| 27 | +// const codegenProduction = dynamicFunction<MatchParser>(codegen); |
| 28 | +// codegenProduction(` |
| 29 | +// const results = []; |
| 30 | +// ${match.map((m, i) => { |
| 31 | +// const dep = codegen.linkDependency(m); |
| 32 | +// return ` |
| 33 | +// const r${i} = ${dep}(str, pos); |
| 34 | +// if (!r${i}) return; |
| 35 | +// pos = r${i}.end; |
| 36 | +// results.push(r${i}); |
| 37 | +// `; |
| 38 | +// }).join('\n')} |
| 39 | +// return {kind: '${name}', pos, end: pos, children: results}; |
| 40 | +// `); |
| 41 | +// return codegenProduction.compile(); |
| 42 | +// } |
| 43 | + |
| 44 | +// public generate() { |
| 45 | +// const {codegen, grammar} = this; |
| 46 | +// const {start, rules} = grammar; |
| 47 | +// // for (const [name, rule] of Object.entries(rules)) { |
| 48 | +// // const dep = codegen.linkDependency(this.compileRule(name, rule)); |
| 49 | +// // this.rules.set(name, dep); |
| 50 | +// // } |
| 51 | +// // const start = this.rules.get(grammar.start); |
| 52 | +// // if (!start) throw new Error('INVALID_START_SYMBOL'); |
| 53 | +// // codegen.return(`${start}(str, pos)`); |
| 54 | +// } |
| 55 | + |
| 56 | +// public compile(): MatchParser { |
| 57 | +// return this.codegen.compile(); |
| 58 | +// } |
| 59 | +// } |
0 commit comments