Skip to content

Commit 3861fd8

Browse files
author
Daniel Watkins
committed
Implement triple-quotes for strings
This fixes #33.
1 parent dc55639 commit 3861fd8

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

parser/src/lexer.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ impl<'input> Lexer<'input> {
139139
let mut string_content = String::new();
140140
let start_pos = self.location;
141141

142+
// If the next two characters are also the quote character, then we have a triple-quoted
143+
// string; consume those two characters and ensure that we require a triple-quote to close
144+
let triple_quoted = if self.chr0 == Some(quote_char) && self.chr1 == Some(quote_char) {
145+
self.next_char();
146+
self.next_char();
147+
true
148+
} else {
149+
false
150+
};
151+
142152
loop {
143153
match self.next_char() {
144154
Some('\\') => {
@@ -198,7 +208,19 @@ impl<'input> Lexer<'input> {
198208
}
199209
Some(c) => {
200210
if c == quote_char {
201-
break;
211+
if triple_quoted {
212+
// Look ahead at the next two characters; if we have two more
213+
// quote_chars, it's the end of the string; consume the remaining
214+
// closing quotes and break the loop
215+
if self.chr0 == Some(quote_char) && self.chr1 == Some(quote_char) {
216+
self.next_char();
217+
self.next_char();
218+
break;
219+
}
220+
string_content.push(c);
221+
} else {
222+
break;
223+
}
202224
} else {
203225
string_content.push(c);
204226
}

tests/snippets/strings.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
assert "a" == 'a'
2+
assert """a""" == "a"
3+
assert len(""" " "" " "" """) == 11
4+
assert "\"" == '"'
5+
assert "\"" == """\""""
6+
7+
assert "\n" == """
8+
"""
9+
10+
assert len(""" " \" """) == 5

0 commit comments

Comments
 (0)