aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parsing.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-12 17:31:42 +0100
committerGitHub <[email protected]>2020-08-12 17:31:42 +0100
commitd583f2c46d22cf8d643ebf98be9cb7059a304431 (patch)
tree9d898eb9600b0c36a74e4f95238f679c683fa566 /crates/ra_syntax/src/parsing.rs
parent3d6889cba72a9d02199f7adaa2ecc69bc30af834 (diff)
parenta1c187eef3ba08076aedb5154929f7eda8d1b424 (diff)
Merge #5729
5729: Rename ra_syntax -> syntax r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/parsing.rs')
-rw-r--r--crates/ra_syntax/src/parsing.rs59
1 files changed, 0 insertions, 59 deletions
diff --git a/crates/ra_syntax/src/parsing.rs b/crates/ra_syntax/src/parsing.rs
deleted file mode 100644
index 68a39eb21..000000000
--- a/crates/ra_syntax/src/parsing.rs
+++ /dev/null
@@ -1,59 +0,0 @@
1//! Lexing, bridging to parser (which does the actual parsing) and
2//! incremental reparsing.
3
4mod lexer;
5mod text_token_source;
6mod text_tree_sink;
7mod reparsing;
8
9use crate::{syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode};
10use text_token_source::TextTokenSource;
11use text_tree_sink::TextTreeSink;
12
13pub use lexer::*;
14
15pub(crate) use self::reparsing::incremental_reparse;
16use parser::SyntaxKind;
17
18pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
19 let (tokens, lexer_errors) = tokenize(&text);
20
21 let mut token_source = TextTokenSource::new(text, &tokens);
22 let mut tree_sink = TextTreeSink::new(text, &tokens);
23
24 parser::parse(&mut token_source, &mut tree_sink);
25
26 let (tree, mut parser_errors) = tree_sink.finish();
27 parser_errors.extend(lexer_errors);
28
29 (tree, parser_errors)
30}
31
32/// Returns `text` parsed as a `T` provided there are no parse errors.
33pub(crate) fn parse_text_fragment<T: AstNode>(
34 text: &str,
35 fragment_kind: parser::FragmentKind,
36) -> Result<T, ()> {
37 let (tokens, lexer_errors) = tokenize(&text);
38 if !lexer_errors.is_empty() {
39 return Err(());
40 }
41
42 let mut token_source = TextTokenSource::new(text, &tokens);
43 let mut tree_sink = TextTreeSink::new(text, &tokens);
44
45 // TextTreeSink assumes that there's at least some root node to which it can attach errors and
46 // tokens. We arbitrarily give it a SourceFile.
47 use parser::TreeSink;
48 tree_sink.start_node(SyntaxKind::SOURCE_FILE);
49 parser::parse_fragment(&mut token_source, &mut tree_sink, fragment_kind);
50 tree_sink.finish_node();
51
52 let (tree, parser_errors) = tree_sink.finish();
53 use parser::TokenSource;
54 if !parser_errors.is_empty() || token_source.current().kind != SyntaxKind::EOF {
55 return Err(());
56 }
57
58 SyntaxNode::new_root(tree).first_child().and_then(T::cast).ok_or(())
59}