Merge pull request #579 from anthrotype/fealib-newline-in-string

fealib: allow newlines in string literals (and skip over them)
This commit is contained in:
Cosimo Lupo 2016-04-18 09:39:53 +01:00
commit b149bd51db
2 changed files with 7 additions and 3 deletions

View File

@ -142,10 +142,12 @@ class Lexer(object):
return (Lexer.SYMBOL, cur_char, location)
if cur_char == '"':
self.pos_ += 1
self.scan_until_('"\r\n')
self.scan_until_('"')
if self.pos_ < self.text_length_ and self.text_[self.pos_] == '"':
self.pos_ += 1
return (Lexer.STRING, text[start + 1:self.pos_ - 1], location)
# strip newlines embedded within a string
string = re.sub("[\r\n]", "", text[start + 1:self.pos_ - 1])
return (Lexer.STRING, string, location)
else:
raise FeatureLibError("Expected '\"' to terminate string",
location)

View File

@ -82,7 +82,9 @@ class LexerTest(unittest.TestCase):
def test_string(self):
self.assertEqual(lex('"foo" "bar"'),
[(Lexer.STRING, "foo"), (Lexer.STRING, "bar")])
self.assertRaises(FeatureLibError, lambda: lex('"foo\n bar"'))
self.assertEqual(lex('"foo \nbar\r baz \r\nqux\n\n "'),
[(Lexer.STRING, "foo bar baz qux ")])
self.assertRaises(FeatureLibError, lambda: lex('"foo\n bar'))
def test_bad_character(self):
self.assertRaises(FeatureLibError, lambda: lex("123 \u0001"))