Skip to content

Commit

Permalink
Transition.serializationType is now named transitionType
Browse files Browse the repository at this point in the history
Also, ambigAlts in error reports are now undefined instead of null.

Signed-off-by: Mike Lischke <[email protected]>
  • Loading branch information
mike-lischke committed Feb 25, 2024
1 parent 7556514 commit 1bb36a1
Show file tree
Hide file tree
Showing 25 changed files with 104 additions and 140 deletions.
2 changes: 1 addition & 1 deletion src/ANTLRErrorListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export interface ANTLRErrorListener {
startIndex: number,
stopIndex: number,
exact: boolean,
ambigAlts: BitSet | null,
ambigAlts: BitSet | undefined,
configs: ATNConfigSet): void;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/BaseErrorListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class BaseErrorListener implements ANTLRErrorListener {
}

public reportAmbiguity(recognizer: Parser, dfa: DFA, startIndex: number, stopIndex: number, exact: boolean,
ambigAlts: BitSet | null, configs: ATNConfigSet): void {
ambigAlts: BitSet | undefined, configs: ATNConfigSet): void {
}

public reportAttemptingFullContext(recognizer: Parser, dfa: DFA, startIndex: number, stopIndex: number,
Expand Down
6 changes: 3 additions & 3 deletions src/DiagnosticErrorListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class DiagnosticErrorListener extends BaseErrorListener {
startIndex: number,
stopIndex: number,
exact: boolean,
ambigAlts: BitSet | null,
ambigAlts: BitSet | undefined,
configs: ATNConfigSet): void => {
if (this.exactOnly && !exact) {
return;
Expand Down Expand Up @@ -124,9 +124,9 @@ export class DiagnosticErrorListener extends BaseErrorListener {
* @returns Returns `reportedAlts` if it is not `null`, otherwise
* returns the set of alternatives represented in `configs`.
*/
protected getConflictingAlts = (reportedAlts: BitSet | null,
protected getConflictingAlts = (reportedAlts: BitSet | undefined,
configs: ATNConfigSet): BitSet | null => {
if (reportedAlts !== null) {
if (reportedAlts) {
return reportedAlts;
}

Expand Down
24 changes: 12 additions & 12 deletions src/ParserInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { RecognitionException } from "./RecognitionException.js";
import { StarLoopEntryState } from "./atn/StarLoopEntryState.js";
import { Token } from "./Token.js";
import { ATNStateType } from "./atn/ATNStateType.js";
import { TransitionType } from "./atn/TransitionType.js";
import { DFA } from "./dfa/DFA.js";
import { PredictionContextCache } from "./atn/PredictionContextCache.js";
import { ATN } from "./atn/ATN.js";
Expand All @@ -32,6 +31,7 @@ import { PrecedencePredicateTransition } from "./atn/PrecedencePredicateTransiti
import { DecisionState } from "./atn/DecisionState.js";
import { TokenSource } from "./TokenSource.js";
import { CharStream } from "./CharStream.js";
import { Transition } from "./atn/Transition.js";

export class ParserInterpreter extends Parser {
public rootContext: InterpreterRuleContext;
Expand Down Expand Up @@ -180,8 +180,8 @@ export class ParserInterpreter extends Parser {
}

const transition = p.transitions[predictedAlt - 1];
switch (transition.serializationType) {
case TransitionType.EPSILON:
switch (transition.transitionType) {
case Transition.EPSILON:
if (this.#pushRecursionContextStates.get(p.stateNumber) &&
!(transition.target.stateType === ATNStateType.LOOP_END)) {
// We are at the start of a left recursive rule's (...)* loop
Expand All @@ -194,24 +194,24 @@ export class ParserInterpreter extends Parser {
}
break;

case TransitionType.ATOM:
case Transition.ATOM:
this.match(transition.label!.minElement);
break;

case TransitionType.RANGE:
case TransitionType.SET:
case TransitionType.NOT_SET:
case Transition.RANGE:
case Transition.SET:
case Transition.NOT_SET:
if (!transition.matches(this.inputStream.LA(1), Token.MIN_USER_TOKEN_TYPE, 65535)) {
this.recoverInline();
}
this.matchWildcard();
break;

case TransitionType.WILDCARD:
case Transition.WILDCARD:
this.matchWildcard();
break;

case TransitionType.RULE:
case Transition.RULE:
const ruleStartState = transition.target as RuleStartState;
const ruleIndex = ruleStartState.ruleIndex;
const newContext = this.createInterpreterRuleContext(this.context, p.stateNumber, ruleIndex);
Expand All @@ -224,20 +224,20 @@ export class ParserInterpreter extends Parser {
}
break;

case TransitionType.PREDICATE:
case Transition.PREDICATE:
const predicateTransition = transition as PredicateTransition;
if (!this.sempred(this.context, predicateTransition.ruleIndex, predicateTransition.predIndex)) {
throw new FailedPredicateException(this);
}

break;

case TransitionType.ACTION:
case Transition.ACTION:
const actionTransition = transition as ActionTransition;
this.action(this.context, actionTransition.ruleIndex, actionTransition.actionIndex);
break;

case TransitionType.PRECEDENCE:
case Transition.PRECEDENCE:
if (!this.precpred(this.context, (transition as PrecedencePredicateTransition).precedence)) {
const precedence = (transition as PrecedencePredicateTransition).precedence;
throw new FailedPredicateException(this, `precpred(_ctx, ${precedence})`);
Expand Down
2 changes: 1 addition & 1 deletion src/ProxyErrorListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ProxyErrorListener extends BaseErrorListener {
}

public override reportAmbiguity(recognizer: Parser, dfa: DFA, startIndex: number, stopIndex: number, exact: boolean,
ambigAlts: BitSet | null, configs: ATNConfigSet): void {
ambigAlts: BitSet | undefined, configs: ATNConfigSet): void {
this.delegates.forEach((d) => {
d.reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs);
});
Expand Down
21 changes: 10 additions & 11 deletions src/atn/ATNDeserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import { LexerPushModeAction } from "./LexerPushModeAction.js";
import { LexerPopModeAction } from "./LexerPopModeAction.js";
import { LexerModeAction } from "./LexerModeAction.js";
import { ATNStateType } from "./ATNStateType.js";
import { TransitionType } from "./TransitionType.js";
import { ATNState } from "./ATNState.js";
import { LexerAction } from "./LexerAction.js";
import { Transition } from "./Transition.js";
Expand Down Expand Up @@ -505,27 +504,27 @@ export class ATNDeserializer {
arg3: number, sets: IntervalSet[]): Transition {
const target = atn.states[trg]!;
switch (type) {
case TransitionType.EPSILON:
case Transition.EPSILON:
return new EpsilonTransition(target);
case TransitionType.RANGE:
case Transition.RANGE:
return arg3 !== 0
? new RangeTransition(target, Token.EOF, arg2)
: new RangeTransition(target, arg1, arg2);
case TransitionType.RULE:
case Transition.RULE:
return new RuleTransition(atn.states[arg1]!, arg2, arg3, target);
case TransitionType.PREDICATE:
case Transition.PREDICATE:
return new PredicateTransition(target, arg1, arg2, arg3 !== 0);
case TransitionType.PRECEDENCE:
case Transition.PRECEDENCE:
return new PrecedencePredicateTransition(target, arg1);
case TransitionType.ATOM:
case Transition.ATOM:
return arg3 !== 0 ? new AtomTransition(target, Token.EOF) : new AtomTransition(target, arg1);
case TransitionType.ACTION:
case Transition.ACTION:
return new ActionTransition(target, arg1, arg2, arg3 !== 0);
case TransitionType.SET:
case Transition.SET:
return new SetTransition(target, sets[arg1]);
case TransitionType.NOT_SET:
case Transition.NOT_SET:
return new NotSetTransition(target, sets[arg1]);
case TransitionType.WILDCARD:
case Transition.WILDCARD:
return new WildcardTransition(target);
default:
throw new Error("The specified transition type: " + type + " is not valid.");
Expand Down
26 changes: 13 additions & 13 deletions src/atn/ATNSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type { RangeTransition } from "./RangeTransition.js";
import { RuleStartState } from "./RuleStartState.js";
import type { RuleTransition } from "./RuleTransition.js";
import type { SetTransition } from "./SetTransition.js";
import { TransitionType } from "./TransitionType.js";
import { Transition } from "./Transition.js";

/**
* This class represents a target neutral serializer for ATNs. An ATN is converted to a list of integers
Expand Down Expand Up @@ -253,8 +253,8 @@ export class ATNSerializer {
}

for (const t of s.transitions) {
const edgeType = t.serializationType;
if (edgeType === TransitionType.SET || edgeType === TransitionType.NOT_SET) {
const edgeType = t.transitionType;
if (edgeType === Transition.SET || edgeType === Transition.NOT_SET) {
const st = t as SetTransition;
this.sets.set(st.set, true);
}
Expand Down Expand Up @@ -285,34 +285,34 @@ export class ATNSerializer {

const src = s.stateNumber;
let trg = t.target.stateNumber;
const edgeType = t.serializationType;
const edgeType = t.transitionType;
let arg1 = 0;
let arg2 = 0;
let arg3 = 0;
switch (edgeType) {
case TransitionType.RULE: {
case Transition.RULE: {
trg = (t as RuleTransition).followState.stateNumber;
arg1 = (t as RuleTransition).target.stateNumber;
arg2 = (t as RuleTransition).ruleIndex;
arg3 = (t as RuleTransition).precedence;
break;
}

case TransitionType.PRECEDENCE: {
case Transition.PRECEDENCE: {
const ppt = t as PrecedencePredicateTransition;
arg1 = ppt.precedence;
break;
}

case TransitionType.PREDICATE: {
case Transition.PREDICATE: {
const pt = t as PredicateTransition;
arg1 = pt.ruleIndex;
arg2 = pt.predIndex;
arg3 = pt.isCtxDependent ? 1 : 0;
break;
}

case TransitionType.RANGE: {
case Transition.RANGE: {
arg1 = (t as RangeTransition).start;
arg2 = (t as RangeTransition).stop;
if (arg1 === Token.EOF) {
Expand All @@ -322,7 +322,7 @@ export class ATNSerializer {
break;
}

case TransitionType.ATOM: {
case Transition.ATOM: {
arg1 = (t as AtomTransition).labelValue;
if (arg1 === Token.EOF) {
arg1 = 0;
Expand All @@ -331,25 +331,25 @@ export class ATNSerializer {
break;
}

case TransitionType.ACTION: {
case Transition.ACTION: {
const at = t as ActionTransition;
arg1 = at.ruleIndex;
arg2 = at.actionIndex;
arg3 = at.isCtxDependent ? 1 : 0;
break;
}

case TransitionType.SET: {
case Transition.SET: {
arg1 = setIndices.get((t as SetTransition).set)!;
break;
}

case TransitionType.NOT_SET: {
case Transition.NOT_SET: {
arg1 = setIndices.get((t as SetTransition).set)!;
break;
}

case TransitionType.WILDCARD: {
case Transition.WILDCARD: {
break;
}

Expand Down
1 change: 1 addition & 0 deletions src/atn/AbstractPredicateTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { ATNState } from "./ATNState.js";
import { Transition } from "./Transition.js";

/** Used as base for PredicateTransition and PrecedencePredicateTransition, without adding their individual fields. */
export abstract class AbstractPredicateTransition extends Transition {
public constructor(target: ATNState) {
super(target);
Expand Down
5 changes: 2 additions & 3 deletions src/atn/ActionTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { ATNState } from "./ATNState.js";
import { Transition } from "./Transition.js";
import { TransitionType } from "./TransitionType.js";

export class ActionTransition extends Transition {
public ruleIndex: number;
Expand All @@ -24,8 +23,8 @@ export class ActionTransition extends Transition {
return true;
}

public get serializationType(): number {
return TransitionType.ACTION;
public get transitionType(): number {
return Transition.ACTION;
}

public matches(_symbol: number, _minVocabSymbol: number, _maxVocabSymbol: number): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/atn/AmbiguityInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ import { DecisionEventInfo } from "./DecisionEventInfo.js";
*/
export interface AmbiguityInfo extends DecisionEventInfo {
/** The set of alternative numbers for this decision event that lead to a valid parse. */
ambigAlts: BitSet | null;
ambigAlts?: BitSet;
}
5 changes: 2 additions & 3 deletions src/atn/AtomTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { IntervalSet } from "../misc/IntervalSet.js";
import { Transition } from "./Transition.js";
import { TransitionType } from "./TransitionType.js";
import { ATNState } from "./ATNState.js";

export class AtomTransition extends Transition {
Expand All @@ -25,8 +24,8 @@ export class AtomTransition extends Transition {
return this.#label;
}

public override get serializationType(): number {
return TransitionType.ATOM;
public override get transitionType(): number {
return Transition.ATOM;
}

public override matches(symbol: number): boolean {
Expand Down
5 changes: 2 additions & 3 deletions src/atn/EpsilonTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { Transition } from "./Transition.js";
import { TransitionType } from "./TransitionType.js";
import { ATNState } from "./ATNState.js";

export class EpsilonTransition extends Transition {
Expand All @@ -32,8 +31,8 @@ export class EpsilonTransition extends Transition {
return true;
}

public get serializationType(): number {
return TransitionType.EPSILON;
public get transitionType(): number {
return Transition.EPSILON;
}

public override matches(): boolean {
Expand Down
Loading

0 comments on commit 1bb36a1

Please sign in to comment.