diff options
Diffstat (limited to 'crates/ra_syntax/src/lexer/strings.rs')
-rw-r--r-- | crates/ra_syntax/src/lexer/strings.rs | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/lexer/strings.rs b/crates/ra_syntax/src/lexer/strings.rs new file mode 100644 index 000000000..5ff483d14 --- /dev/null +++ b/crates/ra_syntax/src/lexer/strings.rs | |||
@@ -0,0 +1,123 @@ | |||
1 | use SyntaxKind::{self, *}; | ||
2 | |||
3 | use lexer::ptr::Ptr; | ||
4 | |||
5 | pub(crate) fn is_string_literal_start(c: char, c1: Option<char>, c2: Option<char>) -> bool { | ||
6 | match (c, c1, c2) { | ||
7 | ('r', Some('"'), _) | ||
8 | | ('r', Some('#'), _) | ||
9 | | ('b', Some('"'), _) | ||
10 | | ('b', Some('\''), _) | ||
11 | | ('b', Some('r'), Some('"')) | ||
12 | | ('b', Some('r'), Some('#')) => true, | ||
13 | _ => false, | ||
14 | } | ||
15 | } | ||
16 | |||
17 | pub(crate) fn scan_char(ptr: &mut Ptr) { | ||
18 | while let Some(c) = ptr.current() { | ||
19 | match c { | ||
20 | '\\' => { | ||
21 | ptr.bump(); | ||
22 | if ptr.at('\\') || ptr.at('\'') { | ||
23 | ptr.bump(); | ||
24 | } | ||
25 | } | ||
26 | '\'' => { | ||
27 | ptr.bump(); | ||
28 | return; | ||
29 | } | ||
30 | '\n' => return, | ||
31 | _ => { | ||
32 | ptr.bump(); | ||
33 | } | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | |||
38 | pub(crate) fn scan_byte_char_or_string(ptr: &mut Ptr) -> SyntaxKind { | ||
39 | // unwrapping and not-exhaustive match are ok | ||
40 | // because of string_literal_start | ||
41 | let c = ptr.bump().unwrap(); | ||
42 | match c { | ||
43 | '\'' => { | ||
44 | scan_byte(ptr); | ||
45 | BYTE | ||
46 | } | ||
47 | '"' => { | ||
48 | scan_byte_string(ptr); | ||
49 | BYTE_STRING | ||
50 | } | ||
51 | 'r' => { | ||
52 | scan_raw_byte_string(ptr); | ||
53 | RAW_BYTE_STRING | ||
54 | } | ||
55 | _ => unreachable!(), | ||
56 | } | ||
57 | } | ||
58 | |||
59 | pub(crate) fn scan_string(ptr: &mut Ptr) { | ||
60 | while let Some(c) = ptr.current() { | ||
61 | match c { | ||
62 | '\\' => { | ||
63 | ptr.bump(); | ||
64 | if ptr.at('\\') || ptr.at('"') { | ||
65 | ptr.bump(); | ||
66 | } | ||
67 | } | ||
68 | '"' => { | ||
69 | ptr.bump(); | ||
70 | return; | ||
71 | } | ||
72 | _ => { | ||
73 | ptr.bump(); | ||
74 | }, | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | |||
79 | pub(crate) fn scan_raw_string(ptr: &mut Ptr) { | ||
80 | let mut hashes = 0; | ||
81 | while ptr.at('#') { | ||
82 | hashes += 1; | ||
83 | ptr.bump(); | ||
84 | } | ||
85 | if !ptr.at('"') { | ||
86 | return; | ||
87 | } | ||
88 | ptr.bump(); | ||
89 | |||
90 | while let Some(c) = ptr.bump() { | ||
91 | if c == '"' { | ||
92 | let mut hashes_left = hashes; | ||
93 | while ptr.at('#') && hashes_left > 0{ | ||
94 | hashes_left -= 1; | ||
95 | ptr.bump(); | ||
96 | } | ||
97 | if hashes_left == 0 { | ||
98 | return; | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | |||
104 | fn scan_byte(ptr: &mut Ptr) { | ||
105 | scan_char(ptr) | ||
106 | } | ||
107 | |||
108 | fn scan_byte_string(ptr: &mut Ptr) { | ||
109 | scan_string(ptr) | ||
110 | } | ||
111 | |||
112 | fn scan_raw_byte_string(ptr: &mut Ptr) { | ||
113 | if !ptr.at('"') { | ||
114 | return; | ||
115 | } | ||
116 | ptr.bump(); | ||
117 | |||
118 | while let Some(c) = ptr.bump() { | ||
119 | if c == '"' { | ||
120 | return; | ||
121 | } | ||
122 | } | ||
123 | } | ||