aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/validation.rs61
-rw-r--r--crates/ra_syntax/src/yellow/syntax_error.rs8
2 files changed, 39 insertions, 30 deletions
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs
index 1778f4b88..a2509dc90 100644
--- a/crates/ra_syntax/src/validation.rs
+++ b/crates/ra_syntax/src/validation.rs
@@ -49,14 +49,16 @@ fn validate_char(node: ast::Char, errors: &mut Vec<SyntaxError>) {
49 if text.len() < 4 { 49 if text.len() < 4 {
50 errors.push(SyntaxError::new(TooShortAsciiCodeEscape, range)); 50 errors.push(SyntaxError::new(TooShortAsciiCodeEscape, range));
51 } else { 51 } else {
52 assert!(text.chars().count() == 4, "AsciiCodeEscape cannot be longer than 4 chars"); 52 assert!(
53 text.chars().count() == 4,
54 "AsciiCodeEscape cannot be longer than 4 chars"
55 );
53 56
54 match u8::from_str_radix(&text[2..], 16) { 57 match u8::from_str_radix(&text[2..], 16) {
55 Ok(code) if code < 128 => { /* Escape code is valid */ }, 58 Ok(code) if code < 128 => { /* Escape code is valid */ }
56 Ok(_) => errors.push(SyntaxError::new(AsciiCodeEscapeOutOfRange, range)), 59 Ok(_) => errors.push(SyntaxError::new(AsciiCodeEscapeOutOfRange, range)),
57 Err(_) => errors.push(SyntaxError::new(MalformedAsciiCodeEscape, range)), 60 Err(_) => errors.push(SyntaxError::new(MalformedAsciiCodeEscape, range)),
58 } 61 }
59
60 } 62 }
61 } 63 }
62 UnicodeEscape => { 64 UnicodeEscape => {
@@ -127,7 +129,7 @@ fn validate_char(node: ast::Char, errors: &mut Vec<SyntaxError>) {
127 if text == "\t" || text == "\r" { 129 if text == "\t" || text == "\r" {
128 errors.push(SyntaxError::new(UnescapedCodepoint, range)); 130 errors.push(SyntaxError::new(UnescapedCodepoint, range));
129 } 131 }
130 }, 132 }
131 } 133 }
132 } 134 }
133 135
@@ -162,13 +164,17 @@ mod test {
162 164
163 fn assert_valid_char(literal: &str) { 165 fn assert_valid_char(literal: &str) {
164 let file = build_file(literal); 166 let file = build_file(literal);
165 assert!(file.errors().len() == 0, "Errors for literal '{}': {:?}", literal, file.errors()); 167 assert!(
168 file.errors().len() == 0,
169 "Errors for literal '{}': {:?}",
170 literal,
171 file.errors()
172 );
166 } 173 }
167 174
168 fn assert_invalid_char(literal: &str) { //, expected_errors: HashSet<SyntaxErrorKind>) { 175 fn assert_invalid_char(literal: &str) {
169 let file = build_file(literal); 176 let file = build_file(literal);
170 assert!(file.errors().len() > 0); 177 assert!(file.errors().len() > 0);
171 //let found_errors = file.errors().iter().map(|e| e.kind()).collect();
172 } 178 }
173 179
174 #[test] 180 #[test]
@@ -184,9 +190,7 @@ mod test {
184 190
185 #[test] 191 #[test]
186 fn test_unicode_codepoints() { 192 fn test_unicode_codepoints() {
187 let valid = [ 193 let valid = ["Ƒ", "バ", "メ", "﷽"];
188 "Ƒ", "バ", "メ", "﷽"
189 ];
190 for c in &valid { 194 for c in &valid {
191 assert_valid_char(c); 195 assert_valid_char(c);
192 } 196 }
@@ -194,9 +198,7 @@ mod test {
194 198
195 #[test] 199 #[test]
196 fn test_unicode_multiple_codepoints() { 200 fn test_unicode_multiple_codepoints() {
197 let invalid = [ 201 let invalid = ["नी", "👨‍👨‍"];
198 "नी", "👨‍👨‍"
199 ];
200 for c in &invalid { 202 for c in &invalid {
201 assert_invalid_char(c); 203 assert_invalid_char(c);
202 } 204 }
@@ -204,9 +206,7 @@ mod test {
204 206
205 #[test] 207 #[test]
206 fn test_valid_ascii_escape() { 208 fn test_valid_ascii_escape() {
207 let valid = [ 209 let valid = [r"\'", "\"", "\\\"", r"\n", r"\r", r"\t", r"\0", "a", "b"];
208 r"\'", "\"", "\\\"", r"\n", r"\r", r"\t", r"\0", "a", "b"
209 ];
210 for c in &valid { 210 for c in &valid {
211 assert_valid_char(c); 211 assert_valid_char(c);
212 } 212 }
@@ -214,9 +214,7 @@ mod test {
214 214
215 #[test] 215 #[test]
216 fn test_invalid_ascii_escape() { 216 fn test_invalid_ascii_escape() {
217 let invalid = [ 217 let invalid = [r"\a", r"\?", r"\"];
218 r"\a", r"\?", r"\"
219 ];
220 for c in &invalid { 218 for c in &invalid {
221 assert_invalid_char(c); 219 assert_invalid_char(c);
222 } 220 }
@@ -224,9 +222,7 @@ mod test {
224 222
225 #[test] 223 #[test]
226 fn test_valid_ascii_code_escape() { 224 fn test_valid_ascii_code_escape() {
227 let valid = [ 225 let valid = [r"\x00", r"\x7F", r"\x55"];
228 r"\x00", r"\x7F", r"\x55"
229 ];
230 for c in &valid { 226 for c in &valid {
231 assert_valid_char(c); 227 assert_valid_char(c);
232 } 228 }
@@ -234,18 +230,20 @@ mod test {
234 230
235 #[test] 231 #[test]
236 fn test_invalid_ascii_code_escape() { 232 fn test_invalid_ascii_code_escape() {
237 let invalid = [ 233 let invalid = [r"\x", r"\x7", r"\xF0"];
238 r"\x", r"\x7", r"\xF0"
239 ];
240 for c in &invalid { 234 for c in &invalid {
241 assert_invalid_char(c); 235 assert_invalid_char(c);
242 } 236 }
243 } 237 }
244 238
245 #[test] 239 #[test]
246 fn test_valid_unicode_escape() { 240 fn test_valid_unicode_escape() {
247 let valid = [ 241 let valid = [
248 r"\u{FF}", r"\u{0}", r"\u{F}", r"\u{10FFFF}", r"\u{1_0__FF___FF_____}" 242 r"\u{FF}",
243 r"\u{0}",
244 r"\u{F}",
245 r"\u{10FFFF}",
246 r"\u{1_0__FF___FF_____}",
249 ]; 247 ];
250 for c in &valid { 248 for c in &valid {
251 assert_valid_char(c); 249 assert_valid_char(c);
@@ -255,7 +253,14 @@ mod test {
255 #[test] 253 #[test]
256 fn test_invalid_unicode_escape() { 254 fn test_invalid_unicode_escape() {
257 let invalid = [ 255 let invalid = [
258 r"\u", r"\u{}", r"\u{", r"\u{FF", r"\u{FFFFFF}", r"\u{_F}", r"\u{00FFFFF}", r"\u{110000}" 256 r"\u",
257 r"\u{}",
258 r"\u{",
259 r"\u{FF",
260 r"\u{FFFFFF}",
261 r"\u{_F}",
262 r"\u{00FFFFF}",
263 r"\u{110000}",
259 ]; 264 ];
260 for c in &invalid { 265 for c in &invalid {
261 assert_invalid_char(c); 266 assert_invalid_char(c);
diff --git a/crates/ra_syntax/src/yellow/syntax_error.rs b/crates/ra_syntax/src/yellow/syntax_error.rs
index e2e660975..c524adf39 100644
--- a/crates/ra_syntax/src/yellow/syntax_error.rs
+++ b/crates/ra_syntax/src/yellow/syntax_error.rs
@@ -98,12 +98,16 @@ impl fmt::Display for SyntaxErrorKind {
98 UnclosedChar => write!(f, "Unclosed char literal"), 98 UnclosedChar => write!(f, "Unclosed char literal"),
99 LongChar => write!(f, "Char literal should be one character long"), 99 LongChar => write!(f, "Char literal should be one character long"),
100 TooShortAsciiCodeEscape => write!(f, "Escape sequence should have two digits"), 100 TooShortAsciiCodeEscape => write!(f, "Escape sequence should have two digits"),
101 AsciiCodeEscapeOutOfRange => write!(f, "Escape sequence should be between \\x00 and \\x7F"), 101 AsciiCodeEscapeOutOfRange => {
102 write!(f, "Escape sequence should be between \\x00 and \\x7F")
103 }
102 MalformedAsciiCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"), 104 MalformedAsciiCodeEscape => write!(f, "Escape sequence should be a hexadecimal number"),
103 UnclosedUnicodeEscape => write!(f, "Missing `}}`"), 105 UnclosedUnicodeEscape => write!(f, "Missing `}}`"),
104 MalformedUnicodeEscape => write!(f, "Malformed unicode escape sequence"), 106 MalformedUnicodeEscape => write!(f, "Malformed unicode escape sequence"),
105 EmptyUnicodeEcape => write!(f, "Empty unicode escape sequence"), 107 EmptyUnicodeEcape => write!(f, "Empty unicode escape sequence"),
106 OverlongUnicodeEscape => write!(f, "Unicode escape sequence should have at most 6 digits"), 108 OverlongUnicodeEscape => {
109 write!(f, "Unicode escape sequence should have at most 6 digits")
110 }
107 UnicodeEscapeOutOfRange => write!(f, "Unicode escape code should be at most 0x10FFFF"), 111 UnicodeEscapeOutOfRange => write!(f, "Unicode escape code should be at most 0x10FFFF"),
108 ParseError(msg) => write!(f, "{}", msg.0), 112 ParseError(msg) => write!(f, "{}", msg.0),
109 } 113 }