big-code-analysis-ast 2.6.0

Parse and classification layer behind big-code-analysis (internal, version-locked)
Documentation
//! `Getter` implementation for Lua.
#![allow(clippy::wildcard_imports, clippy::enum_glob_use)]

use super::*;

impl Getter for LuaCode {
    // The mapping is the `opens` annotations of `roles/lua.rs`
    // (#1696). The rows read no bytes and no chain, so the byte-less
    // spelling forwards with none.
    impl_byte_less_space_kind!(lua);

    fn get_space_kind_with_code<'a>(
        node: &Node<'a>,
        code: &[u8],
        ancestors: Ancestors<'a, '_>,
    ) -> SpaceKind {
        crate::roles::lua::opens_space(node, code, ancestors)
    }

    keyword_roles!(lua);

    paying_roles!(lua);

    fn get_op_type<'a>(node: &Node<'a>, _ancestors: Ancestors<'a, '_>) -> TokenRole {
        match node.kind_id().into() {
            // Control-flow keywords. `local` and `function` are not
            // here: `roles/lua.rs` declares them and `get_keyword_role`
            // bills them (#1693).
            Lua::If
            | Lua::Then
            | Lua::Else
            | Lua::Elseif
            | Lua::End2
            | Lua::For
            | Lua::In
            | Lua::While
            | Lua::Do
            | Lua::Repeat
            | Lua::Until
            | Lua::Return
            | Lua::Goto
            // `not`; `and` / `or` are the `ShortCircuit` row's and
            // `get_paying_role` bills them (#1695)
            | Lua::Not
            // Structural punctuation. Only the *opening* delimiter is an
            // operator: `get_operator_id_as_str` folds `LPAREN`/`LBRACK`/
            // `LBRACE` to the pair glyph `()`/`[]`/`{}`, so a balanced pair
            // is one operator with one occurrence — the convention the
            // C-family majority follows. Counting the matching closer too
            // (the former `RPAREN`/`RBRACK`/`RBRACE` arms) double-counted
            // every balanced pair, inflating n1 and N1 (#695).
            | Lua::SEMI
            | Lua::COMMA
            | Lua::COLON
            | Lua::COLONCOLON
            | Lua::LBRACE
            | Lua::LBRACK
            | Lua::LPAREN
            | Lua::DOT
            | Lua::DOTDOT
            // Arithmetic / concat / length
            | Lua::PLUS
            | Lua::DASH
            | Lua::STAR
            | Lua::SLASH
            | Lua::SLASHSLASH
            | Lua::PERCENT
            | Lua::CARET
            | Lua::HASH
            // Bitwise (Lua 5.3+)
            | Lua::AMP
            | Lua::PIPE
            | Lua::TILDE
            | Lua::LTLT
            | Lua::GTGT
            // `<` / `>` for the spelling the `Comparison` row's parent
            // gate leaves out — a token error recovery strands — which
            // billed before the row existed; the other comparison tokens
            // are the row's and `get_paying_role` bills them (#1695).
            // role-bypass: allow(Comparison) — the operator classification of the spelling the
            // row's `under BinaryExpression` gate leaves out: a recovered token
            | Lua::LT
            | Lua::GT
            // Assignment
            | Lua::EQ
            // `break` is a named leaf node (no anonymous keyword child), so it must be
            // matched directly here — unlike `return`/`goto` which are anonymous tokens.
            | Lua::BreakStatement => TokenRole::Operator,

            // Operands: identifiers and literals
            // role-bypass: allow(StringLit) — the operand classification of the literals,
            // a projection the `StringLit` row does not answer
            Lua::Identifier | Lua::Number | Lua::String | Lua::True | Lua::False | Lua::Nil
            | Lua::VarargExpression => TokenRole::Operand,

            _ => TokenRole::Unknown,
        }
    }

    get_operator!(Lua);
}