2019-02-21 12:24:42 +00:00
|
|
|
//! Lexing, bridging to ra_parser (which does the actual parsing) and
|
|
|
|
//! incremental reparsing.
|
|
|
|
|
2019-02-20 12:47:32 +00:00
|
|
|
mod lexer;
|
2019-02-23 13:07:29 +00:00
|
|
|
mod text_token_source;
|
|
|
|
mod text_tree_sink;
|
2019-02-20 13:24:39 +00:00
|
|
|
mod reparsing;
|
2019-02-20 12:47:32 +00:00
|
|
|
|
2020-06-18 21:43:19 +00:00
|
|
|
use crate::{syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode};
|
2020-01-26 18:44:49 +00:00
|
|
|
use text_token_source::TextTokenSource;
|
|
|
|
use text_tree_sink::TextTreeSink;
|
2019-02-20 12:47:32 +00:00
|
|
|
|
2020-01-26 18:44:49 +00:00
|
|
|
pub use lexer::*;
|
2019-02-20 12:47:32 +00:00
|
|
|
|
|
|
|
pub(crate) use self::reparsing::incremental_reparse;
|
2020-06-18 21:43:19 +00:00
|
|
|
use ra_parser::SyntaxKind;
|
2019-02-20 12:47:32 +00:00
|
|
|
|
|
|
|
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
|
2020-01-28 05:09:13 +00:00
|
|
|
let (tokens, lexer_errors) = tokenize(&text);
|
|
|
|
|
2020-01-26 18:44:49 +00:00
|
|
|
let mut token_source = TextTokenSource::new(text, &tokens);
|
2020-01-28 05:09:13 +00:00
|
|
|
let mut tree_sink = TextTreeSink::new(text, &tokens);
|
|
|
|
|
2019-05-25 12:31:53 +00:00
|
|
|
ra_parser::parse(&mut token_source, &mut tree_sink);
|
2020-01-28 05:09:13 +00:00
|
|
|
|
|
|
|
let (tree, mut parser_errors) = tree_sink.finish();
|
|
|
|
parser_errors.extend(lexer_errors);
|
|
|
|
|
|
|
|
(tree, parser_errors)
|
2019-02-20 12:47:32 +00:00
|
|
|
}
|
2020-06-18 21:43:19 +00:00
|
|
|
|
|
|
|
/// Returns `text` parsed as a `T` provided there are no parse errors.
|
|
|
|
pub(crate) fn parse_text_fragment<T: AstNode>(
|
|
|
|
text: &str,
|
|
|
|
fragment_kind: ra_parser::FragmentKind,
|
|
|
|
) -> Result<T, ()> {
|
|
|
|
let (tokens, lexer_errors) = tokenize(&text);
|
|
|
|
if !lexer_errors.is_empty() {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut token_source = TextTokenSource::new(text, &tokens);
|
|
|
|
let mut tree_sink = TextTreeSink::new(text, &tokens);
|
|
|
|
|
|
|
|
// 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 ra_parser::TreeSink;
|
|
|
|
tree_sink.start_node(SyntaxKind::SOURCE_FILE);
|
|
|
|
ra_parser::parse_fragment(&mut token_source, &mut tree_sink, fragment_kind);
|
|
|
|
tree_sink.finish_node();
|
|
|
|
|
|
|
|
let (tree, parser_errors) = tree_sink.finish();
|
|
|
|
use ra_parser::TokenSource;
|
|
|
|
if !parser_errors.is_empty() || token_source.current().kind != SyntaxKind::EOF {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
SyntaxNode::new_root(tree).first_child().and_then(T::cast).ok_or(())
|
|
|
|
}
|