Skip to content

Commit

Permalink
TestASTStructure and TestAmbigParseTrees tests run successfully
Browse files Browse the repository at this point in the history
- Added index.ts file for tool/ folder, to allow defining initialization order for certain classes.
- Added toStringTree() to CommonTree.
- TestASTStructure and TestAmbigParseTrees tests run successfully.
  • Loading branch information
mike-lischke committed Nov 20, 2024
1 parent b25a851 commit faf25f1
Show file tree
Hide file tree
Showing 21 changed files with 1,115 additions and 897 deletions.
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"Nonnull",
"Postamble",
"Preorder",
"RARROW",
"RBRACK",
"REDEF",
"REFD",
Expand Down Expand Up @@ -110,6 +111,7 @@
"rarr",
"recog",
"retvals",
"ruleref",
"semctx",
"sempred",
"sempreds",
Expand Down
6 changes: 3 additions & 3 deletions src/Tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class Tool implements ITool {
}
content += "\n";

const serializedATN = ATNSerializer.getSerialized(g.atn);
const serializedATN = ATNSerializer.getSerialized(g.atn!);
content += "atn:\n";
content += serializedATN.toString();

Expand Down Expand Up @@ -434,7 +434,7 @@ export class Tool implements ITool {
return undefined;
}

return ParseTreeToASTConverter.convertToAST(grammarSpec, tokens);
return ParseTreeToASTConverter.convertGrammarSpecToAST(grammarSpec, tokens);
}

public generateATNs(g: Grammar): void {
Expand All @@ -447,7 +447,7 @@ export class Tool implements ITool {
for (const ig of grammars) {
for (const r of ig.rules.values()) {
try {
const dot = dotGenerator.getDOTFromState(g.atn.ruleToStartState[r.index]!, g.isLexer());
const dot = dotGenerator.getDOTFromState(g.atn!.ruleToStartState[r.index]!, g.isLexer());
this.writeDOTFile(g, r, dot);
} catch (ioe) {
ErrorManager.get().toolError(ErrorType.CANNOT_WRITE_FILE, ioe);
Expand Down
8 changes: 4 additions & 4 deletions src/analysis/AnalysisPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class AnalysisPipeline {

public process(): void {
// LEFT-RECURSION CHECK
const lr = new LeftRecursionDetector(this.g, this.g.atn);
const lr = new LeftRecursionDetector(this.g, this.g.atn!);
lr.check();
if (lr.listOfRecursiveCycles.length !== 0) {
return;
Expand All @@ -65,7 +65,7 @@ export class AnalysisPipeline {
}

const analyzer = new LL1Analyzer();
const look = analyzer.look(this.g.atn, this.g.atn.ruleToStartState[rule.index]!, undefined);
const look = analyzer.look(this.g.atn!, this.g.atn!.ruleToStartState[rule.index]!, undefined);
if (look.contains(Token.EPSILON)) {
ErrorManager.get().grammarError(ErrorType.EPSILON_TOKEN, this.g.fileName,
(rule.ast.getChild(0) as GrammarAST).token!, rule.name);
Expand All @@ -74,8 +74,8 @@ export class AnalysisPipeline {
}

protected processParser(): void {
this.g.decisionLOOK = new Array<IntervalSet[]>(this.g.atn.getNumberOfDecisions() + 1);
for (const s of this.g.atn.decisionToState) {
this.g.decisionLOOK = new Array<IntervalSet[]>(this.g.atn!.getNumberOfDecisions() + 1);
for (const s of this.g.atn!.decisionToState) {
this.g.tool.logInfo({
component: "LL1",
msg: "\nDECISION " + s.decision + " in rule " + this.g.getRule(s.ruleIndex)?.name
Expand Down
3 changes: 2 additions & 1 deletion src/automata/IATNFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { BlockAST } from "../tool/ast/BlockAST.js";
import { GrammarAST } from "../tool/ast/GrammarAST.js";
import { PredAST } from "../tool/ast/PredAST.js";
import { TerminalAST } from "../tool/ast/TerminalAST.js";
import type { Constructor } from "../misc/Utils.js";

/**
* A pair of states pointing to the left/right (start and end) states of a
Expand All @@ -27,7 +28,7 @@ export interface IATNFactory {
createATN(): ATN;
setCurrentRuleName(name: string): void;
rule(ruleAST: GrammarAST, name: string, blk: IStatePair): IStatePair;
newState(): ATNState;
newState<T extends ATNState>(nodeType: Constructor<T>): T;
label(t: IStatePair): IStatePair;
listLabel(t: IStatePair): IStatePair;
tokenRef(node: TerminalAST): IStatePair | null;
Expand Down
43 changes: 21 additions & 22 deletions src/automata/LexerATNFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
/* eslint-disable jsdoc/require-param, jsdoc/require-returns */

import {
ActionTransition, ATN, ATNState, AtomTransition, CodePointTransitions, CommonToken, IntervalSet, IntStream, Lexer,
LexerAction, LexerChannelAction, LexerCustomAction, LexerModeAction, LexerMoreAction, LexerPopModeAction,
LexerPushModeAction, LexerSkipAction, LexerTypeAction, NotSetTransition, SetTransition, Token, TokensStartState,
Transition
ActionTransition, ATN, ATNState, AtomTransition, BasicState, CodePointTransitions, CommonToken, IntervalSet,
IntStream, Lexer, LexerAction, LexerChannelAction, LexerCustomAction, LexerModeAction, LexerMoreAction,
LexerPopModeAction, LexerPushModeAction, LexerSkipAction, LexerTypeAction, NotSetTransition, SetTransition, Token,
TokensStartState, Transition
} from "antlr4ng";
import type { STGroup } from "stringtemplate4ts";

Expand Down Expand Up @@ -141,10 +141,9 @@ export class LexerATNFactory extends ParserATNFactory {

public override createATN(): ATN {
// BUILD ALL START STATES (ONE PER MODE)
const modes = (this.g as LexerGrammar).modes.keys();
for (const modeName of modes) {
for (const [modeName] of (this.g as LexerGrammar).modes) {
// create s0, start state; implied Tokens rule node
const startState = this.newStateOfType(TokensStartState);
const startState = this.newState(TokensStartState);
this.atn.modeNameToStartState.set(modeName, startState);
this.atn.modeToStartState.push(startState);
this.atn.defineDecisionState(startState);
Expand All @@ -165,7 +164,7 @@ export class LexerATNFactory extends ParserATNFactory {
}

// LINK MODE START STATE TO EACH TOKEN RULE
for (const modeName of modes) {
for (const [modeName] of (this.g as LexerGrammar).modes) {
const rules = (this.g as LexerGrammar).modes.get(modeName)!;
const startState = this.atn.modeNameToStartState.get(modeName) ?? null;
for (const r of rules) {
Expand Down Expand Up @@ -199,8 +198,8 @@ export class LexerATNFactory extends ParserATNFactory {
const [action] = args as [string];

if (action.trim().length === 0) {
const left = this.newState();
const right = this.newState();
const left = this.newState(BasicState);
const right = this.newState(BasicState);
this.epsilon(left, right);

return { left, right };
Expand All @@ -220,8 +219,8 @@ export class LexerATNFactory extends ParserATNFactory {
[node, lexerAction] = args as [GrammarAST, LexerAction];
}

const left = this.newState(node);
const right = this.newState(node);
const left = this.newState(BasicState);
const right = this.newState(BasicState);
const isCtxDependent = false;
const lexerActionIndex = this.getLexerActionIndex(lexerAction);
const a = new ActionTransition(right, this.currentRule!.index, lexerActionIndex, isCtxDependent);
Expand All @@ -247,8 +246,8 @@ export class LexerATNFactory extends ParserATNFactory {
}

public override range(a: GrammarAST, b: GrammarAST): IStatePair {
const left = this.newState(a);
const right = this.newState(b);
const left = this.newState(BasicState);
const right = this.newState(BasicState);
const t1 = CharSupport.getCharValueFromGrammarCharLiteral(a.getText());
const t2 = CharSupport.getCharValueFromGrammarCharLiteral(b.getText());
if (this.checkRange(a, b, t1, t2)) {
Expand All @@ -261,8 +260,8 @@ export class LexerATNFactory extends ParserATNFactory {
}

public override set(associatedAST: GrammarAST, alts: GrammarAST[], invert: boolean): IStatePair {
const left = this.newState(associatedAST);
const right = this.newState(associatedAST);
const left = this.newState(BasicState);
const right = this.newState(BasicState);
const set = new IntervalSet();
for (const t of alts) {
if (t.getType() === ANTLRv4Parser.RANGE) {
Expand Down Expand Up @@ -322,7 +321,7 @@ export class LexerATNFactory extends ParserATNFactory {
*/
public override stringLiteral(stringLiteralAST: TerminalAST): IStatePair {
const chars = stringLiteralAST.getText();
const left = this.newState(stringLiteralAST);
const left = this.newState(BasicState);
let right: ATNState | null;
const s = CharSupport.getStringFromGrammarStringLiteral(chars);
if (s === null) {
Expand All @@ -333,7 +332,7 @@ export class LexerATNFactory extends ParserATNFactory {
let prev = left;
right = null;
for (const char of s) {
right = this.newState(stringLiteralAST);
right = this.newState(BasicState);
const codePoint = char.codePointAt(0)!;
prev.addTransition(this.createTransition(right, codePoint, codePoint, stringLiteralAST));
prev = right;
Expand All @@ -345,8 +344,8 @@ export class LexerATNFactory extends ParserATNFactory {

/** [Aa\t \u1234a-z\]\p{Letter}\-] char sets */
public override charSetLiteral(charSetAST: GrammarAST): IStatePair {
const left = this.newState(charSetAST);
const right = this.newState(charSetAST);
const left = this.newState(BasicState);
const right = this.newState(BasicState);
const set = this.getSetFromCharSetLiteral(charSetAST);

left.addTransition(new SetTransition(right, set));
Expand Down Expand Up @@ -436,8 +435,8 @@ export class LexerATNFactory extends ParserATNFactory {
public override tokenRef(node: TerminalAST): IStatePair | null {
// Ref to EOF in lexer yields char transition on -1
if (node.getText() === "EOF") {
const left = this.newState(node);
const right = this.newState(node);
const left = this.newState(BasicState);
const right = this.newState(BasicState);
left.addTransition(new AtomTransition(right, IntStream.EOF));

return { left, right };
Expand Down
Loading

0 comments on commit faf25f1

Please sign in to comment.