aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/parsing.rs9
-rw-r--r--crates/ra_syntax/src/parsing/builder.rs19
-rw-r--r--crates/ra_syntax/src/parsing/event.rs11
-rw-r--r--crates/ra_syntax/src/parsing/parser.rs3
-rw-r--r--crates/ra_syntax/src/parsing/reparsing.rs2
-rw-r--r--crates/ra_syntax/src/syntax_error.rs5
6 files changed, 24 insertions, 25 deletions
diff --git a/crates/ra_syntax/src/parsing.rs b/crates/ra_syntax/src/parsing.rs
index 941ec501e..138d1394a 100644
--- a/crates/ra_syntax/src/parsing.rs
+++ b/crates/ra_syntax/src/parsing.rs
@@ -9,7 +9,7 @@ mod grammar;
9mod reparsing; 9mod reparsing;
10 10
11use crate::{ 11use crate::{
12 SyntaxError, SyntaxKind, SmolStr, 12 SyntaxKind, SmolStr, SyntaxError,
13 parsing::{ 13 parsing::{
14 builder::GreenBuilder, 14 builder::GreenBuilder,
15 input::ParserInput, 15 input::ParserInput,
@@ -21,11 +21,14 @@ use crate::{
21 21
22pub use self::lexer::{tokenize, Token}; 22pub use self::lexer::{tokenize, Token};
23 23
24#[derive(Debug, Clone, PartialEq, Eq, Hash)]
25pub struct ParseError(pub String);
26
24pub(crate) use self::reparsing::incremental_reparse; 27pub(crate) use self::reparsing::incremental_reparse;
25 28
26pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) { 29pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
27 let tokens = tokenize(&text); 30 let tokens = tokenize(&text);
28 parse_with(GreenBuilder::new(), text, &tokens, grammar::root) 31 parse_with(GreenBuilder::default(), text, &tokens, grammar::root)
29} 32}
30 33
31fn parse_with<S: TreeSink>( 34fn parse_with<S: TreeSink>(
@@ -57,7 +60,7 @@ trait TreeSink {
57 /// branch as current. 60 /// branch as current.
58 fn finish_branch(&mut self); 61 fn finish_branch(&mut self);
59 62
60 fn error(&mut self, error: SyntaxError); 63 fn error(&mut self, error: ParseError);
61 64
62 /// Complete tree building. Make sure that 65 /// Complete tree building. Make sure that
63 /// `start_branch` and `finish_branch` calls 66 /// `start_branch` and `finish_branch` calls
diff --git a/crates/ra_syntax/src/parsing/builder.rs b/crates/ra_syntax/src/parsing/builder.rs
index a05e7f84b..ee0e2cce7 100644
--- a/crates/ra_syntax/src/parsing/builder.rs
+++ b/crates/ra_syntax/src/parsing/builder.rs
@@ -1,19 +1,24 @@
1use crate::{ 1use crate::{
2 parsing::TreeSink, 2 SmolStr, SyntaxKind, SyntaxError, SyntaxErrorKind, TextUnit,
3 parsing::{TreeSink, ParseError},
3 syntax_node::{GreenNode, RaTypes}, 4 syntax_node::{GreenNode, RaTypes},
4 SmolStr, SyntaxKind, SyntaxError,
5}; 5};
6 6
7use rowan::GreenNodeBuilder; 7use rowan::GreenNodeBuilder;
8 8
9pub(crate) struct GreenBuilder { 9pub(crate) struct GreenBuilder {
10 text_pos: TextUnit,
10 errors: Vec<SyntaxError>, 11 errors: Vec<SyntaxError>,
11 inner: GreenNodeBuilder<RaTypes>, 12 inner: GreenNodeBuilder<RaTypes>,
12} 13}
13 14
14impl GreenBuilder { 15impl Default for GreenBuilder {
15 pub(crate) fn new() -> GreenBuilder { 16 fn default() -> GreenBuilder {
16 GreenBuilder { errors: Vec::new(), inner: GreenNodeBuilder::new() } 17 GreenBuilder {
18 text_pos: TextUnit::default(),
19 errors: Vec::new(),
20 inner: GreenNodeBuilder::new(),
21 }
17 } 22 }
18} 23}
19 24
@@ -21,6 +26,7 @@ impl TreeSink for GreenBuilder {
21 type Tree = (GreenNode, Vec<SyntaxError>); 26 type Tree = (GreenNode, Vec<SyntaxError>);
22 27
23 fn leaf(&mut self, kind: SyntaxKind, text: SmolStr) { 28 fn leaf(&mut self, kind: SyntaxKind, text: SmolStr) {
29 self.text_pos += TextUnit::of_str(text.as_str());
24 self.inner.leaf(kind, text); 30 self.inner.leaf(kind, text);
25 } 31 }
26 32
@@ -32,7 +38,8 @@ impl TreeSink for GreenBuilder {
32 self.inner.finish_internal(); 38 self.inner.finish_internal();
33 } 39 }
34 40
35 fn error(&mut self, error: SyntaxError) { 41 fn error(&mut self, error: ParseError) {
42 let error = SyntaxError::new(SyntaxErrorKind::ParseError(error), self.text_pos);
36 self.errors.push(error) 43 self.errors.push(error)
37 } 44 }
38 45
diff --git a/crates/ra_syntax/src/parsing/event.rs b/crates/ra_syntax/src/parsing/event.rs
index 893a42e9a..f6f020eab 100644
--- a/crates/ra_syntax/src/parsing/event.rs
+++ b/crates/ra_syntax/src/parsing/event.rs
@@ -13,14 +13,9 @@ use crate::{
13 SmolStr, 13 SmolStr,
14 SyntaxKind::{self, *}, 14 SyntaxKind::{self, *},
15 TextRange, TextUnit, 15 TextRange, TextUnit,
16 syntax_error::{
17 ParseError,
18 SyntaxError,
19 SyntaxErrorKind,
20 },
21 parsing::{ 16 parsing::{
17 ParseError, TreeSink,
22 lexer::Token, 18 lexer::Token,
23 TreeSink,
24 }, 19 },
25}; 20};
26 21
@@ -159,9 +154,7 @@ impl<'a, S: TreeSink> EventProcessor<'a, S> {
159 .sum::<TextUnit>(); 154 .sum::<TextUnit>();
160 self.leaf(kind, len, n_raw_tokens); 155 self.leaf(kind, len, n_raw_tokens);
161 } 156 }
162 Event::Error { msg } => self 157 Event::Error { msg } => self.sink.error(msg),
163 .sink
164 .error(SyntaxError::new(SyntaxErrorKind::ParseError(msg), self.text_pos)),
165 } 158 }
166 } 159 }
167 self.sink 160 self.sink
diff --git a/crates/ra_syntax/src/parsing/parser.rs b/crates/ra_syntax/src/parsing/parser.rs
index 988fcb518..923b0f2b2 100644
--- a/crates/ra_syntax/src/parsing/parser.rs
+++ b/crates/ra_syntax/src/parsing/parser.rs
@@ -3,10 +3,9 @@ use std::cell::Cell;
3use drop_bomb::DropBomb; 3use drop_bomb::DropBomb;
4 4
5use crate::{ 5use crate::{
6 syntax_error::ParseError,
7 SyntaxKind::{self, ERROR, EOF, TOMBSTONE}, 6 SyntaxKind::{self, ERROR, EOF, TOMBSTONE},
8 parsing::{ 7 parsing::{
9 TokenSource, 8 TokenSource, ParseError,
10 token_set::TokenSet, 9 token_set::TokenSet,
11 event::Event, 10 event::Event,
12 }, 11 },
diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs
index 674b15f9a..f2d218ab9 100644
--- a/crates/ra_syntax/src/parsing/reparsing.rs
+++ b/crates/ra_syntax/src/parsing/reparsing.rs
@@ -61,7 +61,7 @@ fn reparse_block<'node>(
61 if !is_balanced(&tokens) { 61 if !is_balanced(&tokens) {
62 return None; 62 return None;
63 } 63 }
64 let (green, new_errors) = parse_with(GreenBuilder::new(), &text, &tokens, reparser); 64 let (green, new_errors) = parse_with(GreenBuilder::default(), &text, &tokens, reparser);
65 Some((node, green, new_errors)) 65 Some((node, green, new_errors))
66} 66}
67 67
diff --git a/crates/ra_syntax/src/syntax_error.rs b/crates/ra_syntax/src/syntax_error.rs
index 4ff998090..1a00fcc27 100644
--- a/crates/ra_syntax/src/syntax_error.rs
+++ b/crates/ra_syntax/src/syntax_error.rs
@@ -1,6 +1,6 @@
1use std::fmt; 1use std::fmt;
2 2
3use crate::{TextRange, TextUnit}; 3use crate::{TextRange, TextUnit, parsing::ParseError};
4 4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)] 5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct SyntaxError { 6pub struct SyntaxError {
@@ -95,9 +95,6 @@ pub enum SyntaxErrorKind {
95 InvalidMatchInnerAttr, 95 InvalidMatchInnerAttr,
96} 96}
97 97
98#[derive(Debug, Clone, PartialEq, Eq, Hash)]
99pub struct ParseError(pub String);
100
101impl fmt::Display for SyntaxErrorKind { 98impl fmt::Display for SyntaxErrorKind {
102 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 99 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
103 use self::SyntaxErrorKind::*; 100 use self::SyntaxErrorKind::*;