From 5799e878d66e30916653786fa2e55c0c8abc48a1 Mon Sep 17 00:00:00 2001 From: Mike Lischke Date: Mon, 30 Dec 2024 11:44:37 +0100 Subject: [PATCH] Fixed bug that created additional lexer action entries. The subtleties wrt. to object equality vs. reference equality between Java and TypeScript are a constant source of tricky bugs. However, there's not need to always use a OrderedHashMap if the key is a standard type (a type without equals/hashMap methods) as the standard Map already maintains insertion order. --- src/analysis/LeftRecursiveRuleAnalyzer.ts | 6 +++--- src/automata/LexerATNFactory.ts | 6 +++--- src/tool/Grammar.ts | 3 +-- src/tool/LeftRecursiveRule.ts | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/analysis/LeftRecursiveRuleAnalyzer.ts b/src/analysis/LeftRecursiveRuleAnalyzer.ts index 7223875..d6be601 100644 --- a/src/analysis/LeftRecursiveRuleAnalyzer.ts +++ b/src/analysis/LeftRecursiveRuleAnalyzer.ts @@ -36,9 +36,9 @@ enum Associativity { export class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker { public tool: Tool; - public binaryAlts = new OrderedHashMap(); - public ternaryAlts = new OrderedHashMap(); - public suffixAlts = new OrderedHashMap(); + public binaryAlts = new Map(); + public ternaryAlts = new Map(); + public suffixAlts = new Map(); public prefixAndOtherAlts = new Array(); /** Pointer to ID node of ^(= ID element) */ diff --git a/src/automata/LexerATNFactory.ts b/src/automata/LexerATNFactory.ts index d94da24..755e75a 100644 --- a/src/automata/LexerATNFactory.ts +++ b/src/automata/LexerATNFactory.ts @@ -7,8 +7,8 @@ /* eslint-disable jsdoc/require-param, jsdoc/require-returns */ import { - ActionTransition, ATN, ATNState, AtomTransition, BasicState, CodePointTransitions, CommonToken, IntervalSet, - IntStream, Lexer, LexerAction, LexerChannelAction, LexerCustomAction, LexerModeAction, LexerMoreAction, + ActionTransition, ATN, ATNState, AtomTransition, BasicState, CodePointTransitions, CommonToken, HashMap, + IntervalSet, IntStream, Lexer, LexerAction, LexerChannelAction, LexerCustomAction, LexerModeAction, LexerMoreAction, LexerPopModeAction, LexerPushModeAction, LexerSkipAction, LexerTypeAction, NotSetTransition, SetTransition, Token, TokensStartState, Transition } from "antlr4ng"; @@ -122,7 +122,7 @@ export class LexerATNFactory extends ParserATNFactory { /** * Maps from a {@link LexerAction} object to the action index. */ - private actionToIndexMap = new Map(); + private actionToIndexMap = new HashMap(); private readonly ruleCommands = new Array(); diff --git a/src/tool/Grammar.ts b/src/tool/Grammar.ts index bd1e942..dc5c1be 100644 --- a/src/tool/Grammar.ts +++ b/src/tool/Grammar.ts @@ -31,7 +31,6 @@ import { TokenVocabParser } from "../parse/TokenVocabParser.js"; import { GrammarType } from "../support/GrammarType.js"; import type { IGrammar, ITool } from "../types.js"; -import { OrderedHashMap } from "../misc/OrderedHashMap.js"; import type { CommonTree } from "../tree/CommonTree.js"; import { ANTLRMessage } from "./ANTLRMessage.js"; import { ANTLRToolListener } from "./ANTLRToolListener.js"; @@ -151,7 +150,7 @@ export class Grammar implements IGrammar, AttributeResolver { * All rules defined in this specific grammar, not imported. Also does * not include lexical rules if combined. */ - public rules = new OrderedHashMap(); + public rules = new Map(); public indexToRule = new Array(); // used to invent rule names for 'keyword', ';', ... (0..n-1) diff --git a/src/tool/LeftRecursiveRule.ts b/src/tool/LeftRecursiveRule.ts index a830183..58f109b 100644 --- a/src/tool/LeftRecursiveRule.ts +++ b/src/tool/LeftRecursiveRule.ts @@ -115,7 +115,7 @@ export class LeftRecursiveRule extends Rule { /** Get -> labels from those alts we deleted for left-recursive rules. */ public override getAltLabels(): Map> | null { - const labels = new OrderedHashMap>(); + const labels = new Map>(); const normalAltLabels = super.getAltLabels(); if (normalAltLabels !== null) { normalAltLabels.forEach((value, key) => {