From fad15f79b1230b3076be515d6894c8919562809b Mon Sep 17 00:00:00 2001 From: mitchell Date: Sat, 25 Apr 2020 16:26:31 -0400 Subject: Reformatted Lua LPeg lexers and added new convenience functions and pattern. `lexer.range()` replaces `lexer.delimited_range()` and `lexer.nested_pair()`. `lexer.to_eol()` replaces `patt * lexer.nonnewline^0` constructs. `lexer.number` replaces `lexer.float + lexer.integer`. Also added unit tests for lexer functions. --- lexlua/moonscript.lua | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'lexlua/moonscript.lua') diff --git a/lexlua/moonscript.lua b/lexlua/moonscript.lua index 933130b74..d57cd55a9 100644 --- a/lexlua/moonscript.lua +++ b/lexlua/moonscript.lua @@ -110,24 +110,25 @@ lex:add_style('proper_ident', lexer.STYLE_CLASS) lex:add_style('tbl_key', lexer.STYLE_REGEX) local longstring = lpeg.Cmt('[' * lpeg.C(P('=')^0) * '[', - function(input, index, eq) - local _, e = input:find(']'..eq..']', index, true) - return (e or #input) + 1 - end) + function(input, index, eq) + local _, e = input:find(']' .. eq .. ']', index, true) + return (e or #input) + 1 + end) -- Strings. -local sq_str = lexer.delimited_range("'", false, true) -local dq_str = lexer.delimited_range('"', false, true) +local sq_str = lexer.range("'", false, false) +local dq_str = lexer.range('"', false, false) lex:add_rule('string', token(lexer.STRING, sq_str + dq_str) + - token('longstring', longstring)) + token('longstring', longstring)) lex:add_style('longstring', lexer.STYLE_STRING) -- Comments. -lex:add_rule('comment', token(lexer.COMMENT, '--' * (longstring + - lexer.nonnewline^0))) +local line_comment = lexer.to_eol('--') +local block_comment = '--' * longstring +lex:add_rule('comment', token(lexer.COMMENT, block_comment + line_comment)) -- Numbers. -lex:add_rule('number', token(lexer.NUMBER, lexer.float + lexer.integer)) +lex:add_rule('number', token(lexer.NUMBER, lexer.number)) -- Function definition. lex:add_rule('fndef', token('fndef', P('->') + '=>')) -- cgit v1.2.3