vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
r.lua
(1597B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- R LPeg lexer.
3
4 local lexer = require('lexer')
5 local token, word_match = lexer.token, lexer.word_match
6 local P, S = lpeg.P, lpeg.S
7
8 local lex = lexer.new('r')
9
10 -- Whitespace.
11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
12
13 -- Keywords.
14 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
15 'break', 'else', 'for', 'if', 'in', 'next', 'repeat', 'return', 'switch', 'try', 'while', --
16 'Inf', 'NA', 'NaN', 'NULL', 'FALSE', 'TRUE', 'F', 'T',
17 -- Frequently used operators.
18 '|>', '%%', '%*%', '%/%', '%in%', '%o%', '%x%'
19 }))
20
21 -- Types.
22 lex:add_rule('type', token(lexer.TYPE, word_match{
23 'array', 'character', 'closure', 'complex', 'data.frame', 'double', 'environment', 'expression',
24 'externalptr', 'factor', 'function', 'integer', 'list', 'logical', 'matrix', 'numeric',
25 'pairlist', 'promise', 'raw', 'symbol', 'vector'
26 }))
27
28 -- Identifiers.
29 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
30
31 -- Strings.
32 local sq_str = lexer.range("'", true)
33 local dq_str = lexer.range('"', true)
34 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
35
36 -- Comments.
37 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#')))
38
39 -- Numbers.
40 lex:add_rule('number', token(lexer.NUMBER, (lexer.number * P('i')^-1) * P('L')^-1))
41
42 -- Operators.
43 lex:add_rule('operator', token(lexer.OPERATOR, S('<->+*/^=.,:;|$()[]{}')))
44
45 -- Folding
46 lex:add_fold_point(lexer.OPERATOR, '(', ')')
47 lex:add_fold_point(lexer.OPERATOR, '[', ']')
48 lex:add_fold_point(lexer.OPERATOR, '{', '}')
49
50 lexer.property['scintillua.comment'] = '#'
51
52 return lex