UNPKG

antlr4-runtime

Version:

JavaScript runtime for ANTLR4

164 lines (135 loc) 4.15 kB
/* Copyright (c) 2012-2022 The ANTLR Project. All rights reserved. * Use of this file is governed by the BSD 3-clause license that * can be found in the LICENSE.txt file in the project root. */ /** * The following images show the relation of states and * {@link ATNState//transitions} for various grammar constructs. * * <ul> * * <li>Solid edges marked with an &//0949; indicate a required * {@link EpsilonTransition}.</li> * * <li>Dashed edges indicate locations where any transition derived from * {@link Transition} might appear.</li> * * <li>Dashed nodes are place holders for either a sequence of linked * {@link BasicState} states or the inclusion of a block representing a nested * construct in one of the forms below.</li> * * <li>Nodes showing multiple outgoing alternatives with a {@code ...} support * any number of alternatives (one or more). Nodes without the {@code ...} only * support the exact number of alternatives shown in the diagram.</li> * * </ul> * * <h2>Basic Blocks</h2> * * <h3>Rule</h3> * * <embed src="images/Rule.svg" type="image/svg+xml"/> * * <h3>Block of 1 or more alternatives</h3> * * <embed src="images/Block.svg" type="image/svg+xml"/> * * <h2>Greedy Loops</h2> * * <h3>Greedy Closure: {@code (...)*}</h3> * * <embed src="images/ClosureGreedy.svg" type="image/svg+xml"/> * * <h3>Greedy Positive Closure: {@code (...)+}</h3> * * <embed src="images/PositiveClosureGreedy.svg" type="image/svg+xml"/> * * <h3>Greedy Optional: {@code (...)?}</h3> * * <embed src="images/OptionalGreedy.svg" type="image/svg+xml"/> * * <h2>Non-Greedy Loops</h2> * * <h3>Non-Greedy Closure: {@code (...)*?}</h3> * * <embed src="images/ClosureNonGreedy.svg" type="image/svg+xml"/> * * <h3>Non-Greedy Positive Closure: {@code (...)+?}</h3> * * <embed src="images/PositiveClosureNonGreedy.svg" type="image/svg+xml"/> * * <h3>Non-Greedy Optional: {@code (...)??}</h3> * * <embed src="images/OptionalNonGreedy.svg" type="image/svg+xml"/> */ export default class ATNState { constructor() { // Which ATN are we in? this.atn = null; this.stateNumber = ATNState.INVALID_STATE_NUMBER; this.stateType = null; this.ruleIndex = 0; // at runtime, we don't have Rule objects this.epsilonOnlyTransitions = false; // Track the transitions emanating from this ATN state. this.transitions = []; // Used to cache lookahead during parsing, not used during construction this.nextTokenWithinRule = null; } toString() { return this.stateNumber; } equals(other) { if (other instanceof ATNState) { return this.stateNumber===other.stateNumber; } else { return false; } } isNonGreedyExitState() { return false; } addTransition(trans, index) { if(index===undefined) { index = -1; } if (this.transitions.length===0) { this.epsilonOnlyTransitions = trans.isEpsilon; } else if(this.epsilonOnlyTransitions !== trans.isEpsilon) { this.epsilonOnlyTransitions = false; } if (index===-1) { this.transitions.push(trans); } else { this.transitions.splice(index, 1, trans); } } } // constants for serialization ATNState.INVALID_TYPE = 0; ATNState.BASIC = 1; ATNState.RULE_START = 2; ATNState.BLOCK_START = 3; ATNState.PLUS_BLOCK_START = 4; ATNState.STAR_BLOCK_START = 5; ATNState.TOKEN_START = 6; ATNState.RULE_STOP = 7; ATNState.BLOCK_END = 8; ATNState.STAR_LOOP_BACK = 9; ATNState.STAR_LOOP_ENTRY = 10; ATNState.PLUS_LOOP_BACK = 11; ATNState.LOOP_END = 12; ATNState.serializationNames = [ "INVALID", "BASIC", "RULE_START", "BLOCK_START", "PLUS_BLOCK_START", "STAR_BLOCK_START", "TOKEN_START", "RULE_STOP", "BLOCK_END", "STAR_LOOP_BACK", "STAR_LOOP_ENTRY", "PLUS_LOOP_BACK", "LOOP_END" ]; ATNState.INVALID_STATE_NUMBER = -1;