aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/string_lexing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/string_lexing.rs')
-rw-r--r--crates/ra_syntax/src/string_lexing.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/string_lexing.rs b/crates/ra_syntax/src/string_lexing.rs
index d613bb042..4e8c3a91c 100644
--- a/crates/ra_syntax/src/string_lexing.rs
+++ b/crates/ra_syntax/src/string_lexing.rs
@@ -63,6 +63,56 @@ impl<'a> Iterator for StringComponentIterator<'a> {
63 } 63 }
64} 64}
65 65
66pub fn parse_byte_literal(src: &str) -> ByteComponentIterator {
67 ByteComponentIterator {
68 parser: Parser::new(src),
69 has_closing_quote: false,
70 }
71}
72
73pub struct ByteComponentIterator<'a> {
74 parser: Parser<'a>,
75 pub has_closing_quote: bool,
76}
77
78impl<'a> Iterator for ByteComponentIterator<'a> {
79 type Item = CharComponent;
80 fn next(&mut self) -> Option<CharComponent> {
81 if self.parser.pos == 0 {
82 assert!(
83 self.parser.advance() == 'b',
84 "Byte literal should start with a b"
85 );
86
87 assert!(
88 self.parser.advance() == '\'',
89 "Byte literal should start with a b, followed by a quote"
90 );
91 }
92
93
94 if let Some(component) = self.parser.parse_char_component() {
95 return Some(component);
96 }
97
98 // We get here when there are no char components left to parse
99 if self.parser.peek() == Some('\'') {
100 self.parser.advance();
101 self.has_closing_quote = true;
102 }
103
104 assert!(
105 self.parser.peek() == None,
106 "byte literal should leave no unparsed input: src = {}, pos = {}, length = {}",
107 self.parser.src,
108 self.parser.pos,
109 self.parser.src.len()
110 );
111
112 None
113 }
114}
115
66pub fn parse_char_literal(src: &str) -> CharComponentIterator { 116pub fn parse_char_literal(src: &str) -> CharComponentIterator {
67 CharComponentIterator { 117 CharComponentIterator {
68 parser: Parser::new(src), 118 parser: Parser::new(src),