import type { CompiledGrammar, DelimitedMatch, PatternInfo, TokenizeResult } from "./types"; import type { TokenizerIntrospector } from "./introspector"; import { RUN_NONE, RUN_TOKENLESS } from "./types"; // every field is set in the literal that creates an entry, and none is added // later: adding a property after creation moves the object to a map that only // the transition tree holds (weakly). no entry outlives a tokenize() call, so // V8 collected those maps a couple of full GCs later and threw away the // optimised tokenize that had embedded them, once every few full GCs for the // life of the process. -1 means "not resolved". interface ProbeEntry { pos: number; // original position for reset entry_pos: number; // position where probe was entered (after consuming match) state: number; stack_ptr: number; rule_idx: number; probe_state: number; resolved_state: number; resolved_pos: number; } declare const INTROSPECTION: boolean; // shared by every call since tokenize never calls out while holding it, a // larger input gets its own buffer so this one is not pinned const MAX_RETAINED_TOKEN_SLOTS = 1 << 20; let scratch_tokens = new Uint32Array(4096); // stands in for the fallback rule index in probe keys, a range rule never matches where the fallback does const FALLBACK_RULE = 255; function delimited_length(input: string, pos: number, span: DelimitedMatch): number { const start = pos + span.start.length; let cursor = start; while (cursor <= start + span.max_length && cursor < input.length) { if (input.startsWith(span.open, cursor)) { const closing = span.close + input.slice(start, cursor) + span.end; const end = input.indexOf(closing, cursor + span.open.length); return (end < 0 ? input.length : end + closing.length) - pos; } const code = input.charCodeAt(cursor); if (code < 33 || code > 126 || span.exclude.includes(input[cursor])) return 0; cursor++; } return 0; } // helper function to check if a character is an identifier continuation character function is_identifier_char(char_code: number): boolean { return ( (char_code >= 97 && char_code <= 122) || // a-z (char_code >= 65 && char_code <= 90) || // A-Z (char_code >= 48 && char_code <= 57) || // 0-9 char_code === 95 || // _ char_code === 36 // $ ); } // typed arrays over 64 bytes get an off-heap backing store, which costs // several microseconds to allocate. that was most of the fixed cost of a // call on short inputs, so the state stack is reused. a pool rather than one // shared stack keeps a nested call safe. every read is below stack_ptr, so // stale contents from an earlier call are never seen. const stack_pool: Uint16Array[] = []; function scratch_token_buffer(slots: number): Uint32Array { if (slots <= scratch_tokens.length) return scratch_tokens; if (slots > MAX_RETAINED_TOKEN_SLOTS) return new Uint32Array(slots); const grown = Math.max(slots, scratch_tokens.length * 2); scratch_tokens = new Uint32Array(Math.min(grown, MAX_RETAINED_TOKEN_SLOTS)); return scratch_tokens; } // how far before the end of an open input a rule can still match differently const open_reach = new WeakMap(); function reach_of(grammar: CompiledGrammar): number { let reach = open_reach.get(grammar); if (reach !== undefined) return reach; let longest = 1; for (const buckets of grammar.patterns_by_state) { if (buckets === undefined) continue; for (const bucket of buckets) { if (bucket === null) continue; for (const pattern of bucket) if (pattern.length > longest) longest = pattern.length; } } reach = longest + 1; open_reach.set(grammar, reach); return reach; } export function tokenize( input: string, compiled_grammar: CompiledGrammar, introspector: TokenizerIntrospector | null = null, open = false, ): TokenizeResult { const { transitions, char_maps, token_types, patterns_by_state, fallback_transitions, non_ascii_by_state, probe_states, probe_mask, probe_fallbacks, boundary_flags, run_kinds, has_seals, seal_flags, fallback_seal_flags, } = compiled_grammar; const len = input.length; const tokens = introspector === null ? scratch_token_buffer(len * 3) : new Uint32Array(len * 3); let token_count = 0; const state_stack = stack_pool.pop() ?? new Uint16Array(256); let stack_ptr = 0; let current_state = 0; // cache per-state hot references to avoid Map.get and multiplies per char let state_buckets: (PatternInfo[] | null)[] | undefined = patterns_by_state[0]; let char_map_base: number = 0; // current_state * 128 let trans_base3: number = 0; // (current_state * 256) * 3 let non_ascii_state: Int32Array | undefined = non_ascii_by_state[0]; let pos = 0; let prev_advanced_pos = -1; // track the last token for coalescing let last_token_type: number = 65535; let last_token_end: number = -1; // probe mode tracking let probe_entry: ProbeEntry | null = null; // track failed probe attempts - use numeric key for performance // key = (pos << 16) | (state << 8) | rule_idx let failed_probes: Set | null = null; let has_failed_probes = false; // earliest probe decided close enough to the end of an open input to decide otherwise later const late_from = open ? len - reach_of(compiled_grammar) : len; let late_probe_pos = len; // where an open input stops partway through a pattern that would emit a comment const open_comment_id = open ? token_types.indexOf("comment") : -1; let comment_cut = open_comment_id < 0 ? 0 : len; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.init({ input, compiled_grammar: compiled_grammar, initial_state: current_state, }); } // INTROSPECTION_END // probe handling stays inline, an inner function closing over the hot locals // turns every pos++ into a context store, a probe can only be left open at // end of input so that case is resolved at the loop head for (;;) { if (pos >= len) { if (probe_entry === null || !probe_states || !probe_states.has(current_state)) break; if (probe_entry.pos < late_probe_pos) late_probe_pos = probe_entry.pos; const fallback_state = probe_fallbacks?.get(current_state); if (fallback_state !== undefined) { pos = probe_entry.pos; state_stack[probe_entry.stack_ptr] = probe_entry.state; stack_ptr = probe_entry.stack_ptr + 1; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.pushed_state({ from_state: probe_entry.state, to_state: fallback_state, stack_ptr, pos: probe_entry.entry_pos, }); } // INTROSPECTION_END current_state = fallback_state; } else { const key = (probe_entry.pos << 16) | (probe_entry.state << 8) | probe_entry.rule_idx; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.probe_failed({ key, reason: "reached_end", probeEntry: probe_entry, }); } // INTROSPECTION_END if (failed_probes === null) failed_probes = new Set(); failed_probes.add(key); has_failed_probes = true; pos = probe_entry.pos; current_state = probe_entry.state; stack_ptr = probe_entry.stack_ptr; } probe_entry = null; state_buckets = patterns_by_state[current_state]; char_map_base = current_state * 128; trans_base3 = current_state * 256 * 3; non_ascii_state = non_ascii_by_state[current_state]; continue; } // if we advanced since last iteration, clear failed probe cache if (pos > prev_advanced_pos) { if (has_failed_probes) { failed_probes!.clear(); has_failed_probes = false; } prev_advanced_pos = pos; } const char = input.charCodeAt(pos); const start_pos = pos; // compute probe-state membership for this iteration let is_in_probe_state = probe_mask ? !!probe_mask[current_state] : !!(probe_states && probe_states.has(current_state)); // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.before_char({ pos, char, char_str: String.fromCharCode(char), current_state: current_state, stack_ptr, state_stack: state_stack.slice(0, stack_ptr), probe_mode: is_in_probe_state, }); if (stack_ptr > 100) break; } // INTROSPECTION_END if (char < 128) { // check bucketed multi-character patterns first let matched_length: number = 0; let matched_rule_idx: number = 65535; // early bail if no patterns for this state // inline bucket check for hot path if (matched_rule_idx === 65535 && state_buckets) { const bucket = state_buckets[char]; if (bucket) { const bucket_length = bucket.length; for (let b = 0; b < bucket_length; b++) { const pat = bucket[b]; const p_len = pat.length; if (pos + p_len > len) { // only reached in the last characters of the input if ( pos < comment_cut && transitions[trans_base3 + pat.rule_idx * 3 + 1] === open_comment_id ) { let i = 1; while (pos + i < len && input.charCodeAt(pos + i) === pat.codes[i]) i++; if (pos + i === len) comment_cut = pos; } continue; } // we know first char matches; unroll first iteration const codes = pat.codes; let matched = true; for (let i = 1; i < p_len; i++) { if (input.charCodeAt(pos + i) !== codes[i]) { matched = false; break; } } if (matched) { // check boundary if required if (pat.boundary && pos + p_len < len) { const next_char = input.charCodeAt(pos + p_len); if ( (next_char >= 97 && next_char <= 122) || // a-z (next_char >= 65 && next_char <= 90) || // A-Z (next_char >= 48 && next_char <= 57) || // 0-9 next_char === 95 || // _ next_char === 36 // $ ) { // boundary check failed - pattern requires word boundary but next char is identifier char continue; // skip this pattern and try next one } } // check if this rule has failed before (only if we have failed probes) if (has_failed_probes) { const test_key = (pos << 16) | (current_state << 8) | pat.rule_idx; if (failed_probes!.has(test_key)) { // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.skipped_failed_probe({ pattern: pat, testKey: test_key, }); } // INTROSPECTION_END continue; // skip this pattern and try next one } } const span_length = pat.delimited ? delimited_length(input, pos, pat.delimited) : p_len; if (span_length === 0) continue; matched_length = span_length; matched_rule_idx = pat.rule_idx; break; // buckets sorted by length desc → first fit is longest } } } } // directly use matched rule or lookup char map let char_class = matched_rule_idx; if (char_class === 65535) { char_class = char_maps[char_map_base + char]; } if (char_class !== 65535) { // check boundary for single-character matches if required if ( matched_rule_idx === 65535 && boundary_flags !== undefined && boundary_flags[(current_state << 8) + char_class] === 1 ) { // this is a single-char match that requires boundary checking if (pos + 1 < len) { const next_char = input.charCodeAt(pos + 1); if ( (next_char >= 97 && next_char <= 122) || // a-z (next_char >= 65 && next_char <= 90) || // A-Z (next_char >= 48 && next_char <= 57) || // 0-9 next_char === 95 || // _ next_char === 36 // $ ) { // boundary check failed - skip this match char_class = 65535; } } } // check if this single-char char_maps rule has previously failed as a // probe trigger. the multi-char bucket path has this check inline, but // char_maps matches reach here without going through that loop. if (matched_rule_idx === 65535 && has_failed_probes && char_class !== 65535) { const test_key = (pos << 16) | (current_state << 8) | char_class; if (failed_probes!.has(test_key)) { char_class = 65535; } } } if (char_class !== 65535) { // run fast path, a self loop rule consumes its whole run of characters // here instead of a dispatch per character, run_kinds folds every static // condition of this path into one byte at compile time // // the run stops at a char mapping to another rule, at a char starting one // of the state multi char patterns since the bucket would have won, and // at non ascii which the fallback path owns const run_kind = run_kinds[(current_state << 8) + char_class]; if ( run_kind !== RUN_NONE && matched_length === 0 && !has_failed_probes && !(INTROSPECTION && introspector) ) { // a tokenless run is skipped outright, the slow path also leaves the // last_token fields alone for tokenless rules if (run_kind === RUN_TOKENLESS) { pos++; while (pos < len) { const next = input.charCodeAt(pos); if (next > 127) break; if (char_maps[char_map_base + next] !== char_class) break; if (state_buckets !== undefined && state_buckets[next] !== null) break; pos++; } continue; } const token_type = transitions[trans_base3 + char_class * 3 + 1]; if (token_type === last_token_type && pos === last_token_end) { pos++; } else { const out_idx = token_count * 3; tokens[out_idx] = token_type; tokens[out_idx + 1] = pos; token_count++; pos++; } while (pos < len) { const next = input.charCodeAt(pos); if (next > 127) break; if (char_maps[char_map_base + next] !== char_class) break; if (state_buckets !== undefined && state_buckets[next] !== null) break; pos++; } tokens[(token_count - 1) * 3 + 2] = pos; last_token_type = token_type; last_token_end = pos; continue; } const t_base = trans_base3 + char_class * 3; const transition = transitions[t_base]; const token_type = transitions[t_base + 1]; const stack_op = transitions[t_base + 2]; // determine target state let target_state = current_state; if (stack_op === 1 && transition !== 65535) { target_state = transition; } else if (stack_op === 2 && transition !== 65535) { // sideways transition: target is the explicit transition, // not the state below on the stack. target_state = transition; } else if (stack_op === 2 && stack_ptr > 0) { // pure exit (pop): target is the parent on the stack. target_state = state_stack[stack_ptr - 1]; } else if (transition !== 65535) { target_state = transition; } const is_target_probe_state = probe_mask ? !!probe_mask[target_state] : !!(probe_states && probe_states.has(target_state)); if (is_in_probe_state && probe_entry && !is_target_probe_state) { probe_entry.resolved_state = target_state; probe_entry.resolved_pos = pos; } // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.matched_rule({ char_class: char_class, matched_length: matched_length, transition, token_type: token_type, stack_op: stack_op, current_state: current_state, probe_mode: is_in_probe_state, pos, }); } // INTROSPECTION_END // handle probe state entry if (!is_in_probe_state && is_target_probe_state) { // save the position where we'll be after consuming the matched text // this is where the probe state will be entered const probe_entry_pos = pos + (matched_length || 1); probe_entry = { pos: pos, // keep original pos for reset entry_pos: probe_entry_pos, // position where probe is entered state: current_state, stack_ptr: stack_ptr, rule_idx: char_class, probe_state: target_state, // save the probe state we're entering resolved_state: -1, resolved_pos: -1, }; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.enter_probe_mode({ pos, current_state: current_state, stack_ptr, char_class, }); } // INTROSPECTION_END } // emit token only if not in probe state if (!is_in_probe_state && token_type !== 65535) { const new_end = pos + (matched_length || 1); // `matched_length > 0` means the match came from the // multi-char bucket (char_maps hits leave matched_length // at 0 and advance by 1 on emit), so the emission is a // lexeme atom that must not coalesce backward into a // same-type run. seal: true / boundary: true rules also // block coalescing via seal_flags; has_seals is a loop // invariant so V8 short-circuits the lookup on grammars // without any sealing rules. if ( matched_length === 0 && token_type === last_token_type && pos === last_token_end && (!has_seals || !seal_flags![current_state * 256 + char_class]) ) { // extend previous token tokens[(token_count - 1) * 3 + 2] = new_end; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.extended_token({ token_type: token_type, old_end: last_token_end, new_end: new_end, token_index: token_count - 1, }); } // INTROSPECTION_END } else { // emit new token const out_idx = token_count * 3; tokens[out_idx] = token_type; tokens[out_idx + 1] = pos; tokens[out_idx + 2] = new_end; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.emitted_token({ token_type: token_type, token_name: token_types[token_type], start: pos, end: new_end, text: input.substring(pos, new_end), token_index: token_count, }); } // INTROSPECTION_END token_count++; } last_token_type = token_type; last_token_end = new_end; // advance position // for sideways transitions (exit with state), always advance // for regular exits, advance (the token was consumed) pos = new_end; } else { // no token to emit // for sideways transitions (exit with state), only advance if we matched a pattern // for regular exits (pop to parent), don't advance (re-process in parent) // for any: true rules with sideways transition, matched_length is 0, so don't advance if (stack_op !== 2) { // not an exit - advance by matched length pos += matched_length || 1; } else if (stack_op === 2 && transition !== 65535 && matched_length > 0) { // sideways transition with an explicit pattern match - advance by pattern length pos += matched_length; } // otherwise: exit without transition (pop), or sideways with no pattern match - don't advance } if (stack_op === 1) { state_stack[stack_ptr++] = current_state; const prev_state = current_state; current_state = transition; if (is_in_probe_state && probe_entry) { probe_entry.resolved_state = current_state; probe_entry.resolved_pos = pos; } // INTROSPECTION_START if (INTROSPECTION && introspector) { // record the state push only if not in probe mode // if in probe mode, it will be recorded when probe exits if (!is_in_probe_state) { // the entry position for the new state should be after the character that triggered the push // pos has already been advanced by matched_length or to new_end if a token was emitted introspector.pushed_state({ from_state: prev_state, to_state: current_state, stack_ptr, pos: pos, // this is already the position after the matched character }); } } // INTROSPECTION_END // refresh caches state_buckets = patterns_by_state[current_state]; char_map_base = current_state << 7; // *128 trans_base3 = (current_state << 8) * 3; // *256*3 non_ascii_state = non_ascii_by_state[current_state]; } else if (stack_op === 2) { // exit operation - either pop to parent or sideways transition const prev_state = current_state; if (transition !== 65535) { // sideways transition: exit current state and enter new sibling state // the stack depth remains the same current_state = transition; const transition_pos = is_in_probe_state && probe_entry !== null && probe_entry.resolved_pos >= 0 ? probe_entry.resolved_pos : pos; // INTROSPECTION_START if (INTROSPECTION && introspector) { // report as a transition, not a pop, since stack depth doesn't change introspector.transitioned_state({ from_state: prev_state, to_state: current_state, pos: transition_pos, }); } // INTROSPECTION_END } else if (stack_ptr > 0) { // regular exit: pop from stack to parent state. current_state = state_stack[--stack_ptr]; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.popped_state({ from_state: prev_state, to_state: current_state, stack_ptr, pos, }); } // INTROSPECTION_END } else { // can't pop from empty stack - stay in current state // this shouldn't normally happen in well-formed grammars } // refresh caches state_buckets = patterns_by_state[current_state]; char_map_base = current_state << 7; trans_base3 = (current_state << 8) * 3; non_ascii_state = non_ascii_by_state[current_state]; } else if (transition !== 65535) { const prev_state = current_state; current_state = transition; const transition_pos = is_in_probe_state && probe_entry !== null && probe_entry.resolved_pos >= 0 ? probe_entry.resolved_pos : pos; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.transitioned_state({ from_state: prev_state, to_state: current_state, pos: transition_pos, }); } // INTROSPECTION_END // refresh caches state_buckets = patterns_by_state[current_state]; char_map_base = current_state << 7; trans_base3 = (current_state << 8) * 3; non_ascii_state = non_ascii_by_state[current_state]; } // check if exiting probe state if (is_in_probe_state && !is_target_probe_state && probe_entry) { // probe succeeded - reset to entry point and continue in new state const probe_state_val = current_state; // save the probe state we're exiting from pos = probe_entry.pos; // the current state is now the target state we transitioned to // don't restore the old state - we want to continue in the new state stack_ptr = probe_entry.stack_ptr; // if the successful transition was a push, we need to handle it if (stack_op === 1) { // push the saved entry state onto stack state_stack[stack_ptr++] = probe_entry.state; } // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.exit_probe_mode({ success: true, reset_pos: probe_entry.pos, current_state: current_state, pos: probe_entry.pos, }); // record the push that happened inside probe mode, now with correct depth if (stack_op === 1 && probe_entry.resolved_state > 0) { introspector.pushed_state({ from_state: probe_entry.probe_state ?? probe_entry.state, to_state: probe_entry.resolved_state, stack_ptr, pos: probe_entry.resolved_pos >= 0 ? probe_entry.resolved_pos : probe_entry.pos, }); } } // INTROSPECTION_END if (probe_entry.resolved_pos >= late_from && probe_entry.pos < late_probe_pos) { late_probe_pos = probe_entry.pos; } probe_entry = null; // clear probe entry // refresh caches again in case state changed state_buckets = patterns_by_state[current_state]; char_map_base = current_state << 7; trans_base3 = (current_state << 8) * 3; non_ascii_state = non_ascii_by_state[current_state]; } } else { // no match found if (is_in_probe_state && probe_entry) { // in probe mode, skip the non-matching character and continue scanning // probe mode should skip characters until it finds a disambiguating match pos++; // INTROSPECTION_START if (INTROSPECTION && introspector) { // introspector.skippedCharInProbe({ // char: input[pos - 1], // pos: pos - 1, // current_state, // }); } // INTROSPECTION_END } else { // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.no_match({ char, char_str: String.fromCharCode(char), pos, current_state: current_state, }); } // INTROSPECTION_END pos++; } } } else { // handle non-ASCII characters (>= 128) // first check if there's a specific match for this character let matched_rule_idx = 65535; // early bail if no non-ASCII mappings exist at all if (non_ascii_state !== undefined) { // short list, and only reached for codepoints >= 128, so the scan // costs far less than the per-codepoint map it replaced cost to build. for (let i = 0; i < non_ascii_state.length; i += 3) { if (char >= non_ascii_state[i] && char <= non_ascii_state[i + 1]) { matched_rule_idx = non_ascii_state[i + 2]; break; } } } if (matched_rule_idx !== 65535) { // found a specific match for this non-ASCII character const t_base = trans_base3 + matched_rule_idx * 3; const transition = transitions[t_base]; const token_type = transitions[t_base + 1]; const stack_op = transitions[t_base + 2]; // determine target state let target_state = current_state; if (stack_op === 1 && transition !== 65535) { target_state = transition; } else if (stack_op === 2 && transition !== 65535) { // sideways transition: target is the explicit transition, // not the state below on the stack. target_state = transition; } else if (stack_op === 2 && stack_ptr > 0) { // pure exit (pop): target is the parent on the stack. target_state = state_stack[stack_ptr - 1]; } else if (transition !== 65535) { target_state = transition; } const is_target_probe_state = probe_mask ? !!probe_mask[target_state] : !!(probe_states && probe_states.has(target_state)); // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.non_ascii_match({ char, token_type: token_type, pos, current_state: current_state, }); } // INTROSPECTION_END // handle probe state entry if (!is_in_probe_state && is_target_probe_state) { // a probe that already failed here would reopen on its own trigger forever if ( has_failed_probes && failed_probes!.has((pos << 16) | (current_state << 8) | matched_rule_idx) ) { // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.no_match({ char, char_str: String.fromCharCode(char), pos, current_state: current_state, is_non_ascii: true, }); } // INTROSPECTION_END pos++; continue; } probe_entry = { pos: pos, entry_pos: pos + 1, state: current_state, stack_ptr: stack_ptr, rule_idx: matched_rule_idx, probe_state: target_state, resolved_state: -1, resolved_pos: -1, }; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.enter_probe_mode({ char_class: matched_rule_idx, pos, current_state: current_state, stack_ptr, }); } // INTROSPECTION_END } // emit token only if not in probe state if (!is_in_probe_state && token_type !== 65535) { const new_end = pos + 1; // non-ascii matches are always single-char, so sealing // here depends solely on the rule's seal flag. if ( (!has_seals || !seal_flags![current_state * 256 + matched_rule_idx]) && token_type === last_token_type && start_pos === last_token_end ) { // extend previous token tokens[(token_count - 1) * 3 + 2] = new_end; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.extended_token({ token_type: token_type, old_end: last_token_end, new_end: new_end, token_index: token_count - 1, }); } // INTROSPECTION_END } else { // emit new token const out_idx = token_count * 3; tokens[out_idx] = token_type; tokens[out_idx + 1] = pos; tokens[out_idx + 2] = new_end; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.emitted_token({ token_type: token_type, token_name: token_types[token_type], start: pos, end: new_end, text: input.substring(pos, new_end), token_index: token_count, is_non_ascii: true, }); } // INTROSPECTION_END token_count++; } last_token_type = token_type; last_token_end = new_end; pos = new_end; } else { // no token emitted - apply same logic as ASCII path // for sideways transitions with no pattern match (any: true), don't advance if (stack_op !== 2) { pos++; } else if (stack_op === 2 && transition !== 65535) { // sideways transition - don't advance to let new state process the character } // otherwise: regular exit (pop) - don't advance } // handle state transitions (same as ASCII path) if (stack_op === 1) { state_stack[stack_ptr++] = current_state; const prev_state = current_state; current_state = transition; if (is_in_probe_state && probe_entry) { probe_entry.resolved_state = current_state; probe_entry.resolved_pos = pos; } // INTROSPECTION_START if (INTROSPECTION && introspector) { if (!is_in_probe_state) { introspector.pushed_state({ from_state: prev_state, to_state: current_state, stack_ptr, pos, }); } } // INTROSPECTION_END // refresh caches state_buckets = patterns_by_state[current_state]; char_map_base = current_state * 128; trans_base3 = current_state * 256 * 3; non_ascii_state = non_ascii_by_state[current_state]; } else if (stack_op === 2) { // exit operation - either pop to parent or sideways transition const prev_state = current_state; if (transition !== 65535) { // sideways transition: exit current state and enter new sibling state // the stack depth remains the same current_state = transition; const transition_pos = is_in_probe_state && probe_entry !== null && probe_entry.resolved_pos >= 0 ? probe_entry.resolved_pos : pos; // INTROSPECTION_START if (INTROSPECTION && introspector) { // report as a transition, not a pop, since stack depth doesn't change introspector.transitioned_state({ from_state: prev_state, to_state: current_state, pos: transition_pos, }); } // INTROSPECTION_END } else if (stack_ptr > 0) { // regular exit: pop from stack to parent state current_state = state_stack[--stack_ptr]; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.popped_state({ from_state: prev_state, to_state: current_state, stack_ptr, pos, }); } // INTROSPECTION_END } else { // can't pop from empty stack - stay in current state // this shouldn't normally happen in well-formed grammars } // refresh caches state_buckets = patterns_by_state[current_state]; char_map_base = current_state * 128; trans_base3 = current_state * 256 * 3; non_ascii_state = non_ascii_by_state[current_state]; } else if (transition !== 65535) { const prev_state = current_state; current_state = transition; const transition_pos = is_in_probe_state && probe_entry !== null && probe_entry.resolved_pos >= 0 ? probe_entry.resolved_pos : pos; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.transitioned_state({ from_state: prev_state, to_state: current_state, pos: transition_pos, }); } // INTROSPECTION_END // refresh caches state_buckets = patterns_by_state[current_state]; char_map_base = current_state * 128; trans_base3 = current_state * 256 * 3; non_ascii_state = non_ascii_by_state[current_state]; } // check if exiting probe state if (is_in_probe_state && !is_target_probe_state && probe_entry) { // rewind to where the probe opened and keep the state it resolved to pos = probe_entry.pos; stack_ptr = probe_entry.stack_ptr; if (stack_op === 1) state_stack[stack_ptr++] = probe_entry.state; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.exit_probe_mode({ success: true, reset_pos: probe_entry.pos, current_state: current_state, pos: probe_entry.pos, }); if (stack_op === 1 && probe_entry.resolved_state > 0) { introspector.pushed_state({ from_state: probe_entry.probe_state ?? probe_entry.state, to_state: probe_entry.resolved_state, stack_ptr, pos: probe_entry.resolved_pos >= 0 ? probe_entry.resolved_pos : probe_entry.pos, }); } } // INTROSPECTION_END if (probe_entry.resolved_pos >= late_from && probe_entry.pos < late_probe_pos) { late_probe_pos = probe_entry.pos; } probe_entry = null; } } else if (fallback_transitions) { // no specific match, use fallback transitions const idx = current_state * 3; const transition = fallback_transitions[idx]; const token_type = fallback_transitions[idx + 1]; const stack_op = fallback_transitions[idx + 2]; let target_state = current_state; if (stack_op === 1 && transition !== 65535) { target_state = transition; } else if (stack_op === 2 && transition !== 65535) { // sideways, the target is the explicit state not the parent target_state = transition; } else if (stack_op === 2 && stack_ptr > 0) { // pop, the target is the parent target_state = state_stack[stack_ptr - 1]; } else if (transition !== 65535) { target_state = transition; } const is_target_probe_state = probe_mask ? !!probe_mask[target_state] : !!(probe_states && probe_states.has(target_state)); // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.fallback_match({ token_type: token_type, pos, current_state: current_state, }); } // INTROSPECTION_END if (!is_in_probe_state && is_target_probe_state) { // a probe that already failed here would reopen on its own trigger forever if ( has_failed_probes && failed_probes!.has((pos << 16) | (current_state << 8) | FALLBACK_RULE) ) { // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.no_match({ char, char_str: String.fromCharCode(char), pos, current_state: current_state, is_non_ascii: true, }); } // INTROSPECTION_END pos++; continue; } probe_entry = { pos: pos, entry_pos: pos + 1, state: current_state, stack_ptr: stack_ptr, rule_idx: FALLBACK_RULE, probe_state: target_state, resolved_state: -1, resolved_pos: -1, }; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.enter_probe_mode({ char_class: FALLBACK_RULE, pos, current_state: current_state, stack_ptr, }); } // INTROSPECTION_END } // emit token only if not in probe state if (!is_in_probe_state && token_type !== 65535) { const new_end = pos + 1; // fallback transitions cover single non-ascii chars, so // sealing here is a per-state flag. if ( (!has_seals || !fallback_seal_flags![current_state]) && token_type === last_token_type && start_pos === last_token_end ) { // extend previous token tokens[(token_count - 1) * 3 + 2] = new_end; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.extended_token({ token_type: token_type, old_end: last_token_end, new_end: new_end, token_index: token_count - 1, }); } // INTROSPECTION_END } else { // emit new token const out_idx = token_count * 3; tokens[out_idx] = token_type; tokens[out_idx + 1] = pos; tokens[out_idx + 2] = new_end; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.emitted_token({ token_type: token_type, token_name: token_types[token_type], start: pos, end: new_end, text: input.substring(pos, new_end), token_index: token_count, is_fallback: true, is_non_ascii: true, }); } // INTROSPECTION_END token_count++; } last_token_type = token_type; last_token_end = new_end; pos = new_end; } else if (stack_op !== 2) { // a tokenless exit leaves the character for the state it lands in pos++; } // handle state transitions (same as ASCII path). if (stack_op === 1) { state_stack[stack_ptr++] = current_state; const prev_state = current_state; current_state = transition; if (is_in_probe_state && probe_entry) { probe_entry.resolved_state = current_state; probe_entry.resolved_pos = pos; } // INTROSPECTION_START if (INTROSPECTION && introspector) { if (!is_in_probe_state) { introspector.pushed_state({ from_state: prev_state, to_state: current_state, stack_ptr, pos, }); } } // INTROSPECTION_END // refresh caches state_buckets = patterns_by_state[current_state]; char_map_base = current_state * 128; trans_base3 = current_state * 256 * 3; non_ascii_state = non_ascii_by_state[current_state]; } else if (stack_op === 2) { // exit operation - either pop to parent or sideways transition const prev_state = current_state; if (transition !== 65535) { // sideways transition: exit current state and enter new sibling state // the stack depth remains the same current_state = transition; const transition_pos = is_in_probe_state && probe_entry !== null && probe_entry.resolved_pos >= 0 ? probe_entry.resolved_pos : pos; // INTROSPECTION_START if (INTROSPECTION && introspector) { // report as a transition, not a pop, since stack depth doesn't change introspector.transitioned_state({ from_state: prev_state, to_state: current_state, pos: transition_pos, }); } // INTROSPECTION_END } else if (stack_ptr > 0) { // regular exit: pop from stack to parent state current_state = state_stack[--stack_ptr]; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.popped_state({ from_state: prev_state, to_state: current_state, stack_ptr, pos, }); } // INTROSPECTION_END } else { // can't pop from empty stack - stay in current state // this shouldn't normally happen in well-formed grammars } // refresh caches state_buckets = patterns_by_state[current_state]; char_map_base = current_state * 128; trans_base3 = current_state * 256 * 3; non_ascii_state = non_ascii_by_state[current_state]; } else if (transition !== 65535) { const prev_state = current_state; current_state = transition; const transition_pos = is_in_probe_state && probe_entry !== null && probe_entry.resolved_pos >= 0 ? probe_entry.resolved_pos : pos; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.transitioned_state({ from_state: prev_state, to_state: current_state, pos: transition_pos, }); } // INTROSPECTION_END // refresh caches state_buckets = patterns_by_state[current_state]; char_map_base = current_state * 128; trans_base3 = current_state * 256 * 3; non_ascii_state = non_ascii_by_state[current_state]; } if (is_in_probe_state && !is_target_probe_state && probe_entry) { // rewind to where the probe opened and keep the state it resolved to pos = probe_entry.pos; stack_ptr = probe_entry.stack_ptr; if (stack_op === 1) state_stack[stack_ptr++] = probe_entry.state; // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.exit_probe_mode({ success: true, reset_pos: probe_entry.pos, current_state: current_state, pos: probe_entry.pos, }); if (stack_op === 1 && probe_entry.resolved_state > 0) { introspector.pushed_state({ from_state: probe_entry.probe_state ?? probe_entry.state, to_state: probe_entry.resolved_state, stack_ptr, pos: probe_entry.resolved_pos >= 0 ? probe_entry.resolved_pos : probe_entry.pos, }); } } // INTROSPECTION_END if (probe_entry.resolved_pos >= late_from && probe_entry.pos < late_probe_pos) { late_probe_pos = probe_entry.pos; } probe_entry = null; } } else { // no match found if (is_in_probe_state && probe_entry) { // in probe mode, skip the non-matching character and continue scanning // probe mode should skip characters until it finds a disambiguating match pos++; // INTROSPECTION_START // if (INTROSPECTION && introspector) { // introspector.skippedCharInProbe({ // char: input[pos - 1], // pos: pos - 1, // current_state, // }); // } // INTROSPECTION_END } else { // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.no_match({ char, char_str: String.fromCharCode(char), pos, current_state: current_state, is_non_ascii: true, }); } // INTROSPECTION_END pos++; } } } } // INTROSPECTION_START if (INTROSPECTION && introspector) { introspector.complete({ token_count: token_count, final_state: current_state, final_stack_ptr: stack_ptr, }); } // INTROSPECTION_END stack_pool.push(state_stack); // return token_types by reference. reclassifiers that mutate the array // (promote_by_text_set, interface_member_promoter, class_name_promoter, // ...) are responsible for cloning before they push new names. cloning // here penalised every tokenize call, including reclassifier-free ones. // // always copy out since the next call reuses the scratch buffer const used = token_count * 3; const result: TokenizeResult = { tokens: tokens.slice(0, used), token_types, }; if (open) { let stable = Math.max(0, Math.min(late_from, late_probe_pos)); // the one token that reaches the unsettled tail can still grow or split let t = used - 3; while (t >= 0 && tokens[t + 1] >= stable) t -= 3; if (t >= 0 && tokens[t + 2] >= stable) stable = tokens[t + 1]; result.stable_end = stable === 0 ? 0 : input.lastIndexOf("\n", stable - 1) + 1; if (open_comment_id >= 0 && comment_cut < len) { // inside a comment the cut pattern is its closer let c = used - 3; while (c >= 0 && tokens[c + 2] > comment_cut && tokens[c] !== open_comment_id) c -= 3; if (c < 0 || tokens[c + 2] <= comment_cut) result.comment_cut = comment_cut; } } return result; }