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/wsf.lua | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'lexlua/wsf.lua') diff --git a/lexlua/wsf.lua b/lexlua/wsf.lua index 2d64356cc..dfa14b1eb 100644 --- a/lexlua/wsf.lua +++ b/lexlua/wsf.lua @@ -13,12 +13,11 @@ local ws = token(lexer.WHITESPACE, lexer.space^1) lex:add_rule('whitespace', ws) -- Comments. -lex:add_rule('comment', token(lexer.COMMENT, '')^0 * - P('-->')^-1)) +lex:add_rule('comment', token(lexer.COMMENT, lexer.range(''))) local alpha = R('az', 'AZ', '\127\255') -local word_char = lexer.alnum + S('_-:.??') -local identifier = (alpha + S('_-:.??')) * word_char^0 +local word_char = lexer.alnum + S('_-:.?') +local identifier = (alpha + S('_-:.?')) * word_char^0 -- Elements. local element = token('element', '<' * P('/')^-1 * identifier) @@ -47,14 +46,15 @@ local equals = token(lexer.OPERATOR, '=') * in_tag lex:add_rule('equals', equals) -- Strings. +local sq_str = lexer.range("'", false, false) +local dq_str = lexer.range('"', false, false) local string = #S('\'"') * lexer.last_char_includes('=') * - token(lexer.STRING, lexer.delimited_range("'", false, true) + - lexer.delimited_range('"', false, true)) + token(lexer.STRING, sq_str + dq_str) lex:add_rule('string', string) -- Numbers. lex:add_rule('number', #lexer.digit * lexer.last_char_includes('=') * - token(lexer.NUMBER, lexer.digit^1 * P('%')^-1) * in_tag) + token(lexer.NUMBER, lexer.digit^1 * P('%')^-1) * in_tag) -- Entities. lex:add_rule('entity', token('entity', '&' * word_match[[ @@ -74,8 +74,7 @@ lex:add_fold_point(lexer.COMMENT, '') -- Tags that start embedded languages. local embed_start_tag = element * - (ws^1 * attribute * ws^0 * equals * ws^0 * string)^0 * - ws^0 * tag_close + (ws^1 * attribute * ws^0 * equals * ws^0 * string)^0 * ws^0 * tag_close local embed_end_tag = element * tag_close -- Embedded JavaScript. -- cgit v1.2.3