Repository navigation
Expand file tree
/
Copy pathast.rs
More file actions
623 lines (578 loc) · 17.7 KB
/
Copy pathast.rs
File metadata and controls
623 lines (578 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use std::collections::HashSet;
use crate::span::Span;
/// Interned atom identifier. Wraps a `string_interner::DefaultSymbol` index.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AtomId(pub u32);
/// A complete Erlang module.
#[derive(Debug, Clone)]
pub struct Module {
pub name: AtomId,
/// Span covering just the module name atom in `-module(Name).`
pub name_span: Span,
pub exports: Vec<FunArity>,
/// `-compile(export_all)`. `exports` keeps the explicit list; the flag
/// also exports every source function and lists it in `module_info(exports)`.
pub export_all: bool,
pub imports: Vec<ImportDecl>,
pub no_auto_import: NoAutoImport,
pub type_exports: Vec<FunArity>,
pub deprecated: Vec<FunArity>,
pub functions: Vec<Function>,
pub records: Vec<RecordDef>,
pub type_defs: Vec<TypeDef>,
pub specs: Vec<SpecDef>,
pub behaviours: Vec<BehaviourAttr>,
/// Type names referenced in `-spec`, `-callback`, and `-type`/`-opaque` bodies.
/// Collected during parsing as atoms followed by `(` in type contexts.
pub type_refs: HashSet<AtomId>,
pub span: Span,
}
/// Module-level auto-import suppression from compile options.
#[derive(Debug, Clone, Default)]
pub struct NoAutoImport {
pub all: bool,
pub functions: Vec<FunArity>,
}
/// Functions made available without a module qualifier by `-import`.
#[derive(Debug, Clone)]
pub struct ImportDecl {
pub module: AtomId,
pub functions: Vec<FunArity>,
}
/// A `-behaviour(name).` or `-behavior(name).` attribute.
#[derive(Debug, Clone)]
pub struct BehaviourAttr {
pub name: AtomId,
pub span: Span,
}
/// A `-type` or `-opaque` attribute definition.
#[derive(Debug, Clone)]
pub struct TypeDef {
pub name: AtomId,
pub name_span: Span,
pub arity: u32,
pub is_opaque: bool,
pub span: Span,
}
/// A `-spec` or `-callback` attribute definition.
#[derive(Debug, Clone)]
pub struct SpecDef {
pub func_name: AtomId,
pub name_span: Span,
pub arity: u32,
pub is_callback: bool,
pub span: Span,
}
/// A function name / arity pair, e.g. `foo/2`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FunArity {
pub name: AtomId,
pub arity: u32,
pub span: Span,
}
/// A top-level function definition (all clauses sharing the same name/arity).
#[derive(Debug, Clone)]
pub struct Function {
pub name: AtomId,
/// Span covering just the function name atom (for semantic highlighting).
pub name_span: Span,
pub arity: u32,
pub clauses: Vec<FunctionClause>,
pub span: Span,
}
/// One clause of a function definition.
#[derive(Debug, Clone)]
pub struct FunctionClause {
/// Span covering the function name atom for this clause.
pub name_span: Span,
pub patterns: Vec<Pattern>,
pub guard: Option<Guard>,
pub body: Vec<Expr>,
pub span: Span,
}
/// Guard expressions.
#[derive(Debug, Clone)]
pub enum Guard {
/// A single guard test expression.
Test(Box<Expr>),
/// Comma-separated conjunction of guard tests.
Conjunction(Vec<Expr>),
/// Semicolon-separated disjunction of guard conjunctions.
Disjunction(Vec<Guard>),
}
/// Pattern nodes used in function heads, case clauses, and match expressions.
#[derive(Debug, Clone)]
pub enum Pattern {
/// The wildcard pattern `_`.
Wildcard(Span),
/// A variable pattern, e.g. `X`.
Variable(AtomId, Span),
/// An integer literal pattern.
Integer(i64, Span),
/// Canonical decimal integer outside i64.
BigInteger(String, Span),
/// A float literal pattern.
Float(f64, Span),
/// An atom literal pattern.
Atom(AtomId, Span),
/// A string literal pattern.
String(String, Span),
/// A tuple pattern, e.g. `{ok, X}`.
Tuple(Vec<Pattern>, Span),
/// A cons cell pattern, e.g. `[H|T]`.
Cons(Box<Pattern>, Box<Pattern>, Span),
/// The empty list `[]`.
Nil(Span),
/// An alias / match pattern, e.g. `X = {ok, V}`.
Match(Box<Pattern>, Box<Pattern>, Span),
/// A binary pattern, e.g. `<<X:8, Y:16, Rest/binary>>`.
Binary(Vec<BinSegment<Pattern>>, Span),
/// A map pattern, e.g. `#{key := Value}`.
Map(Vec<MapPatternField>, Span),
/// A record pattern, e.g. `#person{name = N, age = _}`.
Record {
record_name: AtomId,
fields: Vec<RecordPatternField>,
span: Span,
},
/// String prefix match pattern: `"prefix" ++ Rest`.
/// LHS is a string literal, RHS is an arbitrary pattern (typically a variable).
/// Desugared to nested `Cons` cells in the pattern compiler.
Concat(Box<Pattern>, Box<Pattern>, Span),
/// An unexpanded macro call in pattern position: `?NAME` or `?NAME(...)`.
/// Only produced when parsing without preprocessor (e.g. formatter path).
MacroCall {
name: AtomId,
args: Option<Vec<Pattern>>,
span: Span,
},
}
/// A single field in a map pattern: `KeyExpr := ValuePattern`.
#[derive(Debug, Clone)]
pub struct MapPatternField {
pub key: Expr,
pub value: Pattern,
pub span: Span,
}
/// An item in a `maybe` block: either a plain expression or a conditional match (`?=`).
#[derive(Debug, Clone)]
pub enum MaybeExpr {
/// A plain expression in the maybe body.
Expr(Expr),
/// A conditional match: `Pattern ?= Expr`.
CondMatch(Pattern, Expr, Span),
}
/// Expression nodes.
#[derive(Debug, Clone)]
pub enum Expr {
Integer(i64, Span),
/// Canonical decimal integer outside i64.
BigInteger(String, Span),
Float(f64, Span),
Atom(AtomId, Span),
String(String, Span),
Nil(Span),
Variable(AtomId, Span),
Tuple(Vec<Expr>, Span),
Cons(Box<Expr>, Box<Expr>, Span),
BinOp(BinOp, Box<Expr>, Box<Expr>, Span),
UnaryOp(UnaryOp, Box<Expr>, Span),
Match(Box<Pattern>, Box<Expr>, Span),
Case(Box<Expr>, Vec<CaseClause>, Span),
If(Vec<IfClause>, Span),
Receive(Vec<CaseClause>, Option<AfterClause>, Span),
Call(CallTarget, Vec<Expr>, Span),
Send(Box<Expr>, Box<Expr>, Span),
Block(Vec<Expr>, Span),
/// A binary expression, e.g. `<<1, 2, X:16>>`.
Binary(Vec<BinSegment<Expr>>, Span),
/// A map expression, e.g. `#{key => value}`.
Map(Vec<MapField>, Span),
/// A map update expression, e.g. `M#{key => value, key2 := value2}`.
MapUpdate(Box<Expr>, Vec<MapUpdateField>, Span),
/// A list comprehension, e.g. `[Expr || Var <- List, Guard]`.
ListComp(Box<Expr>, Vec<LcQualifier>, Span),
/// A binary comprehension, e.g. `<< <<X>> || X <- List >>`.
BinComp(Box<Expr>, Vec<LcQualifier>, Span),
/// A try/catch/after expression, optionally with `of` clauses.
Try {
body: Vec<Expr>,
of_clauses: Vec<CaseClause>,
catch_clauses: Vec<CatchClause>,
after_body: Option<Vec<Expr>>,
span: Span,
},
/// `throw(Reason)` — raises a throw exception.
Throw(Box<Expr>, Span),
/// `catch Expr` — unary catch operator (returns value or `{'EXIT', ...}`).
Catch(Box<Expr>, Span),
/// Anonymous or named fun: `fun(X) -> X + 1 end` or `fun Name(X) -> X end`.
/// The optional `AtomId` is the name for named funs (used for self-recursion).
Fun(Option<AtomId>, Vec<FunctionClause>, Span),
/// Function reference: `fun name/arity`.
FunRef(FunRefTarget, Span),
/// Record creation: `#person{name = "Alice", age = 30}`.
RecordCreate {
record_name: AtomId,
fields: Vec<RecordFieldAssign>,
span: Span,
},
/// Record update: `R#person{name = "Bob"}`.
RecordUpdate {
base: Box<Expr>,
record_name: AtomId,
fields: Vec<RecordFieldAssign>,
span: Span,
},
/// Record field access: `R#person.name`.
RecordAccess {
base: Box<Expr>,
record_name: AtomId,
field_name: AtomId,
field_name_span: Span,
span: Span,
},
/// Record index: `#person.name` → integer (1-based Erlang field index).
RecordIndex {
record_name: AtomId,
field_name: AtomId,
field_name_span: Span,
span: Span,
},
/// A `maybe...end` expression (EEP-49, OTP 25+).
Maybe {
body: Vec<MaybeExpr>,
else_clauses: Vec<CaseClause>,
span: Span,
},
/// An unexpanded macro call: `?NAME` or `?NAME(arg1, arg2, ...)`.
/// Only produced when parsing without preprocessor (e.g. formatter path).
MacroCall {
name: AtomId,
args: Option<Vec<Expr>>,
span: Span,
},
}
impl Expr {
/// Return the source span of this expression.
pub fn span(&self) -> Span {
match self {
Expr::Integer(_, s)
| Expr::BigInteger(_, s)
| Expr::Float(_, s)
| Expr::Atom(_, s)
| Expr::String(_, s)
| Expr::Nil(s)
| Expr::Variable(_, s)
| Expr::Tuple(_, s)
| Expr::Cons(_, _, s)
| Expr::BinOp(_, _, _, s)
| Expr::UnaryOp(_, _, s)
| Expr::Match(_, _, s)
| Expr::Case(_, _, s)
| Expr::If(_, s)
| Expr::Receive(_, _, s)
| Expr::Call(_, _, s)
| Expr::Send(_, _, s)
| Expr::Block(_, s)
| Expr::Binary(_, s)
| Expr::Map(_, s)
| Expr::MapUpdate(_, _, s)
| Expr::ListComp(_, _, s)
| Expr::BinComp(_, _, s)
| Expr::Throw(_, s)
| Expr::Catch(_, s)
| Expr::Fun(_, _, s)
| Expr::FunRef(_, s) => *s,
Expr::Try { span, .. }
| Expr::RecordCreate { span, .. }
| Expr::RecordUpdate { span, .. }
| Expr::RecordAccess { span, .. }
| Expr::RecordIndex { span, .. }
| Expr::Maybe { span, .. }
| Expr::MacroCall { span, .. } => *span,
}
}
}
/// A qualifier in a list or binary comprehension.
#[derive(Debug, Clone)]
pub enum LcQualifier {
/// Generator: `Pattern <- Expr`.
Generator(Pattern, Box<Expr>, Span),
/// Binary generator: `<<Pattern>> <= Expr`.
BinGenerator(Pattern, Box<Expr>, Span),
/// Filter: a boolean expression.
Filter(Box<Expr>, Span),
}
/// Operator used in a map field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MapFieldOp {
/// `=>` — association (map construction)
Assoc,
/// `:=` — exact match (map pattern / update)
Exact,
}
/// A single field in a map expression: `KeyExpr => ValueExpr` or `KeyExpr := ValueExpr`.
#[derive(Debug, Clone)]
pub struct MapField {
pub key: Expr,
pub value: Expr,
pub op: MapFieldOp,
pub op_span: Span,
pub span: Span,
}
/// Operator in a map update field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MapUpdateOp {
/// `=>` — insert or replace (key may or may not exist)
Assoc,
/// `:=` — exact update (key must exist, otherwise `badkey`)
Exact,
}
/// A single field in a map update expression: `KeyExpr Op ValueExpr`.
#[derive(Debug, Clone)]
pub struct MapUpdateField {
pub key: Expr,
pub value: Expr,
pub op: MapUpdateOp,
pub span: Span,
}
// ── Record definitions and fields ──────────────────────────────────────
/// A field definition in a `-record(name, {fields...}).` attribute.
#[derive(Debug, Clone)]
pub struct RecordFieldDef {
/// Field name (an atom).
pub name: AtomId,
/// Span of the field name atom.
pub name_span: Span,
/// Default value expression, or None (defaults to `undefined`).
pub default: Option<Expr>,
pub span: Span,
}
/// A complete record definition from `-record(name, {fields...}).`
#[derive(Debug, Clone)]
pub struct RecordDef {
/// Record name (an atom).
pub name: AtomId,
/// Span of the record name atom.
pub name_span: Span,
/// Ordered list of field definitions.
pub fields: Vec<RecordFieldDef>,
pub span: Span,
}
/// A field assignment in a record creation or update expression.
#[derive(Debug, Clone)]
pub struct RecordFieldAssign {
pub field_name: AtomId,
/// Span of the field name atom.
pub field_name_span: Span,
pub value: Expr,
pub span: Span,
}
/// A field in a record pattern.
#[derive(Debug, Clone)]
pub struct RecordPatternField {
pub field_name: AtomId,
/// Span of the field name atom.
pub field_name_span: Span,
pub pattern: Pattern,
pub span: Span,
}
/// A clause in a `catch` block: `Class:Pattern[:Stacktrace] [when Guard] -> Body`.
#[derive(Debug, Clone)]
pub struct CatchClause {
pub class: CatchClass,
pub pattern: Pattern,
/// Optional stacktrace binding (3-part catch: `Class:Reason:Stacktrace`).
pub stacktrace: Option<Pattern>,
pub guard: Option<Guard>,
pub body: Vec<Expr>,
pub span: Span,
}
/// Exception class in a catch clause.
#[derive(Debug, Clone)]
pub enum CatchClass {
Throw,
Error,
Exit,
/// Variable binding for the class, e.g. `Class:Pattern`.
Variable(AtomId, Span),
}
/// The target of a function call.
#[derive(Debug, Clone)]
pub enum CallTarget {
/// Local call, e.g. `foo(1)`. The span covers the function name atom.
Local(AtomId, Span),
/// Remote call, e.g. `mod:fun(1)` or `Mod:Fun(1)`.
Remote(Box<Expr>, Box<Expr>),
/// Variable call, e.g. `F(1)` where F holds a fun. The span covers the variable.
Variable(AtomId, Span),
/// Call whose callee is an arbitrary expression, e.g.
/// `(fun() -> 42 end)(0)` or `(make_fun())(x)`.
Expr(Box<Expr>),
}
/// The target of a function reference (`fun name/arity` or `fun M:F/A`).
#[derive(Debug, Clone)]
pub enum FunRefTarget {
/// Local function reference: `fun name/arity`.
Local(AtomId, u32),
/// Remote function reference: `fun Module:Function/Arity`.
Remote(AtomId, AtomId, u32),
/// Dynamic remote function reference: `fun Mod:Fun/Arity` where components are expressions.
DynamicRemote(Box<Expr>, Box<Expr>, Box<Expr>),
}
/// A clause in a `case` or `receive` expression.
#[derive(Debug, Clone)]
pub struct CaseClause {
pub pattern: Pattern,
pub guard: Option<Guard>,
pub body: Vec<Expr>,
pub span: Span,
}
/// A clause in an `if` expression.
#[derive(Debug, Clone)]
pub struct IfClause {
pub guard: Guard,
pub body: Vec<Expr>,
pub span: Span,
}
/// The `after` clause of a `receive` expression.
#[derive(Debug, Clone)]
pub struct AfterClause {
pub timeout: Box<Expr>,
pub body: Vec<Expr>,
pub span: Span,
}
/// A single segment in a binary expression or pattern.
/// Represents: `Value:Size/TypeSpecifiers`
#[derive(Debug, Clone)]
pub struct BinSegment<T> {
/// The value: an expression (for construction) or pattern (for matching)
pub value: T,
/// Size in bits (None = default: 8 for integer, all remaining for binary)
pub size: Option<Box<Expr>>,
/// Type specifier
pub type_spec: BinSegmentType,
pub span: Span,
}
/// Binary segment type specifiers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinSegmentType {
pub typ: BinSegmentKind,
pub signedness: Signedness,
pub endianness: Endianness,
/// Only a written `unit:N`; alias units are represented by `typ`.
pub unit: Option<u32>,
/// Whether a `/Type-Specifier` list was written. OTP refuses a string
/// literal in a binary pattern with any list that names no UTF type,
/// even one of defaults such as `"ab"/integer`.
pub explicit: bool,
/// Written declarations, before defaults. Semantic validation owns their
/// merge policy; the effective fields above are used only after it passes.
pub specifiers: Vec<BinSpecifier>,
/// The exact written list, excluding `/`, for lossless formatting.
pub specifiers_span: Option<Span>,
}
impl Default for BinSegmentType {
fn default() -> Self {
BinSegmentType {
typ: BinSegmentKind::Integer,
signedness: Signedness::Unsigned,
endianness: Endianness::Big,
unit: None,
explicit: false,
specifiers: Vec::new(),
specifiers_span: None,
}
}
}
/// One written binary segment declaration. Aliases remain distinct where
/// they declare a unit: `bytes` is binary/unit:8, `bits` is bitstring/unit:1.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinSpecifier {
Type(BinSegmentKind),
Bytes,
Signedness(Signedness),
Endianness(Endianness),
Unit(u32),
}
/// Binary segment type kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinSegmentKind {
Integer,
Float,
Binary,
/// `bitstring` / `bits` — like `binary` but with default unit:1 instead of unit:8.
Bitstring,
Utf8,
Utf16,
Utf32,
}
/// Signedness for binary segment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Signedness {
Unsigned,
Signed,
}
impl std::fmt::Display for Signedness {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Unsigned => "unsigned",
Self::Signed => "signed",
})
}
}
/// Endianness for binary segment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Endianness {
Big,
Little,
Native,
}
impl std::fmt::Display for Endianness {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Big => "big",
Self::Little => "little",
Self::Native => "native",
})
}
}
/// Binary operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div,
IntDiv,
Rem,
Eq,
ExactEq,
NotEq,
ExactNotEq,
Lt,
Gt,
LtEq,
GtEq,
And,
Or,
Xor,
Andalso,
Orelse,
Band,
Bor,
Bxor,
Bsl,
Bsr,
ListAppend,
ListSubtract,
}
/// Unary operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
Pos,
Neg,
Not,
Bnot,
}