diff options
Diffstat (limited to 'crates/ra_syntax/src/yellow')
-rw-r--r-- | crates/ra_syntax/src/yellow/builder.rs | 8 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/mod.rs | 10 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/syntax_error.rs | 89 |
3 files changed, 94 insertions, 13 deletions
diff --git a/crates/ra_syntax/src/yellow/builder.rs b/crates/ra_syntax/src/yellow/builder.rs index d64053409..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}, | 3 | yellow::{GreenNode, RaTypes, SyntaxError}, |
4 | SmolStr, SyntaxKind, TextUnit, | 4 | SmolStr, SyntaxKind, |
5 | }; | 5 | }; |
6 | use rowan::GreenNodeBuilder; | 6 | use rowan::GreenNodeBuilder; |
7 | 7 | ||
@@ -34,11 +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, message: String, offset: TextUnit) { | 37 | fn error(&mut self, error: SyntaxError) { |
38 | let error = SyntaxError { | ||
39 | msg: message, | ||
40 | offset, | ||
41 | }; | ||
42 | self.errors.push(error) | 38 | self.errors.push(error) |
43 | } | 39 | } |
44 | 40 | ||
diff --git a/crates/ra_syntax/src/yellow/mod.rs b/crates/ra_syntax/src/yellow/mod.rs index 650917214..6da948648 100644 --- a/crates/ra_syntax/src/yellow/mod.rs +++ b/crates/ra_syntax/src/yellow/mod.rs | |||
@@ -1,8 +1,9 @@ | |||
1 | mod builder; | 1 | mod builder; |
2 | pub mod syntax_error; | ||
2 | mod syntax_text; | 3 | mod syntax_text; |
3 | 4 | ||
4 | use self::syntax_text::SyntaxText; | 5 | use self::syntax_text::SyntaxText; |
5 | use crate::{SmolStr, SyntaxKind, TextRange, TextUnit}; | 6 | use crate::{SmolStr, SyntaxKind, TextRange}; |
6 | use rowan::Types; | 7 | use rowan::Types; |
7 | use std::{ | 8 | use std::{ |
8 | fmt, | 9 | fmt, |
@@ -10,6 +11,7 @@ use std::{ | |||
10 | }; | 11 | }; |
11 | 12 | ||
12 | pub(crate) use self::builder::GreenBuilder; | 13 | pub(crate) use self::builder::GreenBuilder; |
14 | pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location}; | ||
13 | pub use rowan::{TreeRoot, WalkEvent}; | 15 | pub use rowan::{TreeRoot, WalkEvent}; |
14 | 16 | ||
15 | #[derive(Debug, Clone, Copy)] | 17 | #[derive(Debug, Clone, Copy)] |
@@ -24,12 +26,6 @@ pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>; | |||
24 | 26 | ||
25 | pub type GreenNode = ::rowan::GreenNode<RaTypes>; | 27 | pub type GreenNode = ::rowan::GreenNode<RaTypes>; |
26 | 28 | ||
27 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] | ||
28 | pub struct SyntaxError { | ||
29 | pub msg: String, | ||
30 | pub offset: TextUnit, | ||
31 | } | ||
32 | |||
33 | #[derive(Clone, Copy)] | 29 | #[derive(Clone, Copy)] |
34 | pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(::rowan::SyntaxNode<RaTypes, R>); | 30 | pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(::rowan::SyntaxNode<RaTypes, R>); |
35 | pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; | 31 | pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; |
diff --git a/crates/ra_syntax/src/yellow/syntax_error.rs b/crates/ra_syntax/src/yellow/syntax_error.rs new file mode 100644 index 000000000..f3df6bc15 --- /dev/null +++ b/crates/ra_syntax/src/yellow/syntax_error.rs | |||
@@ -0,0 +1,89 @@ | |||
1 | use std::fmt; | ||
2 | |||
3 | use crate::{TextRange, TextUnit}; | ||
4 | |||
5 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
6 | pub struct SyntaxError { | ||
7 | kind: SyntaxErrorKind, | ||
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 | } | ||
27 | } | ||
28 | |||
29 | impl SyntaxError { | ||
30 | pub fn new<L: Into<Location>>(kind: SyntaxErrorKind, loc: L) -> SyntaxError { | ||
31 | SyntaxError { | ||
32 | kind, | ||
33 | location: loc.into(), | ||
34 | } | ||
35 | } | ||
36 | |||
37 | pub fn location(&self) -> Location { | ||
38 | self.location.clone() | ||
39 | } | ||
40 | |||
41 | pub fn offset(&self) -> TextUnit { | ||
42 | match self.location { | ||
43 | Location::Offset(offset) => offset, | ||
44 | Location::Range(range) => range.start(), | ||
45 | } | ||
46 | } | ||
47 | |||
48 | pub fn add_offset(mut self, plus_offset: TextUnit) -> SyntaxError { | ||
49 | self.location = match self.location { | ||
50 | Location::Range(range) => Location::Range(range + plus_offset), | ||
51 | Location::Offset(offset) => Location::Offset(offset + plus_offset), | ||
52 | }; | ||
53 | |||
54 | self | ||
55 | } | ||
56 | } | ||
57 | |||
58 | impl fmt::Display for SyntaxError { | ||
59 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
60 | self.kind.fmt(f) | ||
61 | } | ||
62 | } | ||
63 | |||
64 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
65 | pub enum SyntaxErrorKind { | ||
66 | ParseError(ParseError), | ||
67 | EmptyChar, | ||
68 | UnclosedChar, | ||
69 | LongChar, | ||
70 | EmptyAsciiEscape, | ||
71 | InvalidAsciiEscape, | ||
72 | } | ||
73 | |||
74 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
75 | pub struct ParseError(pub String); | ||
76 | |||
77 | impl fmt::Display for SyntaxErrorKind { | ||
78 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
79 | use self::SyntaxErrorKind::*; | ||
80 | match self { | ||
81 | EmptyAsciiEscape => write!(f, "Empty escape sequence"), | ||
82 | InvalidAsciiEscape => write!(f, "Invalid escape sequence"), | ||
83 | EmptyChar => write!(f, "Empty char literal"), | ||
84 | UnclosedChar => write!(f, "Unclosed char literal"), | ||
85 | LongChar => write!(f, "Char literal should be one character long"), | ||
86 | ParseError(msg) => write!(f, "{}", msg.0), | ||
87 | } | ||
88 | } | ||
89 | } | ||