quickjs-parse 0.17.34

ECMAScript 2025 lexer, AST and parser for the pure-Rust QuickJS port
Documentation
//! Identifier character classes.
//!
//! §12.1 defines an identifier's characters with the Unicode `ID_Start` / `ID_Continue`
//! properties, plus `$` and `_` in both, plus ZWNJ and ZWJ in `IdentifierPart` only.
//!
//! The tables come from [`crate::unicode_id_tables`], generated from the Unicode Character
//! Database. This module used to be a hand-written *approximation* — `char::is_alphabetic()`
//! plus a dozen `Other_ID_*` code points — on the grounds that it covers every identifier a
//! program realistically contains. It does, and it was still wrong at the edges: the
//! `test/language/identifiers/part-unicode-*.js` family lists the whole of one Unicode
//! version's `ID_Continue` per file, and 60 of those cases failed on a code point the
//! approximation had never heard of.

use crate::unicode_id_tables::{ID_CONTINUE, ID_START};

/// Binary search over a sorted, non-overlapping range table.
fn in_table(table: &[(u32, u32)], code: u32) -> bool {
    if code > 0x10FFFF {
        return false;
    }
    // `partition_point` finds the first range that starts after `code`; the one before it
    // is the only candidate.
    let index = table.partition_point(|&(start, _)| start <= code);
    index > 0 && code <= table[index - 1].1
}

/// `IdentifierStartChar`.
pub(crate) fn is_id_start(c: char) -> bool {
    // `$` and `_` are not `ID_Start`; the grammar adds them by name.
    if c == '$' || c == '_' {
        return true;
    }
    // ASCII fast path: the table has no ASCII letters outside `[A-Za-z]`, so this skips the
    // search for the overwhelmingly common case. It is an ordinary `ID_Start` answer, not
    // an extra rule, which is why the table is still consulted for everything else.
    if c.is_ascii() {
        return c.is_ascii_alphabetic();
    }
    in_table(ID_START, c as u32)
}

/// `IdentifierPartChar`.
pub(crate) fn is_id_continue(c: char) -> bool {
    if c == '$' || c == '_' {
        return true;
    }
    if c.is_ascii() {
        return c.is_ascii_alphanumeric();
    }
    // §12.1 lets ZWNJ and ZWJ appear in an identifier for script joining; they are
    // `Join_Control`, not `ID_Continue`, so the grammar names them separately.
    if c == '\u{200C}' || c == '\u{200D}' {
        return true;
    }
    in_table(ID_CONTINUE, c as u32)
}

#[cfg(test)]
mod tests {
    use super::{is_id_continue, is_id_start};

    #[test]
    fn ascii_letters_and_the_two_extras_start_an_identifier() {
        assert!(is_id_start('a') && is_id_start('Z'));
        assert!(is_id_start('$') && is_id_start('_'));
        assert!(!is_id_start('0') && !is_id_start('-'));
    }

    #[test]
    fn ascii_digits_continue_but_do_not_start() {
        assert!(is_id_continue('0') && is_id_continue('9'));
        assert!(!is_id_start('0'));
    }

    /// Code points the old `is_alphabetic` approximation missed: they are `ID_Start` or
    /// `ID_Continue` in the UCD without being alphabetic or numeric.
    #[test]
    fn the_ucd_decides_not_alphabetic_ness() {
        // `Other_ID_Continue` marks such as U+00B7 MIDDLE DOT.
        assert!(is_id_continue('\u{00B7}'));
        // A combining mark: `ID_Continue`, not alphabetic on its own.
        assert!(is_id_continue('\u{0301}'));
        assert!(!is_id_start('\u{0301}'));
    }

    #[test]
    fn zwnj_and_zwj_continue_an_identifier_only() {
        assert!(is_id_continue('\u{200C}') && is_id_continue('\u{200D}'));
        assert!(!is_id_start('\u{200C}') && !is_id_start('\u{200D}'));
    }

    /// A code point past the end of the tables must not be accepted, and must not be
    /// reachable as an out-of-bounds slice index.
    #[test]
    fn the_top_of_the_range_is_handled() {
        assert!(!is_id_start('\u{10FFFF}'));
        assert!(!is_id_continue('\u{10FFFF}'));
    }
}