UNPKG

antlr4ng

Version:

Alternative JavaScript/TypeScript runtime for ANTLR4

227 lines (221 loc) 6.04 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/utils/MurmurHash.ts var c1 = 3432918353; var c2 = 461845907; var r1 = 15; var r2 = 13; var m = 5; var n = 3864292196; var MurmurHash = class _MurmurHash { static { __name(this, "MurmurHash"); } static defaultSeed = 701; constructor() { } /** * Initialize the hash using the specified {@code seed}. * * @param seed the seed * * @returns the intermediate hash value */ static initialize(seed = _MurmurHash.defaultSeed) { return seed; } static updateFromComparable(hash, value) { return this.update(hash, value?.hashCode() ?? 0); } /** * Update the intermediate hash value for the next input {@code value}. * * @param hash The intermediate hash value. * @param value the value to add to the current hash. * * @returns the updated intermediate hash value */ static update(hash, value) { value = Math.imul(value, c1); value = value << r1 | value >>> 32 - r1; value = Math.imul(value, c2); hash = hash ^ value; hash = hash << r2 | hash >>> 32 - r2; hash = Math.imul(hash, m) + n; return hash; } /** * Apply the final computation steps to the intermediate value {@code hash} * to form the final result of the MurmurHash 3 hash function. * * @param hash The intermediate hash value. * @param entryCount The number of values added to the hash. * * @returns the final hash result */ static finish(hash, entryCount) { hash ^= entryCount * 4; hash ^= hash >>> 16; hash = Math.imul(hash, 2246822507); hash ^= hash >>> 13; hash = Math.imul(hash, 3266489909); hash ^= hash >>> 16; return hash; } /** * An all-in-one convenience method to compute a hash for a single value. * * @param value The value to hash. * @param seed The seed for the hash value. * * @returns The computed hash. */ static hashCode(value, seed) { return _MurmurHash.finish(_MurmurHash.update(seed ?? _MurmurHash.defaultSeed, value), 1); } }; // src/atn/PredictionContext.ts var PredictionContext = class _PredictionContext { static { __name(this, "PredictionContext"); } /** * Represents `$` in an array in full context mode, when `$` * doesn't mean wildcard: `$ + x = [$,x]`. Here, * `$` = {@link EMPTY_RETURN_STATE}. */ static EMPTY_RETURN_STATE = 2147483647; static traceATNSimulator = false; cachedHashCode; constructor(cachedHashCode) { this.cachedHashCode = cachedHashCode; } static calculateEmptyHashCode() { let hash = MurmurHash.initialize(31); hash = MurmurHash.finish(hash, 0); return hash; } static calculateHashCodeSingle(parent, returnState) { let hash = MurmurHash.initialize(31); hash = MurmurHash.updateFromComparable(hash, parent); hash = MurmurHash.update(hash, returnState); hash = MurmurHash.finish(hash, 2); return hash; } static calculateHashCodeList(parents, returnStates) { let hash = MurmurHash.initialize(31); for (const parent of parents) { hash = MurmurHash.updateFromComparable(hash, parent); } for (const returnState of returnStates) { hash = MurmurHash.update(hash, returnState); } hash = MurmurHash.finish(hash, 2 * parents.length); return hash; } isEmpty() { return false; } hasEmptyPath() { return this.getReturnState(this.length - 1) === _PredictionContext.EMPTY_RETURN_STATE; } hashCode() { return this.cachedHashCode; } toString(_recog) { return ""; } }; // src/atn/SingletonPredictionContext.ts var SingletonPredictionContext = class _SingletonPredictionContext extends PredictionContext { static { __name(this, "SingletonPredictionContext"); } parent; returnState; constructor(parent, returnState) { super( parent ? PredictionContext.calculateHashCodeSingle(parent, returnState) : PredictionContext.calculateEmptyHashCode() ); this.parent = parent ?? null; this.returnState = returnState; } getParent(_index) { return this.parent; } getReturnState(_index) { return this.returnState; } equals(other) { if (this === other) { return true; } if (!(other instanceof _SingletonPredictionContext)) { return false; } if (this.hashCode() !== other.hashCode()) { return false; } if (this.returnState !== other.returnState) { return false; } if (this.parent == null) { return other.parent == null; } return this.parent.equals(other.parent); } toString() { const up = this.parent === null ? "" : this.parent.toString(); if (up.length === 0) { if (this.returnState === PredictionContext.EMPTY_RETURN_STATE) { return "$"; } return "" + this.returnState; } else { return "" + this.returnState + " " + up; } } get length() { return 1; } }; // src/atn/EmptyPredictionContext.ts var EmptyPredictionContext = class _EmptyPredictionContext extends SingletonPredictionContext { static { __name(this, "EmptyPredictionContext"); } /** * Represents `$` in local context prediction, which means wildcard. * `*+x = *`. */ static instance = new _EmptyPredictionContext(); constructor() { super(void 0, PredictionContext.EMPTY_RETURN_STATE); } isEmpty() { return true; } getParent() { return null; } getReturnState() { return this.returnState; } equals(other) { return this === other; } toString() { return "$"; } }; // src/atn/helpers.ts var createSingletonPredictionContext = /* @__PURE__ */ __name((parent, returnState) => { if (returnState === PredictionContext.EMPTY_RETURN_STATE && parent === null) { return EmptyPredictionContext.instance; } else { return new SingletonPredictionContext(parent, returnState); } }, "createSingletonPredictionContext"); export { createSingletonPredictionContext };