Skip to content

Commit

Permalink
Fixed regression re RuleNode
Browse files Browse the repository at this point in the history
There was a test still using RuleNode and a wrong check in Trees.js, which lead to test failures.

Signed-off-by: Mike Lischke <mike@lischke-online.de>
  • Loading branch information
mike-lischke committed Sep 5, 2023
1 parent 3f8e84f commit 6e0aaa8
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ParseTreeWalker,
RuleContext,
ParserRuleContext,
RuleNode,
PredictionMode
} from 'antlr4';
import <lexerName> from './<lexerName>.js';
Expand All @@ -25,7 +24,7 @@ class TreeShapeListener extends ParseTreeListener {
for (let i = 0; i \< ctx.getChildCount(); i++) {
const child = ctx.getChild(i) as RuleContext;
const parent = child.parentCtx;
if (parent!.ruleContext !== ctx || !(parent instanceof RuleNode)) {
if (parent!.ruleContext !== ctx || !(parent instanceof RuleContext)) {
throw `Invalid parse tree shape detected.`;
}
}
Expand Down Expand Up @@ -64,4 +63,3 @@ function main(argv: string[]): void {
}

main(process.argv);

3 changes: 1 addition & 2 deletions runtime/JavaScript/src/antlr4/context/RuleContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Parser from "../Parser";
import ParseTree from "../tree/ParseTree.js";
import Interval from "../misc/Interval.js";
import { ParseTreeVisitor } from "../tree/ParseTreeVisitor.js";
import ParseTreeVisitor from "../tree/ParseTreeVisitor.js";

export declare class RuleContext implements ParseTree {
public parentCtx: RuleContext | null;
Expand Down Expand Up @@ -69,7 +69,6 @@ export declare class RuleContext implements ParseTree {
*/
public toStringTree(): string;
public toStringTree(ruleNames: string[] | null, recog: Parser): string;

}

export default RuleContext;
4 changes: 1 addition & 3 deletions runtime/JavaScript/src/antlr4/tree/ParseTree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Interval } from "../misc/Interval.js";
/** The basic notion of a tree has a parent, a payload, and a list of children.
* It is the most abstract interface for all the trees used by ANTLR.
*
* Note: this interface is a combination of 4 Java interfaces: ParseTree, SyntaxTree, Tree and RuleNode.
* Note: this interface is a combination of 3 Java interfaces: ParseTree, SyntaxTree and Tree.
*/
export declare interface ParseTree {
/** The parent of this node. If the return value is null, then this
Expand Down Expand Up @@ -56,8 +56,6 @@ export declare interface ParseTree {
* EOF is unspecified.</p>
*/
getSourceInterval(): Interval;

get ruleContext(): RuleContext;
}

export default ParseTree;
2 changes: 1 addition & 1 deletion runtime/JavaScript/src/antlr4/tree/TerminalNode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ParseTree from "./ParseTree.js";
import Token from "../Token.js";
import Interval from "../misc/Interval.js";

export declare class TerminalNode implements Omit<ParseTree, "ruleContext"> {
export declare class TerminalNode implements ParseTree {
public symbol: Token;
public parentCtx: ParseTree | null;

Expand Down
3 changes: 2 additions & 1 deletion runtime/JavaScript/src/antlr4/tree/Trees.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ErrorNode from './ErrorNode.js';
import TerminalNode from './TerminalNode.js';
import ParseTree from "./ParseTree.js";
import escapeWhitespace from "../utils/escapeWhitespace.js";
import RuleContext from "../context/RuleContext.js";

/** A set of utility routines useful for all kinds of ANTLR trees. */
const Trees = {
Expand Down Expand Up @@ -48,7 +49,7 @@ const Trees = {
ruleNames = recog.ruleNames;
}
if (ruleNames !== null) {
if (t instanceof ParseTree) {
if (t instanceof RuleContext) {
const context = t.ruleContext;
const altNumber = context.getAltNumber();
// use const value of ATN.INVALID_ALT_NUMBER to avoid circular dependency
Expand Down

0 comments on commit 6e0aaa8

Please sign in to comment.