Skip to content

Commit 0bbb1c5

Browse files
committed
make regexp_ast
1 parent b3b5eb1 commit 0bbb1c5

File tree

6 files changed

+89
-18
lines changed

6 files changed

+89
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ast_node/src/encoding/decode.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
9292
let name = &unknown.ident;
9393
assert!(
9494
unknown.discriminant.is_none(),
95-
"custom discriminant unsupport"
95+
"unknown member is not allowed custom discriminant"
9696
);
9797
assert!(
9898
is_with(&unknown.attrs).is_none(),
@@ -133,15 +133,20 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
133133
);
134134
}
135135

136+
let mut discriminant: u32 = 0;
136137
let fields = iter
137-
.enumerate()
138-
.map(|(idx, field)| -> syn::Arm {
139-
let idx = idx + 1; // skip zero
140-
let idx: u32 = idx.try_into().expect("enum tags must not exceed 32 bits");
141-
let idx = idx as u64;
138+
.map(|field| -> syn::Arm {
139+
match field.discriminant.as_ref() {
140+
Some((_, syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }))) => {
141+
discriminant = lit.base10_parse::<u32>().unwrap();
142+
},
143+
Some(_) => panic!("unsupported discriminant type"),
144+
None => (),
145+
};
146+
discriminant += 1;
147+
let idx = discriminant as u64;
142148
let name = &field.ident;
143149

144-
assert!(field.discriminant.is_none(), "custom discriminant is not allowed");
145150
assert!(!is_unknown(&field.attrs), "unknown member must be first");
146151

147152
match enum_type {

crates/ast_node/src/encoding/encode.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
8181
let name = &unknown.ident;
8282
assert!(
8383
unknown.discriminant.is_none(),
84-
"custom discriminant is not allowed"
84+
"unknown member is not allowed custom discriminant"
8585
);
8686
assert!(
8787
is_with(&unknown.attrs).is_none(),
@@ -117,15 +117,19 @@ pub fn expand(DeriveInput { ident, data, .. }: DeriveInput) -> syn::ItemImpl {
117117
);
118118
}
119119

120-
let fields = iter.enumerate().map(|(idx, field)| -> syn::Arm {
121-
let idx = idx + 1; // skip zero
122-
let idx: u32 = idx.try_into().expect("enum tags must not exceed 32 bits");
120+
let mut discriminant: u32 = 0;
121+
let fields = iter.map(|field| -> syn::Arm {
122+
match field.discriminant.as_ref() {
123+
Some((_, syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(lit), .. }))) => {
124+
discriminant = lit.base10_parse::<u32>().unwrap();
125+
},
126+
Some(_) => panic!("unsupported discriminant type"),
127+
None => (),
128+
};
129+
discriminant += 1;
130+
let idx = discriminant;
123131
let name = &field.ident;
124132

125-
assert!(
126-
field.discriminant.is_none(),
127-
"custom discriminant is not allowed"
128-
);
129133
assert!(
130134
!is_unknown(&field.attrs),
131135
"unknown member must be first: {:?}",

crates/swc_common/src/serializer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'de> cbor4ii::core::dec::Decode<'de> for WithChar<char> {
3737
) -> Result<Self, cbor4ii::core::dec::Error<R::Error>> {
3838
let n = u32::decode(reader)?;
3939
let value = char::from_u32(n).ok_or_else(|| cbor4ii::core::dec::Error::Mismatch {
40-
name: &"Token::Delim",
40+
name: &"WithChar",
4141
found: 0,
4242
})?;
4343
Ok(WithChar(value))

crates/swc_ecma_regexp_ast/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ __rkyv = []
1616
default = []
1717
rkyv-impl = []
1818
serde-impl = ["serde"]
19+
encoding-impl = ["cbor4ii", "swc_atoms/encoding-impl", "swc_common/encoding-impl"]
1920

2021
[dependencies]
2122
bitflags = { workspace = true }
2223
is-macro = { workspace = true }
2324
serde = { workspace = true, features = ["derive"], optional = true }
25+
cbor4ii = { workspace = true, features = [ "use_std" ], optional = true }
2426

2527
swc_atoms = { version = "7.0.0", path = "../swc_atoms" }
2628
swc_common = { version = "14.0.4", path = "../swc_common" }

crates/swc_ecma_regexp_ast/src/lib.rs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct Alternative {
3030
}
3131

3232
/// Single unit of [`Alternative`], containing various kinds.
33-
#[ast_node]
33+
#[ast_node(no_unknown)]
3434
#[derive(Eq, Hash, EqIgnoreSpan, Is)]
3535
pub enum Term {
3636
// Assertion
@@ -85,6 +85,10 @@ pub struct BoundaryAssertion {
8585
}
8686

8787
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
88+
#[cfg_attr(
89+
feature = "encoding-impl",
90+
derive(::swc_common::Encode, ::swc_common::Decode)
91+
)]
8892
pub enum BoundaryAssertionKind {
8993
Start = 0,
9094
End = 1,
@@ -104,6 +108,10 @@ pub struct LookAroundAssertion {
104108
}
105109

106110
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
111+
#[cfg_attr(
112+
feature = "encoding-impl",
113+
derive(::swc_common::Encode, ::swc_common::Decode)
114+
)]
107115
pub enum LookAroundAssertionKind {
108116
Lookahead = 0,
109117
NegativeLookahead = 1,
@@ -119,6 +127,10 @@ pub struct Quantifier {
119127
pub span: Span,
120128
pub min: u64,
121129
/// `None` means no upper bound.
130+
#[cfg_attr(
131+
feature = "encoding-impl",
132+
encoding(with = "cbor4ii::core::types::Maybe")
133+
)]
122134
pub max: Option<u64>,
123135
pub greedy: bool,
124136
pub body: Term,
@@ -138,6 +150,10 @@ pub struct Character {
138150
}
139151

140152
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
153+
#[cfg_attr(
154+
feature = "encoding-impl",
155+
derive(::swc_common::Encode, ::swc_common::Decode)
156+
)]
141157
pub enum CharacterKind {
142158
ControlLetter = 0,
143159
HexadecimalEscape = 1,
@@ -163,6 +179,10 @@ pub struct CharacterClassEscape {
163179
}
164180

165181
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
182+
#[cfg_attr(
183+
feature = "encoding-impl",
184+
derive(::swc_common::Encode, ::swc_common::Decode)
185+
)]
166186
pub enum CharacterClassEscapeKind {
167187
D = 0,
168188
NegativeD = 1,
@@ -183,6 +203,10 @@ pub struct UnicodePropertyEscape {
183203
/// strings.
184204
pub strings: bool,
185205
pub name: Atom,
206+
#[cfg_attr(
207+
feature = "encoding-impl",
208+
encoding(with = "cbor4ii::core::types::Maybe")
209+
)]
186210
pub value: Option<Atom>,
187211
}
188212

@@ -211,6 +235,10 @@ pub struct CharacterClass {
211235
}
212236

213237
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
238+
#[cfg_attr(
239+
feature = "encoding-impl",
240+
derive(::swc_common::Encode, ::swc_common::Decode)
241+
)]
214242
pub enum CharacterClassContentsKind {
215243
Union = 0,
216244
/// `UnicodeSetsMode` only.
@@ -219,7 +247,7 @@ pub enum CharacterClassContentsKind {
219247
Subtraction = 2,
220248
}
221249

222-
#[ast_node]
250+
#[ast_node(no_unknown)]
223251
#[derive(Eq, Hash, EqIgnoreSpan, Is)]
224252
pub enum CharacterClassContents {
225253
#[tag("CharacterClassRange")]
@@ -281,6 +309,10 @@ pub struct ClassString {
281309
pub struct CapturingGroup {
282310
pub span: Span,
283311
/// Group name to be referenced by [`NamedReference`].
312+
#[cfg_attr(
313+
feature = "encoding-impl",
314+
encoding(with = "cbor4ii::core::types::Maybe")
315+
)]
284316
pub name: Option<Atom>,
285317
pub body: Disjunction,
286318
}
@@ -291,6 +323,10 @@ pub struct CapturingGroup {
291323
#[derive(Eq, Hash, EqIgnoreSpan)]
292324
pub struct IgnoreGroup {
293325
pub span: Span,
326+
#[cfg_attr(
327+
feature = "encoding-impl",
328+
encoding(with = "cbor4ii::core::types::Maybe")
329+
)]
294330
pub modifiers: Option<Modifiers>,
295331
pub body: Disjunction,
296332
}
@@ -338,6 +374,29 @@ pub struct NamedReference {
338374
pub name: Atom,
339375
}
340376

377+
#[cfg(feature = "encoding-impl")]
378+
impl cbor4ii::core::enc::Encode for Modifier {
379+
fn encode<W: cbor4ii::core::enc::Write>(
380+
&self,
381+
writer: &mut W,
382+
) -> Result<(), cbor4ii::core::enc::Error<W::Error>> {
383+
self.bits().encode(writer)
384+
}
385+
}
386+
387+
#[cfg(feature = "encoding-impl")]
388+
impl<'de> cbor4ii::core::dec::Decode<'de> for Modifier {
389+
fn decode<R: cbor4ii::core::dec::Read<'de>>(
390+
reader: &mut R,
391+
) -> Result<Self, cbor4ii::core::dec::Error<R::Error>> {
392+
let n = u8::decode(reader)?;
393+
Modifier::from_bits(n).ok_or_else(|| cbor4ii::core::dec::Error::Mismatch {
394+
name: &"Modifier",
395+
found: 0,
396+
})
397+
}
398+
}
399+
341400
#[cfg(target_pointer_width = "64")]
342401
#[test]
343402
fn size_asserts() {

0 commit comments

Comments
 (0)