big-code-analysis-ast 2.5.1

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

use super::*;

// Extracts the human-readable head name from the first non-`target`
// child of an Elixir `def` / `defp` / `defmacro` / `defmacrop` /
// `defmodule` Call. Handles three shapes:
//   - `Arguments` wrapper: descend one level and recurse.
//   - `Identifier` / `Alias` leaf: return its source text.
//   - inner `Call` (e.g. `def foo(x, y)`): return the target identifier
//     text of that inner Call.
// Returns `None` when the child does not match any of these shapes,
// allowing the caller to keep scanning siblings (notably the
// `do_block`, which is unconditionally present and never carries the
// name).
fn elixir_extract_head_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
    use Elixir as E;

    let text = |n: &Node| node_text(code, n);
    match node.kind_id().into() {
        E::Identifier | E::Alias => text(node),
        // role-bypass: allow(FuncOpener, Container, Call) — naming, not membership: where a head's
        // text sits
        E::Call => text(&node.child_by_field_name("target")?),
        E::Arguments
        | E::Arguments2
        | E::Arguments3
        | E::Arguments4
        | E::Arguments5
        | E::CallArgumentsWithTrailingSeparator => node
            .children()
            .find_map(|child| elixir_extract_head_name(&child, code)),
        _ => None,
    }
}

impl Getter for ElixirCode {
    // The byte-less spelling is the kind table alone: an anonymous
    // function and the file root. `defmodule` / `def` / `defp` /
    // `defmacro` / `defmacrop` are `call`s told apart by their target
    // text, so only the `_with_code` spelling — the `opens` annotations
    // of `roles/elixir.rs` (#275, #310, #1696) — labels those. Each arm
    // is the ungated subset of its row's owners, the one a byte-less
    // spelling can answer; the derived forward parity test pins both
    // against the rows on every corpus node.
    fn get_space_kind(node: &Node) -> SpaceKind {
        use Elixir as E;

        match node.kind_id().into() {
            // role-bypass: allow(FuncOpener, Closure) — the `FuncOpener` owner with no gate
            E::AnonymousFunction => SpaceKind::Function,
            // role-bypass: allow(Container) — the `Container` owner with no gate
            E::Source => SpaceKind::Unit,
            _ => SpaceKind::Unknown,
        }
    }

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

    // Source-aware name extraction for the macro-shaped declarations.
    // `def foo(x, y) do … end` parses (with the tree-sitter-elixir
    // grammar shipped here) as
    //   `Call { target: Identifier "def", Arguments { Call { target:
    //     Identifier "foo", Arguments { … } } }, DoBlock { … } }`
    // i.e. the head Call is wrapped in an `Arguments` node, not a
    // direct child. `defmodule Foo.Bar do … end` parses similarly with
    // the `Alias` inside `Arguments`. We descend through one
    // `Arguments` layer when present, then either:
    //   - return the `Identifier` / `Alias` text directly
    //     (`defmodule Foo`, `def foo` for an arity-zero head with no
    //     parentheses), or
    //   - return the inner head Call's `target` text
    //     (`def foo(x, y)`).
    // Falls back to the trait default behaviour (`<anonymous>` for
    // nodes without a `name` field) when the Call is not one we
    // recognise.
    fn get_func_space_name<'a, 'tree>(
        node: &Node<'tree>,
        code: &'a [u8],
        ancestors: Ancestors<'tree, '_>,
    ) -> Option<&'a str> {
        use crate::lang_helpers::elixir::elixir_declaration;
        // A `defmodule` always names its head; a method macro only
        // outside a `quote` template, matching the func-space promotion
        // rule (#310) — the one `elixir_declaration` lookup both rows
        // read. Its quote-block climb reads the caller's chain rather
        // than `Node::parent` (#1088): this fires once per *promoted*
        // space rather than per node, but `bca function` and the
        // suppression scan reach it through `get_func_name` on every
        // function node, where the climb was `O(depth)` apiece.
        if elixir_declaration(node, code, ancestors).is_some() {
            let target_id = node.child_by_field_name("target").map(|t| t.id());
            if let Some(name) = node
                .children()
                .filter(|child| Some(child.id()) != target_id)
                .find_map(|child| elixir_extract_head_name(&child, code))
            {
                return Some(name);
            }
        }

        if let Some(name) = node.child_by_field_name("name") {
            return node_text(code, &name);
        }
        Some("<anonymous>")
    }

    keyword_roles!(elixir);

    /// The `Comparison`, `ShortCircuit`, `CaseArm` and `Guard` rows'
    /// payers bill as operators (#1695) — what `paying_roles!` reads —
    /// except that the `Guard` row is not asked. Its `when` payer is
    /// gated on a guard position, which climbs the chain of nested
    /// `when` operators a repeated guard (`when a when b when …`)
    /// builds: asked once per token, that is quadratic in the chain,
    /// and the answer cannot move this reading — a `when` the gate
    /// leaves out (a typespec's `when a: integer`) bills as an operator
    /// through the byte-less arm all the same, and the one position
    /// where it does not, inside an `operator_identifier` (`&when/2`),
    /// is `get_op_type`'s first test, repeated here since this reading
    /// comes first. The counting consumers pay that climb once per
    /// guard, where it decides something.
    // role-bypass: allow(Guard) — the Halstead reading of the row's one payer, which bills as
    // an operator in every position but a name's and so skips the row's position gate
    fn get_paying_role<'a>(
        node: &Node<'a>,
        code: &[u8],
        ancestors: Ancestors<'a, '_>,
    ) -> TokenRole {
        use crate::roles::elixir::{case_arm, comparison, guard, short_circuit};
        let kind = node.kind_id();
        if guard::PAYERS.contains(&kind) {
            return if ancestors.parent_has_kind(node, Elixir::OperatorIdentifier as u16) {
                TokenRole::Unknown
            } else {
                TokenRole::Operator
            };
        }
        if comparison::pays_at_token_kind(kind, node, code, ancestors)
            || short_circuit::pays_at_token_kind(kind, node, code, ancestors)
            || case_arm::pays_at_token_kind(kind, node, code, ancestors)
        {
            TokenRole::Operator
        } else {
            TokenRole::Unknown
        }
    }

    fn get_op_type<'a>(node: &Node<'a>, ancestors: Ancestors<'a, '_>) -> TokenRole {
        use Elixir as E;

        // A *named* operator (`&==/2`, `&and/2`, `Kernel.||(a, b)`) is one
        // `operator_identifier` holding the operator token. The wrapper is
        // the operand, as `foo` is in `&foo/2`, so the token inside it must
        // not also bill an operator (grammar-dispatch §5, #1534). The
        // wrapper is the keeper because it exists for every spelling: `..`
        // is a childless `operator_identifier`, so keeping the leaf would
        // lose it.
        if ancestors.parent_has_kind(node, E::OperatorIdentifier as u16) {
            return TokenRole::Unknown;
        }

        match node.kind_id().into() {
            // Sigil delimiter punctuation. The delimiter choice is
            // spelling, not semantics — `~r/abc/` is not two divisions
            // and `~w(a b)` opens no call — so counting these tokens
            // made `n1`/`N1` vary with the author's delimiter choice
            // (#1256). The `Sigil` node itself is the operand (below)
            // and `~` the single per-sigil operator; the delimiters are
            // suppressed exactly when their parent is the sigil — the
            // compound-leaf guard of grammar-dispatch §5, mirroring
            // Bash's `SimpleExpansion` parent check. Verified via
            // `bca dump` at the pinned grammar: `/ ( { [ < > |` are the
            // only delimiter kinds surfacing as `Sigil` children that
            // the operator arm below also matches; the quote delimiters
            // (`"`, `'`, `"""`, `'''`) and the closers `) } ]` have no
            // operator arm and need no guard. Same-kind tokens outside
            // a sigil (division `a / b`, comparison `<`, pipe `|`) fall
            // through to the operator arm and still count.
            // role-bypass: allow(Comparison) — a sigil delimiter's suppression, not whether `<` is a
            // comparison: the row's `applied` gate already leaves it out
            E::SLASH | E::LPAREN | E::LBRACE | E::LBRACK | E::LT | E::GT | E::PIPE
                if ancestors.parent_has_kind(node, E::Sigil as u16) =>
            {
                TokenRole::Unknown
            }
            // Reserved-word keywords that have dedicated token kinds in
            // the grammar — block delimiters, exception clauses, and
            // word-form logical / membership operators. `fn` is not
            // here: `roles/elixir.rs` declares it and `get_keyword_role`
            // bills it (#1693).
            // (Macro-shaped keywords like `def`/`defp`/`if`/`case`/`cond`
            // are NOT here: they surface as `Identifier` tokens in a
            // `Call`'s `target` field and are counted as operands below.)
            //
            // The comparison and short-circuit tokens sit in this arm for
            // the spellings the `Comparison` / `ShortCircuit` rows'
            // `applied` gate leaves out — a capture (`&==/2`, `&and/2`),
            // a `Kernel.||(a, b)` call's name, a single-token atom or
            // keyword, a recovered token — which bill as the operators
            // they spell, as they did before the rows; the rows pay the
            // applied spellings through `get_paying_role` (#1695), so
            // for these kinds the vocabulary is the union of the two.
            // `when` is not here: `get_paying_role` bills every spelling
            // of the `Guard` row's payer, guard or typespec binding.
            // role-bypass: allow(Comparison, ShortCircuit) — the operator classification of the
            // spellings the rows' `applied` gate leaves out
            E::Do | E::End | E::End2 | E::Else | E::After | E::Catch | E::Rescue
            | E::Not | E::Or | E::And | E::In | E::Notin
            // Structural punctuation acting as operators. Only the
            // *opening* delimiter counts (the pair folds to one glyph in
            // `get_operator_id_as_str`); the former `RPAREN`/`RBRACE`/
            // `RBRACK` arms double-counted every balanced pair, inflating
            // n1/N1 (#695). `LTLT`/`GTGT` are the bitstring `<<`/`>>`
            // delimiters — left unfolded and counted as the majority of
            // languages count their shift-like glyphs. `LPAREN2`/`LBRACK2`
            // are defensive — the runtime `public_symbol_map` collapses them
            // to `LPAREN`/`LBRACK` before `kind_id()`, so they never fire
            // (#768; see the Cpp impl note).
            | E::LPAREN | E::LPAREN2 | E::LBRACE
            | E::LBRACK | E::LBRACK2 | E::LTLT | E::GTGT
            | E::COMMA | E::SEMI | E::COLON | E::COLONCOLON | E::DOT
            | E::DOTDOT | E::DOTDOTDOT | E::PERCENT | E::AT
            // Arithmetic / unary
            | E::PLUS | E::DASH | E::STAR | E::STARSTAR | E::SLASH
            // Comparison
            | E::EQEQ | E::EQEQEQ | E::BANGEQ | E::BANGEQEQ
            | E::LT | E::GT | E::LTEQ | E::GTEQ
            // Logical
            | E::AMPAMP | E::PIPEPIPE | E::BANG
            // Bitwise / Erlang-band. `TILDE` is only reachable as the
            // sigil marker in this grammar (Elixir has no unary `~`;
            // the Erlang-style ops are `~~~` etc.) and is kept as the
            // deliberate single per-sigil operator (#1256).
            | E::AMP | E::PIPE | E::CARET | E::TILDE
            | E::AMPAMPAMP | E::PIPEPIPEPIPE | E::CARETCARETCARET | E::TILDETILDETILDE
            | E::LTLTLT | E::GTGTGT
            // Assignment / match
            | E::EQ
            // Concat / list operations
            | E::PLUSPLUS | E::DASHDASH | E::LTGT
            | E::PLUSPLUSPLUS | E::DASHDASHDASH
            // Pipe / capture / generator / stab arrow
            | E::PIPEGT | E::LTPIPEGT | E::DASHGT | E::LTDASH
            // Map pair / default arg / regex match / range step
            | E::EQGT | E::BSLASHBSLASH | E::EQTILDE | E::SLASHSLASH
            // Custom / less common Elixir operators
            | E::LTTILDE | E::TILDEGT | E::LTTILDEGT | E::LTLTTILDE | E::TILDEGTGT
                => TokenRole::Operator,

            // String literals contribute exactly one operand each when
            // they are inert. When they carry an `interpolation` child,
            // the interpolated expressions are already walked and counted
            // as operands in their own right; counting the wrapping
            // literal as well would double-count the inner identifiers'
            // contribution (issue #180).
            //
            // The `#{` marker itself contributes nothing. It was an
            // operator via `HASHLBRACE` until #1314, which settled the
            // rule across every interpolating language: an
            // interpolation opener is spelling, not an operation, and
            // the interpolated expression's own operators are counted
            // either way. Elixir spells the marker with the *same*
            // token as Ruby, so leaving it counted here would have made
            // the two disagree on one construct — the divergence that
            // rule exists to remove. Elixir Halstead operator counts
            // drop for interpolated strings, charlists and sigils.
            // role-bypass: allow(StringLit) — the operand classification of the literals
            // under the row's `wraps` list, a projection the row does not answer
            E::String | E::Charlist | E::Sigil => {
                Self::string_operand_type(node, crate::roles::elixir::string_lit::WRAPS)
            }

            // Operands: identifiers and literals. Sigil names/modifiers
            // (`~r`, the trailing `i`/`u` flags) stay as operands even
            // for interpolated sigils — they are distinct tokens with
            // their own text.
            //
            // `boolean: choice("true", "false")` and `nil: "nil"` each
            // wrap a keyword leaf, so a literal reaches the walker
            // twice; listing the wrapper and the leaf both inflated `N2`
            // by one per literal while the text-keyed `n2` hid it
            // (#1253). Only the wrappers count, mirroring Ruby's `Nil` /
            // `Nil2` split. Unlike C#, the leaves need no parent guard:
            // those two rules are the grammar's only producers of them,
            // and the one other position accepting the reserved words —
            // the right-hand side of a remote dot, `Foo.nil` — aliases
            // them to `identifier`.
            E::Identifier | E::Alias | E::OperatorIdentifier
            | E::SigilName | E::SigilName2 | E::SigilModifiers
            | E::Keyword | E::Keyword2 | E::QuotedKeyword
            | E::Integer | E::Float | E::Char
            | E::Atom | E::Atom2 | E::QuotedAtom
            | E::Boolean | E::Nil
                => TokenRole::Operand,

            _ => TokenRole::Unknown,
        }
    }

    get_operator!(Elixir);
}