aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/yellow/syntax_error.rs
diff options
context:
space:
mode:
authorAdolfo OchagavĂ­a <[email protected]>2018-11-05 17:38:34 +0000
committerAdolfo OchagavĂ­a <[email protected]>2018-11-05 17:38:34 +0000
commitfda8ddc5fe8a764c0dc91fecb92af1bdf3078485 (patch)
tree1de03cfa42f40bb5ca19956534f53d0b92d271d5 /crates/ra_syntax/src/yellow/syntax_error.rs
parent3b42ddae601fbd73f672e82028e04c3abdf1252d (diff)
Introduce Location and make SyntaxError fields private
Diffstat (limited to 'crates/ra_syntax/src/yellow/syntax_error.rs')
-rw-r--r--crates/ra_syntax/src/yellow/syntax_error.rs54
1 files changed, 49 insertions, 5 deletions
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 @@
1use std::fmt; 1use std::fmt;
2 2
3use crate::TextRange; 3use crate::{TextRange, TextUnit};
4 4
5#[derive(Debug, Clone, PartialEq, Eq, Hash)] 5#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct SyntaxError { 6pub 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)]
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 }
9} 27}
10 28
11impl SyntaxError { 29impl 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
55impl 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