Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle single quoted strings. #304

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()