/** * Named precedence levels for the Pratt parser, from loosest (`Lowest`) to * tightest (`Call`) binding. A custom infix parselet compares its own * binding power against the current expression's to decide whether to * consume the next operator (higher power binds tighter, e.g. `*` over `+`). * * Package authors reference these when declaring an infix parselet in a * package's `infixParselets`. See the built-in packages for examples of * picking an appropriate level (e.g. arithmetic `+`/`-` use `Sum`, `*`/`/` * use `Product`). */ export const BindingPower = { Lowest: 0, Assignment: 10, LogicalOr: 12, // `or`, `||` — loosest of the boolean-logic operators LogicalAnd: 14, // `&&` — binds tighter than `or` ("a or b and c" = "a or (b and c)") // The word "and" (`AND_CONJ`), which is both arithmetic addition ("5 and 3" // is 8) and boolean conjunction ("true and false"). A phrase parselet // collecting a list ("average of X, Y and Z") parses each argument at this // level, and an argument stops at any operator whose power is <= the level it // was parsed at, so putting "and" here is what makes it end an argument // rather than be consumed as the addition it also is. Everything an argument // should keep binds tighter, including a genuine "+" (`Sum`). // // It sits BELOW `Conditional` because the boolean reading needs comparisons // to be its operands: at 28, "5 > 3 and 2 > 1" parsed as "5 > (3 and 2) > 1" // and answered false. Above `LogicalAnd` so that "and" is still the tighter // of the two conjunctions, and two steps off each neighbour to leave room. Conjunction: 16, // The bitwise trio and the shifts, ordered exactly as C and JavaScript order // them, which is the order every language that borrowed these operators from // C uses: `|` looser than `xor`, `xor` looser than `&`, all three looser than // comparison, and the shifts tighter than comparison but looser than `+`. // They used to share one level (35) between `Sum` and `Product`, so // "1 + 2 << 3" answered 17 where JavaScript answers 24 and "4 & 3 + 1" // answered 1 where JavaScript answers 4. Each has its own level now because // a shared level makes "4 | 6 & 3" group left instead of letting `&` win. // // They stay TIGHTER than `Conjunction` so that a phrase argument parsed at // that level ("average of 5 | 3, 2 and 4") keeps its bitwise operators // instead of stopping at the first one. BitwiseOr: 18, // `|` BitwiseXor: 20, // the `xor` keyword (`BIT_XOR`), not the `^` exponent token BitwiseAnd: 22, // `&` // The comparisons (`==`, `!=`, `<`, `>`, `<=`, `>=`) and the two sides of a // `check`. One step below `Conditional`, so every phrase operator at that // level binds to its own side of a comparison rather than to the comparison: // `255 in hex == 0xff in hex` is `(255 in hex) == (0xff in hex)`, and // `check 255 in hex == 255` has a whole conversion on its left. When the two // shared a level, the second conversion took the comparison as its operand // (`((255 in hex) == 0xff) in hex`, the hex form of true) and a check's left // side stopped before its `as`. Tighter than the bitwise trio, as before. Comparison: 23, // The phrase operators that read a whole arithmetic expression on each side: // display conversions (`as hex`, `in binary`), `to` as a percentage change, // `is what % of`, `increase by`, and the rest. Looser than the shifts and // arithmetic, tighter than a comparison. Conditional: 24, Shift: 27, // `<<`, `>>`, `>>>` Sum: 30, Product: 40, // `±` (the uncertainty operator), tighter than `+ - * /` and looser than `^`, // so the tolerance attaches to its adjacent values: `12.3 ± 0.5 * 4` reads as // `(12.3 ± 0.5) * 4` (the parenthesised headline case), and `2 + 3 ± 0.5` as // `2 + (3 ± 0.5)`. A Tier-2 parselet (packages/uncertainty/), so this level is // read off the parselet rather than the Tier-1 BUILTIN_INFIX_BP table below. Uncertainty: 45, // `^`, RIGHT-associative: "2 ^ 3 ^ 2" is 2^(3^2) = 512, as in mathematics, // Python, Ruby and JavaScript's `**`. See PrecedenceParser.parseExpression // and BinaryOpParselet for the two places that associativity is implemented. Exponent: 50, Prefix: 60, Postfix: 70, Call: 80, } as const; /** * Set the binding power of a named operator. * * Binding power is what decides precedence in the Pratt parser: a higher number * binds tighter, so `*` outranks `+`. Changing one changes how every existing * expression using that operator parses, which is why packages should allocate * a new name rather than adjust a built-in. * * @param name - Operator name. * @param power - Higher binds tighter. */ export function setBindingPower(name: string, power: number): void { (BindingPower as Record)[name] = power; } /** * Read the binding power of a named operator. * * @param name - Operator name. * @returns Its binding power, or 0 when the name is unknown, which makes an * unregistered operator terminate an expression rather than swallow the rest * of it. Only a level the table holds as its own counts: a name every object * inherits (`constructor`, `toString`) is unknown, where it used to answer the * inherited function in place of a number. */ export function getBindingPower(name: string): number { if (!Object.prototype.hasOwnProperty.call(BindingPower, name)) return 0; const power = (BindingPower as Record)[name]; return typeof power === "number" ? power : 0; } // ── Built-in Infix Operator Binding Power Table ────────────────────────────── // Pre-computed Uint8Array lookup indexed by token typeId. // value > 0 → built-in infix operator with that binding power // value = 0 → not a built-in infix, fall through to Tier 2 parselet registry // // Built at module load via buildBindingPowerTable(). The table is sparse // most entries are 0 since only ~15 of 80+ token types are infix operators. // // Performance: Uint8Array[typeId] is a single typed array load, no Map.get() // no string hashing, no property chain. V8 eliminates bounds checks when typeId // is known to be within array length. import { TokenTypes } from "@solve-js/lexer/Token"; import { tokenTypeId } from "@solve-js/lexer/Token"; /** * Mapping of token type names → binding powers for built-in infix operators. * Only operators listed here get the Tier 1 fast path in PrecedenceParser. * All other infix tokens use the Tier 2 parselet registry fallback. */ export const BUILTIN_INFIX_BP: Record = { // Arithmetic (infix, left-associative) [TokenTypes.PLUS]: BindingPower.Sum, [TokenTypes.MINUS]: BindingPower.Sum, [TokenTypes.STAR]: BindingPower.Product, [TokenTypes.SLASH]: BindingPower.Product, [TokenTypes.MOD]: BindingPower.Product, // Arithmetic (infix, RIGHT-associative, PrecedenceParser handles this specially) [TokenTypes.CARET]: BindingPower.Exponent, // Bitwise and shift (infix, left-associative), one level each, ordered as C // and JavaScript order them. `>>>` used to be missing from this table // entirely, which left it running at its Tier 2 parselet level while `>>` // ran at the Tier 1 one, so "16 >> 3 & 1" and "16 >>> 3 & 1" grouped // differently and answered 0 and 8. [TokenTypes.BIT_OR]: BindingPower.BitwiseOr, [TokenTypes.BIT_XOR]: BindingPower.BitwiseXor, [TokenTypes.BIT_AND]: BindingPower.BitwiseAnd, [TokenTypes.LSHIFT]: BindingPower.Shift, [TokenTypes.RSHIFT]: BindingPower.Shift, [TokenTypes.URSHIFT]: BindingPower.Shift, // Postfix (no right operand, PrecedenceParser handles this specially) [TokenTypes.PERCENT]: BindingPower.Postfix, // Percentage keyword (infix, left-associative: "50% of 200" → MUL) [TokenTypes.OF]: BindingPower.Product, }; // Note: These token types are intentionally NOT in the BP_TABLE, they stay in // the Tier 2 parselet registry because they require complex handling: // IN, TO → UoM conversion parselets (peek at target unit token, complex logic) // DOT → Property access (chained OBJ_GET, needs left-token tracking) // EQUALS → Assignment (VariableParselet handles :var = expr pattern) // EQUALITY, NEQ, GTE, LTE → Comparison ops (need dedicated VM opcodes, task 2.15a) // COMMA → Argument separator (function calls, array/object literals) /** * The token types the parser reads itself, before it consults the parselet * registry: `PrecedenceParser.parsePrefix`'s inline cases for a prefix, and * every token in {@link BUILTIN_INFIX_BP} for an infix. A parselet a package * registers for one of these never runs (#719). A spec checks this list * against the parser's own behaviour. */ export const FAST_PATH_PREFIX_TOKENS: ReadonlySet = new Set([ "NUMBER", "BIGINT", "STRING", "IDENT", "LPAREN", "MINUS", "PLUS", ]); /** * Whether the parser reads `tokenType` on its own fast path, so a parselet * registered for it in that position never runs. See {@link FAST_PATH_PREFIX_TOKENS}. * * @param tokenType - The token type a parselet is registered for. * @param position - Whether the parselet is a prefix or an infix one. */ export function isFastPathToken(tokenType: string, position: "prefix" | "infix"): boolean { if (position === "prefix") return FAST_PATH_PREFIX_TOKENS.has(tokenType); return Object.prototype.hasOwnProperty.call(BUILTIN_INFIX_BP, tokenType); } /** Cached BP_TABLE, built once at module load, immutable thereafter. */ let _bpTable: Uint8Array | null = null; /** * Build the Uint8Array binding power table indexed by token typeId. * Idempotent, returns cached table on subsequent calls. * * Must be called after all token types are registered (via registerAllTokenTypes()) * and after plugin providers have registered their token types. */ export function buildBindingPowerTable(): Uint8Array { if (_bpTable) return _bpTable; // Size to the largest registered typeId + 1 let maxId = 0; for (const name of Object.keys(BUILTIN_INFIX_BP)) { const id = tokenTypeId(name); if (id > maxId) maxId = id; } // Also scan all TokenTypes to ensure table covers all registered IDs for (const name of Object.values(TokenTypes)) { const id = tokenTypeId(name); if (id > maxId) maxId = id; } const table = new Uint8Array(maxId + 1); for (const [name, bp] of Object.entries(BUILTIN_INFIX_BP)) { const id = tokenTypeId(name); table[id] = bp; } _bpTable = table; return table; } /** * Drop the cached table, so the next {@link buildBindingPowerTable} call builds * a fresh one sized to the token types registered by then. * * It does NOT add a plugin's operator to the Tier 1 fast path, which is what * this comment used to promise. The values in the table come from * `BUILTIN_INFIX_BP`, which is module-private and has no registration path, so * a rebuilt table holds the same binding powers and a plugin token still reads * 0. `PrecedenceParser.BP_TABLE` is a static initialized once at class * definition time and never re-read, so it would not see a rebuild anyway. * * A plugin's infix operator takes the Tier 2 parselet-registry route instead, * which is where an operator whose binding power the engine does not already * know is meant to live. */ export function invalidateBindingPowerTable(): void { _bpTable = null; }