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
//! AST builder.
//!
//! AST nodes are created by builder methods defined on the AST types themselves,
//! which are passed a `&B where B: GetAstBuilder` or `&A where A: GetAllocator`:
//!
//! * `Statement::new_expression_statement(span, expr, &builder)`
//! * `Vec::new_in(&builder)`, `Ident::from_str_in(str, &builder)`
//!
//! [`AstBuilder`] provides the memory arena and [`NodeId`]s that these methods use.
//! It is not [`Copy`] or [`Clone`], and is passed by reference.
//! Its `allocator` field is private - use the `allocator` method provided by the [`GetAllocator`] trait.
//!
//! Implementing [`GetAstBuilder`] on types which hold an [`AstBuilder`] allows for a shorter syntax:
//!
//! * Long: `Vec::new_in(&self.ast)`
//! * Short: `Vec::new_in(self)`
//!
//! e.g.:
//!
//! ```
//! use oxc_allocator::{Allocator, ArenaVec, GetAllocator};
//! use oxc_ast::{ast::*, builder::{AstBuilder, GetAstBuilder}};
//! use oxc_span::SPAN;
//!
//! struct MyAstProcessor<'a> {
//! builder: AstBuilder<'a>,
//! }
//!
//! impl<'a> GetAstBuilder<'a> for MyAstProcessor<'a> {
//! type Builder = AstBuilder<'a>;
//!
//! fn builder(&self) -> &AstBuilder<'a> {
//! &self.builder
//! }
//! }
//!
//! impl<'a> GetAllocator<'a> for MyAstProcessor<'a> {
//! fn allocator(&self) -> &'a Allocator {
//! self.builder.allocator()
//! }
//! }
//!
//! impl<'a> MyAstProcessor<'a> {
//! pub fn new(allocator: &'a Allocator) -> Self {
//! let builder = AstBuilder::new(allocator);
//! Self { builder }
//! }
//!
//! /// Create a `Vec` of 3 x `null` expressions.
//! pub fn null_literals_array(&self) -> ArenaVec<'a, Expression<'a>> {
//! // Can just pass `self` to all these methods, because `GetAstBuilder`
//! // and `GetAllocator` are implemented on `MyAstProcessor`
//! ArenaVec::from_array_in(
//! [
//! Expression::new_null_literal(SPAN, self),
//! Expression::new_null_literal(SPAN, self),
//! Expression::new_null_literal(SPAN, self),
//! ],
//! self
//! )
//! }
//! }
//! ```
//!
//! These AST type builder methods are defined in `../generated/builder_methods.rs` and `custom.rs`.
//!
//! ## Migration from the old builder
//!
//! [`AstBuilder`] used to be a [`Copy`] type with its own methods for creating AST nodes
//! (e.g. `builder.statement_expression(span, expr)`) and primitives (e.g. `builder.vec()`,`builder.ident(str)`).
//! Those methods have now been removed. Use the AST type builder methods described above instead.
//!
//! [`AstBuilder`] and [`NONE`] are no longer re-exported from the crate root either -
//! import them from this module instead.
//!
//! Explanation of the motivation for this change here: <https://github.com/oxc-project/oxc/issues/23043>.
use ;
use NodeId;
/// Trait for types which can create AST nodes.
///
/// Implemented by [`AstBuilder`], and provides the memory arena and [`NodeId`]s used by the AST node
/// builder methods defined on AST types (e.g. `Statement::new_expression_statement`).
///
/// AST node builder methods are generic over `A: GetAstBuilder<'a>`, so they can be called with a
/// builder directly, or with a type which exposes one by implementing [`GetAstBuilder`]
/// (e.g. parser or traverse context).
///
/// Further [`AstBuild`] implementations will be added later:
///
/// * Version for parser which counts AST nodes (to accurately pre-allocate `Vec`s in `SemanticBuilder`).
/// * Version for transformer/minifier which assigns unique [`NodeId`]s to all AST nodes.
///
/// [`AstBuild`] types must also implement:
///
/// * [`GetAstBuilder`] - referring to itself
/// * [`GetAllocator`]
///
/// These bounds mean that any type returned by [`GetAstBuilder::builder`] can be passed to
/// any other method which accepts any `&B where B: GetAstBuilder<'a>` or `&A where A: GetAllocator<'a>`
/// (i.e. other AST builder methods).
/// Trait for types which provide access to an [`AstBuild`]er.
///
/// Implemented by the [`AstBuild`]ers themselves (returning `self`), and by types which hold one
/// (e.g. parser or traverse context). AST node builder methods are generic over `B: GetAstBuilder<'a>`,
/// so they can be called with a builder directly, or with a type which holds one.
/// AST builder which assigns dummy [`NodeId`]s to AST nodes.
///
/// For use where no `NodeId`s are required on AST nodes as they are built
/// e.g. parser, because `NodeId`s are assigned later when building `Semantic`.
/// Type that can be used in any AST builder method call which requires either:
///
/// * `IntoIn<'a, Option<Box<'a, T>>`.
/// * `IntoIn<'a, Option<Vec<'a, T>>`.
///
/// Pass `NONE` instead of `None::<Box<'a, T>>`.
;