aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/yellow
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-11-05 21:32:25 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-11-05 21:32:25 +0000
commitf605f6e70a3a15b77900942933253d16dd3ce1f6 (patch)
treea7e8c5097124f8dae7ef15af4b85b1c208d3358c /crates/ra_syntax/src/yellow
parent43665eb166e1bd0319a1e13a97b753a536e4b4d2 (diff)
parent59405bfe4ad0afa0b7ff533c7bfbc3ad4170604c (diff)
Merge #188
188: Introduce `SyntaxErrorKind` and `TextRange` to `SyntaxError` r=matklad a=aochagavia Co-authored-by: Adolfo OchagavĂ­a <[email protected]> Co-authored-by: Adolfo OchagavĂ­a <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/yellow')
-rw-r--r--crates/ra_syntax/src/yellow/builder.rs8
-rw-r--r--crates/ra_syntax/src/yellow/mod.rs10
-rw-r--r--crates/ra_syntax/src/yellow/syntax_error.rs89
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 @@
1use crate::{ 1use 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};
6use rowan::GreenNodeBuilder; 6use 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 @@
1mod builder; 1mod builder;
2pub mod syntax_error;
2mod syntax_text; 3mod syntax_text;
3 4
4use self::syntax_text::SyntaxText; 5use self::syntax_text::SyntaxText;
5use crate::{SmolStr, SyntaxKind, TextRange, TextUnit}; 6use crate::{SmolStr, SyntaxKind, TextRange};
6use rowan::Types; 7use rowan::Types;
7use std::{ 8use std::{
8 fmt, 9 fmt,
@@ -10,6 +11,7 @@ use std::{
10}; 11};
11 12
12pub(crate) use self::builder::GreenBuilder; 13pub(crate) use self::builder::GreenBuilder;
14pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location};
13pub use rowan::{TreeRoot, WalkEvent}; 15pub 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
25pub type GreenNode = ::rowan::GreenNode<RaTypes>; 27pub type GreenNode = ::rowan::GreenNode<RaTypes>;
26 28
27#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
28pub struct SyntaxError {
29 pub msg: String,
30 pub offset: TextUnit,
31}
32
33#[derive(Clone, Copy)] 29#[derive(Clone, Copy)]
34pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(::rowan::SyntaxNode<RaTypes, R>); 30pub struct SyntaxNode<R: TreeRoot<RaTypes> = OwnedRoot>(::rowan::SyntaxNode<RaTypes, R>);
35pub type SyntaxNodeRef<'a> = SyntaxNode<RefRoot<'a>>; 31pub 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 @@
1use std::fmt;
2
3use crate::{TextRange, TextUnit};
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct SyntaxError {
7 kind: SyntaxErrorKind,
8 location: Location,
9}
10
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub enum Location {
13 Offset(TextUnit),
14 Range(TextRange),
15}
16
17impl Into<Location> for TextUnit {
18 fn into(self) -> Location {
19 Location::Offset(self)
20 }
21}
22
23impl Into<Location> for TextRange {
24 fn into(self) -> Location {
25 Location::Range(self)
26 }
27}
28
29impl 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
58impl 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)]
65pub enum SyntaxErrorKind {
66 ParseError(ParseError),
67 EmptyChar,
68 UnclosedChar,
69 LongChar,
70 EmptyAsciiEscape,
71 InvalidAsciiEscape,
72}
73
74#[derive(Debug, Clone, PartialEq, Eq, Hash)]
75pub struct ParseError(pub String);
76
77impl 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}