Skip to content

Commit

Permalink
TestASTStructure succeeds
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-lischke committed Nov 21, 2024
1 parent 2c6541a commit b54f274
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"codegen",
"decapitalize",
"ebnf",
"errornode",
"graphviz",
"interp",
"javax",
Expand Down
2 changes: 1 addition & 1 deletion src/tree/CommonTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class CommonTree implements ParseTree {
* Delete children from start to stop and replace with t even if t is
* a list (nil-root tree). num of children can increase or decrease.
* For huge child lists, inserting children can force walking rest of
* children to set their childindex; could be slow.
* children to set their child index; could be slow.
*
* @param startChildIndex The index to start deleting children.
* @param stopChildIndex The index to stop deleting children.
Expand Down
58 changes: 32 additions & 26 deletions tests/TestASTStructure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CharStream, CommonTokenStream, type Parser, type ParseTree } from "antl

import { ANTLRv4Lexer } from "../src/generated/ANTLRv4Lexer.js";
import {
ANTLRv4Parser, type DelegateGrammarsContext, type EbnfContext, type ElementContext, type GrammarSpecContext, type LexerElementContext, type RuleSpecContext
ANTLRv4Parser, type AtomContext, type DelegateGrammarsContext, type EbnfContext, type ElementContext, type GrammarSpecContext, type LabeledElementContext, type LexerElementContext, type RulerefContext, type RuleSpecContext
} from "../src/generated/ANTLRv4Parser.js";
import { ParseTreeToASTConverter } from "../src/support/ParseTreeToASTConverter.js";
import { GrammarAST } from "../src/tool/ast/GrammarAST.js";
Expand All @@ -38,21 +38,27 @@ describe("TestASTStructure", () => {
// TODO: is.setLine(scriptLine);

const tokens = new CommonTokenStream(lexer);

/*tokens.fill();
const t = tokens.getTokens();*/

// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
const parser = new ANTLRv4Parser(tokens) as (ANTLRv4Parser & Record<string, Function>);

if (parser.numberOfSyntaxErrors > 0) {
throw new Error("The grammar file contains syntax errors.");
}

// set up customized tree adaptor if necessary
/*if (this.adaptorClassName !== null) {
const m = parserClass.getMethod("setTreeAdaptor", TreeAdaptor.class);
const adaptorClass = Class.forName(this.adaptorClassName).asSubclass(TreeAdaptor.class);
m.invoke(parser, adaptorClass.newInstance());
}*/

return callMethod(parser, ruleName) as ParseTree;
const result = callMethod(parser, ruleName) as ParseTree;

if (parser.numberOfSyntaxErrors > 0) {
throw new Error("The grammar file contains syntax errors.");
}

return result;
};

it("grammarSpec1", () => {
Expand Down Expand Up @@ -257,10 +263,10 @@ describe("TestASTStructure", () => {
});

it("element1", () => {
const context = execParser("element", "~A", 129) as ElementContext;
const context = execParser("atom", "~A", 129) as AtomContext;

const dummy = new GrammarAST();
const ast = ParseTreeToASTConverter.convertElementToAST(context, dummy);
const ast = ParseTreeToASTConverter.convertAtomToAST(context, dummy);

const actual = ast!.toStringTree();
const expecting = "(~ (SET A))";
Expand Down Expand Up @@ -356,12 +362,12 @@ describe("TestASTStructure", () => {
});

it("element10", () => {
const context = execParser("element", "a[3]", 138) as ElementContext;
const context = execParser("ruleref", "a[3]", 138) as RulerefContext;

const dummy = new GrammarAST();
const ast = ParseTreeToASTConverter.convertElementToAST(context, dummy);
const ast = ParseTreeToASTConverter.convertRulerefToAST(context, dummy);

const actual = ast!.toStringTree();
const actual = ast.toStringTree();
const expecting = "(a 3)";
expect(actual).toBe(expecting);
});
Expand All @@ -378,12 +384,12 @@ describe("TestASTStructure", () => {
});

it("element12", () => {
const context = execParser("element", "x=ID", 140) as ElementContext;
const context = execParser("labeledElement", "x=ID", 140) as LabeledElementContext;

const dummy = new GrammarAST();
const ast = ParseTreeToASTConverter.convertElementToAST(context, dummy);
const ast = ParseTreeToASTConverter.convertLabeledElementToAST(context, dummy);

const actual = ast!.toStringTree();
const actual = ast.toStringTree();
const expecting = "(= x ID)";
expect(actual).toBe(expecting);
});
Expand Down Expand Up @@ -411,45 +417,45 @@ describe("TestASTStructure", () => {
});

it("element15", () => {
const context = execParser("element", "x=b", 143) as ElementContext;
const context = execParser("labeledElement", "x=b", 143) as LabeledElementContext;

const dummy = new GrammarAST();
const ast = ParseTreeToASTConverter.convertElementToAST(context, dummy);
const ast = ParseTreeToASTConverter.convertLabeledElementToAST(context, dummy);

const actual = ast!.toStringTree();
const actual = ast.toStringTree();
const expecting = "(= x b)";
expect(actual).toBe(expecting);
});

it("element16", () => {
const context = execParser("element", "x=(A|B)", 144) as ElementContext;
const context = execParser("labeledElement", "x=(A|B)", 144) as LabeledElementContext;

const dummy = new GrammarAST();
const ast = ParseTreeToASTConverter.convertElementToAST(context, dummy);
const ast = ParseTreeToASTConverter.convertLabeledElementToAST(context, dummy);

const actual = ast!.toStringTree();
const actual = ast.toStringTree();
const expecting = "(= x (BLOCK (ALT A) (ALT B)))";
expect(actual).toBe(expecting);
});

it("element17", () => {
const context = execParser("element", "x=~(A|B)", 145) as ElementContext;
const context = execParser("labeledElement", "x=~(A|B)", 145) as LabeledElementContext;

const dummy = new GrammarAST();
const ast = ParseTreeToASTConverter.convertElementToAST(context, dummy);
const ast = ParseTreeToASTConverter.convertLabeledElementToAST(context, dummy);

const actual = ast!.toStringTree();
const actual = ast.toStringTree();
const expecting = "(= x (~ (SET A B)))";
expect(actual).toBe(expecting);
});

it("element18", () => {
const context = execParser("element", "x+=~(A|B)", 146) as ElementContext;
const context = execParser("labeledElement", "x+=~(A|B)", 146) as LabeledElementContext;

const dummy = new GrammarAST();
const ast = ParseTreeToASTConverter.convertElementToAST(context, dummy);
const ast = ParseTreeToASTConverter.convertLabeledElementToAST(context, dummy);

const actual = ast!.toStringTree();
const actual = ast.toStringTree();
const expecting = "(+= x (~ (SET A B)))";
expect(actual).toBe(expecting);
});
Expand Down

0 comments on commit b54f274

Please sign in to comment.