Skip to content

Commit

Permalink
codegen/ done
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-lischke committed Oct 11, 2024
1 parent a952a2a commit 37736a8
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 305 deletions.
6 changes: 4 additions & 2 deletions src/antlr3/BaseRecognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export abstract class BaseRecognizer {
public static getRuleInvocationStack(e: Error, recognizerClassName: string): string[] {
//const rules = new Array<string>();
const stack = e.stack ?? "";

/*let i: number;
for (i = stack.length - 1; i >= 0; i--) {
const t = stack[i];
Expand Down Expand Up @@ -112,6 +113,7 @@ export abstract class BaseRecognizer {

return matchedSymbol;
}

if (this.state.backtracking > 0) {
this.state.failed = true;

Expand Down Expand Up @@ -725,7 +727,7 @@ export abstract class BaseRecognizer {
this.beginResync();
input.consume(); // simply delete extra token
this.endResync();
this.reportError(e); // report after consuming so AW sees the token in the exception
this.reportError(e); // report after consuming so AW sees the token in the exception
// we want to return the token we're actually matching
const matchedSymbol = this.getCurrentInputSymbol(input);
input.consume(); // move past ttype token as if all were ok
Expand All @@ -739,7 +741,7 @@ export abstract class BaseRecognizer {
e = new RecognitionException({
message: "Missing Token", input: input as TokenStream, recognizer: null, ctx: null,
});
this.reportError(e); // report after inserting so AW sees the token in the exception
this.reportError(e); // report after inserting so AW sees the token in the exception

return inserted;
}
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 @@ -260,7 +260,7 @@ export class SourceGenTriggers extends TreeParser {

// $ANTLR start "block"
// ./SourceGenTriggers.g:61:1: block[GrammarAST label, GrammarAST ebnfRoot] returns [List<? extends SrcOp> omos] : ^(blk= BLOCK ( ^( OPTIONS ( . )+ ) )? ( alternative )+ ) ;
public readonly block(label: GrammarAST, ebnfRoot: GrammarAST): Array<SrcOp> {
public readonly block(label: GrammarAST | null, ebnfRoot: GrammarAST | null): Array<SrcOp> {
let omos = null;


Expand Down
4 changes: 2 additions & 2 deletions tool/src/org/antlr/v4/codegen/CodeGenPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

import type { ST } from "stringtemplate4ts";
import type { IST } from "stringtemplate4ts";

import { grammarOptions } from "../grammar-options.js";
import { Grammar } from "../tool/Grammar.js";
Expand Down Expand Up @@ -104,7 +104,7 @@ export class CodeGenPipeline {
this.gen.writeVocabFile();
}

protected writeRecognizer(template: ST, gen: CodeGenerator, header: boolean): void {
protected writeRecognizer(template: IST, gen: CodeGenerator, header: boolean): void {
gen.writeRecognizer(template, header);
}
}
30 changes: 15 additions & 15 deletions tool/src/org/antlr/v4/codegen/CodeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { writeFileSync } from "fs";
import { Token } from "antlr4ng";
import { AutoIndentWriter, ST, StringWriter, type STGroup } from "stringtemplate4ts";
import { writeFileSync } from "fs";
import { AutoIndentWriter, ST, StringWriter, type IST, type STGroup } from "stringtemplate4ts";

import { Tool } from "../Tool.js";
import { ErrorType } from "../tool/ErrorType.js";
Expand Down Expand Up @@ -90,7 +90,7 @@ export class CodeGenerator {
return this.target.getTemplates();
}

public generateLexer(header?: boolean): ST {
public generateLexer(header?: boolean): IST {
if (!header) {
return this.generateLexer(false);
}
Expand All @@ -99,15 +99,15 @@ export class CodeGenerator {

}

public generateParser(header?: boolean): ST {
public generateParser(header?: boolean): IST {
if (!header) {
return this.generateParser(false);
}

return this.walk(this.createController().buildParserOutputModel(header), header);
}

public generateListener(header?: boolean): ST {
public generateListener(header?: boolean): IST {
if (!header) {
return this.generateListener(false);
}
Expand All @@ -116,47 +116,47 @@ export class CodeGenerator {

}

public generateBaseListener(header?: boolean): ST {
public generateBaseListener(header?: boolean): IST {
if (!header) {
return this.generateBaseListener(false);
}

return this.walk(this.createController().buildBaseListenerOutputModel(header), header);
}

public generateVisitor(header?: boolean): ST {
public generateVisitor(header?: boolean): IST {
if (!header) {
return this.generateVisitor(false);
}

return this.walk(this.createController().buildVisitorOutputModel(header), header);
}

public generateBaseVisitor(header?: boolean): ST {
public generateBaseVisitor(header?: boolean): IST {
if (!header) {
return this.generateBaseVisitor(false);
}

return this.walk(this.createController().buildBaseVisitorOutputModel(header), header);
}

public writeRecognizer(outputFileST: ST, header: boolean): void {
public writeRecognizer(outputFileST: IST, header: boolean): void {
this.target.genFile(this.g, outputFileST, this.getRecognizerFileName(header));
}

public writeListener(outputFileST: ST, header: boolean): void {
public writeListener(outputFileST: IST, header: boolean): void {
this.target.genFile(this.g, outputFileST, this.getListenerFileName(header));
}

public writeBaseListener(outputFileST: ST, header: boolean): void {
public writeBaseListener(outputFileST: IST, header: boolean): void {
this.target.genFile(this.g, outputFileST, this.getBaseListenerFileName(header));
}

public writeVisitor(outputFileST: ST, header: boolean): void {
public writeVisitor(outputFileST: IST, header: boolean): void {
this.target.genFile(this.g, outputFileST, this.getVisitorFileName(header));
}

public writeBaseVisitor(outputFileST: ST, header: boolean): void {
public writeBaseVisitor(outputFileST: IST, header: boolean): void {
this.target.genFile(this.g, outputFileST, this.getBaseVisitorFileName(header));
}

Expand All @@ -170,7 +170,7 @@ export class CodeGenerator {
}
}

public write(code: ST, fileName: string): void {
public write(code: IST, fileName: string): void {
try {
fileName = this.tool.getOutputFile(this.g!, fileName);
const w = new StringWriter();
Expand Down Expand Up @@ -289,7 +289,7 @@ export class CodeGenerator {
return controller;
}

private walk(outputModel: OutputModelObject, header: boolean): ST {
private walk(outputModel: OutputModelObject, header: boolean): IST {
const walker = new OutputModelWalker(this.tool, this.getTemplates());

return walker.walk(outputModel, header);
Expand Down
4 changes: 3 additions & 1 deletion tool/src/org/antlr/v4/codegen/DefaultOutputModelFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export abstract class DefaultOutputModelFactory extends BlankOutputModelFactory

public override getRoot(): OutputModelObject | null { return this.controller.getRoot(); }

public override getCurrentRuleFunction(): RuleFunction { return this.controller.getCurrentRuleFunction(); }
public override getCurrentRuleFunction(): RuleFunction | undefined {
return this.controller.getCurrentRuleFunction();
}

public override getCurrentOuterMostAlt(): Alternative { return this.controller.getCurrentOuterMostAlt(); }

Expand Down
Loading

0 comments on commit 37736a8

Please sign in to comment.