aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/yellow
diff options
context:
space:
mode:
authorAdolfo OchagavĂ­a <[email protected]>2018-11-04 15:45:22 +0000
committerAdolfo OchagavĂ­a <[email protected]>2018-11-04 20:16:38 +0000
commit3b42ddae601fbd73f672e82028e04c3abdf1252d (patch)
treed63c4114d2fccf1c085502dfd1048b4b5f68266b /crates/ra_syntax/src/yellow
parent576b9a0727ebbf00521bc1131cda808145696d06 (diff)
Introduce SyntaxErrorKind and TextRange in SyntaxError
Diffstat (limited to 'crates/ra_syntax/src/yellow')
-rw-r--r--crates/ra_syntax/src/yellow/builder.rs11
-rw-r--r--crates/ra_syntax/src/yellow/mod.rs10
-rw-r--r--crates/ra_syntax/src/yellow/syntax_error.rs42
3 files changed, 49 insertions, 14 deletions
diff --git a/crates/ra_syntax/src/yellow/builder.rs b/crates/ra_syntax/src/yellow/builder.rs
index d64053409..dbe2df125 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, SyntaxErrorKind},
4 SmolStr, SyntaxKind, TextUnit, 4 SmolStr, SyntaxKind, TextRange,
5}; 5};
6use rowan::GreenNodeBuilder; 6use rowan::GreenNodeBuilder;
7 7
@@ -34,11 +34,8 @@ 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, kind: SyntaxErrorKind, range: TextRange) {
38 let error = SyntaxError { 38 let error = SyntaxError { kind, range };
39 msg: message,
40 offset,
41 };
42 self.errors.push(error) 39 self.errors.push(error)
43 } 40 }
44 41
diff --git a/crates/ra_syntax/src/yellow/mod.rs b/crates/ra_syntax/src/yellow/mod.rs
index 650917214..fd2b5bd33 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};
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..e8c818dc6
--- /dev/null
+++ b/crates/ra_syntax/src/yellow/syntax_error.rs
@@ -0,0 +1,42 @@
1use std::fmt;
2
3use crate::TextRange;
4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct SyntaxError {
7 pub kind: SyntaxErrorKind,
8 pub range: TextRange,
9}
10
11impl SyntaxError {
12 pub fn new(kind: SyntaxErrorKind, range: TextRange) -> SyntaxError {
13 SyntaxError { kind, range }
14 }
15}
16
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub enum SyntaxErrorKind {
19 ParseError(ParseError),
20 EmptyChar,
21 UnclosedChar,
22 LongChar,
23 EmptyAsciiEscape,
24 InvalidAsciiEscape,
25}
26
27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
28pub struct ParseError(pub String);
29
30impl fmt::Display for SyntaxErrorKind {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 use self::SyntaxErrorKind::*;
33 match self {
34 EmptyAsciiEscape => write!(f, "Empty escape sequence"),
35 InvalidAsciiEscape => write!(f, "Invalid escape sequence"),
36 EmptyChar => write!(f, "Empty char literal"),
37 UnclosedChar => write!(f, "Unclosed char literal"),
38 LongChar => write!(f, "Char literal should be one character long"),
39 ParseError(msg) => write!(f, "{}", msg.0),
40 }
41 }
42}