aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/syntax_node
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-02-20 13:16:14 +0000
committerAleksey Kladov <[email protected]>2019-02-20 13:16:14 +0000
commita4a1e08ab81193112a2e14413d084916241c3fca (patch)
treecef2c72994495e37b4e34cb7f7ff5a9b93765cd3 /crates/ra_syntax/src/syntax_node
parent9f6883fbf10567e4177dbe77857c970b6dc61f48 (diff)
flatten modules
Diffstat (limited to 'crates/ra_syntax/src/syntax_node')
-rw-r--r--crates/ra_syntax/src/syntax_node/syntax_error.rs146
-rw-r--r--crates/ra_syntax/src/syntax_node/syntax_text.rs144
2 files changed, 0 insertions, 290 deletions
diff --git a/crates/ra_syntax/src/syntax_node/syntax_error.rs b/crates/ra_syntax/src/syntax_node/syntax_error.rs
deleted file mode 100644
index 4ff998090..000000000
--- a/crates/ra_syntax/src/syntax_node/syntax_error.rs
+++ /dev/null
@@ -1,146 +0,0 @@
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 { kind, location: loc.into() }
32 }
33
34 pub fn kind(&self) -> SyntaxErrorKind {
35 self.kind.clone()
36 }
37
38 pub fn location(&self) -> Location {
39 self.location.clone()
40 }
41
42 pub fn offset(&self) -> TextUnit {
43 match self.location {
44 Location::Offset(offset) => offset,
45 Location::Range(range) => range.start(),
46 }
47 }
48
49 pub fn add_offset(mut self, plus_offset: TextUnit) -> SyntaxError {
50 self.location = match self.location {
51 Location::Range(range) => Location::Range(range + plus_offset),
52 Location::Offset(offset) => Location::Offset(offset + plus_offset),
53 };
54
55 self
56 }
57}
58
59impl fmt::Display for SyntaxError {
60 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 self.kind.fmt(f)
62 }
63}
64
65#[derive(Debug, Clone, PartialEq, Eq, Hash)]
66pub enum SyntaxErrorKind {
67 ParseError(ParseError),
68 UnescapedCodepoint,
69 EmptyChar,
70 UnclosedChar,
71 OverlongChar,
72 EmptyByte,
73 UnclosedByte,
74 OverlongByte,
75 ByteOutOfRange,
76 UnescapedByte,
77 EmptyByteEscape,
78 InvalidByteEscape,
79 TooShortByteCodeEscape,
80 MalformedByteCodeEscape,
81 UnicodeEscapeForbidden,
82 EmptyAsciiEscape,
83 InvalidAsciiEscape,
84 TooShortAsciiCodeEscape,
85 AsciiCodeEscapeOutOfRange,
86 MalformedAsciiCodeEscape,
87 UnclosedUnicodeEscape,
88 MalformedUnicodeEscape,
89 EmptyUnicodeEcape,
90 OverlongUnicodeEscape,
91 UnicodeEscapeOutOfRange,
92 UnclosedString,
93 InvalidSuffix,
94 InvalidBlockAttr,
95 InvalidMatchInnerAttr,
96}
97
98#[derive(Debug, Clone, PartialEq, Eq, Hash)]
99pub struct ParseError(pub String);
100
101impl fmt::Display for SyntaxErrorKind {
102 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
103 use self::SyntaxErrorKind::*;
104 match self {
105 UnescapedCodepoint => write!(f, "This codepoint should always be escaped"),
106 EmptyAsciiEscape => write!(f, "Empty escape sequence"),
107 InvalidAsciiEscape => write!(f, "Invalid escape sequence"),
108 EmptyChar => write!(f, "Empty char literal"),
109 UnclosedChar => write!(f, "Unclosed char literal"),
110 OverlongChar => write!(f, "Char literal should be one character long"),
111 EmptyByte => write!(f, "Empty byte literal"),
112 UnclosedByte => write!(f, "Unclosed byte literal"),
113 OverlongByte => write!(f, "Byte literal should be one character long"),
114 ByteOutOfRange => write!(f, "Byte should be a valid ASCII character"),
115 UnescapedByte => write!(f, "This byte should always be escaped"),
116 EmptyByteEscape => write!(f, "Empty escape sequence"),
117 InvalidByteEscape => write!(f, "Invalid escape sequence"),
118 TooShortByteCodeEscape => write!(f, "Escape sequence should have two digits"),
119 MalformedByteCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"),
120 UnicodeEscapeForbidden => {
121 write!(f, "Unicode escapes are not allowed in byte literals or byte strings")
122 }
123 TooShortAsciiCodeEscape => write!(f, "Escape sequence should have two digits"),
124 AsciiCodeEscapeOutOfRange => {
125 write!(f, "Escape sequence should be between \\x00 and \\x7F")
126 }
127 MalformedAsciiCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"),
128 UnclosedUnicodeEscape => write!(f, "Missing `}}`"),
129 MalformedUnicodeEscape => write!(f, "Malformed unicode escape sequence"),
130 EmptyUnicodeEcape => write!(f, "Empty unicode escape sequence"),
131 OverlongUnicodeEscape => {
132 write!(f, "Unicode escape sequence should have at most 6 digits")
133 }
134 UnicodeEscapeOutOfRange => write!(f, "Unicode escape code should be at most 0x10FFFF"),
135 UnclosedString => write!(f, "Unclosed string literal"),
136 InvalidSuffix => write!(f, "Invalid literal suffix"),
137 InvalidBlockAttr => {
138 write!(f, "A block in this position cannot accept inner attributes")
139 }
140 InvalidMatchInnerAttr => {
141 write!(f, "Inner attributes are only allowed directly after the opening brace of the match expression")
142 }
143 ParseError(msg) => write!(f, "{}", msg.0),
144 }
145 }
146}
diff --git a/crates/ra_syntax/src/syntax_node/syntax_text.rs b/crates/ra_syntax/src/syntax_node/syntax_text.rs
deleted file mode 100644
index 84e5b231a..000000000
--- a/crates/ra_syntax/src/syntax_node/syntax_text.rs
+++ /dev/null
@@ -1,144 +0,0 @@
1use std::{fmt, ops};
2
3use crate::{SyntaxNode, TextRange, TextUnit};
4
5#[derive(Clone)]
6pub struct SyntaxText<'a> {
7 node: &'a SyntaxNode,
8 range: TextRange,
9}
10
11impl<'a> SyntaxText<'a> {
12 pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> {
13 SyntaxText { node, range: node.range() }
14 }
15
16 pub fn chunks(&self) -> impl Iterator<Item = &'a str> {
17 let range = self.range;
18 self.node.descendants().filter_map(move |node| {
19 let text = node.leaf_text()?;
20 let range = range.intersection(&node.range())?;
21 let range = range - node.range().start();
22 Some(&text[range])
23 })
24 }
25
26 pub fn push_to(&self, buf: &mut String) {
27 self.chunks().for_each(|it| buf.push_str(it));
28 }
29
30 pub fn to_string(&self) -> String {
31 self.chunks().collect()
32 }
33
34 pub fn contains(&self, c: char) -> bool {
35 self.chunks().any(|it| it.contains(c))
36 }
37
38 pub fn find(&self, c: char) -> Option<TextUnit> {
39 let mut acc: TextUnit = 0.into();
40 for chunk in self.chunks() {
41 if let Some(pos) = chunk.find(c) {
42 let pos: TextUnit = (pos as u32).into();
43 return Some(acc + pos);
44 }
45 acc += TextUnit::of_str(chunk);
46 }
47 None
48 }
49
50 pub fn len(&self) -> TextUnit {
51 self.range.len()
52 }
53
54 pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> {
55 let range = range.restrict(self.range).unwrap_or_else(|| {
56 panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range)
57 });
58 SyntaxText { node: self.node, range }
59 }
60
61 pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> {
62 let mut start: TextUnit = 0.into();
63 let offset = offset.into();
64 for chunk in self.chunks() {
65 let end = start + TextUnit::of_str(chunk);
66 if start <= offset && offset < end {
67 let off: usize = u32::from(offset - start) as usize;
68 return Some(chunk[off..].chars().next().unwrap());
69 }
70 start = end;
71 }
72 None
73 }
74}
75
76impl<'a> fmt::Debug for SyntaxText<'a> {
77 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78 fmt::Debug::fmt(&self.to_string(), f)
79 }
80}
81
82impl<'a> fmt::Display for SyntaxText<'a> {
83 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84 fmt::Display::fmt(&self.to_string(), f)
85 }
86}
87
88pub trait SyntaxTextSlice: fmt::Debug {
89 fn restrict(&self, range: TextRange) -> Option<TextRange>;
90}
91
92impl SyntaxTextSlice for TextRange {
93 fn restrict(&self, range: TextRange) -> Option<TextRange> {
94 self.intersection(&range)
95 }
96}
97
98impl SyntaxTextSlice for ops::RangeTo<TextUnit> {
99 fn restrict(&self, range: TextRange) -> Option<TextRange> {
100 if !range.contains_inclusive(self.end) {
101 return None;
102 }
103 Some(TextRange::from_to(range.start(), self.end))
104 }
105}
106
107impl SyntaxTextSlice for ops::RangeFrom<TextUnit> {
108 fn restrict(&self, range: TextRange) -> Option<TextRange> {
109 if !range.contains_inclusive(self.start) {
110 return None;
111 }
112 Some(TextRange::from_to(self.start, range.end()))
113 }
114}
115
116impl SyntaxTextSlice for ops::Range<TextUnit> {
117 fn restrict(&self, range: TextRange) -> Option<TextRange> {
118 TextRange::from_to(self.start, self.end).restrict(range)
119 }
120}
121
122impl From<SyntaxText<'_>> for String {
123 fn from(text: SyntaxText) -> String {
124 text.to_string()
125 }
126}
127
128impl PartialEq<str> for SyntaxText<'_> {
129 fn eq(&self, mut rhs: &str) -> bool {
130 for chunk in self.chunks() {
131 if !rhs.starts_with(chunk) {
132 return false;
133 }
134 rhs = &rhs[chunk.len()..];
135 }
136 rhs.is_empty()
137 }
138}
139
140impl PartialEq<&'_ str> for SyntaxText<'_> {
141 fn eq(&self, rhs: &&str) -> bool {
142 self == *rhs
143 }
144}