Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
Added long string support
Browse files Browse the repository at this point in the history
  • Loading branch information
Arghnews committed Feb 14, 2017
1 parent 85100bd commit 9a73f37
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
Binary file added .actual_grammar.swp
Binary file not shown.
Binary file added .baba.py.swp
Binary file not shown.
Binary file added .example.lua.swp
Binary file not shown.
21 changes: 21 additions & 0 deletions baba.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def tokenize(code):
token_specification = [
# Order in the Number matters, hex first
("Comment", r'--.*\n'), # only works on single line comment
("LongString", r'\[(?P<longString>=*)\[(.|\n)*?\](?P=longString)\]'),
("Number", r'0[xX]([0-9a-fA-F]*)(\.[0-9a-fA-F]+)([pP]-?[0-9]+)?|0[xX]([0-9a-fA-F]+)(\.[0-9a-fA-F]*)?([pP]-?[0-9]+)?|([0-9]+)(\.[0-9]*)?([eE]-?[0-9]+)?|([0-9]*)(\.[0-9]+)([eE]-?[0-9]+)?' ),
("Name", r'[_a-zA-Z][_a-zA-Z0-9]*'), # should be before keyword
("Keyword", keywords_regex),
Expand All @@ -50,6 +51,26 @@ def tokenize(code):
else:
if kind == "String" and value:
value = value[1:-1] # remove first and last chars
elif kind == "LongString" and value:
# strip [==[ from start of long string
num_square_brackets = 0
prefix_suffix = 0
for v in value:
prefix_suffix += 1
if v == "[":
num_square_brackets += 1
if num_square_brackets == 2:
value = value[prefix_suffix:]
# strip newline if first char
if value[0] == '\n':
value = value[1:]
break

# strip ]==] from end of long string
value = value[:len(value)-prefix_suffix]

kind = "String"
pass
elif kind == "Name" and value in keywords:
kind = "Keyword" # to convert keywords picked up as names to keywords
column = mo.start() - line_start
Expand Down
2 changes: 1 addition & 1 deletion example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end
--
-- The training loop and learning rate schedule
--

local a =
[====[
hi world
]====]
Expand Down

0 comments on commit 9a73f37

Please sign in to comment.