Use tail recursion to skip spaces and comments
I think this looks nicer than the other version with the separate loop.
@@ -52,7 +52,7 @@ end
local symbol = P(false)
do
- -- Ordered by decreasing length, to follow the longest match rule.
+ -- Ordered by decreasing length, to prioritize the longest match.
local strs = "... .. // << >> == ~= <= >= :: -> + - * / % ^ & | ~ < > = ( ) [ ] { } ; , . :"
for s in string.gmatch(strs, "%S+") do
symbol = symbol + P(s)
@@ -197,9 +197,9 @@ end
function Lexer:next()
- while true do
if self:try(space) then
- -- skip
+ return self:next()
+
elseif self:try("--") then
if self:try(longstring_open) then
local s = self:read_long_string(#self.matched)
@@ -207,12 +207,9 @@ function Lexer:next()
else
self:try(comment_line)
end
- else
- break
- end
- end
+ return self:next()
- if self:try(string_delimiter) then
+ elseif self:try(string_delimiter) then
local s, err = self:read_short_string(self.matched)
if not s then return false, err end
return "STRING", s
