2020-08-12 15:06:49 +00:00
|
|
|
//! Lexing, bridging to parser (which does the actual parsing) and
|
2019-02-21 12:24:42 +00:00
|
|
|
//! incremental reparsing.
|
|
|
|
|
2020-11-02 12:13:32 +00:00
|
|
|
pub(crate) mod lexer;
|
2019-02-23 13:07:29 +00:00
|
|
|
mod text_tree_sink;
|
2019-02-20 13:24:39 +00:00
|
|
|
mod reparsing;
|
2019-02-20 12:47:32 +00:00
|
|
|
|
2021-05-13 10:44:47 +00:00
|
|
|
use parser::SyntaxKind;
|
2020-01-26 18:44:49 +00:00
|
|
|
use text_tree_sink::TextTreeSink;
|
2019-02-20 12:47:32 +00:00
|
|
|
|
2021-05-13 10:44:47 +00:00
|
|
|
use crate::{syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode};
|
2019-02-20 12:47:32 +00:00
|
|
|
|
2021-05-13 10:44:47 +00:00
|
|
|
pub(crate) use crate::parsing::{lexer::*, reparsing::incremental_reparse};
|
2019-02-20 12:47:32 +00:00
|
|
|
|
|
|
|
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
|
2021-12-12 14:58:45 +00:00
|
|
|
let (lexer_tokens, lexer_errors) = tokenize(text);
|
|
|
|
let parser_tokens = to_parser_tokens(text, &lexer_tokens);
|
2020-01-28 05:09:13 +00:00
|
|
|
|
2021-12-12 14:58:45 +00:00
|
|
|
let mut tree_sink = TextTreeSink::new(text, &lexer_tokens);
|
2020-01-28 05:09:13 +00:00
|
|
|
|
2021-12-12 14:58:45 +00:00
|
|
|
parser::parse_source_file(&parser_tokens, &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.
|
2021-09-06 15:34:03 +00:00
|
|
|
pub(crate) fn parse_text_as<T: AstNode>(
|
2020-06-18 21:43:19 +00:00
|
|
|
text: &str,
|
2021-09-06 15:34:03 +00:00
|
|
|
entry_point: parser::ParserEntryPoint,
|
2020-06-18 21:43:19 +00:00
|
|
|
) -> Result<T, ()> {
|
2021-12-12 14:58:45 +00:00
|
|
|
let (lexer_tokens, lexer_errors) = tokenize(text);
|
2020-06-18 21:43:19 +00:00
|
|
|
if !lexer_errors.is_empty() {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
2021-12-12 14:58:45 +00:00
|
|
|
let parser_tokens = to_parser_tokens(text, &lexer_tokens);
|
|
|
|
|
|
|
|
let mut tree_sink = TextTreeSink::new(text, &lexer_tokens);
|
2020-06-18 21:43:19 +00: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 15:06:49 +00:00
|
|
|
use parser::TreeSink;
|
2020-06-18 21:43:19 +00:00
|
|
|
tree_sink.start_node(SyntaxKind::SOURCE_FILE);
|
2021-12-12 14:58:45 +00:00
|
|
|
parser::parse(&parser_tokens, &mut tree_sink, entry_point);
|
2020-06-18 21:43:19 +00:00
|
|
|
tree_sink.finish_node();
|
|
|
|
|
2021-12-12 14:58:45 +00:00
|
|
|
let (tree, parser_errors, eof) = tree_sink.finish_eof();
|
|
|
|
if !parser_errors.is_empty() || !eof {
|
2020-06-18 21:43:19 +00:00
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
SyntaxNode::new_root(tree).first_child().and_then(T::cast).ok_or(())
|
|
|
|
}
|
2021-12-12 14:58:45 +00:00
|
|
|
|
|
|
|
pub(crate) fn to_parser_tokens(text: &str, lexer_tokens: &[lexer::Token]) -> ::parser::Tokens {
|
|
|
|
let mut off = 0;
|
|
|
|
let mut res = parser::Tokens::default();
|
|
|
|
let mut was_joint = true;
|
|
|
|
for t in lexer_tokens {
|
|
|
|
if t.kind.is_trivia() {
|
|
|
|
was_joint = false;
|
|
|
|
} else if t.kind == SyntaxKind::IDENT {
|
|
|
|
let token_text = &text[off..][..usize::from(t.len)];
|
|
|
|
let contextual_kw =
|
|
|
|
SyntaxKind::from_contextual_keyword(token_text).unwrap_or(SyntaxKind::IDENT);
|
|
|
|
res.push_ident(contextual_kw);
|
|
|
|
} else {
|
2021-12-12 15:38:49 +00:00
|
|
|
res.was_joint(was_joint);
|
|
|
|
res.push(t.kind);
|
2021-12-12 14:58:45 +00:00
|
|
|
was_joint = true;
|
|
|
|
}
|
|
|
|
off += usize::from(t.len);
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|