This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 664
refactor(rome_js_parser): Streamline parser events #2327
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
//! Events emitted by the Parser which are then constructed into a syntax tree | ||
|
||
use std::mem; | ||
use std::num::NonZeroU32; | ||
|
||
use crate::lexer::TextSize; | ||
use crate::{ParseDiagnostic, Parser, TreeSink}; | ||
use rome_js_syntax::JsSyntaxKind::{self, *}; | ||
use rome_rowan::TextRange; | ||
|
||
use crate::parser::rewrite_parser::{RewriteParser, RewriteToken}; | ||
use crate::parser::Checkpoint; | ||
use crate::{ParseDiagnostic, Parser, TreeSink}; | ||
use rome_js_syntax::JsSyntaxKind::{self, *}; | ||
|
||
/// Events emitted by the Parser, these events are later | ||
/// made into a syntax tree with `process` into TreeSink. | ||
|
@@ -23,29 +22,25 @@ pub enum Event { | |
/// become the children of the respective node. | ||
Start { | ||
kind: JsSyntaxKind, | ||
start: TextSize, | ||
forward_parent: Option<u32>, | ||
forward_parent: Option<NonZeroU32>, | ||
}, | ||
|
||
/// Complete the previous `Start` event | ||
Finish { end: TextSize }, | ||
Finish, | ||
|
||
/// Produce a single leaf-element. | ||
/// `n_raw_tokens` is used to glue complex contextual tokens. | ||
/// For example, lexer tokenizes `>>` as `>`, `>`, and | ||
/// `n_raw_tokens = 2` is used to produced a single `>>`. | ||
Token { | ||
kind: JsSyntaxKind, | ||
range: TextRange, | ||
/// The end offset of this token. | ||
end: TextSize, | ||
}, | ||
} | ||
|
||
impl Event { | ||
pub fn tombstone(start: TextSize) -> Self { | ||
pub fn tombstone() -> Self { | ||
Event::Start { | ||
kind: TOMBSTONE, | ||
forward_parent: None, | ||
start, | ||
} | ||
} | ||
} | ||
|
@@ -57,7 +52,7 @@ pub fn process(sink: &mut impl TreeSink, mut events: Vec<Event>, errors: Vec<Par | |
let mut forward_parents = Vec::new(); | ||
|
||
for i in 0..events.len() { | ||
match mem::replace(&mut events[i], Event::tombstone(TextSize::default())) { | ||
match &mut events[i] { | ||
Event::Start { | ||
kind: TOMBSTONE, .. | ||
} => (), | ||
|
@@ -72,14 +67,13 @@ pub fn process(sink: &mut impl TreeSink, mut events: Vec<Event>, errors: Vec<Par | |
// while with the magic forward_parent, it writes: `C <- B <- A`. | ||
|
||
// append `A` into parents. | ||
forward_parents.push(kind); | ||
forward_parents.push(*kind); | ||
let mut idx = i; | ||
let mut fp = forward_parent; | ||
let mut fp = *forward_parent; | ||
while let Some(fwd) = fp { | ||
idx += fwd as usize; | ||
idx += u32::from(fwd) as usize; | ||
// append `A`'s forward_parent `B` | ||
fp = match mem::replace(&mut events[idx], Event::tombstone(TextSize::default())) | ||
{ | ||
fp = match mem::replace(&mut events[idx], Event::tombstone()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replacing is necessary here because we don't want to visit the start node of a forwarded parent again. |
||
Event::Start { | ||
kind, | ||
forward_parent, | ||
|
@@ -100,8 +94,8 @@ pub fn process(sink: &mut impl TreeSink, mut events: Vec<Event>, errors: Vec<Par | |
} | ||
} | ||
Event::Finish { .. } => sink.finish_node(), | ||
Event::Token { kind, range } => { | ||
sink.token(kind, range.len()); | ||
Event::Token { kind, end } => { | ||
sink.token(*kind, *end); | ||
} | ||
} | ||
} | ||
|
@@ -113,9 +107,9 @@ struct RewriteParseEventsTreeSink<'r, 'p, T> { | |
} | ||
|
||
impl<'r, 'p, T: RewriteParseEvents> TreeSink for RewriteParseEventsTreeSink<'r, 'p, T> { | ||
fn token(&mut self, kind: JsSyntaxKind, length: TextSize) { | ||
fn token(&mut self, kind: JsSyntaxKind, end: TextSize) { | ||
self.reparse | ||
.token(RewriteToken::new(kind, length), &mut self.parser); | ||
.token(RewriteToken::new(kind, end), &mut self.parser); | ||
} | ||
|
||
fn start_node(&mut self, kind: JsSyntaxKind) { | ||
|
@@ -156,7 +150,6 @@ pub(crate) fn rewrite_events<T: RewriteParseEvents>( | |
// The current parsed grammar is a super-set of the grammar that gets re-parsed. Thus, any | ||
// error that applied to the old grammar also applies to the sub-grammar. | ||
let events: Vec<_> = p.events.split_off(checkpoint.event_pos + 1); | ||
p.last_token_event_pos = checkpoint.last_token_pos; | ||
|
||
let mut sink = RewriteParseEventsTreeSink { | ||
parser: RewriteParser::new(p, checkpoint.token_source), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to replace the events with tombstone because the iterator is only moving forward from here