mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
refactor(parser): macro for ASCII identifier byte handlers (#2351)
Add a macro for ASCII identifier byte handlers. This is a preparatory step towards #2352.
This commit is contained in:
parent
6f597b18bc
commit
6910e4f71b
1 changed files with 65 additions and 23 deletions
|
|
@ -132,6 +132,49 @@ macro_rules! ascii_byte_handler {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::unnecessary_safety_comment)]
|
||||||
|
/// Macro for defining byte handler for an ASCII character which is start of an identifier
|
||||||
|
/// (`a`-`z`, `A`-`Z`, `$` or `_`).
|
||||||
|
///
|
||||||
|
/// Macro calls `Lexer::identifier_name_handler` to get the text of the identifier,
|
||||||
|
/// and slices off first character.
|
||||||
|
///
|
||||||
|
/// # SAFETY
|
||||||
|
/// Only use this macro to define byte handlers for ASCII characters.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// ascii_identifier_handler!(L_G(id_without_first_char) match id_without_first_char {
|
||||||
|
/// "et" => Kind::Get,
|
||||||
|
/// "lobal" => Kind::Global,
|
||||||
|
/// _ => Kind::Ident,
|
||||||
|
/// });
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// expands to:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// const L_G: ByteHandler = {
|
||||||
|
/// #[allow(non_snake_case)]
|
||||||
|
/// fn L_G(lexer: &mut Lexer) -> Kind {
|
||||||
|
/// let id_without_first_char = &lexer.identifier_name_handler()[1..];
|
||||||
|
/// match id_without_first_char {
|
||||||
|
/// "et" => Kind::Get,
|
||||||
|
/// "lobal" => Kind::Global,
|
||||||
|
/// _ => Kind::Ident,
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// L_G
|
||||||
|
/// };
|
||||||
|
/// ```
|
||||||
|
macro_rules! ascii_identifier_handler {
|
||||||
|
($id:ident($str:ident) $body:expr) => {
|
||||||
|
byte_handler!($id(lexer) {
|
||||||
|
let $str = &lexer.identifier_name_handler()[1..];
|
||||||
|
$body
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// `\0` `\1` etc
|
// `\0` `\1` etc
|
||||||
ascii_byte_handler!(ERR(lexer) {
|
ascii_byte_handler!(ERR(lexer) {
|
||||||
let c = lexer.consume_char();
|
let c = lexer.consume_char();
|
||||||
|
|
@ -196,8 +239,7 @@ ascii_byte_handler!(HAS(lexer) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// `A..=Z`, `a..=z` (except special cases below), `_`, `$`
|
// `A..=Z`, `a..=z` (except special cases below), `_`, `$`
|
||||||
ascii_byte_handler!(IDT(lexer) {
|
ascii_identifier_handler!(IDT(_id_without_first_char) {
|
||||||
lexer.identifier_name_handler();
|
|
||||||
Kind::Ident
|
Kind::Ident
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -461,7 +503,7 @@ ascii_byte_handler!(TLD(lexer) {
|
||||||
Kind::Tilde
|
Kind::Tilde
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_A(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_A(id_without_first_char) match id_without_first_char {
|
||||||
"wait" => Kind::Await,
|
"wait" => Kind::Await,
|
||||||
"sync" => Kind::Async,
|
"sync" => Kind::Async,
|
||||||
"bstract" => Kind::Abstract,
|
"bstract" => Kind::Abstract,
|
||||||
|
|
@ -473,14 +515,14 @@ ascii_byte_handler!(L_A(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_B(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_B(id_without_first_char) match id_without_first_char {
|
||||||
"reak" => Kind::Break,
|
"reak" => Kind::Break,
|
||||||
"oolean" => Kind::Boolean,
|
"oolean" => Kind::Boolean,
|
||||||
"igint" => Kind::BigInt,
|
"igint" => Kind::BigInt,
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_C(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_C(id_without_first_char) match id_without_first_char {
|
||||||
"onst" => Kind::Const,
|
"onst" => Kind::Const,
|
||||||
"lass" => Kind::Class,
|
"lass" => Kind::Class,
|
||||||
"ontinue" => Kind::Continue,
|
"ontinue" => Kind::Continue,
|
||||||
|
|
@ -490,7 +532,7 @@ ascii_byte_handler!(L_C(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_D(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_D(id_without_first_char) match id_without_first_char {
|
||||||
"o" => Kind::Do,
|
"o" => Kind::Do,
|
||||||
"elete" => Kind::Delete,
|
"elete" => Kind::Delete,
|
||||||
"eclare" => Kind::Declare,
|
"eclare" => Kind::Declare,
|
||||||
|
|
@ -499,7 +541,7 @@ ascii_byte_handler!(L_D(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_E(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_E(id_without_first_char) match id_without_first_char {
|
||||||
"lse" => Kind::Else,
|
"lse" => Kind::Else,
|
||||||
"num" => Kind::Enum,
|
"num" => Kind::Enum,
|
||||||
"xport" => Kind::Export,
|
"xport" => Kind::Export,
|
||||||
|
|
@ -507,7 +549,7 @@ ascii_byte_handler!(L_E(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_F(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_F(id_without_first_char) match id_without_first_char {
|
||||||
"unction" => Kind::Function,
|
"unction" => Kind::Function,
|
||||||
"alse" => Kind::False,
|
"alse" => Kind::False,
|
||||||
"or" => Kind::For,
|
"or" => Kind::For,
|
||||||
|
|
@ -516,13 +558,13 @@ ascii_byte_handler!(L_F(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_G(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_G(id_without_first_char) match id_without_first_char {
|
||||||
"et" => Kind::Get,
|
"et" => Kind::Get,
|
||||||
"lobal" => Kind::Global,
|
"lobal" => Kind::Global,
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_I(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_I(id_without_first_char) match id_without_first_char {
|
||||||
"f" => Kind::If,
|
"f" => Kind::If,
|
||||||
"nstanceof" => Kind::Instanceof,
|
"nstanceof" => Kind::Instanceof,
|
||||||
"n" => Kind::In,
|
"n" => Kind::In,
|
||||||
|
|
@ -535,23 +577,23 @@ ascii_byte_handler!(L_I(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_K(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_K(id_without_first_char) match id_without_first_char {
|
||||||
"eyof" => Kind::KeyOf,
|
"eyof" => Kind::KeyOf,
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_L(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_L(id_without_first_char) match id_without_first_char {
|
||||||
"et" => Kind::Let,
|
"et" => Kind::Let,
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_M(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_M(id_without_first_char) match id_without_first_char {
|
||||||
"eta" => Kind::Meta,
|
"eta" => Kind::Meta,
|
||||||
"odule" => Kind::Module,
|
"odule" => Kind::Module,
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_N(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_N(id_without_first_char) match id_without_first_char {
|
||||||
"ull" => Kind::Null,
|
"ull" => Kind::Null,
|
||||||
"ew" => Kind::New,
|
"ew" => Kind::New,
|
||||||
"umber" => Kind::Number,
|
"umber" => Kind::Number,
|
||||||
|
|
@ -560,7 +602,7 @@ ascii_byte_handler!(L_N(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_O(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_O(id_without_first_char) match id_without_first_char {
|
||||||
"f" => Kind::Of,
|
"f" => Kind::Of,
|
||||||
"bject" => Kind::Object,
|
"bject" => Kind::Object,
|
||||||
"ut" => Kind::Out,
|
"ut" => Kind::Out,
|
||||||
|
|
@ -568,7 +610,7 @@ ascii_byte_handler!(L_O(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_P(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_P(id_without_first_char) match id_without_first_char {
|
||||||
"ackage" => Kind::Package,
|
"ackage" => Kind::Package,
|
||||||
"rivate" => Kind::Private,
|
"rivate" => Kind::Private,
|
||||||
"rotected" => Kind::Protected,
|
"rotected" => Kind::Protected,
|
||||||
|
|
@ -576,14 +618,14 @@ ascii_byte_handler!(L_P(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_R(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_R(id_without_first_char) match id_without_first_char {
|
||||||
"eturn" => Kind::Return,
|
"eturn" => Kind::Return,
|
||||||
"equire" => Kind::Require,
|
"equire" => Kind::Require,
|
||||||
"eadonly" => Kind::Readonly,
|
"eadonly" => Kind::Readonly,
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_S(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_S(id_without_first_char) match id_without_first_char {
|
||||||
"et" => Kind::Set,
|
"et" => Kind::Set,
|
||||||
"uper" => Kind::Super,
|
"uper" => Kind::Super,
|
||||||
"witch" => Kind::Switch,
|
"witch" => Kind::Switch,
|
||||||
|
|
@ -594,7 +636,7 @@ ascii_byte_handler!(L_S(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_T(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_T(id_without_first_char) match id_without_first_char {
|
||||||
"his" => Kind::This,
|
"his" => Kind::This,
|
||||||
"rue" => Kind::True,
|
"rue" => Kind::True,
|
||||||
"hrow" => Kind::Throw,
|
"hrow" => Kind::Throw,
|
||||||
|
|
@ -605,7 +647,7 @@ ascii_byte_handler!(L_T(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_U(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_U(id_without_first_char) match id_without_first_char {
|
||||||
"ndefined" => Kind::Undefined,
|
"ndefined" => Kind::Undefined,
|
||||||
"sing" => Kind::Using,
|
"sing" => Kind::Using,
|
||||||
"nique" => Kind::Unique,
|
"nique" => Kind::Unique,
|
||||||
|
|
@ -613,19 +655,19 @@ ascii_byte_handler!(L_U(lexer) match &lexer.identifier_name_handler()[1..] {
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_V(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_V(id_without_first_char) match id_without_first_char {
|
||||||
"ar" => Kind::Var,
|
"ar" => Kind::Var,
|
||||||
"oid" => Kind::Void,
|
"oid" => Kind::Void,
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_W(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_W(id_without_first_char) match id_without_first_char {
|
||||||
"hile" => Kind::While,
|
"hile" => Kind::While,
|
||||||
"ith" => Kind::With,
|
"ith" => Kind::With,
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
||||||
ascii_byte_handler!(L_Y(lexer) match &lexer.identifier_name_handler()[1..] {
|
ascii_identifier_handler!(L_Y(id_without_first_char) match id_without_first_char {
|
||||||
"ield" => Kind::Yield,
|
"ield" => Kind::Yield,
|
||||||
_ => Kind::Ident,
|
_ => Kind::Ident,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue