Skip to content

Commit

Permalink
TestATNParserPrediction + TestATNSerialization done
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-lischke committed Dec 1, 2024
1 parent 0d44ae4 commit c16b27e
Show file tree
Hide file tree
Showing 10 changed files with 1,734 additions and 1,714 deletions.
5 changes: 3 additions & 2 deletions src/tool/Grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,9 +825,10 @@ export class Grammar implements IGrammar, AttributeResolver {
* @returns The token names of all tokens defined in the grammar.
*/
public getTokenNames(): Array<string | null> {
const max = this.getMaxTokenType();
const tokenNames: Array<string | null> = [];
for (let i = 0; i < tokenNames.length; i++) {
tokenNames[i] = this.getTokenName(i);
for (let i = 0; i <= max; ++i) {
tokenNames.push(this.getTokenName(i));
}

return tokenNames;
Expand Down
15 changes: 6 additions & 9 deletions src/tool/LeftRecursiveRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ export class LeftRecursiveRule extends Rule {
* That maps predicted alt 1 to original alt 2 and predicted 2 to alt 4.
*/
public getPrimaryAlts(): number[] {
const alts: number[] = [];
for (let i = 0; i < this.recPrimaryAlts.length; i++) { // recPrimaryAlts is a List not Map like recOpAlts
const altInfo = this.recPrimaryAlts[i];
alts[i + 1] = altInfo.altNum;
const alts: number[] = [0];
for (const altInfo of this.recPrimaryAlts) {
alts.push(altInfo.altNum);
}

return alts;
Expand All @@ -106,11 +105,9 @@ export class LeftRecursiveRule extends Rule {
* That maps predicted alt 1 to original alt 1 and predicted 2 to alt 3.
*/
public getRecursiveOpAlts(): number[] {
const alts: number[] = [];
let alt = 1;
for (const altInfo of this.recOpAlts.values()) {
alts[alt] = altInfo.altNum;
alt++; // recOpAlts has alts possibly with gaps
const alts: number[] = [0];
for (const [_, altInfo] of this.recOpAlts) {
alts.push(altInfo.altNum);
}

return alts;
Expand Down
4 changes: 2 additions & 2 deletions tests/ATNDescriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class ATNDescriber {
}
}

return result.toString();
return result;
}

public getTokenName(t: number): string {
Expand Down Expand Up @@ -241,7 +241,7 @@ export class ATNDescriber {
}

private appendSets(input: string, data: number[], p: number, nsets: number): [number, string] {
let result = "";
let result = input;

for (let i = 0; i < nsets; i++) {
const nintervals = data[p++];
Expand Down
11 changes: 5 additions & 6 deletions tests/ParserInterpreterForTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class DummyParser extends Parser {

readonly #atn: ATN;

public constructor(g: Grammar, atn: ATN, input: TokenStream) {
super(input);
public constructor(g: Grammar, atn: ATN, input: TokenStream | null) {
super(input!);
this.g = g;
this.#atn = atn;

Expand Down Expand Up @@ -55,8 +55,8 @@ export class ParserInterpreterForTesting {
protected atnSimulator: ParserATNSimulator;
protected input: TokenStream;

public constructor(g: Grammar, input?: TokenStream) {
if (!input) {
public constructor(g: Grammar, input?: TokenStream | null) {
if (input === undefined) {
this.g = g;
} else {
this.parser = new DummyParser(g, g.atn!, input);
Expand All @@ -65,8 +65,7 @@ export class ParserInterpreterForTesting {
}
}

public adaptivePredict(input: TokenStream, decision: number,
outerContext: ParserRuleContext): number {
public adaptivePredict(input: TokenStream, decision: number, outerContext: ParserRuleContext | null): number {
return this.atnSimulator.adaptivePredict(input, decision, outerContext);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestATNLexerInterpreter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { describe, expect, it } from "vitest";

import { Token, Lexer, CharStream, ATN, ATNState, LexerATNSimulator, DFA } from "antlr4ng";
import { ATN, CharStream, DFA, Lexer, LexerATNSimulator, Token } from "antlr4ng";
import { LexerGrammar } from "../src/tool/index.js";
import { ToolTestUtils } from "./ToolTestUtils.js";

Expand Down
Loading

0 comments on commit c16b27e

Please sign in to comment.