Skip to content

Commit

Permalink
Support of escaped characters
Browse files Browse the repository at this point in the history
  • Loading branch information
xatavian committed Jun 6, 2018
1 parent c3ab749 commit 5613ba4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
37 changes: 28 additions & 9 deletions lu6/scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,43 @@ def read_number_literal(self):
return result

def read_string_literal(self):
result = ""
try:
temp = self.next_char()

if temp != "\"":
raise InvalidLiteralException("Trying to tokenize a invalid string literal")
raise InvalidLiteralException("Trying to tokenize a invalid string literal", self.current_line)

while not ScannerHelper.is_string_literal(result):
if temp.startswith("\\"):
pass # TODO: handle escaped characters
result = "\""
temp = self.next_char()
while True:
if temp == "\n":
raise InvalidLiteralException("End of line was encountered in the middle of a string literal", self.current_line)
elif temp.startswith("\\"):
temp = self.next_char()
if temp == "n":
result += '\n'
elif temp == "\"":
result += "\""
elif temp == "t":
result += "\t"
elif temp == "r":
result += "\r"
elif temp == "a":
result += "\a"
elif temp == "\\":
result += "\\"
else:
raise InvalidLiteralException("Unrecognized escaped sequence was defined ({})".format("\\" + temp), self.current_line)
temp = self.next_char()
else:
result += temp
temp = self.next_char()

self.save_char(temp)
if temp != "\"":
temp = self.next_char()
else:
break
except EOFException:
if len(result) < 2 or result[0] != "\"" or result[-1] != "\"":
raise InvalidLiteralException("Encountered EOF while scanning a string literal")
raise InvalidLiteralException("Encountered EOF while scanning a string literal", self.current_line)

return result

Expand Down
5 changes: 4 additions & 1 deletion lu6/syntaxtree/literals/stringliteral.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@

class StringLiteral(Literal):
def codegen(self, output_stream, base_indent=0):
output_stream.print(self.value.image.strip("\""), "h_file", base_indent)
start_index = 1 if self.value.image[0] == "\"" else 0
end_index = -1 if self.value.image[-1] == "\"" else len(self.value.image)

output_stream.print(self.value.image[start_index:end_index], "h_file", base_indent)

0 comments on commit 5613ba4

Please sign in to comment.