UNPKG

antlr4ng

Version:

Alternative JavaScript/TypeScript runtime for ANTLR4

268 lines (260 loc) 8.09 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/tree/ParseTreeWalker.ts var ParseTreeWalker_exports = {}; __export(ParseTreeWalker_exports, { ParseTreeWalker: () => ParseTreeWalker }); module.exports = __toCommonJS(ParseTreeWalker_exports); // 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(); } }; // src/tree/ErrorNode.ts var ErrorNode = class extends TerminalNode { static { __name(this, "ErrorNode"); } accept(visitor) { return visitor.visitErrorNode(this); } }; // src/tree/ParseTreeWalker.ts var ParseTreeWalker = class _ParseTreeWalker { static { __name(this, "ParseTreeWalker"); } static DEFAULT = new _ParseTreeWalker(); /** * Performs a walk on the given parse tree starting at the root and going down recursively * with depth-first search. On each node, {@link ParseTreeWalker.enterRule} is called before * recursively walking down into child nodes, then * {@link ParseTreeWalker.exitRule} is called after the recursive call to wind up. * * @param listener The listener used by the walker to process grammar rules * @param t The parse tree to be walked on */ walk(listener, t) { const errorNode = t instanceof ErrorNode; if (errorNode) { listener.visitErrorNode(t); } else if (t instanceof TerminalNode) { listener.visitTerminal(t); } else { const r = t; this.enterRule(listener, r); for (let i = 0; i < t.getChildCount(); i++) { this.walk(listener, t.getChild(i)); } this.exitRule(listener, r); } } /** * Enters a grammar rule by first triggering the generic event {@link ParseTreeListener.enterEveryRule} * then by triggering the event specific to the given parse tree node * * @param listener The listener responding to the trigger events * @param r The grammar rule containing the rule context */ enterRule(listener, r) { const ctx = r.ruleContext; listener.enterEveryRule(ctx); ctx.enterRule(listener); } /** * Exits a grammar rule by first triggering the event specific to the given parse tree node * then by triggering the generic event {@link ParseTreeListener.exitEveryRule} * * @param listener The listener responding to the trigger events * @param r The grammar rule containing the rule context */ exitRule(listener, r) { const ctx = r.ruleContext; ctx.exitRule(listener); listener.exitEveryRule(ctx); } };