1use bitflags::bitflags;
2use is_macro::Is;
3use swc_atoms::Atom;
4use swc_common::{ast_node, EqIgnoreSpan, Span};
5
6mod display;
7
8#[ast_node]
10#[derive(Eq, Hash, EqIgnoreSpan)]
11pub struct Pattern {
12 pub span: Span,
13 pub body: Disjunction,
14}
15
16#[ast_node]
18#[derive(Eq, Hash, EqIgnoreSpan)]
19pub struct Disjunction {
20 pub span: Span,
21 pub body: Vec<Alternative>,
22}
23
24#[ast_node]
26#[derive(Eq, Hash, EqIgnoreSpan)]
27pub struct Alternative {
28 pub span: Span,
29 pub body: Vec<Term>,
30}
31
32#[ast_node(no_unknown)]
34#[derive(Eq, Hash, EqIgnoreSpan, Is)]
35pub enum Term {
36 #[tag("BoundaryAssertion")]
39 BoundaryAssertion(Box<BoundaryAssertion>),
40
41 #[tag("LookAroundAssertion")]
42 LookAroundAssertion(Box<LookAroundAssertion>),
43
44 #[tag("Quantifier")]
45 Quantifier(Box<Quantifier>),
46
47 #[tag("Character")]
50 Character(Box<Character>),
51
52 #[tag("CharacterClassEscape")]
53 Dot(Dot),
54
55 #[tag("CharacterClassEscape")]
56 CharacterClassEscape(Box<CharacterClassEscape>),
57
58 #[tag("UnicodePropertyEscape")]
59 UnicodePropertyEscape(Box<UnicodePropertyEscape>),
60
61 #[tag("ClassStringDisjunction")]
62 CharacterClass(Box<CharacterClass>),
63
64 #[tag("ClassStringDisjunction")]
65 CapturingGroup(Box<CapturingGroup>),
66
67 #[tag("ClassStringDisjunction")]
68 IgnoreGroup(Box<IgnoreGroup>),
69
70 #[tag("ClassStringDisjunction")]
71 IndexedReference(Box<IndexedReference>),
72
73 #[tag("ClassStringDisjunction")]
74 NamedReference(Box<NamedReference>),
75}
76
77#[ast_node]
80#[derive(Eq, Hash, EqIgnoreSpan)]
81pub struct BoundaryAssertion {
82 pub span: Span,
83 #[use_eq]
84 pub kind: BoundaryAssertionKind,
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
88pub enum BoundaryAssertionKind {
89 Start = 0,
90 End = 1,
91 Boundary = 2,
92 NegativeBoundary = 3,
93}
94
95#[ast_node]
98#[derive(Eq, Hash, EqIgnoreSpan)]
99pub struct LookAroundAssertion {
100 pub span: Span,
101 #[use_eq]
102 pub kind: LookAroundAssertionKind,
103 pub body: Disjunction,
104}
105
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
107pub enum LookAroundAssertionKind {
108 Lookahead = 0,
109 NegativeLookahead = 1,
110 Lookbehind = 2,
111 NegativeLookbehind = 3,
112}
113
114#[ast_node]
117#[derive(Eq, Hash, EqIgnoreSpan)]
118pub struct Quantifier {
119 pub span: Span,
120 pub min: u64,
121 pub max: Option<u64>,
123 pub greedy: bool,
124 pub body: Term,
125}
126
127#[ast_node]
129#[derive(Eq, Hash, EqIgnoreSpan)]
130pub struct Character {
131 pub span: Span,
134 #[use_eq]
135 pub kind: CharacterKind,
136 pub value: u32,
138}
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
141pub enum CharacterKind {
142 ControlLetter = 0,
143 HexadecimalEscape = 1,
144 Identifier = 2,
145 Null = 3,
146 Octal1 = 4,
148 Octal2 = 5,
149 Octal3 = 6,
150 SingleEscape = 7,
151 Symbol = 8,
152 UnicodeEscape = 9,
153}
154
155#[ast_node]
158#[derive(Eq, Hash, EqIgnoreSpan)]
159pub struct CharacterClassEscape {
160 pub span: Span,
161 #[use_eq]
162 pub kind: CharacterClassEscapeKind,
163}
164
165#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
166pub enum CharacterClassEscapeKind {
167 D = 0,
168 NegativeD = 1,
169 S = 2,
170 NegativeS = 3,
171 W = 4,
172 NegativeW = 5,
173}
174
175#[ast_node]
178#[derive(Eq, Hash, EqIgnoreSpan)]
179pub struct UnicodePropertyEscape {
180 pub span: Span,
181 pub negative: bool,
182 pub strings: bool,
185 pub name: Atom,
186 pub value: Option<Atom>,
187}
188
189#[ast_node]
191#[derive(Eq, Hash, EqIgnoreSpan)]
192pub struct Dot {
193 pub span: Span,
194}
195
196#[ast_node]
199#[derive(Eq, Hash, EqIgnoreSpan)]
200pub struct CharacterClass {
201 pub span: Span,
202 pub negative: bool,
203 pub strings: bool,
208 #[use_eq]
209 pub kind: CharacterClassContentsKind,
210 pub body: Vec<CharacterClassContents>,
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
214pub enum CharacterClassContentsKind {
215 Union = 0,
216 Intersection = 1,
218 Subtraction = 2,
220}
221
222#[ast_node(no_unknown)]
223#[derive(Eq, Hash, EqIgnoreSpan, Is)]
224pub enum CharacterClassContents {
225 #[tag("CharacterClassRange")]
226 CharacterClassRange(Box<CharacterClassRange>),
227
228 #[tag("CharacterClassEscape")]
229 CharacterClassEscape(Box<CharacterClassEscape>),
230
231 #[tag("UnicodePropertyEscape")]
232 UnicodePropertyEscape(Box<UnicodePropertyEscape>),
233
234 #[tag("Character")]
235 Character(Box<Character>),
236
237 #[tag("NestedCharacterClass")]
239 NestedCharacterClass(Box<CharacterClass>),
240
241 #[tag("ClassStringDisjunction")]
243 ClassStringDisjunction(Box<ClassStringDisjunction>),
244}
245
246#[ast_node]
249#[derive(Eq, Hash, EqIgnoreSpan)]
250pub struct CharacterClassRange {
251 pub span: Span,
252 pub min: Character,
253 pub max: Character,
254}
255
256#[ast_node]
258#[derive(Eq, Hash, EqIgnoreSpan)]
259pub struct ClassStringDisjunction {
260 pub span: Span,
261 pub strings: bool,
264 pub body: Vec<ClassString>,
265}
266
267#[ast_node]
269#[derive(Eq, Hash, EqIgnoreSpan)]
270pub struct ClassString {
271 pub span: Span,
272 pub strings: bool,
274 pub body: Vec<Character>,
275}
276
277#[ast_node]
280#[derive(Eq, Hash, EqIgnoreSpan)]
281pub struct CapturingGroup {
282 pub span: Span,
283 pub name: Option<Atom>,
285 pub body: Disjunction,
286}
287
288#[ast_node]
291#[derive(Eq, Hash, EqIgnoreSpan)]
292pub struct IgnoreGroup {
293 pub span: Span,
294 pub modifiers: Option<Modifiers>,
295 pub body: Disjunction,
296}
297
298#[ast_node]
301#[derive(Eq, Hash, EqIgnoreSpan)]
302pub struct Modifiers {
303 pub span: Span,
304 #[use_eq]
305 pub enabling: Modifier,
306 #[use_eq]
307 pub disabling: Modifier,
308}
309
310bitflags! {
311 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
313 pub struct Modifier: u8 {
314 const I = 1 << 0;
316 const M = 1 << 1;
318 const S = 1 << 2;
320 }
321}
322
323#[ast_node]
326#[derive(Eq, Hash, EqIgnoreSpan)]
327pub struct IndexedReference {
328 pub span: Span,
329 pub index: u32,
330}
331
332#[ast_node]
335#[derive(Eq, Hash, EqIgnoreSpan)]
336pub struct NamedReference {
337 pub span: Span,
338 pub name: Atom,
339}
340
341#[cfg(target_pointer_width = "64")]
342#[test]
343fn size_asserts() {
344 use std::mem::size_of;
345
346 assert!(size_of::<Term>() == 16);
347 assert!(size_of::<CharacterClassContents>() == 16);
348}