Skip to main content

dsl_parse/
ast.rs

1//! The internal AST shape consumed by every downstream crate.
2//!
3//! Kept small on purpose: only the fields we need for completion, hover,
4//! and analysis. Adding a field is cheap; removing one once a backend has
5//! grown to fill it costs more, so we start conservative.
6
7use serde::Serialize;
8use text_size::TextRange;
9
10#[derive(Debug, Clone, Serialize)]
11pub struct Statement {
12  pub range: TextRange,
13  pub kind: StatementKind,
14}
15
16#[derive(Debug, Clone, Serialize)]
17pub enum StatementKind {
18  Select(SelectStmt),
19  Insert(InsertStmt),
20  Update(UpdateStmt),
21  Delete(DeleteStmt),
22  CreateTable(CreateTableStmt),
23  AlterTable(AlterTableStmt),
24  DropTable(DropTableStmt),
25  /// Any statement we don't model in v0.1, or one that failed to parse.
26  Unknown {
27    text: String,
28  },
29}
30
31// ---------------------------------------------------------------------------
32// SELECT
33// ---------------------------------------------------------------------------
34
35#[derive(Debug, Clone, Default, Serialize)]
36pub struct SelectStmt {
37  pub projections: Vec<Projection>,
38  pub from: Vec<TableRef>,
39  pub joins: Vec<JoinClause>,
40  pub where_clause: Option<Expr>,
41  /// CTE table names declared in a leading `WITH x AS (...) [, y AS (...)]`
42  /// clause. Populated by the parser backend so the resolver can bind
43  /// them as scope tables -- otherwise referencing the CTE in the outer
44  /// SELECT looks like an unresolved table to completion + diagnostics.
45  #[serde(default)]
46  pub cte_names: Vec<String>,
47}
48
49#[derive(Debug, Clone, Serialize)]
50pub enum Projection {
51  Star,
52  QualifiedStar(String),
53  Expr { expr: Expr, alias: Option<String> },
54}
55
56// ---------------------------------------------------------------------------
57// Tables and joins
58// ---------------------------------------------------------------------------
59
60#[derive(Debug, Clone, Serialize, Default)]
61pub struct TableRef {
62  /// `schema.table` -- schema is the part before the dot when present.
63  pub schema: Option<String>,
64  pub name: String,
65  pub alias: Option<String>,
66  pub range: TextRange,
67}
68
69#[derive(Debug, Clone, Serialize)]
70pub struct JoinClause {
71  pub kind: JoinKind,
72  pub table: TableRef,
73  pub on: Option<Expr>,
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
77pub enum JoinKind {
78  Inner,
79  Left,
80  Right,
81  Full,
82  Cross,
83}
84
85// ---------------------------------------------------------------------------
86// Expressions
87// ---------------------------------------------------------------------------
88
89#[derive(Debug, Clone, Serialize)]
90pub enum Expr {
91  Column {
92    qualifier: Option<String>,
93    name: String,
94    range: TextRange,
95  },
96  Literal(String),
97  BinaryOp {
98    op: String,
99    left: Box<Expr>,
100    right: Box<Expr>,
101  },
102  Call {
103    name: String,
104    args: Vec<Expr>,
105  },
106  /// Anything we don't model individually yet -- stringified upstream AST.
107  Other(String),
108  /// Flat container of sub-expressions. Used when the parser can't yet
109  /// build a structured AST for a clause (WHERE / ON predicates,
110  /// argument lists in unsupported constructs) but still wants
111  /// downstream rules to see the column references inside it. Walkers
112  /// recurse element-wise.
113  List(Vec<Expr>),
114}
115
116// ---------------------------------------------------------------------------
117// DML
118// ---------------------------------------------------------------------------
119
120#[derive(Debug, Clone, Default, Serialize)]
121pub struct InsertStmt {
122  pub table: TableRef,
123  pub columns: Vec<String>,
124}
125
126#[derive(Debug, Clone, Default, Serialize)]
127pub struct UpdateStmt {
128  pub table: TableRef,
129  pub assignments: Vec<(String, Expr)>,
130  pub where_clause: Option<Expr>,
131  /// Tables introduced by `UPDATE ... FROM <list>` -- not the target,
132  /// but exposed for column refs in SET RHS and WHERE.
133  pub from_tables: Vec<TableRef>,
134}
135
136#[derive(Debug, Clone, Default, Serialize)]
137pub struct DeleteStmt {
138  pub table: TableRef,
139  pub where_clause: Option<Expr>,
140  /// Tables introduced by `DELETE FROM <tgt> USING <list>` -- exposed
141  /// for column refs in WHERE and RETURNING.
142  pub using_tables: Vec<TableRef>,
143}
144
145// ---------------------------------------------------------------------------
146// DDL
147// ---------------------------------------------------------------------------
148
149#[derive(Debug, Clone, Default, Serialize)]
150pub struct CreateTableStmt {
151  pub table: TableRef,
152  pub if_not_exists: bool,
153  pub columns: Vec<ColumnDef>,
154}
155
156#[derive(Debug, Clone, Serialize)]
157pub struct ColumnDef {
158  pub name: String,
159  pub type_name: String,
160  pub nullable: bool,
161  pub default: Option<String>,
162  pub range: TextRange,
163}
164
165#[derive(Debug, Clone, Default, Serialize)]
166pub struct AlterTableStmt {
167  pub table: TableRef,
168}
169
170#[derive(Debug, Clone, Default, Serialize)]
171pub struct DropTableStmt {
172  pub tables: Vec<TableRef>,
173  pub if_exists: bool,
174}