diff options
Diffstat (limited to 'lua/lexers/ruby.lua')
| -rw-r--r-- | lua/lexers/ruby.lua | 79 |
1 files changed, 39 insertions, 40 deletions
diff --git a/lua/lexers/ruby.lua b/lua/lexers/ruby.lua index f6ba415..51334f2 100644 --- a/lua/lexers/ruby.lua +++ b/lua/lexers/ruby.lua @@ -1,42 +1,27 @@ --- Copyright 2006-2022 Mitchell. See LICENSE. +-- Copyright 2006-2024 Mitchell. See LICENSE. -- Ruby LPeg lexer. -local lexer = require('lexer') -local token, word_match = lexer.token, lexer.word_match +local lexer = lexer local P, S = lpeg.P, lpeg.S -local lex = lexer.new('ruby') - --- Whitespace. -lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1)) +local lex = lexer.new(...) -- Keywords. -lex:add_rule('keyword', token(lexer.KEYWORD, word_match{ - 'BEGIN', 'END', 'alias', 'and', 'begin', 'break', 'case', 'class', 'def', 'defined?', 'do', - 'else', 'elsif', 'end', 'ensure', 'false', 'for', 'if', 'in', 'module', 'next', 'nil', 'not', - 'or', 'redo', 'rescue', 'retry', 'return', 'self', 'super', 'then', 'true', 'undef', 'unless', - 'until', 'when', 'while', 'yield', '__FILE__', '__LINE__' -})) +lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD))) -- Functions. -lex:add_rule('function', token(lexer.FUNCTION, word_match{ - 'at_exit', 'autoload', 'binding', 'caller', 'catch', 'chop', 'chop!', 'chomp', 'chomp!', 'eval', - 'exec', 'exit', 'exit!', 'extend', 'fail', 'fork', 'format', 'gets', 'global_variables', 'gsub', - 'gsub!', 'include', 'iterator?', 'lambda', 'load', 'local_variables', 'loop', 'module_function', - 'open', 'p', 'print', 'printf', 'proc', 'putc', 'puts', 'raise', 'rand', 'readline', 'readlines', - 'require', 'require_relative', 'select', 'sleep', 'split', 'sprintf', 'srand', 'sub', 'sub!', - 'syscall', 'system', 'test', 'trace_var', 'trap', 'untrace_var' -}) * -S('.:|')) +local builtin_func = lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN)) +lex:add_rule('function', -lpeg.B('.') * builtin_func * -S('.:|')) -- Identifiers. local word_char = lexer.alnum + S('_!?') local word = (lexer.alpha + '_') * word_char^0 -lex:add_rule('identifier', token(lexer.IDENTIFIER, word)) +lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, word)) -- Comments. local line_comment = lexer.to_eol('#', true) local block_comment = lexer.range(lexer.starts_line('=begin'), lexer.starts_line('=end')) -lex:add_rule('comment', token(lexer.COMMENT, block_comment + line_comment)) +lex:add_rule('comment', lex:tag(lexer.COMMENT, block_comment + line_comment)) -- Strings. local delimiter_matches = {['('] = ')', ['['] = ']', ['{'] = '}'} @@ -66,42 +51,36 @@ local heredoc = '<<' * P(function(input, index) local s, e, indented, _, delimiter = input:find('([%-~]?)(["`]?)([%a_][%w_]*)%2[\n\r\f;]+', index) if s == index and delimiter then local end_heredoc = (#indented > 0 and '[\n\r\f]+ *' or '[\n\r\f]+') - e = select(2, input:find(end_heredoc .. delimiter, e)) + s, e = input:find(end_heredoc .. delimiter, e) return e and e + 1 or #input + 1 end end) -local string = token(lexer.STRING, (sq_str + dq_str + lit_str + heredoc + cmd_str + lit_cmd + +local string = lex:tag(lexer.STRING, (sq_str + dq_str + lit_str + heredoc + cmd_str + lit_cmd + lit_array) * S('f')^-1) -- TODO: regex_str fails with `obj.method /patt/` syntax. -local regex_str = - #P('/') * lexer.last_char_includes('!%^&*([{-=+|:;,?<>~') * lexer.range('/', true) * S('iomx')^0 +local regex_str = lexer.after_set('!%^&*([{-=+|:;,?<>~', lexer.range('/', true) * S('iomx')^0) local lit_regex = '%r' * literal_delimited * S('iomx')^0 -local regex = token(lexer.REGEX, regex_str + lit_regex) +local regex = lex:tag(lexer.REGEX, regex_str + lit_regex) lex:add_rule('string', string + regex) -- Numbers. -local dec = lexer.digit^1 * ('_' * lexer.digit^1)^0 * S('ri')^-1 -local bin = '0b' * S('01')^1 * ('_' * S('01')^1)^0 * -lexer.xdigit -local integer = S('+-')^-1 * (bin + lexer.hex_num + lexer.oct_num + dec) --- TODO: meta, control, etc. for numeric_literal. -local numeric_literal = '?' * (lexer.any - lexer.space) * -word_char -lex:add_rule('number', token(lexer.NUMBER, lexer.float * S('ri')^-1 + integer + numeric_literal)) +local numeric_literal = '?' * (lexer.any - lexer.space) * -word_char -- TODO: meta, control, etc. +lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number_('_') * S('ri')^-1 + numeric_literal)) -- Variables. local global_var = '$' * (word + S('!@L+`\'=~/\\,.;<>_*"$?:') + lexer.digit + '-' * S('0FadiIKlpvw')) local class_var = '@@' * word local inst_var = '@' * word -lex:add_rule('variable', token(lexer.VARIABLE, global_var + class_var + inst_var)) +lex:add_rule('variable', lex:tag(lexer.VARIABLE, global_var + class_var + inst_var)) -- Symbols. -lex:add_rule('symbol', token('symbol', ':' * P(function(input, index) - if input:sub(index - 2, index - 2) ~= ':' then return index end +lex:add_rule('symbol', lex:tag(lexer.STRING .. '.symbol', ':' * P(function(input, index) + if input:sub(index - 2, index - 2) ~= ':' then return true end end) * (word_char^1 + sq_str + dq_str))) -lex:add_style('symbol', lexer.styles.constant) -- Operators. -lex:add_rule('operator', token(lexer.OPERATOR, S('!%^&*()[]{}-=+/|:;.,?<>~'))) +lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('!%^&*()[]{}-=+/|:;.,?<>~'))) -- Fold points. local function disambiguate(text, pos, line, s) @@ -123,6 +102,26 @@ lex:add_fold_point(lexer.OPERATOR, '(', ')') lex:add_fold_point(lexer.OPERATOR, '[', ']') lex:add_fold_point(lexer.OPERATOR, '{', '}') lex:add_fold_point(lexer.COMMENT, '=begin', '=end') -lex:add_fold_point(lexer.COMMENT, lexer.fold_consecutive_lines('#')) + +-- Word lists. +lex:set_word_list(lexer.KEYWORD, { + 'BEGIN', 'END', 'alias', 'and', 'begin', 'break', 'case', 'class', 'def', 'defined?', 'do', + 'else', 'elsif', 'end', 'ensure', 'false', 'for', 'if', 'in', 'module', 'next', 'nil', 'not', + 'or', 'redo', 'rescue', 'retry', 'return', 'self', 'super', 'then', 'true', 'undef', 'unless', + 'until', 'when', 'while', 'yield', '__FILE__', '__LINE__' +}) + +lex:set_word_list(lexer.FUNCTION_BUILTIN, { + 'at_exit', 'autoload', 'binding', 'caller', 'catch', 'chop', 'chop!', 'chomp', 'chomp!', 'eval', + 'exec', 'exit', 'exit!', 'extend', 'fail', 'fork', 'format', 'gets', 'global_variables', 'gsub', + 'gsub!', 'include', 'iterator?', 'lambda', 'load', 'local_variables', 'loop', 'module_function', + 'open', 'p', 'print', 'printf', 'proc', 'putc', 'puts', 'raise', 'rand', 'readline', 'readlines', + 'require', 'require_relative', 'select', 'sleep', 'split', 'sprintf', 'srand', 'sub', 'sub!', + 'syscall', 'system', 'test', 'trace_var', 'trap', 'untrace_var' +}) + +lexer.property['scintillua.comment'] = '#' +lexer.property['scintillua.word.chars'] = + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_?!' return lex |
