aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/validation/byte_string.rs
diff options
context:
space:
mode:
authorAdolfo Ochagavía <[email protected]>2018-11-11 19:41:43 +0000
committerAdolfo Ochagavía <[email protected]>2018-11-11 19:41:43 +0000
commit30cd4d5acb7dfd40cea264a926d1c89f0c3522c3 (patch)
treed71024919de98d74bf1a6bedc8a755c8810f1723 /crates/ra_syntax/src/validation/byte_string.rs
parentc258b4fdb0e421813330c2428985c4537c787582 (diff)
Validate byte string literals
Diffstat (limited to 'crates/ra_syntax/src/validation/byte_string.rs')
-rw-r--r--crates/ra_syntax/src/validation/byte_string.rs178
1 files changed, 178 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/validation/byte_string.rs b/crates/ra_syntax/src/validation/byte_string.rs
new file mode 100644
index 000000000..7b830e97c
--- /dev/null
+++ b/crates/ra_syntax/src/validation/byte_string.rs
@@ -0,0 +1,178 @@
1use crate::{
2 ast::{self, AstNode},
3 string_lexing::{self, StringComponentKind},
4 yellow::{
5 SyntaxError,
6 SyntaxErrorKind::*,
7 },
8};
9
10use super::byte;
11
12pub(crate) fn validate_byte_string_node(node: ast::ByteString, errors: &mut Vec<SyntaxError>) {
13 let literal_text = node.text();
14 let literal_range = node.syntax().range();
15 let mut components = string_lexing::parse_byte_string_literal(literal_text);
16 for component in &mut components {
17 let range = component.range + literal_range.start();
18
19 match component.kind {
20 StringComponentKind::Char(kind) => {
21 // Chars must escape \t, \n and \r codepoints, but strings don't
22 let text = &literal_text[component.range];
23 match text {
24 "\t" | "\n" | "\r" => { /* always valid */ }
25 _ => byte::validate_byte_component(text, kind, range, errors),
26 }
27 }
28 StringComponentKind::IgnoreNewline => { /* always valid */ }
29 }
30 }
31
32 if !components.has_closing_quote {
33 errors.push(SyntaxError::new(UnclosedString, literal_range));
34 }
35}
36
37#[cfg(test)]
38mod test {
39 use crate::SourceFileNode;
40
41 fn build_file(literal: &str) -> SourceFileNode {
42 let src = format!(r#"const S: &'static [u8] = b"{}";"#, literal);
43 println!("Source: {}", src);
44 SourceFileNode::parse(&src)
45 }
46
47 fn assert_valid_str(literal: &str) {
48 let file = build_file(literal);
49 assert!(
50 file.errors().len() == 0,
51 "Errors for literal '{}': {:?}",
52 literal,
53 file.errors()
54 );
55 }
56
57 fn assert_invalid_str(literal: &str) {
58 let file = build_file(literal);
59 assert!(file.errors().len() > 0);
60 }
61
62 #[test]
63 fn test_ansi_codepoints() {
64 for byte in 0..128 {
65 match byte {
66 b'\"' | b'\\' => { /* Ignore string close and backslash */ }
67 _ => assert_valid_str(&(byte as char).to_string()),
68 }
69 }
70
71 for byte in 128..=255u8 {
72 assert_invalid_str(&(byte as char).to_string());
73 }
74 }
75
76 #[test]
77 fn test_unicode_codepoints() {
78 let invalid = ["Ƒ", "バ", "メ", "﷽"];
79 for c in &invalid {
80 assert_invalid_str(c);
81 }
82 }
83
84 #[test]
85 fn test_unicode_multiple_codepoints() {
86 let invalid = ["नी", "👨‍👨‍"];
87 for c in &invalid {
88 assert_invalid_str(c);
89 }
90 }
91
92 #[test]
93 fn test_valid_ascii_escape() {
94 let valid = [r"\'", r#"\""#, r"\\", r"\n", r"\r", r"\t", r"\0", "a", "b"];
95 for c in &valid {
96 assert_valid_str(c);
97 }
98 }
99
100 #[test]
101 fn test_invalid_ascii_escape() {
102 let invalid = [r"\a", r"\?", r"\"];
103 for c in &invalid {
104 assert_invalid_str(c);
105 }
106 }
107
108 #[test]
109 fn test_valid_ascii_code_escape() {
110 let valid = [r"\x00", r"\x7F", r"\x55", r"\xF0"];
111 for c in &valid {
112 assert_valid_str(c);
113 }
114 }
115
116 #[test]
117 fn test_invalid_ascii_code_escape() {
118 let invalid = [r"\x", r"\x7"];
119 for c in &invalid {
120 assert_invalid_str(c);
121 }
122 }
123
124 #[test]
125 fn test_invalid_unicode_escape() {
126 let well_formed = [
127 r"\u{FF}",
128 r"\u{0}",
129 r"\u{F}",
130 r"\u{10FFFF}",
131 r"\u{1_0__FF___FF_____}",
132 ];
133 for c in &well_formed {
134 assert_invalid_str(c);
135 }
136
137 let invalid = [
138 r"\u",
139 r"\u{}",
140 r"\u{",
141 r"\u{FF",
142 r"\u{FFFFFF}",
143 r"\u{_F}",
144 r"\u{00FFFFF}",
145 r"\u{110000}",
146 ];
147 for c in &invalid {
148 assert_invalid_str(c);
149 }
150 }
151
152 #[test]
153 fn test_mixed_invalid() {
154 assert_invalid_str(
155 r"This is the tale of a string
156with a newline in between, some emoji (👨‍👨‍) here and there,
157unicode escapes like this: \u{1FFBB} and weird stuff like
158this ﷽",
159 );
160 }
161
162 #[test]
163 fn test_mixed_valid() {
164 assert_valid_str(
165 r"This is the tale of a string
166with a newline in between, no emoji at all,
167nor unicode escapes or weird stuff",
168 );
169 }
170
171 #[test]
172 fn test_ignore_newline() {
173 assert_valid_str(
174 "Hello \
175 World",
176 );
177 }
178}