Skip to content

Commit

Permalink
ParseTreeVisitor should use ParseTree, not ParserRuleContext
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Lischke <[email protected]>
  • Loading branch information
mike-lischke committed Apr 13, 2024
1 parent ab78b5a commit e06e2e5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export abstract class Lexer extends Recognizer<LexerATNSimulator> implements Tok

/**
* Once we see EOF on char stream, next token will be EOF.
* If you have DONE : EOF ; then you see DONE EOF.
* If you have DONE : EOF ; then you see DONE EOF.
*/
#hitEOF = false;

Expand All @@ -127,6 +127,8 @@ export abstract class Lexer extends Recognizer<LexerATNSimulator> implements Tok

public constructor(input: CharStream, options?: Partial<LexerOptions>) {
super();

// Override the default options with the provided options.
this.options = { ...this.options, ...options };
this.#input = input;
this.#factory = CommonTokenFactory.DEFAULT;
Expand Down Expand Up @@ -313,7 +315,7 @@ export abstract class Lexer extends Recognizer<LexerATNSimulator> implements Tok
*/
public getAllTokens(): Token[] {
const tokens = [];
let t = this.nextToken()!;
let t = this.nextToken();
while (t.type !== Token.EOF) {
tokens.push(t);
t = this.nextToken()!;
Expand Down
5 changes: 2 additions & 3 deletions src/tree/AbstractParseTreeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { ParserRuleContext } from "../ParserRuleContext.js";
import { ErrorNode } from "./ErrorNode.js";
import { ParseTree } from "./ParseTree.js";
import { ParseTreeVisitor } from "./ParseTreeVisitor.js";
Expand All @@ -15,7 +14,7 @@ export abstract class AbstractParseTreeVisitor<T> implements ParseTreeVisitor<T>
return tree.accept(this);
}

public visitChildren(node: ParserRuleContext): T | null {
public visitChildren(node: ParseTree): T | null {
let result = this.defaultResult();
const n = node.getChildCount();
for (let i = 0; i < n; i++) {
Expand Down Expand Up @@ -45,7 +44,7 @@ export abstract class AbstractParseTreeVisitor<T> implements ParseTreeVisitor<T>
return null;
}

protected shouldVisitNextChild(_node: ParserRuleContext, _currentResult: T | null): boolean {
protected shouldVisitNextChild(_node: ParseTree, _currentResult: T | null): boolean {
return true;
}

Expand Down
5 changes: 2 additions & 3 deletions src/tree/ParseTreeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
*/

import { type ErrorNode } from "./ErrorNode.js";
import { type TerminalNode } from "./TerminalNode.js";
import { type ParseTree } from "./ParseTree.js";
import { type ParserRuleContext } from "../ParserRuleContext.js";
import { type TerminalNode } from "./TerminalNode.js";

/**
* This interface defines the basic notion of a parse tree visitor. Generated
Expand All @@ -34,7 +33,7 @@ export interface ParseTreeVisitor<T> {
* @param node The {@link RuleNode} whose children should be visited.
* @returns The result of visiting the children of the node.
*/
visitChildren(node: ParserRuleContext): T | null;
visitChildren(node: ParseTree): T | null;

/**
* Visit a terminal node, and return a user-defined result of the operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@

// Fixes https://github.com/antlr/antlr4/issues/413
// "Tree pattern compilation doesn't check for a complete parse"
// eslint-disable-next-line @typescript-eslint/naming-convention
export class StartRuleDoesNotConsumeFullPatternError extends Error {
};

0 comments on commit e06e2e5

Please sign in to comment.