Skip to content

Commit

Permalink
Brought back @ModelElement annotation
Browse files Browse the repository at this point in the history
That's necessary to limit the output model parts used in the target templates.
  • Loading branch information
mike-lischke committed Nov 16, 2024
1 parent b3d8cc6 commit e6869b5
Show file tree
Hide file tree
Showing 23 changed files with 153 additions and 24 deletions.
17 changes: 9 additions & 8 deletions src/codegen/OutputModelWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Tool } from "../Tool.js";
import { ErrorManager } from "../tool/ErrorManager.js";
import { ErrorType } from "../tool/ErrorType.js";
import { OutputModelObject } from "./model/OutputModelObject.js";
import { isModelElement } from "../misc/ModelElement.js";

/**
* Convert an output model tree to template hierarchy by walking
Expand Down Expand Up @@ -64,15 +65,15 @@ export class OutputModelWalker {
const [modelArgName] = [...formalArgs.keys()];
st.add(modelArgName, omo);

// COMPUTE STs FOR EACH NESTED MODEL OBJECT MARKED WITH @ModelElement AND MAKE ST ATTRIBUTE
// Compute templates for each nested model object. The original code uses an annotation to identify
// which fields are model objects. For now, we'll assume that all fields are model objects.
const usedFieldNames = new Set<string>();
const fields = Object.keys(omo);
for (const fieldName of fields) {
/* let annotation = fi.getAnnotation(ModelElement.class);
if (annotation === null) {
for (const fieldName in omo) {
if (!isModelElement(omo, fieldName)) {
continue;
}*/
}

console.log(`${omo.constructor.name}.${fieldName}`);
if (usedFieldNames.has(fieldName)) {
ErrorManager.get().toolError(ErrorType.INTERNAL_ERROR, "Model object " + omo.constructor.name +
" has multiple fields named '" + fieldName + "'");
Expand All @@ -93,8 +94,8 @@ export class OutputModelWalker {
st.add(fieldName, nestedST);
} else {
if (Array.isArray(o)) {
const nestedOmos = o as unknown[];
for (const nestedOmo of nestedOmos) {
const nestedObjects = o as unknown[];
for (const nestedOmo of nestedObjects) {
if (nestedOmo === null) {
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/model/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { IST, ST } from "stringtemplate4ts";

import { ANTLRv4Parser } from "../../generated/ANTLRv4Parser.js";

import { ModelElement } from "../../misc/ModelElement.js";
import { ActionAST } from "../../tool/ast/ActionAST.js";
import { ActionTranslator } from "../ActionTranslator.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
Expand All @@ -20,6 +21,7 @@ import { RuleElement } from "./RuleElement.js";

export class Action extends RuleElement {

@ModelElement
public chunks: ActionChunk[] = [];

public constructor(factory: OutputModelFactory, ast?: ActionAST);
Expand Down
3 changes: 3 additions & 0 deletions src/codegen/model/Choice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { IntervalSet } from "antlr4ng";

import { ModelElement } from "../../misc/ModelElement.js";
import { Utils } from "../../misc/Utils.js";
import { GrammarAST } from "../../tool/ast/GrammarAST.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
Expand Down Expand Up @@ -34,8 +35,10 @@ export abstract class Choice extends RuleElement {
public decision = -1;
public label: Decl;

@ModelElement
public alts: CodeBlockForAlt[] = [];

@ModelElement
public preamble: SrcOp[] = [];

public constructor(factory: OutputModelFactory,
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/model/ExceptionClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { ModelElement } from "../../misc/ModelElement.js";
import { ActionAST } from "../../tool/ast/ActionAST.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { Action } from "./Action.js";
import { SrcOp } from "./SrcOp.js";

export class ExceptionClause extends SrcOp {

@ModelElement
public catchArg: Action;

@ModelElement
public catchAction: Action;

public constructor(factory: OutputModelFactory,
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/model/InvokeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { ANTLRv4Parser } from "../../generated/ANTLRv4Parser.js";
import { ModelElement } from "../../misc/ModelElement.js";

import { ActionAST } from "../../tool/ast/ActionAST.js";
import { GrammarAST } from "../../tool/ast/GrammarAST.js";
Expand All @@ -23,6 +24,7 @@ export class InvokeRule extends RuleElement implements LabeledOp {
public readonly labels = new Set<Decl>(); // TODO: should need just 1
public readonly ctxName: string;

@ModelElement
public argExprsChunks: ActionChunk[];

public constructor(factory: ParserFactory, ast: GrammarAST, labelAST: GrammarAST | null) {
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/model/LL1Choice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { ModelElement } from "../../misc/ModelElement.js";
import { GrammarAST } from "../../tool/ast/GrammarAST.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { Choice } from "./Choice.js";
Expand All @@ -16,6 +17,7 @@ export abstract class LL1Choice extends Choice {
/** Token names for each alt 0..n-1 */
public altLook: TokenInfo[][];

@ModelElement
public error: ThrowNoViableAlt;

public constructor(factory: OutputModelFactory, blkAST: GrammarAST,
Expand Down
3 changes: 3 additions & 0 deletions src/codegen/model/LL1Loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Choice } from "./Choice.js";
import { CodeBlockForAlt } from "./CodeBlockForAlt.js";
import { OutputModelObject } from "./OutputModelObject.js";
import { SrcOp } from "./SrcOp.js";
import { ModelElement } from "../../misc/ModelElement.js";

export abstract class LL1Loop extends Choice {

Expand All @@ -22,8 +23,10 @@ export abstract class LL1Loop extends Choice {
public blockStartStateNumber: number;
public loopBackStateNumber: number;

@ModelElement
public loopExpr: OutputModelObject | null;

@ModelElement
public iteration: SrcOp[] = [];

public constructor(factory: OutputModelFactory,
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/model/LL1OptionalBlockSingleAlt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { DecisionState } from "antlr4ng";
import { ModelElement } from "../../misc/ModelElement.js";
import { GrammarAST } from "../../tool/ast/GrammarAST.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { CodeBlockForAlt } from "./CodeBlockForAlt.js";
Expand All @@ -13,9 +14,10 @@ import { SrcOp } from "./SrcOp.js";

/** (A B C)? */
export class LL1OptionalBlockSingleAlt extends LL1Choice {

@ModelElement
public expr: SrcOp | null;

@ModelElement
public followExpr: SrcOp[]; // might not work in template if size>1

public constructor(factory: OutputModelFactory,
Expand Down
3 changes: 3 additions & 0 deletions src/codegen/model/LexerFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { grammarOptions } from "../../grammar-options.js";
import { ModelElement } from "../../misc/ModelElement.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { Action } from "./Action.js";
import { Lexer } from "./Lexer.js";
Expand All @@ -16,8 +17,10 @@ export class LexerFile extends OutputFile {
public genListener: boolean; // from -listener cmd-line
public genVisitor: boolean; // from -visitor cmd-line

@ModelElement
public lexer: Lexer;

@ModelElement
public namedActions: Map<string, Action>;

public constructor(factory: OutputModelFactory, fileName: string) {
Expand Down
3 changes: 3 additions & 0 deletions src/codegen/model/ListenerFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { grammarOptions } from "../../grammar-options.js";
import { ModelElement } from "../../misc/ModelElement.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { Action } from "./Action.js";
import { OutputFile } from "./OutputFile.js";
Expand All @@ -30,8 +31,10 @@ export class ListenerFile extends OutputFile {
*/
public listenerLabelRuleNames = new Map<string, string>();

@ModelElement
public header: Action;

@ModelElement
public namedActions: Map<string, Action>;

public constructor(factory: OutputModelFactory, fileName: string) {
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/model/Loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { ModelElement } from "../../misc/ModelElement.js";
import { GrammarAST } from "../../tool/ast/GrammarAST.js";
import { QuantifierAST } from "../../tool/ast/QuantifierAST.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
Expand All @@ -16,6 +17,7 @@ export class Loop extends Choice {
public loopBackStateNumber: number;
public readonly exitAlt: number;

@ModelElement
public iteration: SrcOp[] = [];

public constructor(factory: OutputModelFactory,
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/model/MatchSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

import { SetTransition } from "antlr4ng";

import { ModelElement } from "../../misc/ModelElement.js";
import { GrammarAST } from "../../tool/ast/GrammarAST.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { CaptureNextTokenType } from "./CaptureNextTokenType.js";
Expand All @@ -13,9 +15,10 @@ import { MatchToken } from "./MatchToken.js";
import { TestSetInline } from "./TestSetInline.js";

export class MatchSet extends MatchToken {

@ModelElement
public expr: TestSetInline;

@ModelElement
public capture: CaptureNextTokenType;

public constructor(factory: OutputModelFactory, ast: GrammarAST) {
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/model/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { ModelElement } from "../../misc/ModelElement.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { ParserFile } from "./ParserFile.js";
import { Recognizer } from "./Recognizer.js";
Expand All @@ -12,6 +13,7 @@ import { RuleFunction } from "./RuleFunction.js";
export class Parser extends Recognizer {
public file: ParserFile;

@ModelElement
public funcs = new Array<RuleFunction>();

public constructor(factory: OutputModelFactory, file: ParserFile) {
Expand Down
7 changes: 6 additions & 1 deletion src/codegen/model/ParserFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { grammarOptions } from "../../grammar-options.js";
import { ModelElement } from "../../misc/ModelElement.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { Action } from "./Action.js";
import { ActionChunk } from "./chunk/ActionChunk.js";
Expand All @@ -18,12 +19,16 @@ export class ParserFile extends OutputFile {
public genListener: boolean; // from -listener cmd-line
public genVisitor: boolean; // from -visitor cmd-line

public grammarName: string;

@ModelElement
public parser: Parser;

@ModelElement
public namedActions: Map<string, Action>;

@ModelElement
public contextSuperClass: ActionChunk;
public grammarName: string;

public constructor(factory: OutputModelFactory, fileName: string) {
super(factory, fileName);
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/model/PlusBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { OutputModelFactory } from "../OutputModelFactory.js";
import { CodeBlockForAlt } from "./CodeBlockForAlt.js";
import { Loop } from "./Loop.js";
import { ThrowNoViableAlt } from "./ThrowNoViableAlt.js";
import { ModelElement } from "../../misc/ModelElement.js";

export class PlusBlock extends Loop {

@ModelElement
public error: ThrowNoViableAlt;

public constructor(factory: OutputModelFactory,
Expand Down
6 changes: 5 additions & 1 deletion src/codegen/model/Recognizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { ModelElement } from "../../misc/ModelElement.js";
import { Rule } from "../../tool/Rule.js";
import { CodeGenerator } from "../CodeGenerator.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
Expand Down Expand Up @@ -35,10 +36,13 @@ export abstract class Recognizer extends OutputModelObject {
public ruleNames: Set<string>;
public rules: Rule[];

public superClass?: ActionChunk;
@ModelElement
public superClass: ActionChunk;

@ModelElement
public atn: SerializedATN;

@ModelElement
public sempredFuncs = new Map<Rule, RuleSempredFunction>();

public constructor(factory: OutputModelFactory) {
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/model/RuleActionFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { ModelElement } from "../../misc/ModelElement.js";
import { Rule } from "../../tool/Rule.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { Action } from "./Action.js";
Expand All @@ -16,6 +17,7 @@ export class RuleActionFunction extends OutputModelObject {
public readonly ruleIndex: number;

/** Map actionIndex to Action */
@ModelElement
public actions = new Map<number, Action>();

public constructor(factory: OutputModelFactory, r: Rule, ctxType: string) {
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/model/SemPred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* can be found in the LICENSE.txt file in the project root.
*/

import { ModelElement } from "../../misc/ModelElement.js";
import { ActionAST } from "../../tool/ast/ActionAST.js";
import { ActionTranslator } from "../ActionTranslator.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
Expand Down Expand Up @@ -33,7 +34,7 @@ export class SemPred extends Action {
* <p>
* {@code {pred}?<fail={"Java literal"}>}</p>
*/

@ModelElement
public failChunks: ActionChunk[];

public constructor(factory: OutputModelFactory, ast: ActionAST) {
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/model/VisitorFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { grammarOptions } from "../../grammar-options.js";
import { ModelElement } from "../../misc/ModelElement.js";
import { OutputModelFactory } from "../OutputModelFactory.js";
import { Action } from "./Action.js";
import { OutputFile } from "./OutputFile.js";
Expand All @@ -28,16 +29,18 @@ export class VisitorFile extends OutputFile {
*/
public visitorLabelRuleNames = new Map<string, string>();

@ModelElement
public header: Action;

@ModelElement
public namedActions: Map<string, Action>;

public constructor(factory: OutputModelFactory, fileName: string) {
super(factory, fileName);

const g = factory.getGrammar()!;
this.namedActions = this.buildNamedActions(g, (ast) => {
return ast.getScope() === null;
return ast.getScope() === null;
});
this.parserName = g.getRecognizerName();
this.grammarName = g.name;
Expand Down
Loading

0 comments on commit e6869b5

Please sign in to comment.