Parser now uses context to store details of the computation
This commit is contained in:
@@ -6,3 +6,31 @@ const nearleyGrammar = nearley.Grammar.fromCompiled(grammar);
|
||||
export default function parser(){
|
||||
return new nearley.Parser(nearleyGrammar);
|
||||
}
|
||||
|
||||
export class CompilationContext {
|
||||
constructor(){
|
||||
this.errors = [];
|
||||
this.rolls = [];
|
||||
}
|
||||
storeError(e){
|
||||
this.errors.push(e);
|
||||
}
|
||||
storeRoll(r){
|
||||
this.rolls.push(r);
|
||||
}
|
||||
}
|
||||
|
||||
export function parse(string){
|
||||
let parser = new nearley.Parser(nearleyGrammar);
|
||||
parser.feed(string);
|
||||
let results = parser.results;
|
||||
if (results.length === 1){
|
||||
return results[0];
|
||||
} else if (results.length === 0){
|
||||
// Valid parsing up until now, but need more. Unexpected end of input.
|
||||
return null;
|
||||
} else {
|
||||
console.warn('Grammar is ambiguous!', {string, results});
|
||||
return results[0];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user