Merge #11117
11117: internal: replace TreeSink with a data structure r=matklad a=matklad The general theme of this is to make parser a better independent library. The specific thing we do here is replacing callback based TreeSink with a data structure. That is, rather than calling user-provided tree construction methods, the parser now spits out a very bare-bones tree, effectively a log of a DFS traversal. This makes the parser usable without any *specifc* tree sink, and allows us to, eg, move tests into this crate. Now, it's also true that this is a distinction without a difference, as the old and the new interface are equivalent in expressiveness. Still, this new thing seems somewhat simpler. But yeah, I admit I don't have a suuper strong motivation here, just a hunch that this is better. cc #10765 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
c456b217d8
@ -10,7 +10,7 @@ mod parser;
|
|||||||
mod expander;
|
mod expander;
|
||||||
mod syntax_bridge;
|
mod syntax_bridge;
|
||||||
mod tt_iter;
|
mod tt_iter;
|
||||||
mod to_parser_tokens;
|
mod to_parser_input;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod benchmark;
|
mod benchmark;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
//! Conversions between [`SyntaxNode`] and [`tt::TokenTree`].
|
//! Conversions between [`SyntaxNode`] and [`tt::TokenTree`].
|
||||||
|
|
||||||
use parser::{ParseError, TreeSink};
|
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, make::tokens::doc_comment},
|
ast::{self, make::tokens::doc_comment},
|
||||||
@ -11,7 +10,7 @@ use syntax::{
|
|||||||
use tt::buffer::{Cursor, TokenBuffer};
|
use tt::buffer::{Cursor, TokenBuffer};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
to_parser_tokens::to_parser_tokens, tt_iter::TtIter, ExpandError, ParserEntryPoint, TokenMap,
|
to_parser_input::to_parser_input, tt_iter::TtIter, ExpandError, ParserEntryPoint, TokenMap,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Convert the syntax node to a `TokenTree` (what macro
|
/// Convert the syntax node to a `TokenTree` (what macro
|
||||||
@ -55,9 +54,19 @@ pub fn token_tree_to_syntax_node(
|
|||||||
}
|
}
|
||||||
_ => TokenBuffer::from_subtree(tt),
|
_ => TokenBuffer::from_subtree(tt),
|
||||||
};
|
};
|
||||||
let parser_tokens = to_parser_tokens(&buffer);
|
let parser_input = to_parser_input(&buffer);
|
||||||
|
let parser_output = parser::parse(&parser_input, entry_point);
|
||||||
let mut tree_sink = TtTreeSink::new(buffer.begin());
|
let mut tree_sink = TtTreeSink::new(buffer.begin());
|
||||||
parser::parse(&parser_tokens, &mut tree_sink, entry_point);
|
for event in parser_output.iter() {
|
||||||
|
match event {
|
||||||
|
parser::Step::Token { kind, n_input_tokens: n_raw_tokens } => {
|
||||||
|
tree_sink.token(kind, n_raw_tokens)
|
||||||
|
}
|
||||||
|
parser::Step::Enter { kind } => tree_sink.start_node(kind),
|
||||||
|
parser::Step::Exit => tree_sink.finish_node(),
|
||||||
|
parser::Step::Error { msg } => tree_sink.error(msg.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
if tree_sink.roots.len() != 1 {
|
if tree_sink.roots.len() != 1 {
|
||||||
return Err(ExpandError::ConversionError);
|
return Err(ExpandError::ConversionError);
|
||||||
}
|
}
|
||||||
@ -643,7 +652,7 @@ fn delim_to_str(d: tt::DelimiterKind, closing: bool) -> &'static str {
|
|||||||
&texts[idx..texts.len() - (1 - idx)]
|
&texts[idx..texts.len() - (1 - idx)]
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TreeSink for TtTreeSink<'a> {
|
impl<'a> TtTreeSink<'a> {
|
||||||
fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) {
|
fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) {
|
||||||
if kind == LIFETIME_IDENT {
|
if kind == LIFETIME_IDENT {
|
||||||
n_tokens = 2;
|
n_tokens = 2;
|
||||||
@ -741,7 +750,7 @@ impl<'a> TreeSink for TtTreeSink<'a> {
|
|||||||
*self.roots.last_mut().unwrap() -= 1;
|
*self.roots.last_mut().unwrap() -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn error(&mut self, error: ParseError) {
|
fn error(&mut self, error: String) {
|
||||||
self.inner.error(error, self.text_pos)
|
self.inner.error(error, self.text_pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
use syntax::{SyntaxKind, SyntaxKind::*, T};
|
use syntax::{SyntaxKind, SyntaxKind::*, T};
|
||||||
use tt::buffer::TokenBuffer;
|
use tt::buffer::TokenBuffer;
|
||||||
|
|
||||||
pub(crate) fn to_parser_tokens(buffer: &TokenBuffer) -> parser::Tokens {
|
pub(crate) fn to_parser_input(buffer: &TokenBuffer) -> parser::Input {
|
||||||
let mut res = parser::Tokens::default();
|
let mut res = parser::Input::default();
|
||||||
|
|
||||||
let mut current = buffer.begin();
|
let mut current = buffer.begin();
|
||||||
|
|
@ -1,11 +1,10 @@
|
|||||||
//! A "Parser" structure for token trees. We use this when parsing a declarative
|
//! A "Parser" structure for token trees. We use this when parsing a declarative
|
||||||
//! macro definition into a list of patterns and templates.
|
//! macro definition into a list of patterns and templates.
|
||||||
|
|
||||||
use crate::{to_parser_tokens::to_parser_tokens, ExpandError, ExpandResult, ParserEntryPoint};
|
use crate::{to_parser_input::to_parser_input, ExpandError, ExpandResult, ParserEntryPoint};
|
||||||
|
|
||||||
use parser::TreeSink;
|
|
||||||
use syntax::SyntaxKind;
|
use syntax::SyntaxKind;
|
||||||
use tt::buffer::{Cursor, TokenBuffer};
|
use tt::buffer::TokenBuffer;
|
||||||
|
|
||||||
macro_rules! err {
|
macro_rules! err {
|
||||||
() => {
|
() => {
|
||||||
@ -94,34 +93,28 @@ impl<'a> TtIter<'a> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
entry_point: ParserEntryPoint,
|
entry_point: ParserEntryPoint,
|
||||||
) -> ExpandResult<Option<tt::TokenTree>> {
|
) -> ExpandResult<Option<tt::TokenTree>> {
|
||||||
struct OffsetTokenSink<'a> {
|
|
||||||
cursor: Cursor<'a>,
|
|
||||||
error: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> TreeSink for OffsetTokenSink<'a> {
|
|
||||||
fn token(&mut self, kind: SyntaxKind, mut n_tokens: u8) {
|
|
||||||
if kind == SyntaxKind::LIFETIME_IDENT {
|
|
||||||
n_tokens = 2;
|
|
||||||
}
|
|
||||||
for _ in 0..n_tokens {
|
|
||||||
self.cursor = self.cursor.bump_subtree();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn start_node(&mut self, _kind: SyntaxKind) {}
|
|
||||||
fn finish_node(&mut self) {}
|
|
||||||
fn error(&mut self, _error: parser::ParseError) {
|
|
||||||
self.error = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let buffer = TokenBuffer::from_tokens(self.inner.as_slice());
|
let buffer = TokenBuffer::from_tokens(self.inner.as_slice());
|
||||||
let parser_tokens = to_parser_tokens(&buffer);
|
let parser_input = to_parser_input(&buffer);
|
||||||
let mut sink = OffsetTokenSink { cursor: buffer.begin(), error: false };
|
let tree_traversal = parser::parse(&parser_input, entry_point);
|
||||||
|
|
||||||
parser::parse(&parser_tokens, &mut sink, entry_point);
|
let mut cursor = buffer.begin();
|
||||||
|
let mut error = false;
|
||||||
|
for step in tree_traversal.iter() {
|
||||||
|
match step {
|
||||||
|
parser::Step::Token { kind, mut n_input_tokens } => {
|
||||||
|
if kind == SyntaxKind::LIFETIME_IDENT {
|
||||||
|
n_input_tokens = 2;
|
||||||
|
}
|
||||||
|
for _ in 0..n_input_tokens {
|
||||||
|
cursor = cursor.bump_subtree();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parser::Step::Enter { .. } | parser::Step::Exit => (),
|
||||||
|
parser::Step::Error { .. } => error = true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut err = if !sink.cursor.is_root() || sink.error {
|
let mut err = if !cursor.is_root() || error {
|
||||||
Some(err!("expected {:?}", entry_point))
|
Some(err!("expected {:?}", entry_point))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -130,8 +123,8 @@ impl<'a> TtIter<'a> {
|
|||||||
let mut curr = buffer.begin();
|
let mut curr = buffer.begin();
|
||||||
let mut res = vec![];
|
let mut res = vec![];
|
||||||
|
|
||||||
if sink.cursor.is_root() {
|
if cursor.is_root() {
|
||||||
while curr != sink.cursor {
|
while curr != cursor {
|
||||||
if let Some(token) = curr.token_tree() {
|
if let Some(token) = curr.token_tree() {
|
||||||
res.push(token);
|
res.push(token);
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,8 @@
|
|||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ParseError,
|
output::Output,
|
||||||
SyntaxKind::{self, *},
|
SyntaxKind::{self, *},
|
||||||
TreeSink,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// `Parser` produces a flat list of `Event`s.
|
/// `Parser` produces a flat list of `Event`s.
|
||||||
@ -77,7 +76,7 @@ pub(crate) enum Event {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Error {
|
Error {
|
||||||
msg: ParseError,
|
msg: String,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,7 +87,8 @@ impl Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Generate the syntax tree with the control of events.
|
/// Generate the syntax tree with the control of events.
|
||||||
pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) {
|
pub(super) fn process(mut events: Vec<Event>) -> Output {
|
||||||
|
let mut res = Output::default();
|
||||||
let mut forward_parents = Vec::new();
|
let mut forward_parents = Vec::new();
|
||||||
|
|
||||||
for i in 0..events.len() {
|
for i in 0..events.len() {
|
||||||
@ -117,15 +117,17 @@ pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) {
|
|||||||
|
|
||||||
for kind in forward_parents.drain(..).rev() {
|
for kind in forward_parents.drain(..).rev() {
|
||||||
if kind != TOMBSTONE {
|
if kind != TOMBSTONE {
|
||||||
sink.start_node(kind);
|
res.enter_node(kind);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Finish => sink.finish_node(),
|
Event::Finish => res.leave_node(),
|
||||||
Event::Token { kind, n_raw_tokens } => {
|
Event::Token { kind, n_raw_tokens } => {
|
||||||
sink.token(kind, n_raw_tokens);
|
res.token(kind, n_raw_tokens);
|
||||||
}
|
}
|
||||||
Event::Error { msg } => sink.error(msg),
|
Event::Error { msg } => res.error(msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
res
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,26 @@
|
|||||||
//! Input for the parser -- a sequence of tokens.
|
//! See [`Input`].
|
||||||
//!
|
|
||||||
//! As of now, parser doesn't have access to the *text* of the tokens, and makes
|
|
||||||
//! decisions based solely on their classification. Unlike `LexerToken`, the
|
|
||||||
//! `Tokens` doesn't include whitespace and comments.
|
|
||||||
|
|
||||||
use crate::SyntaxKind;
|
use crate::SyntaxKind;
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
type bits = u64;
|
type bits = u64;
|
||||||
|
|
||||||
/// Main input to the parser.
|
/// Input for the parser -- a sequence of tokens.
|
||||||
///
|
///
|
||||||
/// A sequence of tokens represented internally as a struct of arrays.
|
/// As of now, parser doesn't have access to the *text* of the tokens, and makes
|
||||||
|
/// decisions based solely on their classification. Unlike `LexerToken`, the
|
||||||
|
/// `Tokens` doesn't include whitespace and comments. Main input to the parser.
|
||||||
|
///
|
||||||
|
/// Struct of arrays internally, but this shouldn't really matter.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Tokens {
|
pub struct Input {
|
||||||
kind: Vec<SyntaxKind>,
|
kind: Vec<SyntaxKind>,
|
||||||
joint: Vec<bits>,
|
joint: Vec<bits>,
|
||||||
contextual_kind: Vec<SyntaxKind>,
|
contextual_kind: Vec<SyntaxKind>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `pub` impl used by callers to create `Tokens`.
|
/// `pub` impl used by callers to create `Tokens`.
|
||||||
impl Tokens {
|
impl Input {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn push(&mut self, kind: SyntaxKind) {
|
pub fn push(&mut self, kind: SyntaxKind) {
|
||||||
self.push_impl(kind, SyntaxKind::EOF)
|
self.push_impl(kind, SyntaxKind::EOF)
|
||||||
@ -63,7 +63,7 @@ impl Tokens {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// pub(crate) impl used by the parser to consume `Tokens`.
|
/// pub(crate) impl used by the parser to consume `Tokens`.
|
||||||
impl Tokens {
|
impl Input {
|
||||||
pub(crate) fn kind(&self, idx: usize) -> SyntaxKind {
|
pub(crate) fn kind(&self, idx: usize) -> SyntaxKind {
|
||||||
self.kind.get(idx).copied().unwrap_or(SyntaxKind::EOF)
|
self.kind.get(idx).copied().unwrap_or(SyntaxKind::EOF)
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ impl Tokens {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tokens {
|
impl Input {
|
||||||
fn bit_index(&self, n: usize) -> (usize, usize) {
|
fn bit_index(&self, n: usize) -> (usize, usize) {
|
||||||
let idx = n / (bits::BITS as usize);
|
let idx = n / (bits::BITS as usize);
|
||||||
let b_idx = n % (bits::BITS as usize);
|
let b_idx = n % (bits::BITS as usize);
|
@ -122,8 +122,8 @@ impl<'a> LexedStr<'a> {
|
|||||||
self.error.iter().map(|it| (it.token as usize, it.msg.as_str()))
|
self.error.iter().map(|it| (it.token as usize, it.msg.as_str()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_tokens(&self) -> crate::Tokens {
|
pub fn to_input(&self) -> crate::Input {
|
||||||
let mut res = crate::Tokens::default();
|
let mut res = crate::Input::default();
|
||||||
let mut was_joint = false;
|
let mut was_joint = false;
|
||||||
for i in 0..self.len() {
|
for i in 0..self.len() {
|
||||||
let kind = self.kind(i);
|
let kind = self.kind(i);
|
||||||
|
@ -24,32 +24,20 @@ mod syntax_kind;
|
|||||||
mod event;
|
mod event;
|
||||||
mod parser;
|
mod parser;
|
||||||
mod grammar;
|
mod grammar;
|
||||||
mod tokens;
|
mod input;
|
||||||
|
mod output;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
pub(crate) use token_set::TokenSet;
|
pub(crate) use token_set::TokenSet;
|
||||||
|
|
||||||
pub use crate::{lexed_str::LexedStr, syntax_kind::SyntaxKind, tokens::Tokens};
|
pub use crate::{
|
||||||
|
input::Input,
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
lexed_str::LexedStr,
|
||||||
pub struct ParseError(pub Box<String>);
|
output::{Output, Step},
|
||||||
|
syntax_kind::SyntaxKind,
|
||||||
/// `TreeSink` abstracts details of a particular syntax tree implementation.
|
};
|
||||||
pub trait TreeSink {
|
|
||||||
/// Adds new token to the current branch.
|
|
||||||
fn token(&mut self, kind: SyntaxKind, n_tokens: u8);
|
|
||||||
|
|
||||||
/// Start new branch and make it current.
|
|
||||||
fn start_node(&mut self, kind: SyntaxKind);
|
|
||||||
|
|
||||||
/// Finish current branch and restore previous
|
|
||||||
/// branch as current.
|
|
||||||
fn finish_node(&mut self);
|
|
||||||
|
|
||||||
fn error(&mut self, error: ParseError);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// rust-analyzer parser allows you to choose one of the possible entry points.
|
/// rust-analyzer parser allows you to choose one of the possible entry points.
|
||||||
///
|
///
|
||||||
@ -74,11 +62,19 @@ pub enum ParserEntryPoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Parse given tokens into the given sink as a rust file.
|
/// Parse given tokens into the given sink as a rust file.
|
||||||
pub fn parse_source_file(tokens: &Tokens, tree_sink: &mut dyn TreeSink) {
|
pub fn parse_source_file(inp: &Input) -> Output {
|
||||||
parse(tokens, tree_sink, ParserEntryPoint::SourceFile);
|
parse(inp, ParserEntryPoint::SourceFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(tokens: &Tokens, tree_sink: &mut dyn TreeSink, entry_point: ParserEntryPoint) {
|
/// Parses the given [`Input`] into [`Output`] assuming that the top-level
|
||||||
|
/// syntactic construct is the given [`ParserEntryPoint`].
|
||||||
|
///
|
||||||
|
/// Both input and output here are fairly abstract. The overall flow is that the
|
||||||
|
/// caller has some "real" tokens, converts them to [`Input`], parses them to
|
||||||
|
/// [`Output`], and then converts that into a "real" tree. The "real" tree is
|
||||||
|
/// made of "real" tokens, so this all hinges on rather tight coordination of
|
||||||
|
/// indices between the four stages.
|
||||||
|
pub fn parse(inp: &Input, entry_point: ParserEntryPoint) -> Output {
|
||||||
let entry_point: fn(&'_ mut parser::Parser) = match entry_point {
|
let entry_point: fn(&'_ mut parser::Parser) = match entry_point {
|
||||||
ParserEntryPoint::SourceFile => grammar::entry_points::source_file,
|
ParserEntryPoint::SourceFile => grammar::entry_points::source_file,
|
||||||
ParserEntryPoint::Path => grammar::entry_points::path,
|
ParserEntryPoint::Path => grammar::entry_points::path,
|
||||||
@ -96,10 +92,10 @@ pub fn parse(tokens: &Tokens, tree_sink: &mut dyn TreeSink, entry_point: ParserE
|
|||||||
ParserEntryPoint::Attr => grammar::entry_points::attr,
|
ParserEntryPoint::Attr => grammar::entry_points::attr,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut p = parser::Parser::new(tokens);
|
let mut p = parser::Parser::new(inp);
|
||||||
entry_point(&mut p);
|
entry_point(&mut p);
|
||||||
let events = p.finish();
|
let events = p.finish();
|
||||||
event::process(tree_sink, events);
|
event::process(events)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A parsing function for a specific braced-block.
|
/// A parsing function for a specific braced-block.
|
||||||
@ -119,11 +115,11 @@ impl Reparser {
|
|||||||
///
|
///
|
||||||
/// Tokens must start with `{`, end with `}` and form a valid brace
|
/// Tokens must start with `{`, end with `}` and form a valid brace
|
||||||
/// sequence.
|
/// sequence.
|
||||||
pub fn parse(self, tokens: &Tokens, tree_sink: &mut dyn TreeSink) {
|
pub fn parse(self, tokens: &Input) -> Output {
|
||||||
let Reparser(r) = self;
|
let Reparser(r) = self;
|
||||||
let mut p = parser::Parser::new(tokens);
|
let mut p = parser::Parser::new(tokens);
|
||||||
r(&mut p);
|
r(&mut p);
|
||||||
let events = p.finish();
|
let events = p.finish();
|
||||||
event::process(tree_sink, events);
|
event::process(events)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
76
crates/parser/src/output.rs
Normal file
76
crates/parser/src/output.rs
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
//! See [`Output`]
|
||||||
|
|
||||||
|
use crate::SyntaxKind;
|
||||||
|
|
||||||
|
/// Output of the parser -- a DFS traversal of a concrete syntax tree.
|
||||||
|
///
|
||||||
|
/// Use the [`Output::iter`] method to iterate over traversal steps and consume
|
||||||
|
/// a syntax tree.
|
||||||
|
///
|
||||||
|
/// In a sense, this is just a sequence of [`SyntaxKind`]-colored parenthesis
|
||||||
|
/// interspersed into the original [`crate::Input`]. The output is fundamentally
|
||||||
|
/// coordinated with the input and `n_input_tokens` refers to the number of
|
||||||
|
/// times [`crate::Input::push`] was called.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Output {
|
||||||
|
/// 32-bit encoding of events. If LSB is zero, then that's an index into the
|
||||||
|
/// error vector. Otherwise, it's one of the thee other variants, with data encoded as
|
||||||
|
///
|
||||||
|
/// |16 bit kind|8 bit n_input_tokens|4 bit tag|4 bit leftover|
|
||||||
|
///
|
||||||
|
event: Vec<u32>,
|
||||||
|
error: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Step<'a> {
|
||||||
|
Token { kind: SyntaxKind, n_input_tokens: u8 },
|
||||||
|
Enter { kind: SyntaxKind },
|
||||||
|
Exit,
|
||||||
|
Error { msg: &'a str },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Output {
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = Step<'_>> {
|
||||||
|
self.event.iter().map(|&event| {
|
||||||
|
if event & 0b1 == 0 {
|
||||||
|
return Step::Error { msg: self.error[(event as usize) >> 1].as_str() };
|
||||||
|
}
|
||||||
|
let tag = ((event & 0x0000_00F0) >> 4) as u8;
|
||||||
|
match tag {
|
||||||
|
0 => {
|
||||||
|
let kind: SyntaxKind = (((event & 0xFFFF_0000) >> 16) as u16).into();
|
||||||
|
let n_input_tokens = ((event & 0x0000_FF00) >> 8) as u8;
|
||||||
|
Step::Token { kind, n_input_tokens }
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
let kind: SyntaxKind = (((event & 0xFFFF_0000) >> 16) as u16).into();
|
||||||
|
Step::Enter { kind }
|
||||||
|
}
|
||||||
|
2 => Step::Exit,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
|
||||||
|
let e = ((kind as u16 as u32) << 16) | ((n_tokens as u32) << 8) | (0 << 4) | 1;
|
||||||
|
self.event.push(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn enter_node(&mut self, kind: SyntaxKind) {
|
||||||
|
let e = ((kind as u16 as u32) << 16) | (1 << 4) | 1;
|
||||||
|
self.event.push(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn leave_node(&mut self) {
|
||||||
|
let e = 2 << 4 | 1;
|
||||||
|
self.event.push(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn error(&mut self, error: String) {
|
||||||
|
let idx = self.error.len();
|
||||||
|
self.error.push(error);
|
||||||
|
let e = (idx as u32) << 1;
|
||||||
|
self.event.push(e);
|
||||||
|
}
|
||||||
|
}
|
@ -7,8 +7,7 @@ use limit::Limit;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
event::Event,
|
event::Event,
|
||||||
tokens::Tokens,
|
input::Input,
|
||||||
ParseError,
|
|
||||||
SyntaxKind::{self, EOF, ERROR, TOMBSTONE},
|
SyntaxKind::{self, EOF, ERROR, TOMBSTONE},
|
||||||
TokenSet, T,
|
TokenSet, T,
|
||||||
};
|
};
|
||||||
@ -23,7 +22,7 @@ use crate::{
|
|||||||
/// "start expression, consume number literal,
|
/// "start expression, consume number literal,
|
||||||
/// finish expression". See `Event` docs for more.
|
/// finish expression". See `Event` docs for more.
|
||||||
pub(crate) struct Parser<'t> {
|
pub(crate) struct Parser<'t> {
|
||||||
tokens: &'t Tokens,
|
inp: &'t Input,
|
||||||
pos: usize,
|
pos: usize,
|
||||||
events: Vec<Event>,
|
events: Vec<Event>,
|
||||||
steps: Cell<u32>,
|
steps: Cell<u32>,
|
||||||
@ -32,8 +31,8 @@ pub(crate) struct Parser<'t> {
|
|||||||
static PARSER_STEP_LIMIT: Limit = Limit::new(15_000_000);
|
static PARSER_STEP_LIMIT: Limit = Limit::new(15_000_000);
|
||||||
|
|
||||||
impl<'t> Parser<'t> {
|
impl<'t> Parser<'t> {
|
||||||
pub(super) fn new(tokens: &'t Tokens) -> Parser<'t> {
|
pub(super) fn new(inp: &'t Input) -> Parser<'t> {
|
||||||
Parser { tokens, pos: 0, events: Vec::new(), steps: Cell::new(0) }
|
Parser { inp, pos: 0, events: Vec::new(), steps: Cell::new(0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn finish(self) -> Vec<Event> {
|
pub(crate) fn finish(self) -> Vec<Event> {
|
||||||
@ -56,7 +55,7 @@ impl<'t> Parser<'t> {
|
|||||||
assert!(PARSER_STEP_LIMIT.check(steps as usize).is_ok(), "the parser seems stuck");
|
assert!(PARSER_STEP_LIMIT.check(steps as usize).is_ok(), "the parser seems stuck");
|
||||||
self.steps.set(steps + 1);
|
self.steps.set(steps + 1);
|
||||||
|
|
||||||
self.tokens.kind(self.pos + n)
|
self.inp.kind(self.pos + n)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the current token is `kind`.
|
/// Checks if the current token is `kind`.
|
||||||
@ -92,7 +91,7 @@ impl<'t> Parser<'t> {
|
|||||||
T![<<=] => self.at_composite3(n, T![<], T![<], T![=]),
|
T![<<=] => self.at_composite3(n, T![<], T![<], T![=]),
|
||||||
T![>>=] => self.at_composite3(n, T![>], T![>], T![=]),
|
T![>>=] => self.at_composite3(n, T![>], T![>], T![=]),
|
||||||
|
|
||||||
_ => self.tokens.kind(self.pos + n) == kind,
|
_ => self.inp.kind(self.pos + n) == kind,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,17 +130,17 @@ impl<'t> Parser<'t> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn at_composite2(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind) -> bool {
|
fn at_composite2(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind) -> bool {
|
||||||
self.tokens.kind(self.pos + n) == k1
|
self.inp.kind(self.pos + n) == k1
|
||||||
&& self.tokens.kind(self.pos + n + 1) == k2
|
&& self.inp.kind(self.pos + n + 1) == k2
|
||||||
&& self.tokens.is_joint(self.pos + n)
|
&& self.inp.is_joint(self.pos + n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn at_composite3(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind, k3: SyntaxKind) -> bool {
|
fn at_composite3(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind, k3: SyntaxKind) -> bool {
|
||||||
self.tokens.kind(self.pos + n) == k1
|
self.inp.kind(self.pos + n) == k1
|
||||||
&& self.tokens.kind(self.pos + n + 1) == k2
|
&& self.inp.kind(self.pos + n + 1) == k2
|
||||||
&& self.tokens.kind(self.pos + n + 2) == k3
|
&& self.inp.kind(self.pos + n + 2) == k3
|
||||||
&& self.tokens.is_joint(self.pos + n)
|
&& self.inp.is_joint(self.pos + n)
|
||||||
&& self.tokens.is_joint(self.pos + n + 1)
|
&& self.inp.is_joint(self.pos + n + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the current token is in `kinds`.
|
/// Checks if the current token is in `kinds`.
|
||||||
@ -151,7 +150,7 @@ impl<'t> Parser<'t> {
|
|||||||
|
|
||||||
/// Checks if the current token is contextual keyword with text `t`.
|
/// Checks if the current token is contextual keyword with text `t`.
|
||||||
pub(crate) fn at_contextual_kw(&self, kw: SyntaxKind) -> bool {
|
pub(crate) fn at_contextual_kw(&self, kw: SyntaxKind) -> bool {
|
||||||
self.tokens.contextual_kind(self.pos) == kw
|
self.inp.contextual_kind(self.pos) == kw
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Starts a new node in the syntax tree. All nodes and tokens
|
/// Starts a new node in the syntax tree. All nodes and tokens
|
||||||
@ -196,7 +195,7 @@ impl<'t> Parser<'t> {
|
|||||||
/// structured errors with spans and notes, like rustc
|
/// structured errors with spans and notes, like rustc
|
||||||
/// does.
|
/// does.
|
||||||
pub(crate) fn error<T: Into<String>>(&mut self, message: T) {
|
pub(crate) fn error<T: Into<String>>(&mut self, message: T) {
|
||||||
let msg = ParseError(Box::new(message.into()));
|
let msg = message.into();
|
||||||
self.push_event(Event::Error { msg });
|
self.push_event(Event::Error { msg });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,24 +4,18 @@
|
|||||||
mod text_tree_sink;
|
mod text_tree_sink;
|
||||||
mod reparsing;
|
mod reparsing;
|
||||||
|
|
||||||
use parser::SyntaxKind;
|
use crate::{
|
||||||
use text_tree_sink::TextTreeSink;
|
parsing::text_tree_sink::build_tree, syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode,
|
||||||
|
};
|
||||||
use crate::{syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode};
|
|
||||||
|
|
||||||
pub(crate) use crate::parsing::reparsing::incremental_reparse;
|
pub(crate) use crate::parsing::reparsing::incremental_reparse;
|
||||||
|
|
||||||
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
|
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
|
||||||
let lexed = parser::LexedStr::new(text);
|
let lexed = parser::LexedStr::new(text);
|
||||||
let parser_tokens = lexed.to_tokens();
|
let parser_input = lexed.to_input();
|
||||||
|
let parser_output = parser::parse_source_file(&parser_input);
|
||||||
let mut tree_sink = TextTreeSink::new(lexed);
|
let (node, errors, _eof) = build_tree(lexed, parser_output, false);
|
||||||
|
(node, errors)
|
||||||
parser::parse_source_file(&parser_tokens, &mut tree_sink);
|
|
||||||
|
|
||||||
let (tree, parser_errors) = tree_sink.finish();
|
|
||||||
|
|
||||||
(tree, parser_errors)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `text` parsed as a `T` provided there are no parse errors.
|
/// Returns `text` parsed as a `T` provided there are no parse errors.
|
||||||
@ -33,21 +27,13 @@ pub(crate) fn parse_text_as<T: AstNode>(
|
|||||||
if lexed.errors().next().is_some() {
|
if lexed.errors().next().is_some() {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
let parser_tokens = lexed.to_tokens();
|
let parser_input = lexed.to_input();
|
||||||
|
let parser_output = parser::parse(&parser_input, entry_point);
|
||||||
|
let (node, errors, eof) = build_tree(lexed, parser_output, true);
|
||||||
|
|
||||||
let mut tree_sink = TextTreeSink::new(lexed);
|
if !errors.is_empty() || !eof {
|
||||||
|
|
||||||
// TextTreeSink assumes that there's at least some root node to which it can attach errors and
|
|
||||||
// tokens. We arbitrarily give it a SourceFile.
|
|
||||||
use parser::TreeSink;
|
|
||||||
tree_sink.start_node(SyntaxKind::SOURCE_FILE);
|
|
||||||
parser::parse(&parser_tokens, &mut tree_sink, entry_point);
|
|
||||||
tree_sink.finish_node();
|
|
||||||
|
|
||||||
let (tree, parser_errors, eof) = tree_sink.finish_eof();
|
|
||||||
if !parser_errors.is_empty() || !eof {
|
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
|
|
||||||
SyntaxNode::new_root(tree).first_child().and_then(T::cast).ok_or(())
|
SyntaxNode::new_root(node).first_child().and_then(T::cast).ok_or(())
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ use parser::Reparser;
|
|||||||
use text_edit::Indel;
|
use text_edit::Indel;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
parsing::text_tree_sink::TextTreeSink,
|
parsing::text_tree_sink::build_tree,
|
||||||
syntax_node::{GreenNode, GreenToken, NodeOrToken, SyntaxElement, SyntaxNode},
|
syntax_node::{GreenNode, GreenToken, NodeOrToken, SyntaxElement, SyntaxNode},
|
||||||
SyntaxError,
|
SyntaxError,
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
@ -89,16 +89,14 @@ fn reparse_block(
|
|||||||
let text = get_text_after_edit(node.clone().into(), edit);
|
let text = get_text_after_edit(node.clone().into(), edit);
|
||||||
|
|
||||||
let lexed = parser::LexedStr::new(text.as_str());
|
let lexed = parser::LexedStr::new(text.as_str());
|
||||||
let parser_tokens = lexed.to_tokens();
|
let parser_input = lexed.to_input();
|
||||||
if !is_balanced(&lexed) {
|
if !is_balanced(&lexed) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut tree_sink = TextTreeSink::new(lexed);
|
let tree_traversal = reparser.parse(&parser_input);
|
||||||
|
|
||||||
reparser.parse(&parser_tokens, &mut tree_sink);
|
let (green, new_parser_errors, _eof) = build_tree(lexed, tree_traversal, false);
|
||||||
|
|
||||||
let (green, new_parser_errors) = tree_sink.finish();
|
|
||||||
|
|
||||||
Some((node.replace_with(green), new_parser_errors, node.text_range()))
|
Some((node.replace_with(green), new_parser_errors, node.text_range()))
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
use parser::{LexedStr, ParseError, TreeSink};
|
use parser::LexedStr;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast,
|
ast,
|
||||||
@ -12,10 +12,37 @@ use crate::{
|
|||||||
SyntaxTreeBuilder, TextRange,
|
SyntaxTreeBuilder, TextRange,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Bridges the parser with our specific syntax tree representation.
|
pub(crate) fn build_tree(
|
||||||
///
|
lexed: LexedStr<'_>,
|
||||||
/// `TextTreeSink` also handles attachment of trivia (whitespace) to nodes.
|
parser_output: parser::Output,
|
||||||
pub(crate) struct TextTreeSink<'a> {
|
synthetic_root: bool,
|
||||||
|
) -> (GreenNode, Vec<SyntaxError>, bool) {
|
||||||
|
let mut builder = Builder::new(lexed);
|
||||||
|
|
||||||
|
if synthetic_root {
|
||||||
|
builder.enter(SyntaxKind::SOURCE_FILE);
|
||||||
|
}
|
||||||
|
|
||||||
|
for event in parser_output.iter() {
|
||||||
|
match event {
|
||||||
|
parser::Step::Token { kind, n_input_tokens: n_raw_tokens } => {
|
||||||
|
builder.token(kind, n_raw_tokens)
|
||||||
|
}
|
||||||
|
parser::Step::Enter { kind } => builder.enter(kind),
|
||||||
|
parser::Step::Exit => builder.exit(),
|
||||||
|
parser::Step::Error { msg } => {
|
||||||
|
let text_pos = builder.lexed.text_start(builder.pos).try_into().unwrap();
|
||||||
|
builder.inner.error(msg.to_string(), text_pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if synthetic_root {
|
||||||
|
builder.exit()
|
||||||
|
}
|
||||||
|
builder.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Builder<'a> {
|
||||||
lexed: LexedStr<'a>,
|
lexed: LexedStr<'a>,
|
||||||
pos: usize,
|
pos: usize,
|
||||||
state: State,
|
state: State,
|
||||||
@ -28,61 +55,12 @@ enum State {
|
|||||||
PendingFinish,
|
PendingFinish,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TreeSink for TextTreeSink<'a> {
|
impl<'a> Builder<'a> {
|
||||||
fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
|
fn new(lexed: parser::LexedStr<'a>) -> Self {
|
||||||
match mem::replace(&mut self.state, State::Normal) {
|
|
||||||
State::PendingStart => unreachable!(),
|
|
||||||
State::PendingFinish => self.inner.finish_node(),
|
|
||||||
State::Normal => (),
|
|
||||||
}
|
|
||||||
self.eat_trivias();
|
|
||||||
self.do_token(kind, n_tokens as usize);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn start_node(&mut self, kind: SyntaxKind) {
|
|
||||||
match mem::replace(&mut self.state, State::Normal) {
|
|
||||||
State::PendingStart => {
|
|
||||||
self.inner.start_node(kind);
|
|
||||||
// No need to attach trivias to previous node: there is no
|
|
||||||
// previous node.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
State::PendingFinish => self.inner.finish_node(),
|
|
||||||
State::Normal => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
let n_trivias =
|
|
||||||
(self.pos..self.lexed.len()).take_while(|&it| self.lexed.kind(it).is_trivia()).count();
|
|
||||||
let leading_trivias = self.pos..self.pos + n_trivias;
|
|
||||||
let n_attached_trivias = n_attached_trivias(
|
|
||||||
kind,
|
|
||||||
leading_trivias.rev().map(|it| (self.lexed.kind(it), self.lexed.text(it))),
|
|
||||||
);
|
|
||||||
self.eat_n_trivias(n_trivias - n_attached_trivias);
|
|
||||||
self.inner.start_node(kind);
|
|
||||||
self.eat_n_trivias(n_attached_trivias);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn finish_node(&mut self) {
|
|
||||||
match mem::replace(&mut self.state, State::PendingFinish) {
|
|
||||||
State::PendingStart => unreachable!(),
|
|
||||||
State::PendingFinish => self.inner.finish_node(),
|
|
||||||
State::Normal => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn error(&mut self, error: ParseError) {
|
|
||||||
let text_pos = self.lexed.text_start(self.pos).try_into().unwrap();
|
|
||||||
self.inner.error(error, text_pos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> TextTreeSink<'a> {
|
|
||||||
pub(super) fn new(lexed: parser::LexedStr<'a>) -> Self {
|
|
||||||
Self { lexed, pos: 0, state: State::PendingStart, inner: SyntaxTreeBuilder::default() }
|
Self { lexed, pos: 0, state: State::PendingStart, inner: SyntaxTreeBuilder::default() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn finish_eof(mut self) -> (GreenNode, Vec<SyntaxError>, bool) {
|
fn build(mut self) -> (GreenNode, Vec<SyntaxError>, bool) {
|
||||||
match mem::replace(&mut self.state, State::Normal) {
|
match mem::replace(&mut self.state, State::Normal) {
|
||||||
State::PendingFinish => {
|
State::PendingFinish => {
|
||||||
self.eat_trivias();
|
self.eat_trivias();
|
||||||
@ -106,9 +84,46 @@ impl<'a> TextTreeSink<'a> {
|
|||||||
(node, errors, is_eof)
|
(node, errors, is_eof)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn finish(self) -> (GreenNode, Vec<SyntaxError>) {
|
fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
|
||||||
let (node, errors, _eof) = self.finish_eof();
|
match mem::replace(&mut self.state, State::Normal) {
|
||||||
(node, errors)
|
State::PendingStart => unreachable!(),
|
||||||
|
State::PendingFinish => self.inner.finish_node(),
|
||||||
|
State::Normal => (),
|
||||||
|
}
|
||||||
|
self.eat_trivias();
|
||||||
|
self.do_token(kind, n_tokens as usize);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enter(&mut self, kind: SyntaxKind) {
|
||||||
|
match mem::replace(&mut self.state, State::Normal) {
|
||||||
|
State::PendingStart => {
|
||||||
|
self.inner.start_node(kind);
|
||||||
|
// No need to attach trivias to previous node: there is no
|
||||||
|
// previous node.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
State::PendingFinish => self.inner.finish_node(),
|
||||||
|
State::Normal => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
let n_trivias =
|
||||||
|
(self.pos..self.lexed.len()).take_while(|&it| self.lexed.kind(it).is_trivia()).count();
|
||||||
|
let leading_trivias = self.pos..self.pos + n_trivias;
|
||||||
|
let n_attached_trivias = n_attached_trivias(
|
||||||
|
kind,
|
||||||
|
leading_trivias.rev().map(|it| (self.lexed.kind(it), self.lexed.text(it))),
|
||||||
|
);
|
||||||
|
self.eat_n_trivias(n_trivias - n_attached_trivias);
|
||||||
|
self.inner.start_node(kind);
|
||||||
|
self.eat_n_trivias(n_attached_trivias);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn exit(&mut self) {
|
||||||
|
match mem::replace(&mut self.state, State::PendingFinish) {
|
||||||
|
State::PendingStart => unreachable!(),
|
||||||
|
State::PendingFinish => self.inner.finish_node(),
|
||||||
|
State::Normal => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eat_trivias(&mut self) {
|
fn eat_trivias(&mut self) {
|
||||||
|
@ -69,7 +69,7 @@ impl SyntaxTreeBuilder {
|
|||||||
self.inner.finish_node();
|
self.inner.finish_node();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn error(&mut self, error: parser::ParseError, text_pos: TextSize) {
|
pub fn error(&mut self, error: String, text_pos: TextSize) {
|
||||||
self.errors.push(SyntaxError::new_at_offset(*error.0, text_pos));
|
self.errors.push(SyntaxError::new_at_offset(error, text_pos));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user