mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
refactor(parser): remove start params for byte_search macro arms (#2553)
Simplify `byte_search` macro a bit more.
This commit is contained in:
parent
6d43e851e8
commit
18cff6aab8
4 changed files with 26 additions and 49 deletions
|
|
@ -69,11 +69,11 @@ impl<'a> Lexer<'a> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handle_match: |_next_byte, _start| {
|
handle_match: |_next_byte| {
|
||||||
self.token.is_on_new_line = true;
|
self.token.is_on_new_line = true;
|
||||||
Kind::Skip
|
Kind::Skip
|
||||||
},
|
},
|
||||||
handle_eof: |_start| {
|
handle_eof: || {
|
||||||
self.trivia_builder.add_single_line_comment(self.token.start, self.offset());
|
self.trivia_builder.add_single_line_comment(self.token.start, self.offset());
|
||||||
Kind::Skip
|
Kind::Skip
|
||||||
},
|
},
|
||||||
|
|
@ -139,11 +139,11 @@ impl<'a> Lexer<'a> {
|
||||||
return self.skip_multi_line_comment_after_line_break(after_line_break);
|
return self.skip_multi_line_comment_after_line_break(after_line_break);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handle_match: |_next_byte, _start| {
|
handle_match: |_next_byte| {
|
||||||
self.trivia_builder.add_multi_line_comment(self.token.start, self.offset());
|
self.trivia_builder.add_multi_line_comment(self.token.start, self.offset());
|
||||||
Kind::Skip
|
Kind::Skip
|
||||||
},
|
},
|
||||||
handle_eof: |_start| {
|
handle_eof: || {
|
||||||
self.error(diagnostics::UnterminatedMultiLineComment(self.unterminated_range()));
|
self.error(diagnostics::UnterminatedMultiLineComment(self.unterminated_range()));
|
||||||
Kind::Eof
|
Kind::Eof
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -265,18 +265,16 @@ pub(crate) use safe_byte_match_table;
|
||||||
/// byte_search! {
|
/// byte_search! {
|
||||||
/// lexer: self,
|
/// lexer: self,
|
||||||
/// table: NOT_STUFF_TABLE,
|
/// table: NOT_STUFF_TABLE,
|
||||||
/// handle_match: |matched_byte, start| {
|
/// handle_match: |matched_byte| {
|
||||||
/// // Matching byte has been found.
|
/// // Matching byte has been found.
|
||||||
/// // `matched_byte` is `u8` value of first byte which matched the table.
|
/// // `matched_byte` is `u8` value of first byte which matched the table.
|
||||||
/// // `start` is `SourcePosition` where search began.
|
|
||||||
/// // `lexer.source` is now positioned on first matching byte.
|
/// // `lexer.source` is now positioned on first matching byte.
|
||||||
/// // Handle the next matching byte (deal with any special cases).
|
/// // Handle the next matching byte (deal with any special cases).
|
||||||
/// // Value this block evaluates to will be returned from enclosing function.
|
/// // Value this block evaluates to will be returned from enclosing function.
|
||||||
/// matched_byte == b'X'
|
/// matched_byte == b'X'
|
||||||
/// },
|
/// },
|
||||||
/// handle_eof: |start| {
|
/// handle_eof: || {
|
||||||
/// // No bytes from start position to end of source matched the table.
|
/// // No bytes from start position to end of source matched the table.
|
||||||
/// // `start` is `SourcePosition` where search began.
|
|
||||||
/// // `lexer.source` is now positioned at EOF.
|
/// // `lexer.source` is now positioned at EOF.
|
||||||
/// // Handle EOF in some way.
|
/// // Handle EOF in some way.
|
||||||
/// // Value this block evaluates to will be returned from enclosing function.
|
/// // Value this block evaluates to will be returned from enclosing function.
|
||||||
|
|
@ -387,17 +385,17 @@ macro_rules! byte_search {
|
||||||
(
|
(
|
||||||
lexer: $lexer:ident,
|
lexer: $lexer:ident,
|
||||||
table: $table:ident,
|
table: $table:ident,
|
||||||
handle_match: |$match_byte:ident, $match_start:ident| $match_handler:expr,
|
handle_match: |$match_byte:ident| $match_handler:expr,
|
||||||
handle_eof: |$eof_start:ident| $eof_handler:expr,
|
handle_eof: || $eof_handler:expr,
|
||||||
) => {{
|
) => {{
|
||||||
let start = $lexer.source.position();
|
let start = $lexer.source.position();
|
||||||
byte_search! {
|
byte_search! {
|
||||||
lexer: $lexer,
|
lexer: $lexer,
|
||||||
table: $table,
|
table: $table,
|
||||||
start: start,
|
start: start,
|
||||||
continue_if: |__byte, pos| false,
|
continue_if: |byte, pos| false,
|
||||||
handle_match: |$match_byte, $match_start| $match_handler,
|
handle_match: |$match_byte| $match_handler,
|
||||||
handle_eof: |$eof_start| $eof_handler,
|
handle_eof: || $eof_handler,
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
|
@ -407,8 +405,8 @@ macro_rules! byte_search {
|
||||||
lexer: $lexer:ident,
|
lexer: $lexer:ident,
|
||||||
table: $table:ident,
|
table: $table:ident,
|
||||||
continue_if: |$continue_byte:ident, $pos:ident| $should_continue:expr,
|
continue_if: |$continue_byte:ident, $pos:ident| $should_continue:expr,
|
||||||
handle_match: |$match_byte:ident, $match_start:ident| $match_handler:expr,
|
handle_match: |$match_byte:ident| $match_handler:expr,
|
||||||
handle_eof: |$eof_start:ident| $eof_handler:expr,
|
handle_eof: || $eof_handler:expr,
|
||||||
) => {{
|
) => {{
|
||||||
let start = $lexer.source.position();
|
let start = $lexer.source.position();
|
||||||
byte_search! {
|
byte_search! {
|
||||||
|
|
@ -416,8 +414,8 @@ macro_rules! byte_search {
|
||||||
table: $table,
|
table: $table,
|
||||||
start: start,
|
start: start,
|
||||||
continue_if: |$continue_byte, $pos| $should_continue,
|
continue_if: |$continue_byte, $pos| $should_continue,
|
||||||
handle_match: |$match_byte, $match_start| $match_handler,
|
handle_match: |$match_byte| $match_handler,
|
||||||
handle_eof: |$eof_start| $eof_handler,
|
handle_eof: || $eof_handler,
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
|
@ -433,39 +431,20 @@ macro_rules! byte_search {
|
||||||
lexer: $lexer,
|
lexer: $lexer,
|
||||||
table: $table,
|
table: $table,
|
||||||
start: $start,
|
start: $start,
|
||||||
continue_if: |__byte, pos| false,
|
continue_if: |byte, pos| false,
|
||||||
handle_match: |$match_byte, __start| $match_handler,
|
handle_match: |$match_byte| $match_handler,
|
||||||
handle_eof: |__start| $eof_handler,
|
handle_eof: || $eof_handler,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Provide your own `start` position, and `continue_if`
|
|
||||||
(
|
|
||||||
lexer: $lexer:ident,
|
|
||||||
table: $table:ident,
|
|
||||||
start: $start:ident,
|
|
||||||
continue_if: |$continue_byte:ident, $pos:ident| $should_continue:expr,
|
|
||||||
handle_match: |$match_byte:ident| $match_handler:expr,
|
|
||||||
handle_eof: || $eof_handler:expr,
|
|
||||||
) => {{
|
|
||||||
byte_search! {
|
|
||||||
lexer: $lexer,
|
|
||||||
table: $table,
|
|
||||||
start: $start,
|
|
||||||
continue_if: |$continue_byte, $pos| $should_continue,
|
|
||||||
handle_match: |$match_byte, __start| $match_handler,
|
|
||||||
handle_eof: |__start| $eof_handler,
|
|
||||||
}
|
|
||||||
}};
|
|
||||||
|
|
||||||
// Actual implementation
|
// Actual implementation
|
||||||
(
|
(
|
||||||
lexer: $lexer:ident,
|
lexer: $lexer:ident,
|
||||||
table: $table:ident,
|
table: $table:ident,
|
||||||
start: $start:ident,
|
start: $start:ident,
|
||||||
continue_if: |$continue_byte:ident, $pos:ident| $should_continue:expr,
|
continue_if: |$continue_byte:ident, $pos:ident| $should_continue:expr,
|
||||||
handle_match: |$match_byte:ident, $match_start:ident| $match_handler:expr,
|
handle_match: |$match_byte:ident| $match_handler:expr,
|
||||||
handle_eof: |$eof_start:ident| $eof_handler:expr,
|
handle_eof: || $eof_handler:expr,
|
||||||
) => {{
|
) => {{
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// If `$table` is a `SafeByteMatchTable`, it's guaranteed that `lexer.source`
|
// If `$table` is a `SafeByteMatchTable`, it's guaranteed that `lexer.source`
|
||||||
|
|
@ -531,7 +510,6 @@ macro_rules! byte_search {
|
||||||
// Advance `lexer.source`'s position to end of file.
|
// Advance `lexer.source`'s position to end of file.
|
||||||
$lexer.source.set_position($pos);
|
$lexer.source.set_position($pos);
|
||||||
|
|
||||||
let $eof_start = $start;
|
|
||||||
return $eof_handler;
|
return $eof_handler;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -553,7 +531,6 @@ macro_rules! byte_search {
|
||||||
// SAFETY: See above about UTF-8 character boundaries invariant.
|
// SAFETY: See above about UTF-8 character boundaries invariant.
|
||||||
$lexer.source.set_position($pos);
|
$lexer.source.set_position($pos);
|
||||||
|
|
||||||
let $match_start = $start;
|
|
||||||
return $match_handler;
|
return $match_handler;
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,10 +73,10 @@ impl<'a> Lexer<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handle_match: |_next_byte, _start| {
|
handle_match: |_next_byte| {
|
||||||
ret
|
ret
|
||||||
},
|
},
|
||||||
handle_eof: |_start| {
|
handle_eof: || {
|
||||||
self.error(diagnostics::UnterminatedString(self.unterminated_range()));
|
self.error(diagnostics::UnterminatedString(self.unterminated_range()));
|
||||||
Kind::Undetermined
|
Kind::Undetermined
|
||||||
},
|
},
|
||||||
|
|
@ -295,14 +295,14 @@ impl<'a> Lexer<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
handle_match: |_next_byte, _start| {
|
handle_match: |_next_byte| {
|
||||||
self.save_template_string(
|
self.save_template_string(
|
||||||
is_valid_escape_sequence,
|
is_valid_escape_sequence,
|
||||||
str.into_bump_str(),
|
str.into_bump_str(),
|
||||||
);
|
);
|
||||||
ret
|
ret
|
||||||
},
|
},
|
||||||
handle_eof: |_start| {
|
handle_eof: || {
|
||||||
self.error(diagnostics::UnterminatedString(self.unterminated_range()));
|
self.error(diagnostics::UnterminatedString(self.unterminated_range()));
|
||||||
Kind::Undetermined
|
Kind::Undetermined
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,10 @@ impl<'a> Lexer<'a> {
|
||||||
byte_search! {
|
byte_search! {
|
||||||
lexer: self,
|
lexer: self,
|
||||||
table: NOT_REGULAR_WHITESPACE_OR_LINE_BREAK_TABLE,
|
table: NOT_REGULAR_WHITESPACE_OR_LINE_BREAK_TABLE,
|
||||||
handle_match: |_next_byte, _start| {
|
handle_match: |_next_byte| {
|
||||||
Kind::Skip
|
Kind::Skip
|
||||||
},
|
},
|
||||||
handle_eof: |_start| {
|
handle_eof: || {
|
||||||
Kind::Skip
|
Kind::Skip
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue