Skip to content

Commit

Permalink
fix: cyclic dependency between RuleContext.js, Trees.js and ParserRul…
Browse files Browse the repository at this point in the history
…eContext.js

check for RuleNode instead of RuleContext since that it the only implementation (RuleNode -> RuleContext -> ParserRuleContext)
  • Loading branch information
carocad committed Mar 8, 2020
1 parent 225249f commit 2956f07
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
6 changes: 2 additions & 4 deletions runtime/JavaScript/src/antlr4/RuleContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
var RuleNode = require('./tree/Tree').RuleNode;
var INVALID_INTERVAL = require('./tree/Tree').INVALID_INTERVAL;
var INVALID_ALT_NUMBER = require('./atn/ATN').INVALID_ALT_NUMBER || 0; // TODO: solve cyclic dependency to avoid || 0
const Trees = require('./tree/Trees');

function RuleContext(parent, invokingState) {
// What context invoked this rule?
Expand Down Expand Up @@ -116,10 +117,6 @@ RuleContext.prototype.accept = function(visitor) {
return visitor.visitChildren(this);
};

//need to manage circular dependencies, so export now
module.exports = RuleContext;
var Trees = require('./tree/Trees');


// Print out a whole tree, not just a node, in LISP format
// (root child1 .. childN). Print just a node if this is a leaf.
Expand Down Expand Up @@ -154,3 +151,4 @@ RuleContext.prototype.toString = function(ruleNames, stop) {
return s;
};

module.exports = RuleContext;
4 changes: 4 additions & 0 deletions runtime/JavaScript/src/antlr4/tree/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class RuleNode extends ParseTree {
constructor() {
super();
}

getRuleContext(){
throw new Error("missing interface implementation")
}
}

class TerminalNode extends ParseTree {
Expand Down
11 changes: 5 additions & 6 deletions runtime/JavaScript/src/antlr4/tree/Trees.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

const Utils = require('./../Utils');
const {Token} = require('./../Token');
const {ErrorNode, TerminalNode} = require('./Tree');
const {ParserRuleContext} = require('./../ParserRuleContext');
const RuleContext = require('./../RuleContext');
const {ErrorNode, TerminalNode, RuleNode} = require('./Tree');
const ATN = require('./../atn/ATN');

/** A set of utility routines useful for all kinds of ANTLR trees. */
Expand Down Expand Up @@ -49,8 +47,9 @@ const Trees = {
ruleNames = recog.ruleNames;
}
if(ruleNames!==null) {
if (t instanceof RuleContext) {
const altNumber = t.getAltNumber();
if (t instanceof RuleNode) {
const context = t.getRuleContext()
const altNumber = context.getAltNumber();
if ( altNumber != (ATN.INVALID_ALT_NUMBER || 0) ) { // TODO: solve cyclic dependency to avoid || 0
return ruleNames[t.ruleIndex]+":"+altNumber;
}
Expand Down Expand Up @@ -116,7 +115,7 @@ const Trees = {
if(t.symbol.type===index) {
nodes.push(t);
}
} else if(!findTokens && (t instanceof ParserRuleContext)) {
} else if(!findTokens && (t instanceof RuleNode)) {
if(t.ruleIndex===index) {
nodes.push(t);
}
Expand Down

0 comments on commit 2956f07

Please sign in to comment.