Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/parser/src/lib.rs')
| -rw-r--r-- | crates/parser/src/lib.rs | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index dc02ae6e83..da78889f35 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -24,32 +24,20 @@ mod syntax_kind; mod event; mod parser; mod grammar; -mod tokens; +mod input; +mod output; #[cfg(test)] mod tests; pub(crate) use token_set::TokenSet; -pub use crate::{lexed_str::LexedStr, syntax_kind::SyntaxKind, tokens::Tokens}; - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct ParseError(pub Box<String>); - -/// `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); -} +pub use crate::{ + input::Input, + lexed_str::LexedStr, + output::{Output, Step}, + syntax_kind::SyntaxKind, +}; /// 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. -pub fn parse_source_file(tokens: &Tokens, tree_sink: &mut dyn TreeSink) { - parse(tokens, tree_sink, ParserEntryPoint::SourceFile); +pub fn parse_source_file(inp: &Input) -> Output { + 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 { ParserEntryPoint::SourceFile => grammar::entry_points::source_file, 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, }; - let mut p = parser::Parser::new(tokens); + let mut p = parser::Parser::new(inp); entry_point(&mut p); let events = p.finish(); - event::process(tree_sink, events); + event::process(events) } /// 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 /// sequence. - pub fn parse(self, tokens: &Tokens, tree_sink: &mut dyn TreeSink) { + pub fn parse(self, tokens: &Input) -> Output { let Reparser(r) = self; let mut p = parser::Parser::new(tokens); r(&mut p); let events = p.finish(); - event::process(tree_sink, events); + event::process(events) } } |