Skip to content

Commit

Permalink
Merge pull request #3080 from antlr/fix-javascript-semantic-predicates
Browse files Browse the repository at this point in the history
fix javascript semantic predicates
  • Loading branch information
ericvergnaud authored Feb 13, 2021
2 parents 9668ce7 + 94f9759 commit 00fbf95
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion runtime/JavaScript/src/antlr4/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

function arrayToString(a) {
return "[" + a.join(", ") + "]";
return Array.isArray(a) ? ("[" + a.join(", ") + "]") : "null";
}

String.prototype.seed = String.prototype.seed || Math.round(Math.random() * Math.pow(2, 32));
Expand Down
2 changes: 1 addition & 1 deletion runtime/JavaScript/src/antlr4/atn/ParserATNSimulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ class ParserATNSimulator extends ATNSimulator {
// First figure out where we can reach on input t
for (let i=0; i<closure.items.length;i++) {
const c = closure.items[i];
if(this.debug_add) {
if(this.debug) {
console.log("testing " + this.getTokenName(t) + " at " + c);
}
if (c.state instanceof RuleStopState) {
Expand Down
31 changes: 14 additions & 17 deletions runtime/JavaScript/src/antlr4/atn/SemanticContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

const {Set, Hash} = require('./../Utils');
const { Set, Hash, equalArrays } = require('./../Utils');

/**
* A tree structure used to record the semantic context in which
Expand All @@ -14,6 +14,7 @@ const {Set, Hash} = require('./../Utils');
* {@link SemanticContext} within the scope of this outer class.</p>
*/
class SemanticContext {

hashCode() {
const hash = new Hash();
this.updateHashCode(hash);
Expand Down Expand Up @@ -93,6 +94,7 @@ class SemanticContext {


class Predicate extends SemanticContext {

constructor(ruleIndex, predIndex, isCtxDependent) {
super();
this.ruleIndex = ruleIndex === undefined ? -1 : ruleIndex;
Expand Down Expand Up @@ -134,6 +136,7 @@ SemanticContext.NONE = new Predicate();


class PrecedencePredicate extends SemanticContext {

constructor(precedence) {
super();
this.precedence = precedence === undefined ? 0 : precedence;
Expand All @@ -156,7 +159,7 @@ class PrecedencePredicate extends SemanticContext {
}

updateHashCode(hash) {
hash.update(31);
hash.update(this.precedence);
}

equals(other) {
Expand All @@ -170,7 +173,7 @@ class PrecedencePredicate extends SemanticContext {
}

toString() {
return "{"+this.precedence+">=prec}?";
return "{" + this.precedence + ">=prec}?";
}

static filterPrecedencePredicates(set) {
Expand Down Expand Up @@ -217,7 +220,7 @@ class AND extends SemanticContext {
});
operands.add(reduced);
}
this.opnds = operands.values();
this.opnds = Array.from(operands.values());
}

equals(other) {
Expand All @@ -226,7 +229,7 @@ class AND extends SemanticContext {
} else if (!(other instanceof AND)) {
return false;
} else {
return this.opnds === other.opnds;
return equalArrays(this.opnds, other.opnds);
}
}

Expand Down Expand Up @@ -280,11 +283,8 @@ class AND extends SemanticContext {
}

toString() {
let s = "";
this.opnds.map(function(o) {
s += "&& " + o.toString();
});
return s.length > 3 ? s.slice(3) : s;
const s = this.opnds.map(o => o.toString());
return (s.length > 3 ? s.slice(3) : s).join("&&");
}
}

Expand Down Expand Up @@ -321,7 +321,7 @@ class OR extends SemanticContext {
const reduced = s[s.length-1];
operands.add(reduced);
}
this.opnds = operands.values();
this.opnds = Array.from(operands.values());
}

equals(other) {
Expand All @@ -330,7 +330,7 @@ class OR extends SemanticContext {
} else if (!(other instanceof OR)) {
return false;
} else {
return this.opnds === other.opnds;
return equalArrays(this.opnds, other.opnds);
}
}

Expand Down Expand Up @@ -382,11 +382,8 @@ class OR extends SemanticContext {
}

toString() {
let s = "";
this.opnds.map(function(o) {
s += "|| " + o.toString();
});
return s.length > 3 ? s.slice(3) : s;
const s = this.opnds.map(o => o.toString());
return (s.length > 3 ? s.slice(3) : s).join("||");
}
}

Expand Down

0 comments on commit 00fbf95

Please sign in to comment.