Skip to content

Commit

Permalink
Fixed left recursive rule invocation problem
Browse files Browse the repository at this point in the history
The initial invocation of a left recursive rule requires a precedence value of 0, which was not set before.
  • Loading branch information
mike-lischke committed Dec 29, 2024
1 parent a6094e7 commit a92c36c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/analysis/LeftRecursiveRuleAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ export class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
opPrecRuleAlts.set(key, value);
});

for (const alt of opPrecRuleAlts.keys()) {
const altInfo = opPrecRuleAlts.get(alt);
for (const [alt, altInfo] of opPrecRuleAlts) {
const altST = LeftRecursiveRuleAnalyzer.#recRuleTemplates.getInstanceOf("recRuleAlt")!;
const predST = this.codegenTemplates.getInstanceOf("recRuleAltPredicate")!;
predST.add("opPrec", this.precedence(alt));
Expand Down
5 changes: 3 additions & 2 deletions src/analysis/LeftRecursiveRuleTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ export class LeftRecursiveRuleTransformer {
}

// update all refs to recursive rules to have [0] argument
for (const r of this.ast.getNodesWithType(ANTLRv4Parser.RULE_REF)) {
if (r.getParent()!.getType() !== ANTLRv4Parser.RULE) { // must be rule def
const ruleRefs = this.ast.getNodesWithType(ANTLRv4Parser.RULE_REF);
for (const r of ruleRefs) {
if (r.getParent()!.getType() === ANTLRv4Parser.RULE) { // must be rule def
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/support/ParseTreeToASTConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class ParseTreeToASTConverter {
const ruleAST = this.createVirtualASTNode(RuleAST, ANTLRv4Lexer.RULE, rule, "RULE");
ast.addChild(ruleAST);

ruleAST.addChild(this.createVirtualASTNode(RuleRefAST, ANTLRv4Parser.RULE_REF, parserRule.RULE_REF()));
ruleAST.addChild(new TerminalAST(this.createToken(ANTLRv4Parser.RULE_REF, parserRule.RULE_REF())));
if (parserRule.argActionBlock()) {
this.convertArgActionBlockToAST(ANTLRv4Lexer.ARG_ACTION, parserRule.argActionBlock()!, ruleAST);
}
Expand Down
10 changes: 5 additions & 5 deletions src/tool/ast/GrammarASTWithOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { GrammarAST } from "./GrammarAST.js";
export abstract class GrammarASTWithOptions extends GrammarAST {
public override readonly astType: string = "GrammarASTWithOptions";

#options = new Map<string, GrammarAST | null>();
private options = new Map<string, GrammarAST | null>();

public setOption(key: string, node: GrammarAST | null): void {
this.#options.set(key, node);
this.options.set(key, node);
}

public getOptionString(key: string): string | undefined {
Expand Down Expand Up @@ -44,15 +44,15 @@ export abstract class GrammarASTWithOptions extends GrammarAST {
* and command-line forced options.
*/
public getOptionAST(key: string): GrammarAST | undefined {
return this.#options.get(key) ?? undefined;
return this.options.get(key) ?? undefined;
}

public getNumberOfOptions(): number {
return this.#options.size;
return this.options.size;
}

public getOptions(): Map<string, GrammarAST | null> {
return this.#options;
return this.options;
}

public abstract override dupNode(): GrammarASTWithOptions;
Expand Down
2 changes: 1 addition & 1 deletion src/tree-walkers/SourceGenTriggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ export class SourceGenTriggers extends TreeParser {
public ruleref(label: GrammarAST | null): SrcOp[] | null {
let omos = null;

let RULE_REF27 = null;
let RULE_REF27: GrammarAST | null = null;
let ARG_ACTION28 = null;

try {
Expand Down

0 comments on commit a92c36c

Please sign in to comment.