diff options
Diffstat (limited to 'lua/lexers/rstats.lua')
| -rw-r--r-- | lua/lexers/rstats.lua | 78 |
1 files changed, 38 insertions, 40 deletions
diff --git a/lua/lexers/rstats.lua b/lua/lexers/rstats.lua index ad9b5aa..165b693 100644 --- a/lua/lexers/rstats.lua +++ b/lua/lexers/rstats.lua @@ -1,53 +1,51 @@ --- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE. +-- Copyright 2006-2022 Mitchell. See LICENSE. -- R LPeg lexer. -local l = require('lexer') -local token, word_match = l.token, l.word_match -local P, R, S = lpeg.P, lpeg.R, lpeg.S +local lexer = require('lexer') +local token, word_match = lexer.token, lexer.word_match +local P, S = lpeg.P, lpeg.S -local M = {_NAME = 'rstats'} +local lex = lexer.new('rstats') -- Whitespace. -local ws = token(l.WHITESPACE, l.space^1) - --- Comments. -local comment = token(l.COMMENT, '#' * l.nonnewline^0) - --- Strings. -local sq_str = l.delimited_range("'", true) -local dq_str = l.delimited_range('"', true) -local string = token(l.STRING, sq_str + dq_str) - --- Numbers. -local number = token(l.NUMBER, (l.float + l.integer) * P('i')^-1) +lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1)) -- Keywords. -local keyword = token(l.KEYWORD, word_match{ - 'break', 'else', 'for', 'if', 'in', 'next', 'repeat', 'return', 'switch', - 'try', 'while', 'Inf', 'NA', 'NaN', 'NULL', 'FALSE', 'TRUE' -}) +lex:add_rule('keyword', token(lexer.KEYWORD, word_match{ + 'break', 'else', 'for', 'if', 'in', 'next', 'repeat', 'return', 'switch', 'try', 'while', -- + 'Inf', 'NA', 'NaN', 'NULL', 'FALSE', 'TRUE', 'F', 'T', + -- Frequently used operators. + '|>', '%%', '%*%', '%/%', '%in%', '%o%', '%x%' +})) -- Types. -local type = token(l.TYPE, word_match{ - 'array', 'character', 'complex', 'data.frame', 'double', 'factor', 'function', - 'integer', 'list', 'logical', 'matrix', 'numeric', 'vector' -}) +lex:add_rule('type', token(lexer.TYPE, word_match{ + 'array', 'character', 'closure', 'complex', 'data.frame', 'double', 'environment', 'expression', + 'externalptr', 'factor', 'function', 'integer', 'list', 'logical', 'matrix', 'numeric', + 'pairlist', 'promise', 'raw', 'symbol', 'vector' +})) -- Identifiers. -local identifier = token(l.IDENTIFIER, l.word) +lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word)) + +-- Strings. +local sq_str = lexer.range("'", true) +local dq_str = lexer.range('"', true) +lex:add_rule('string', token(lexer.STRING, sq_str + dq_str)) + +-- Comments. +lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#'))) + +-- Numbers. +lex:add_rule('number', token(lexer.NUMBER, (lexer.number * P('i')^-1) * P('L')^-1)) -- Operators. -local operator = token(l.OPERATOR, S('<->+*/^=.,:;|$()[]{}')) - -M._rules = { - {'whitespace', ws}, - {'keyword', keyword}, - {'type', type}, - {'identifier', identifier}, - {'string', string}, - {'comment', comment}, - {'number', number}, - {'operator', operator}, -} - -return M +lex:add_rule('operator', token(lexer.OPERATOR, S('<->+*/^=.,:;|$()[]{}'))) + +-- Folding +lex:add_fold_point(lexer.OPERATOR, '(', ')') +lex:add_fold_point(lexer.OPERATOR, '[', ']') +lex:add_fold_point(lexer.OPERATOR, '{', '}') +lex:add_fold_point(lexer.COMMENT, lexer.fold_consecutive_lines('#')) + +return lex |
