antlr4ts
Version:
ANTLR 4 runtime for JavaScript written in Typescript
186 lines • 6.28 kB
JavaScript
"use strict";
/*!
* Copyright 2016 The ANTLR Project. All rights reserved.
* Licensed under the BSD-3-Clause license. See LICENSE file in the project root for license information.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ATNState = void 0;
const Decorators_1 = require("../Decorators");
const INITIAL_NUM_TRANSITIONS = 4;
/**
* The following images show the relation of states and
* {@link ATNState#transitions} for various grammar constructs.
*
* * Solid edges marked with an ε indicate a required
* {@link EpsilonTransition}.
*
* * Dashed edges indicate locations where any transition derived from
* {@link Transition} might appear.
*
* * 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.
*
* * Nodes showing multiple outgoing alternatives with a `...` support
* any number of alternatives (one or more). Nodes without the `...` only
* support the exact number of alternatives shown in the diagram.
*
* <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: `(...)*`</h3>
*
* <embed src="images/ClosureGreedy.svg" type="image/svg+xml"/>
*
* <h3>Greedy Positive Closure: `(...)+`</h3>
*
* <embed src="images/PositiveClosureGreedy.svg" type="image/svg+xml"/>
*
* <h3>Greedy Optional: `(...)?`</h3>
*
* <embed src="images/OptionalGreedy.svg" type="image/svg+xml"/>
*
* <h2>Non-Greedy Loops</h2>
*
* <h3>Non-Greedy Closure: `(...)*?`</h3>
*
* <embed src="images/ClosureNonGreedy.svg" type="image/svg+xml"/>
*
* <h3>Non-Greedy Positive Closure: `(...)+?`</h3>
*
* <embed src="images/PositiveClosureNonGreedy.svg" type="image/svg+xml"/>
*
* <h3>Non-Greedy Optional: `(...)??`</h3>
*
* <embed src="images/OptionalNonGreedy.svg" type="image/svg+xml"/>
*/
class ATNState {
constructor() {
this.stateNumber = ATNState.INVALID_STATE_NUMBER;
this.ruleIndex = 0; // at runtime, we don't have Rule objects
this.epsilonOnlyTransitions = false;
/** Track the transitions emanating from this ATN state. */
this.transitions = [];
this.optimizedTransitions = this.transitions;
}
/**
* Gets the state number.
*
* @returns the state number
*/
getStateNumber() {
return this.stateNumber;
}
/**
* For all states except {@link RuleStopState}, this returns the state
* number. Returns -1 for stop states.
*
* @returns -1 for {@link RuleStopState}, otherwise the state number
*/
get nonStopStateNumber() {
return this.getStateNumber();
}
hashCode() {
return this.stateNumber;
}
equals(o) {
// are these states same object?
if (o instanceof ATNState) {
return this.stateNumber === o.stateNumber;
}
return false;
}
get isNonGreedyExitState() {
return false;
}
toString() {
return String(this.stateNumber);
}
getTransitions() {
return this.transitions.slice(0);
}
get numberOfTransitions() {
return this.transitions.length;
}
addTransition(e, index) {
if (this.transitions.length === 0) {
this.epsilonOnlyTransitions = e.isEpsilon;
}
else if (this.epsilonOnlyTransitions !== e.isEpsilon) {
this.epsilonOnlyTransitions = false;
throw new Error("ATN state " + this.stateNumber + " has both epsilon and non-epsilon transitions.");
}
this.transitions.splice(index !== undefined ? index : this.transitions.length, 0, e);
}
transition(i) {
return this.transitions[i];
}
setTransition(i, e) {
this.transitions[i] = e;
}
removeTransition(index) {
return this.transitions.splice(index, 1)[0];
}
get onlyHasEpsilonTransitions() {
return this.epsilonOnlyTransitions;
}
setRuleIndex(ruleIndex) {
this.ruleIndex = ruleIndex;
}
get isOptimized() {
return this.optimizedTransitions !== this.transitions;
}
get numberOfOptimizedTransitions() {
return this.optimizedTransitions.length;
}
getOptimizedTransition(i) {
return this.optimizedTransitions[i];
}
addOptimizedTransition(e) {
if (!this.isOptimized) {
this.optimizedTransitions = new Array();
}
this.optimizedTransitions.push(e);
}
setOptimizedTransition(i, e) {
if (!this.isOptimized) {
throw new Error("This ATNState is not optimized.");
}
this.optimizedTransitions[i] = e;
}
removeOptimizedTransition(i) {
if (!this.isOptimized) {
throw new Error("This ATNState is not optimized.");
}
this.optimizedTransitions.splice(i, 1);
}
}
__decorate([
Decorators_1.Override
], ATNState.prototype, "hashCode", null);
__decorate([
Decorators_1.Override
], ATNState.prototype, "equals", null);
__decorate([
Decorators_1.Override
], ATNState.prototype, "toString", null);
exports.ATNState = ATNState;
(function (ATNState) {
ATNState.INVALID_STATE_NUMBER = -1;
})(ATNState = exports.ATNState || (exports.ATNState = {}));
//# sourceMappingURL=ATNState.js.map