From 3b42ddae601fbd73f672e82028e04c3abdf1252d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 4 Nov 2018 16:45:22 +0100 Subject: Introduce SyntaxErrorKind and TextRange in SyntaxError --- crates/ra_syntax/src/yellow/builder.rs | 11 +++----- crates/ra_syntax/src/yellow/mod.rs | 10 +++---- crates/ra_syntax/src/yellow/syntax_error.rs | 42 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 crates/ra_syntax/src/yellow/syntax_error.rs (limited to 'crates/ra_syntax/src/yellow') 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 @@ use crate::{ parser_impl::Sink, - yellow::{GreenNode, RaTypes, SyntaxError}, - SmolStr, SyntaxKind, TextUnit, + yellow::{GreenNode, RaTypes, SyntaxError, SyntaxErrorKind}, + SmolStr, SyntaxKind, TextRange, }; use rowan::GreenNodeBuilder; @@ -34,11 +34,8 @@ impl Sink for GreenBuilder { self.inner.finish_internal(); } - fn error(&mut self, message: String, offset: TextUnit) { - let error = SyntaxError { - msg: message, - offset, - }; + fn error(&mut self, kind: SyntaxErrorKind, range: TextRange) { + let error = SyntaxError { kind, range }; self.errors.push(error) } 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 @@ mod builder; +pub mod syntax_error; mod syntax_text; use self::syntax_text::SyntaxText; -use crate::{SmolStr, SyntaxKind, TextRange, TextUnit}; +use crate::{SmolStr, SyntaxKind, TextRange}; use rowan::Types; use std::{ fmt, @@ -10,6 +11,7 @@ use std::{ }; pub(crate) use self::builder::GreenBuilder; +pub use self::syntax_error::{SyntaxError, SyntaxErrorKind}; pub use rowan::{TreeRoot, WalkEvent}; #[derive(Debug, Clone, Copy)] @@ -24,12 +26,6 @@ pub type RefRoot<'a> = ::rowan::RefRoot<'a, RaTypes>; pub type GreenNode = ::rowan::GreenNode; -#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] -pub struct SyntaxError { - pub msg: String, - pub offset: TextUnit, -} - #[derive(Clone, Copy)] pub struct SyntaxNode = OwnedRoot>(::rowan::SyntaxNode); pub type SyntaxNodeRef<'a> = SyntaxNode>; 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 @@ +use std::fmt; + +use crate::TextRange; + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct SyntaxError { + pub kind: SyntaxErrorKind, + pub range: TextRange, +} + +impl SyntaxError { + pub fn new(kind: SyntaxErrorKind, range: TextRange) -> SyntaxError { + SyntaxError { kind, range } + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum SyntaxErrorKind { + ParseError(ParseError), + EmptyChar, + UnclosedChar, + LongChar, + EmptyAsciiEscape, + InvalidAsciiEscape, +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct ParseError(pub String); + +impl fmt::Display for SyntaxErrorKind { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::SyntaxErrorKind::*; + match self { + EmptyAsciiEscape => write!(f, "Empty escape sequence"), + InvalidAsciiEscape => write!(f, "Invalid escape sequence"), + EmptyChar => write!(f, "Empty char literal"), + UnclosedChar => write!(f, "Unclosed char literal"), + LongChar => write!(f, "Char literal should be one character long"), + ParseError(msg) => write!(f, "{}", msg.0), + } + } +} -- cgit v1.2.3