2020-08-12 17:06:49 +02:00
|
|
|
//! Lexing, bridging to parser (which does the actual parsing) and
|
2019-02-21 15:24:42 +03:00
|
|
|
//! incremental reparsing.
|
|
|
|
|
2019-02-23 16:07:29 +03:00
|
|
|
mod text_tree_sink;
|
2019-02-20 16:24:39 +03:00
|
|
|
mod reparsing;
|
2019-02-20 15:47:32 +03:00
|
|
|
|
2021-05-13 13:44:47 +03:00
|
|
|
use parser::SyntaxKind;
|
2020-01-26 20:44:49 +02:00
|
|
|
use text_tree_sink::TextTreeSink;
|
2019-02-20 15:47:32 +03:00
|
|
|
|
2021-05-13 13:44:47 +03:00
|
|
|
use crate::{syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode};
|
2019-02-20 15:47:32 +03:00
|
|
|
|
2021-12-18 17:20:38 +03:00
|
|
|
pub(crate) use crate::parsing::reparsing::incremental_reparse;
|
2019-02-20 15:47:32 +03:00
|
|
|
|
|
|
|
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
|
2021-12-18 17:20:38 +03:00
|
|
|
let lexed = parser::LexedStr::new(text);
|
|
|
|
let parser_tokens = lexed.to_tokens();
|
2020-01-28 07:09:13 +02:00
|
|
|
|
2021-12-18 17:20:38 +03:00
|
|
|
let mut tree_sink = TextTreeSink::new(lexed);
|
2020-01-28 07:09:13 +02:00
|
|
|
|
2021-12-12 17:58:45 +03:00
|
|
|
parser::parse_source_file(&parser_tokens, &mut tree_sink);
|
2020-01-28 07:09:13 +02:00
|
|
|
|
2021-12-18 17:20:38 +03:00
|
|
|
let (tree, parser_errors) = tree_sink.finish();
|
2020-01-28 07:09:13 +02:00
|
|
|
|
|
|
|
(tree, parser_errors)
|
2019-02-20 15:47:32 +03:00
|
|
|
}
|
2020-06-19 07:43:19 +10:00
|
|
|
|
|
|
|
/// Returns `text` parsed as a `T` provided there are no parse errors.
|
2021-09-06 18:34:03 +03:00
|
|
|
pub(crate) fn parse_text_as<T: AstNode>(
|
2020-06-19 07:43:19 +10:00
|
|
|
text: &str,
|
2021-09-06 18:34:03 +03:00
|
|
|
entry_point: parser::ParserEntryPoint,
|
2020-06-19 07:43:19 +10:00
|
|
|
) -> Result<T, ()> {
|
2021-12-18 17:20:38 +03:00
|
|
|
let lexed = parser::LexedStr::new(text);
|
|
|
|
if lexed.errors().next().is_some() {
|
2020-06-19 07:43:19 +10:00
|
|
|
return Err(());
|
|
|
|
}
|
2021-12-18 17:20:38 +03:00
|
|
|
let parser_tokens = lexed.to_tokens();
|
2020-06-19 07:43:19 +10:00
|
|
|
|
2021-12-18 17:20:38 +03:00
|
|
|
let mut tree_sink = TextTreeSink::new(lexed);
|
2020-06-19 07:43:19 +10:00
|
|
|
|
|
|
|
// TextTreeSink assumes that there's at least some root node to which it can attach errors and
|
|
|
|
// tokens. We arbitrarily give it a SourceFile.
|
2020-08-12 17:06:49 +02:00
|
|
|
use parser::TreeSink;
|
2020-06-19 07:43:19 +10:00
|
|
|
tree_sink.start_node(SyntaxKind::SOURCE_FILE);
|
2021-12-12 17:58:45 +03:00
|
|
|
parser::parse(&parser_tokens, &mut tree_sink, entry_point);
|
2020-06-19 07:43:19 +10:00
|
|
|
tree_sink.finish_node();
|
|
|
|
|
2021-12-12 17:58:45 +03:00
|
|
|
let (tree, parser_errors, eof) = tree_sink.finish_eof();
|
|
|
|
if !parser_errors.is_empty() || !eof {
|
2020-06-19 07:43:19 +10:00
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
SyntaxNode::new_root(tree).first_child().and_then(T::cast).ok_or(())
|
|
|
|
}
|