aboutsummaryrefslogtreecommitdiffhomepage
path: root/lexlua/ruby.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lexlua/ruby.lua')
-rw-r--r--lexlua/ruby.lua51
1 files changed, 25 insertions, 26 deletions
diff --git a/lexlua/ruby.lua b/lexlua/ruby.lua
index efa2a1779..e25ca6000 100644
--- a/lexlua/ruby.lua
+++ b/lexlua/ruby.lua
@@ -33,23 +33,22 @@ local word = (lexer.alpha + '_') * word_char^0
lex:add_rule('identifier', token(lexer.IDENTIFIER, word))
-- Comments.
-local line_comment = '#' * lexer.nonnewline_esc^0
-local block_comment = lexer.starts_line('=begin') *
- (lexer.any - lexer.newline * '=end')^0 *
- (lexer.newline * '=end')^-1
+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))
local delimiter_matches = {['('] = ')', ['['] = ']', ['{'] = '}'}
-local literal_delimitted = P(function(input, index)
+local literal_delimited = P(function(input, index)
local delimiter = input:sub(index, index)
if not delimiter:find('[%w\r\n\f\t ]') then -- only non alpha-numerics
local match_pos, patt
if delimiter_matches[delimiter] then
-- Handle nested delimiter/matches in strings.
local s, e = delimiter, delimiter_matches[delimiter]
- patt = lexer.delimited_range(s..e, false, false, true)
+ patt = lexer.range(s, e, false, true, true)
else
- patt = lexer.delimited_range(delimiter)
+ patt = lexer.range(delimiter)
end
match_pos = lpeg.match(patt, input, index)
return match_pos or #input + 1
@@ -57,29 +56,29 @@ local literal_delimitted = P(function(input, index)
end)
-- Strings.
-local cmd_str = lexer.delimited_range('`')
-local lit_cmd = '%x' * literal_delimitted
-local lit_array = '%w' * literal_delimitted
-local sq_str = lexer.delimited_range("'")
-local dq_str = lexer.delimited_range('"')
-local lit_str = '%' * S('qQ')^-1 * literal_delimitted
+local cmd_str = lexer.range('`')
+local lit_cmd = '%x' * literal_delimited
+local lit_array = '%w' * literal_delimited
+local sq_str = lexer.range("'")
+local dq_str = lexer.range('"')
+local lit_str = '%' * S('qQ')^-1 * literal_delimited
local heredoc = '<<' * P(function(input, index)
- local s, e, indented, _, delimiter =
- input:find('(%-?)(["`]?)([%a_][%w_]*)%2[\n\r\f;]+', 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]+')
- local _, e = input:find(end_heredoc..delimiter, e)
+ local _, 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 + lit_array) * S('f')^-1)
-- TODO: regex_str fails with `obj.method /patt/` syntax.
local regex_str = #P('/') * lexer.last_char_includes('!%^&*([{-=+|:;,?<>~') *
- lexer.delimited_range('/', true, false) * S('iomx')^0
-local lit_regex = '%r' * literal_delimitted * S('iomx')^0
-lex:add_rule('string', token(lexer.STRING, (sq_str + dq_str + lit_str +
- heredoc + cmd_str + lit_cmd +
- lit_array) * S('f')^-1) +
- token(lexer.REGEX, regex_str + lit_regex))
+ lexer.range('/', true) * S('iomx')^0
+local lit_regex = '%r' * literal_delimited * S('iomx')^0
+local regex = token(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
@@ -88,15 +87,15 @@ 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))
+ numeric_literal))
-- Variables.
local global_var = '$' * (word + S('!@L+`\'=~/\\,.;<>_*"$?:') + lexer.digit +
- '-' * S('0FadiIKlpvw'))
+ '-' * S('0FadiIKlpvw'))
local class_var = '@@' * word
local inst_var = '@' * word
lex:add_rule('variable', token(lexer.VARIABLE, global_var + class_var +
- inst_var))
+ inst_var))
-- Symbols.
lex:add_rule('symbol', token('symbol', ':' * P(function(input, index)
@@ -110,7 +109,7 @@ lex:add_rule('operator', token(lexer.OPERATOR, S('!%^&*()[]{}-=+/|:;.,?<>~')))
-- Fold points.
local function disambiguate(text, pos, line, s)
return line:sub(1, s - 1):match('^%s*$') and
- not text:sub(1, pos - 1):match('\\[ \t]*\r?\n$') and 1 or 0
+ not text:sub(1, pos - 1):match('\\[ \t]*\r?\n$') and 1 or 0
end
lex:add_fold_point(lexer.KEYWORD, 'begin', 'end')
lex:add_fold_point(lexer.KEYWORD, 'class', 'end')