Skip to content

Commit

Permalink
Make parser accessible in postprocessing rules
Browse files Browse the repository at this point in the history
As first suggested here: #203
  • Loading branch information
swwu committed Jul 3, 2023
1 parent 6e24450 commit 6e2cd22
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
output += "interface NearleyRule {\n";
output += " name: string;\n";
output += " symbols: NearleySymbol[];\n";
output += " postprocess?: (d: any[], loc?: number, reject?: {}) => any;\n";
output += " postprocess?: (d: any[], loc?: number, reject?: {}, parser: any) => any;\n";
output += "};\n";
output += "\n";
output += "type NearleySymbol = string | { literal: any } | { test: (token: any) => boolean };\n";
Expand Down
12 changes: 6 additions & 6 deletions lib/nearley.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
return children;
};

State.prototype.finish = function() {
State.prototype.finish = function(parser) {
if (this.rule.postprocess) {
this.data = this.rule.postprocess(this.data, this.reference, Parser.fail);
this.data = this.rule.postprocess(this.data, this.reference, Parser.fail, parser);
}
};

Expand All @@ -80,7 +80,7 @@
}


Column.prototype.process = function(nextColumn) {
Column.prototype.process = function(parser) {
var states = this.states;
var wants = this.wants;
var completed = this.completed;
Expand All @@ -89,7 +89,7 @@
var state = states[w];

if (state.isComplete) {
state.finish();
state.finish(parser);
if (state.data !== Parser.fail) {
// complete
var wantedBy = state.wantedBy;
Expand Down Expand Up @@ -269,7 +269,7 @@
column.wants[grammar.start] = [];
column.predict(grammar.start);
// TODO what if start rule is nullable?
column.process();
column.process(this);
this.current = 0; // token index
}

Expand Down Expand Up @@ -335,7 +335,7 @@
// To prevent duplication, we also keep track of rules we have already
// added

nextColumn.process();
nextColumn.process(this);

// If needed, throw an error:
if (nextColumn.states.length === 0) {
Expand Down

0 comments on commit 6e2cd22

Please sign in to comment.