Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make parser accessible in postprocessing rules #638

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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