squonk_ast/ast/ident.rs
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 Moderately AI Inc.
3
4//! Identifier AST nodes: `Ident`, its `QuoteStyle`, and the dotted `ObjectName`.
5
6use crate::vocab::{Meta, Symbol};
7use thin_vec::ThinVec;
8
9/// An interned identifier plus the quote style used in source.
10#[derive(Clone, Debug, PartialEq, Eq, Hash)]
11#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
12#[cfg_attr(feature = "serde-deserialize", derive(serde::Deserialize))]
13pub struct Ident {
14 /// The interned identifier text.
15 pub sym: Symbol,
16 /// Delimiter used to quote the source token.
17 pub quote: QuoteStyle,
18 /// Source location and node identity.
19 pub meta: Meta,
20}
21
22/// Surface quote spelling for an identifier.
23#[derive(Clone, Debug, PartialEq, Eq, Hash)]
24#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
25#[cfg_attr(feature = "serde-deserialize", derive(serde::Deserialize))]
26pub enum QuoteStyle {
27 /// Unquoted — a bare identifier.
28 None,
29 /// Single quotes `'…'`. Not an identifier delimiter in standard SQL — this records
30 /// a MySQL string literal used as a column alias (`SELECT 1 AS 'x'`), whose value is
31 /// interned as the identifier and rendered back single-quoted so it round-trips.
32 Single,
33 /// Double-quoted `"…"` — the standard SQL delimited identifier.
34 Double,
35 /// PostgreSQL / SQL-standard Unicode-escaped delimited identifier `U&"…"`, optionally
36 /// followed by a `UESCAPE 'c'` clause. Like [`Double`](Self::Double) the delimiter is a
37 /// double quote, but the body carries `\XXXX` / `\+XXXXXX` escapes decoded against the
38 /// default `\` (or the `UESCAPE` override). The interned [`sym`](Ident::sym) holds the
39 /// *decoded* value — target-neutral, so a `TargetDialect` re-spell and the redacted
40 /// fingerprint emit the plain `"…"` form — while a source-fidelity render replays the
41 /// exact `U&"…" [UESCAPE 'c']` spelling from the node's span. The distinct variant is
42 /// what tells the renderer the decoded value and the source spelling differ (a plain
43 /// `Double` ident's value already *is* its spelling).
44 UnicodeDouble,
45 /// Backtick-quoted `` `…` `` (MySQL).
46 Backtick,
47 /// Bracket-quoted `[…]` (T-SQL).
48 Bracket,
49}
50
51/// A qualified object name such as `catalog.schema.table`.
52#[derive(Clone, Debug, PartialEq, Eq, Hash)]
53#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
54#[cfg_attr(feature = "serde-deserialize", derive(serde::Deserialize))]
55pub struct ObjectName(pub ThinVec<Ident>);