Skip to content

Commit

Permalink
Fixed an error where the labels list in InvokeRule was modified as a …
Browse files Browse the repository at this point in the history
…copy, leading to missing labels.

Closes #25
  • Loading branch information
mike-lischke committed Dec 29, 2024
1 parent b935fe7 commit bdc4607
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/Tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export class Tool implements ITool {
this.grammarFiles = this.toolParameters.args;
this.errorManager = new ErrorManager(this.toolParameters.msgFormat, this.toolParameters.longMessages,
this.toolParameters.warningsAreErrors);
if (this.grammarFiles.length === 0) {
this.errorManager.toolError(ErrorType.NO_GRAMMARS_FOUND);
}
}

public static main(args: string[]): void {
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/ParserFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export class ParserFactory extends DefaultOutputModelFactory {
const actionRefsAsToken = currentOuterMostAlt.tokenRefsInActions.has(id.getText());
const actionRefsAsRule = currentOuterMostAlt.ruleRefsInActions.has(id.getText());

return op.getLabels().length === 0 && (actionRefsAsToken || actionRefsAsRule);
return op.labels.length === 0 && (actionRefsAsToken || actionRefsAsRule);
}

// support
Expand All @@ -358,7 +358,7 @@ export class ParserFactory extends DefaultOutputModelFactory {
}
}

op.getLabels().push(d);
op.labels.push(d);

// all labels must be in scope struct in case we exec action out of context
this.getCurrentRuleFunction()!.addContextDecl(ast.getAltLabel()!, d);
Expand All @@ -370,7 +370,7 @@ export class ParserFactory extends DefaultOutputModelFactory {
const target = this.gen.getTarget();
const listLabel = target.getListLabel(label.getText());
const listRuntimeName = target.escapeIfNeeded(listLabel);
labelOp = new AddToLabelList(this, listRuntimeName, op.getLabels()[0]);
labelOp = new AddToLabelList(this, listRuntimeName, op.labels[0]);
}

return labelOp;
Expand Down
11 changes: 3 additions & 8 deletions src/codegen/model/InvokeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* can be found in the LICENSE.txt file in the project root.
*/

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

Expand All @@ -22,7 +21,7 @@ import { RuleContextListDecl } from "./decl/RuleContextListDecl.js";
export class InvokeRule extends RuleElement implements LabeledOp {
public readonly name: string;
public readonly escapedName: string;
public readonly labels = new OrderedHashSet<Decl>(); // TODO: should need just 1
public readonly labels: Decl[] = []; // TODO: should need just 1
public readonly ctxName: string;

@ModelElement
Expand Down Expand Up @@ -54,7 +53,7 @@ export class InvokeRule extends RuleElement implements LabeledOp {
decl = new RuleContextListDecl(factory, listLabel, this.ctxName);
} else {
decl = new RuleContextDecl(factory, label, this.ctxName);
this.labels.add(decl);
this.labels.push(decl);
}
rf.addContextDecl(ast.getAltLabel()!, decl);
}
Expand All @@ -68,12 +67,8 @@ export class InvokeRule extends RuleElement implements LabeledOp {
if (factory.getCurrentOuterMostAlt().ruleRefsInActions.has(identifier)) {
const label = gen.getTarget().getImplicitRuleLabel(identifier);
const d = new RuleContextDecl(factory, label, this.ctxName);
this.labels.add(d);
this.labels.push(d);
rf.addContextDecl(ast.getAltLabel()!, d);
}
}

public getLabels(): Decl[] {
return this.labels.toArray();
}
}
2 changes: 1 addition & 1 deletion src/codegen/model/LabeledOp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import { Decl } from "./decl/Decl.js";

/** All the rule elements we can label like tokens, rules, sets, wildcard. */
export interface LabeledOp {
getLabels(): Decl[];
readonly labels: Decl[];
}
4 changes: 0 additions & 4 deletions src/codegen/model/MatchToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@ export class MatchToken extends RuleElement implements LabeledOp {
this.escapedName = target.escapeIfNeeded(this.name);
}
}

public getLabels(): Decl[] {
return this.labels;
}
}
3 changes: 2 additions & 1 deletion src/tool/ErrorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ export class ErrorManager {
// [fall-through]
}

case ErrorSeverity.Error: {
case ErrorSeverity.Error:
case ErrorSeverity.Fatal: {
this.error(msg);
break;
}
Expand Down
5 changes: 2 additions & 3 deletions src/tool/ErrorType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import { antlrVersion } from "../tool-parameters.js";
import { ErrorSeverity } from "./ErrorSeverity.js";

export class ErrorType {
/*
* Tool errors
*/
public static readonly NO_GRAMMARS_FOUND = new ErrorType(0, "no grammars found\n",
ErrorSeverity.Fatal, "NO_GRAMMARS_FOUND");

/**
* Compiler Error 1.
Expand Down
16 changes: 8 additions & 8 deletions tests/TestUnicodeData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe("TestUnicodeData", () => {

// The shortcut "L" is used in multiple categories, which would fail this test.
// Instead we use an explicit category, to make it work.
checkValue("general_category=L", 0x58);
checkValue("general_category=L", 0x78);
checkValue("L", 0x58);
checkValue("L", 0x78);
checkValue("N", 0x30);
checkValue("Z", 0x20);
});
Expand All @@ -37,8 +37,8 @@ describe("TestUnicodeData", () => {
checkValue("Lu", 0x1E96, false);
checkValue("Ll", 0x1E3B);
checkValue("Ll", 0x1E00, false);
checkValue("general_category=L", 0x1E3A);
checkValue("general_category=L", 0x1E3B);
checkValue("L", 0x1E3A);
checkValue("L", 0x1E3B);
checkValue("N", 0x1BB0);
checkValue("N", 0x1E3A, false);
checkValue("Z", 0x2028);
Expand All @@ -50,8 +50,8 @@ describe("TestUnicodeData", () => {
checkValue("Lu", 0x1D770, false);
checkValue("Ll", 0x1D770);
checkValue("Ll", 0x1D5D5, false);
checkValue("general_category=L", 0x1D5D4);
checkValue("general_category=L", 0x1D770);
checkValue("L", 0x1D5D4);
checkValue("L", 0x1D770);
checkValue("N", 0x11C50);
checkValue("N", 0x1D5D4, false);
});
Expand Down Expand Up @@ -128,8 +128,8 @@ describe("TestUnicodeData", () => {
});

it("testPropertyCaseInsensitivity", () => {
checkValue("general_category=l", 0x78);
checkValue("general_category=l", 0x30, false);
checkValue("l", 0x78);
checkValue("l", 0x30, false);
checkValue("common", 0x30);
});

Expand Down

0 comments on commit bdc4607

Please sign in to comment.