rust-analyzer/crates/syntax/src/parsing.rs

40 lines
1.2 KiB
Rust
Raw Normal View History

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.
2019-02-23 13:07:29 +00:00
mod text_tree_sink;
mod reparsing;
use crate::{
parsing::text_tree_sink::build_tree, syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode,
};
pub(crate) use crate::parsing::reparsing::incremental_reparse;
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
let lexed = parser::LexedStr::new(text);
2021-12-25 18:59:02 +00:00
let parser_input = lexed.to_input();
let parser_output = parser::parse_source_file(&parser_input);
let (node, errors, _eof) = build_tree(lexed, parser_output, false);
(node, errors)
}
/// Returns `text` parsed as a `T` provided there are no parse errors.
pub(crate) fn parse_text_as<T: AstNode>(
text: &str,
entry_point: parser::ParserEntryPoint,
) -> Result<T, ()> {
let lexed = parser::LexedStr::new(text);
if lexed.errors().next().is_some() {
return Err(());
}
2021-12-25 18:59:02 +00:00
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);
if !errors.is_empty() || !eof {
return Err(());
}
SyntaxNode::new_root(node).first_child().and_then(T::cast).ok_or(())
}