Skip to content

Commit

Permalink
Merge pull request #3130 from antlr/fix-3216
Browse files Browse the repository at this point in the history
Fix #3126
  • Loading branch information
ericvergnaud authored Mar 19, 2021
2 parents c2f104c + d82e892 commit 3590d4d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
9 changes: 7 additions & 2 deletions runtime/JavaScript/src/antlr4/IntervalSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ const {Token} = require('./Token');

/* stop is not included! */
class Interval {

constructor(start, stop) {
this.start = start;
this.stop = stop;
}

clone() {
return new Interval(this.start, this.stop);
}

contains(item) {
return item >= this.start && item < this.stop;
}
Expand Down Expand Up @@ -55,7 +60,7 @@ class IntervalSet {
addInterval(toAdd) {
if (this.intervals === null) {
this.intervals = [];
this.intervals.push(toAdd);
this.intervals.push(toAdd.clone());
} else {
// find insert pos
for (let pos = 0; pos < this.intervals.length; pos++) {
Expand All @@ -78,7 +83,7 @@ class IntervalSet {
}
}
// greater than any existing
this.intervals.push(toAdd);
this.intervals.push(toAdd.clone());
}
}

Expand Down
31 changes: 11 additions & 20 deletions runtime/JavaScript/src/antlr4/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,16 @@ class Parser extends Recognizer {
}
}

// Remove all parse listeners.
// Remove all parse listeners.
removeParseListeners() {
this._parseListeners = null;
}

// Notify any parse listeners of an enter rule event.
// Notify any parse listeners of an enter rule event.
triggerEnterRuleEvent() {
if (this._parseListeners !== null) {
const ctx = this._ctx;
this._parseListeners.map(function(listener) {
this._parseListeners.forEach(function(listener) {
listener.enterEveryRule(ctx);
ctx.enterRule(listener);
});
Expand All @@ -248,7 +248,7 @@ class Parser extends Recognizer {
if (this._parseListeners !== null) {
// reverse order walk of listeners
const ctx = this._ctx;
this._parseListeners.slice(0).reverse().map(function(listener) {
this._parseListeners.slice(0).reverse().forEach(function(listener) {
ctx.exitRule(listener);
listener.exitEveryRule(ctx);
});
Expand Down Expand Up @@ -392,7 +392,7 @@ class Parser extends Recognizer {
}
node.invokingState = this.state;
if (hasListener) {
this._parseListeners.map(function(listener) {
this._parseListeners.forEach(function(listener) {
if (node instanceof ErrorNode || (node.isErrorNode !== undefined && node.isErrorNode())) {
listener.visitErrorNode(node);
} else if (node instanceof TerminalNode) {
Expand Down Expand Up @@ -422,17 +422,13 @@ class Parser extends Recognizer {
if (this.buildParseTrees) {
this.addContextToParseTree();
}
if (this._parseListeners !== null) {
this.triggerEnterRuleEvent();
}
this.triggerEnterRuleEvent();
}

exitRule() {
this._ctx.stop = this._input.LT(-1);
// trigger event on _ctx, before it reverts to parent
if (this._parseListeners !== null) {
this.triggerExitRuleEvent();
}
this.triggerExitRuleEvent();
this.state = this._ctx.invokingState;
this._ctx = this._ctx.parentCtx;
}
Expand Down Expand Up @@ -469,10 +465,7 @@ class Parser extends Recognizer {
this._precedenceStack.push(precedence);
this._ctx = localctx;
this._ctx.start = this._input.LT(1);
if (this._parseListeners !== null) {
this.triggerEnterRuleEvent(); // simulates rule entry for
// left-recursive rules
}
this.triggerEnterRuleEvent(); // simulates rule entry for left-recursive rules
}

// Like {@link //enterRule} but for recursive rules.
Expand All @@ -487,18 +480,16 @@ class Parser extends Recognizer {
if (this.buildParseTrees) {
this._ctx.addChild(previous);
}
if (this._parseListeners !== null) {
this.triggerEnterRuleEvent(); // simulates rule entry for
// left-recursive rules
}
this.triggerEnterRuleEvent(); // simulates rule entry for left-recursive rules
}

unrollRecursionContexts(parentCtx) {
this._precedenceStack.pop();
this._ctx.stop = this._input.LT(-1);
const retCtx = this._ctx; // save current ctx (return value)
// unroll so _ctx is as it was before call to recursive method
if (this._parseListeners !== null) {
const parseListeners = this.getParseListeners();
if (parseListeners !== null && parseListeners.length > 0) {
while (this._ctx !== parentCtx) {
this.triggerExitRuleEvent();
this._ctx = this._ctx.parentCtx;
Expand Down

0 comments on commit 3590d4d

Please sign in to comment.