UNPKG

antlr4ng

Version:

Alternative JavaScript/TypeScript runtime for ANTLR4

134 lines (132 loc) 4.58 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/misc/Interval.ts var Interval_exports = {}; __export(Interval_exports, { Interval: () => Interval }); module.exports = __toCommonJS(Interval_exports); 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; } };