aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/string_lexing/string.rs
diff options
context:
space:
mode:
authorAdolfo OchagavĂ­a <[email protected]>2018-11-11 20:00:31 +0000
committerAdolfo OchagavĂ­a <[email protected]>2018-11-11 20:00:31 +0000
commitc96bfe7e2d4465653fe6b0eff053f0dfb48313fa (patch)
tree93c56d8301131a01de13b73010f615291eb1d6d4 /crates/ra_syntax/src/string_lexing/string.rs
parent30cd4d5acb7dfd40cea264a926d1c89f0c3522c3 (diff)
Split string lexing and run rustfmt
Diffstat (limited to 'crates/ra_syntax/src/string_lexing/string.rs')
-rw-r--r--crates/ra_syntax/src/string_lexing/string.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/string_lexing/string.rs b/crates/ra_syntax/src/string_lexing/string.rs
new file mode 100644
index 000000000..1b23029c6
--- /dev/null
+++ b/crates/ra_syntax/src/string_lexing/string.rs
@@ -0,0 +1,46 @@
1use super::parser::Parser;
2use super::StringComponent;
3
4pub fn parse_string_literal(src: &str) -> StringComponentIterator {
5 StringComponentIterator {
6 parser: Parser::new(src),
7 has_closing_quote: false,
8 }
9}
10
11pub struct StringComponentIterator<'a> {
12 parser: Parser<'a>,
13 pub has_closing_quote: bool,
14}
15
16impl<'a> Iterator for StringComponentIterator<'a> {
17 type Item = StringComponent;
18 fn next(&mut self) -> Option<StringComponent> {
19 if self.parser.pos == 0 {
20 assert!(
21 self.parser.advance() == '"',
22 "string literal should start with double quotes"
23 );
24 }
25
26 if let Some(component) = self.parser.parse_string_component() {
27 return Some(component);
28 }
29
30 // We get here when there are no char components left to parse
31 if self.parser.peek() == Some('"') {
32 self.parser.advance();
33 self.has_closing_quote = true;
34 }
35
36 assert!(
37 self.parser.peek() == None,
38 "string literal should leave no unparsed input: src = {}, pos = {}, length = {}",
39 self.parser.src,
40 self.parser.pos,
41 self.parser.src.len()
42 );
43
44 None
45 }
46}