UNPKG

antlr4ng

Version:

Alternative JavaScript/TypeScript runtime for ANTLR4

182 lines (177 loc) 5.04 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/misc/Interval.ts var Interval = class _Interval { static { __name(this, "Interval"); } static INVALID_INTERVAL = new _Interval(-1, -2); static INTERVAL_POOL_MAX_VALUE = 1e3; static cache = []; start; stop; cachedHashCode; constructor(start, stop) { this.start = start; this.stop = stop; this.cachedHashCode = Math.imul(651 + start, 31) + stop; } /** * Creates a new interval from the given values. * * Interval objects are used readonly so share all with the * same single value a==b up to some max size. Use an array as a perfect hash. * Return shared object for 0..INTERVAL_POOL_MAX_VALUE or a new * Interval object with a..a in it. On Java.g4, 218623 IntervalSets * have a..a (set with 1 element). * * @param a The start of the interval. * @param b The end of the interval (inclusive). * * @returns A cached or new interval. */ static of(a, b) { if (a !== b || a < 0 || a > _Interval.INTERVAL_POOL_MAX_VALUE) { return new _Interval(a, b); } if (!_Interval.cache[a]) { _Interval.cache[a] = new _Interval(a, a); } return _Interval.cache[a]; } equals(o) { return this.start === o.start && this.stop === o.stop; } hashCode() { return this.cachedHashCode; } /** Does this start completely before other? Disjoint */ startsBeforeDisjoint(other) { return this.start < other.start && this.stop < other.start; } /** Does this start at or before other? Nondisjoint */ startsBeforeNonDisjoint(other) { return this.start <= other.start && this.stop >= other.start; } /** Does this.start start after other.stop? May or may not be disjoint */ startsAfter(other) { return this.start > other.start; } /** Does this start completely after other? Disjoint */ startsAfterDisjoint(other) { return this.start > other.stop; } /** Does this start after other? NonDisjoint */ startsAfterNonDisjoint(other) { return this.start > other.start && this.start <= other.stop; } /** Are both ranges disjoint? I.e., no overlap? */ disjoint(other) { return this.startsBeforeDisjoint(other) || this.startsAfterDisjoint(other); } /** Are two intervals adjacent such as 0..41 and 42..42? */ adjacent(other) { return this.start === other.stop + 1 || this.stop === other.start - 1; } properlyContains(other) { return other.start >= this.start && other.stop <= this.stop; } /** Return the interval computed from combining this and other */ union(other) { return _Interval.of(Math.min(this.start, other.start), Math.max(this.stop, other.stop)); } /** Return the interval in common between this and o */ intersection(other) { return _Interval.of(Math.max(this.start, other.start), Math.min(this.stop, other.stop)); } /** * Return the interval with elements from this not in other; * other must not be totally enclosed (properly contained) * within this, which would result in two disjoint intervals * instead of the single one returned by this method. */ differenceNotProperlyContained(other) { let diff = null; if (other.startsBeforeNonDisjoint(this)) { diff = _Interval.of(Math.max(this.start, other.stop + 1), this.stop); } else if (other.startsAfterNonDisjoint(this)) { diff = _Interval.of(this.start, other.start - 1); } return diff; } toString() { return `${this.start}..${this.stop}`; } get length() { if (this.stop < this.start) { return 0; } return this.stop - this.start + 1; } }; // src/IntStream.ts var IntStream; ((IntStream2) => { IntStream2.EOF = -1; IntStream2.UNKNOWN_SOURCE_NAME = "<unknown>"; })(IntStream || (IntStream = {})); // src/Token.ts var Token; ((Token2) => { Token2.INVALID_TYPE = 0; Token2.EPSILON = -2; Token2.MIN_USER_TOKEN_TYPE = 1; Token2.EOF = IntStream.EOF; Token2.DEFAULT_CHANNEL = 0; Token2.HIDDEN_CHANNEL = 1; Token2.MIN_USER_CHANNEL_VALUE = 2; })(Token || (Token = {})); // src/tree/TerminalNode.ts var TerminalNode = class { static { __name(this, "TerminalNode"); } parent = null; symbol; constructor(symbol) { this.symbol = symbol; } getChild(_i) { return null; } getSymbol() { return this.symbol; } getPayload() { return this.symbol; } getSourceInterval() { if (this.symbol === null) { return Interval.INVALID_INTERVAL; } const tokenIndex = this.symbol.tokenIndex; return new Interval(tokenIndex, tokenIndex); } getChildCount() { return 0; } accept(visitor) { return visitor.visitTerminal(this); } getText() { return this.symbol?.text ?? ""; } toString() { if (this.symbol?.type === Token.EOF) { return "<EOF>"; } else { return this.symbol?.text ?? ""; } } toStringTree() { return this.toString(); } }; export { TerminalNode };