diff options
Diffstat (limited to 'crates/ra_syntax/src/validation.rs')
-rw-r--r-- | crates/ra_syntax/src/validation.rs | 213 |
1 files changed, 203 insertions, 10 deletions
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 2b26e388d..f345dbd6e 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs | |||
@@ -1,3 +1,7 @@ | |||
1 | use std::u32; | ||
2 | |||
3 | use arrayvec::ArrayString; | ||
4 | |||
1 | use crate::{ | 5 | use crate::{ |
2 | algo::visit::{visitor_ctx, VisitorCtx}, | 6 | algo::visit::{visitor_ctx, VisitorCtx}, |
3 | ast::{self, AstNode}, | 7 | ast::{self, AstNode}, |
@@ -42,18 +46,90 @@ fn validate_char(node: ast::Char, errors: &mut Vec<SyntaxError>) { | |||
42 | } | 46 | } |
43 | } | 47 | } |
44 | AsciiCodeEscape => { | 48 | AsciiCodeEscape => { |
45 | // TODO: | 49 | // An AsciiCodeEscape has 4 chars, example: `\xDD` |
46 | // * First digit is octal | 50 | if text.len() < 4 { |
47 | // * Second digit is hex | 51 | errors.push(SyntaxError::new(TooShortAsciiCodeEscape, range)); |
52 | } else { | ||
53 | assert!( | ||
54 | text.chars().count() == 4, | ||
55 | "AsciiCodeEscape cannot be longer than 4 chars" | ||
56 | ); | ||
57 | |||
58 | match u8::from_str_radix(&text[2..], 16) { | ||
59 | Ok(code) if code < 128 => { /* Escape code is valid */ } | ||
60 | Ok(_) => errors.push(SyntaxError::new(AsciiCodeEscapeOutOfRange, range)), | ||
61 | Err(_) => errors.push(SyntaxError::new(MalformedAsciiCodeEscape, range)), | ||
62 | } | ||
63 | } | ||
48 | } | 64 | } |
49 | UnicodeEscape => { | 65 | UnicodeEscape => { |
50 | // TODO: | 66 | assert!(&text[..2] == "\\u", "UnicodeEscape always starts with \\u"); |
51 | // * Only hex digits or underscores allowed | 67 | |
52 | // * Max 6 chars | 68 | if text.len() == 2 { |
53 | // * Within allowed range (must be at most 10FFFF) | 69 | // No starting `{` |
70 | errors.push(SyntaxError::new(MalformedUnicodeEscape, range)); | ||
71 | return; | ||
72 | } | ||
73 | |||
74 | if text.len() == 3 { | ||
75 | // Only starting `{` | ||
76 | errors.push(SyntaxError::new(UnclosedUnicodeEscape, range)); | ||
77 | return; | ||
78 | } | ||
79 | |||
80 | let mut code = ArrayString::<[_; 6]>::new(); | ||
81 | let mut closed = false; | ||
82 | for c in text[3..].chars() { | ||
83 | assert!(!closed, "no characters after escape is closed"); | ||
84 | |||
85 | if c.is_digit(16) { | ||
86 | if code.len() == 6 { | ||
87 | errors.push(SyntaxError::new(OverlongUnicodeEscape, range)); | ||
88 | return; | ||
89 | } | ||
90 | |||
91 | code.push(c); | ||
92 | } else if c == '_' { | ||
93 | // Reject leading _ | ||
94 | if code.len() == 0 { | ||
95 | errors.push(SyntaxError::new(MalformedUnicodeEscape, range)); | ||
96 | return; | ||
97 | } | ||
98 | } else if c == '}' { | ||
99 | closed = true; | ||
100 | } else { | ||
101 | errors.push(SyntaxError::new(MalformedUnicodeEscape, range)); | ||
102 | return; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | if !closed { | ||
107 | errors.push(SyntaxError::new(UnclosedUnicodeEscape, range)) | ||
108 | } | ||
109 | |||
110 | if code.len() == 0 { | ||
111 | errors.push(SyntaxError::new(EmptyUnicodeEcape, range)); | ||
112 | return; | ||
113 | } | ||
114 | |||
115 | match u32::from_str_radix(&code, 16) { | ||
116 | Ok(code_u32) if code_u32 > 0x10FFFF => { | ||
117 | errors.push(SyntaxError::new(UnicodeEscapeOutOfRange, range)); | ||
118 | } | ||
119 | Ok(_) => { | ||
120 | // Valid escape code | ||
121 | } | ||
122 | Err(_) => { | ||
123 | errors.push(SyntaxError::new(MalformedUnicodeEscape, range)); | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | CodePoint => { | ||
128 | // These code points must always be escaped | ||
129 | if text == "\t" || text == "\r" { | ||
130 | errors.push(SyntaxError::new(UnescapedCodepoint, range)); | ||
131 | } | ||
54 | } | 132 | } |
55 | // Code points are always valid | ||
56 | CodePoint => (), | ||
57 | } | 133 | } |
58 | } | 134 | } |
59 | 135 | ||
@@ -72,7 +148,124 @@ fn validate_char(node: ast::Char, errors: &mut Vec<SyntaxError>) { | |||
72 | 148 | ||
73 | fn is_ascii_escape(code: char) -> bool { | 149 | fn is_ascii_escape(code: char) -> bool { |
74 | match code { | 150 | match code { |
75 | '\'' | '"' | 'n' | 'r' | 't' | '0' => true, | 151 | '\\' | '\'' | '"' | 'n' | 'r' | 't' | '0' => true, |
76 | _ => false, | 152 | _ => false, |
77 | } | 153 | } |
78 | } | 154 | } |
155 | |||
156 | #[cfg(test)] | ||
157 | mod test { | ||
158 | use crate::File; | ||
159 | |||
160 | fn build_file(literal: &str) -> File { | ||
161 | let src = format!("const C: char = '{}';", literal); | ||
162 | File::parse(&src) | ||
163 | } | ||
164 | |||
165 | fn assert_valid_char(literal: &str) { | ||
166 | let file = build_file(literal); | ||
167 | assert!( | ||
168 | file.errors().len() == 0, | ||
169 | "Errors for literal '{}': {:?}", | ||
170 | literal, | ||
171 | file.errors() | ||
172 | ); | ||
173 | } | ||
174 | |||
175 | fn assert_invalid_char(literal: &str) { | ||
176 | let file = build_file(literal); | ||
177 | assert!(file.errors().len() > 0); | ||
178 | } | ||
179 | |||
180 | #[test] | ||
181 | fn test_ansi_codepoints() { | ||
182 | for byte in 0..=255u8 { | ||
183 | match byte { | ||
184 | b'\n' | b'\r' | b'\t' => assert_invalid_char(&(byte as char).to_string()), | ||
185 | b'\'' | b'\\' => { /* Ignore character close and backslash */ } | ||
186 | _ => assert_valid_char(&(byte as char).to_string()), | ||
187 | } | ||
188 | } | ||
189 | } | ||
190 | |||
191 | #[test] | ||
192 | fn test_unicode_codepoints() { | ||
193 | let valid = ["Ƒ", "バ", "メ", "﷽"]; | ||
194 | for c in &valid { | ||
195 | assert_valid_char(c); | ||
196 | } | ||
197 | } | ||
198 | |||
199 | #[test] | ||
200 | fn test_unicode_multiple_codepoints() { | ||
201 | let invalid = ["नी", "👨👨"]; | ||
202 | for c in &invalid { | ||
203 | assert_invalid_char(c); | ||
204 | } | ||
205 | } | ||
206 | |||
207 | #[test] | ||
208 | fn test_valid_ascii_escape() { | ||
209 | let valid = [ | ||
210 | r"\'", "\"", "\\\\", "\\\"", r"\n", r"\r", r"\t", r"\0", "a", "b", | ||
211 | ]; | ||
212 | for c in &valid { | ||
213 | assert_valid_char(c); | ||
214 | } | ||
215 | } | ||
216 | |||
217 | #[test] | ||
218 | fn test_invalid_ascii_escape() { | ||
219 | let invalid = [r"\a", r"\?", r"\"]; | ||
220 | for c in &invalid { | ||
221 | assert_invalid_char(c); | ||
222 | } | ||
223 | } | ||
224 | |||
225 | #[test] | ||
226 | fn test_valid_ascii_code_escape() { | ||
227 | let valid = [r"\x00", r"\x7F", r"\x55"]; | ||
228 | for c in &valid { | ||
229 | assert_valid_char(c); | ||
230 | } | ||
231 | } | ||
232 | |||
233 | #[test] | ||
234 | fn test_invalid_ascii_code_escape() { | ||
235 | let invalid = [r"\x", r"\x7", r"\xF0"]; | ||
236 | for c in &invalid { | ||
237 | assert_invalid_char(c); | ||
238 | } | ||
239 | } | ||
240 | |||
241 | #[test] | ||
242 | fn test_valid_unicode_escape() { | ||
243 | let valid = [ | ||
244 | r"\u{FF}", | ||
245 | r"\u{0}", | ||
246 | r"\u{F}", | ||
247 | r"\u{10FFFF}", | ||
248 | r"\u{1_0__FF___FF_____}", | ||
249 | ]; | ||
250 | for c in &valid { | ||
251 | assert_valid_char(c); | ||
252 | } | ||
253 | } | ||
254 | |||
255 | #[test] | ||
256 | fn test_invalid_unicode_escape() { | ||
257 | let invalid = [ | ||
258 | r"\u", | ||
259 | r"\u{}", | ||
260 | r"\u{", | ||
261 | r"\u{FF", | ||
262 | r"\u{FFFFFF}", | ||
263 | r"\u{_F}", | ||
264 | r"\u{00FFFFF}", | ||
265 | r"\u{110000}", | ||
266 | ]; | ||
267 | for c in &invalid { | ||
268 | assert_invalid_char(c); | ||
269 | } | ||
270 | } | ||
271 | } | ||