diff options
author | Adolfo OchagavĂa <[email protected]> | 2018-11-05 17:38:34 +0000 |
---|---|---|
committer | Adolfo OchagavĂa <[email protected]> | 2018-11-05 17:38:34 +0000 |
commit | fda8ddc5fe8a764c0dc91fecb92af1bdf3078485 (patch) | |
tree | 1de03cfa42f40bb5ca19956534f53d0b92d271d5 /crates/ra_syntax/src/yellow | |
parent | 3b42ddae601fbd73f672e82028e04c3abdf1252d (diff) |
Introduce Location and make SyntaxError fields private
Diffstat (limited to 'crates/ra_syntax/src/yellow')
-rw-r--r-- | crates/ra_syntax/src/yellow/builder.rs | 7 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/mod.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/syntax_error.rs | 54 |
3 files changed, 53 insertions, 10 deletions
diff --git a/crates/ra_syntax/src/yellow/builder.rs b/crates/ra_syntax/src/yellow/builder.rs index dbe2df125..9fcebfb93 100644 --- a/crates/ra_syntax/src/yellow/builder.rs +++ b/crates/ra_syntax/src/yellow/builder.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use crate::{ | 1 | use crate::{ |
2 | parser_impl::Sink, | 2 | parser_impl::Sink, |
3 | yellow::{GreenNode, RaTypes, SyntaxError, SyntaxErrorKind}, | 3 | yellow::{GreenNode, RaTypes, SyntaxError}, |
4 | SmolStr, SyntaxKind, TextRange, | 4 | SmolStr, SyntaxKind, |
5 | }; | 5 | }; |
6 | use rowan::GreenNodeBuilder; | 6 | use rowan::GreenNodeBuilder; |
7 | 7 | ||
@@ -34,8 +34,7 @@ impl Sink for GreenBuilder { | |||
34 | self.inner.finish_internal(); | 34 | self.inner.finish_internal(); |
35 | } | 35 | } |
36 | 36 | ||
37 | fn error(&mut self, kind: SyntaxErrorKind, range: TextRange) { | 37 | fn error(&mut self, error: SyntaxError) { |
38 | let error = SyntaxError { kind, range }; | ||
39 | self.errors.push(error) | 38 | self.errors.push(error) |
40 | } | 39 | } |
41 | 40 | ||
diff --git a/crates/ra_syntax/src/yellow/mod.rs b/crates/ra_syntax/src/yellow/mod.rs index fd2b5bd33..6da948648 100644 --- a/crates/ra_syntax/src/yellow/mod.rs +++ b/crates/ra_syntax/src/yellow/mod.rs | |||
@@ -11,7 +11,7 @@ use std::{ | |||
11 | }; | 11 | }; |
12 | 12 | ||
13 | pub(crate) use self::builder::GreenBuilder; | 13 | pub(crate) use self::builder::GreenBuilder; |
14 | pub use self::syntax_error::{SyntaxError, SyntaxErrorKind}; | 14 | pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; |
15 | pub use rowan::{TreeRoot, WalkEvent}; | 15 | pub use rowan::{TreeRoot, WalkEvent}; |
16 | 16 | ||
17 | #[derive(Debug, Clone, Copy)] | 17 | #[derive(Debug, Clone, Copy)] |
diff --git a/crates/ra_syntax/src/yellow/syntax_error.rs b/crates/ra_syntax/src/yellow/syntax_error.rs index e8c818dc6..098366f85 100644 --- a/crates/ra_syntax/src/yellow/syntax_error.rs +++ b/crates/ra_syntax/src/yellow/syntax_error.rs | |||
@@ -1,16 +1,60 @@ | |||
1 | use std::fmt; | 1 | use std::fmt; |
2 | 2 | ||
3 | use crate::TextRange; | 3 | use crate::{TextRange, TextUnit}; |
4 | 4 | ||
5 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 5 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
6 | pub struct SyntaxError { | 6 | pub struct SyntaxError { |
7 | pub kind: SyntaxErrorKind, | 7 | kind: SyntaxErrorKind, |
8 | pub range: TextRange, | 8 | location: Location, |
9 | } | ||
10 | |||
11 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
12 | pub enum Location { | ||
13 | Offset(TextUnit), | ||
14 | Range(TextRange), | ||
15 | } | ||
16 | |||
17 | impl Into<Location> for TextUnit { | ||
18 | fn into(self) -> Location { | ||
19 | Location::Offset(self) | ||
20 | } | ||
21 | } | ||
22 | |||
23 | impl Into<Location> for TextRange { | ||
24 | fn into(self) -> Location { | ||
25 | Location::Range(self) | ||
26 | } | ||
9 | } | 27 | } |
10 | 28 | ||
11 | impl SyntaxError { | 29 | impl SyntaxError { |
12 | pub fn new(kind: SyntaxErrorKind, range: TextRange) -> SyntaxError { | 30 | pub fn new<L: Into<Location>>(kind: SyntaxErrorKind, loc: L) -> SyntaxError { |
13 | SyntaxError { kind, range } | 31 | SyntaxError { kind, location: loc.into() } |
32 | } | ||
33 | |||
34 | pub fn location(&self) -> Location { | ||
35 | self.location.clone() | ||
36 | } | ||
37 | |||
38 | pub fn offset(&self) -> TextUnit { | ||
39 | match self.location { | ||
40 | Location::Offset(offset) => offset, | ||
41 | Location::Range(range) => range.start(), | ||
42 | } | ||
43 | } | ||
44 | |||
45 | pub fn add_offset(mut self, plus_offset: TextUnit) -> SyntaxError { | ||
46 | self.location = match self.location { | ||
47 | Location::Range(range) => Location::Range(range + plus_offset), | ||
48 | Location::Offset(offset) => Location::Offset(offset + plus_offset) | ||
49 | }; | ||
50 | |||
51 | self | ||
52 | } | ||
53 | } | ||
54 | |||
55 | impl fmt::Display for SyntaxError { | ||
56 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
57 | self.kind.fmt(f) | ||
14 | } | 58 | } |
15 | } | 59 | } |
16 | 60 | ||