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;
|
2019-02-20 13:24:39 +00:00
|
|
|
mod reparsing;
|
2019-02-20 12:47:32 +00:00
|
|
|
|
internal: replace TreeSink with a data structure
The general theme of this is to make parser a better independent
library.
The specific thing we do here is replacing callback based TreeSink with
a data structure. That is, rather than calling user-provided tree
construction methods, the parser now spits out a very bare-bones tree,
effectively a log of a DFS traversal.
This makes the parser usable without any *specifc* tree sink, and allows
us to, eg, move tests into this crate.
Now, it's also true that this is a distinction without a difference, as
the old and the new interface are equivalent in expressiveness. Still,
this new thing seems somewhat simpler. But yeah, I admit I don't have a
suuper strong motivation here, just a hunch that this is better.
2021-12-19 14:36:23 +00:00
|
|
|
use crate::{
|
|
|
|
parsing::text_tree_sink::build_tree, syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode,
|
|
|
|
};
|
2019-02-20 12:47:32 +00:00
|
|
|
|
2021-12-18 14:20:38 +00:00
|
|
|
pub(crate) use crate::parsing::reparsing::incremental_reparse;
|
2019-02-20 12:47:32 +00:00
|
|
|
|
|
|
|
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
|
2021-12-18 14:20:38 +00:00
|
|
|
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);
|
internal: replace TreeSink with a data structure
The general theme of this is to make parser a better independent
library.
The specific thing we do here is replacing callback based TreeSink with
a data structure. That is, rather than calling user-provided tree
construction methods, the parser now spits out a very bare-bones tree,
effectively a log of a DFS traversal.
This makes the parser usable without any *specifc* tree sink, and allows
us to, eg, move tests into this crate.
Now, it's also true that this is a distinction without a difference, as
the old and the new interface are equivalent in expressiveness. Still,
this new thing seems somewhat simpler. But yeah, I admit I don't have a
suuper strong motivation here, just a hunch that this is better.
2021-12-19 14:36:23 +00:00
|
|
|
(node, 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-18 14:20:38 +00:00
|
|
|
let lexed = parser::LexedStr::new(text);
|
|
|
|
if lexed.errors().next().is_some() {
|
2020-06-18 21:43:19 +00:00
|
|
|
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);
|
2020-06-18 21:43:19 +00:00
|
|
|
|
internal: replace TreeSink with a data structure
The general theme of this is to make parser a better independent
library.
The specific thing we do here is replacing callback based TreeSink with
a data structure. That is, rather than calling user-provided tree
construction methods, the parser now spits out a very bare-bones tree,
effectively a log of a DFS traversal.
This makes the parser usable without any *specifc* tree sink, and allows
us to, eg, move tests into this crate.
Now, it's also true that this is a distinction without a difference, as
the old and the new interface are equivalent in expressiveness. Still,
this new thing seems somewhat simpler. But yeah, I admit I don't have a
suuper strong motivation here, just a hunch that this is better.
2021-12-19 14:36:23 +00:00
|
|
|
if !errors.is_empty() || !eof {
|
2020-06-18 21:43:19 +00:00
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
internal: replace TreeSink with a data structure
The general theme of this is to make parser a better independent
library.
The specific thing we do here is replacing callback based TreeSink with
a data structure. That is, rather than calling user-provided tree
construction methods, the parser now spits out a very bare-bones tree,
effectively a log of a DFS traversal.
This makes the parser usable without any *specifc* tree sink, and allows
us to, eg, move tests into this crate.
Now, it's also true that this is a distinction without a difference, as
the old and the new interface are equivalent in expressiveness. Still,
this new thing seems somewhat simpler. But yeah, I admit I don't have a
suuper strong motivation here, just a hunch that this is better.
2021-12-19 14:36:23 +00:00
|
|
|
SyntaxNode::new_root(node).first_child().and_then(T::cast).ok_or(())
|
2020-06-18 21:43:19 +00:00
|
|
|
}
|