mirror of
https://github.com/vale981/tridactyl
synced 2025-03-10 12:46:38 -04:00
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
![]() |
import * as nearley from "nearley"
|
||
|
|
||
|
/** Friendlier interface around nearley parsers */
|
||
|
export class Parser {
|
||
|
private parser
|
||
|
private initial_state
|
||
|
/* public results */
|
||
|
|
||
|
constructor(grammar) {
|
||
|
this.parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar))
|
||
|
this.initial_state = this.parser.save()
|
||
|
/* this.results = this.parser.results */
|
||
|
}
|
||
|
|
||
|
feedUntilError(input) {
|
||
|
let lastResult = undefined
|
||
|
let consumedIndex = 0
|
||
|
try {
|
||
|
for (let val of input) {
|
||
|
this.parser.feed(val)
|
||
|
lastResult = this.parser.results[0]
|
||
|
consumedIndex++
|
||
|
}
|
||
|
} catch (e) {
|
||
|
} finally {
|
||
|
this.reset()
|
||
|
if (lastResult === undefined) {
|
||
|
throw "Error: no result!"
|
||
|
} else {
|
||
|
return [lastResult, input.slice(consumedIndex)]
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private reset() {
|
||
|
this.parser.restore(this.initial_state)
|
||
|
}
|
||
|
|
||
|
/* feed(input) { */
|
||
|
/* return this.parser.feed(input) */
|
||
|
/* } */
|
||
|
}
|