Skip to content

Commit

Permalink
Handle single quoted strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanAlbert committed May 11, 2023
1 parent cc38bab commit 4182bd9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions dcs/lua/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def parse(self):
c = self.char()
if c == '{':
return self.object()
elif c == '"':
elif c in ('"', "'"):
return self.string()
elif c.isnumeric() or c == '-':
return self.number()
Expand Down Expand Up @@ -145,13 +145,15 @@ def str_function(self) -> str:
return s

def string(self) -> str:
if self.char() != '"':
if self.char() not in ('"', "'"):
se = SyntaxError()
se.lineno = self.lineno
se.offset = self.pos
se.text = "Expected character '\"', got '{char}'".format(char=self.char())
se.text = "Expected character '\"' or \"'\", got '{char}'".format(char=self.char())
raise se

terminator = self.char()

state = 0
s = ''
while state != 1:
Expand All @@ -160,7 +162,7 @@ def string(self) -> str:

c = self.char()
if state == 0:
if c == '"':
if c == terminator:
state = 1
self.pos += 1
elif c == '\\':
Expand Down
11 changes: 11 additions & 0 deletions dcs/lua/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ def test_block_comment(self) -> None:
)
self.assertEqual(r["foo"], "bar")

def test_single_quoted_string(self) -> None:
r = loads("name = 'foobar'")
self.assertEqual(r["name"], "foobar")

def test_mixed_quote_strings(self) -> None:
r = loads("name = 'foo \"bar\" baz'")
self.assertEqual(r["name"], 'foo "bar" baz')

r = loads("name = \"foo 'bar' baz\"")
self.assertEqual(r["name"], "foo 'bar' baz")


if __name__ == '__main__':
unittest.main()

0 comments on commit 4182bd9

Please sign in to comment.