vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
antlr.lua
(1913B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- ANTLR 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('antlr')
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 'abstract', 'break', 'case', 'catch', 'continue', 'default', 'do', 'else', 'extends', 'final',
16 'finally', 'for', 'if', 'implements', 'instanceof', 'native', 'new', 'private', 'protected',
17 'public', 'return', 'static', 'switch', 'synchronized', 'throw', 'throws', 'transient', 'try',
18 'volatile', 'while', 'package', 'import', 'header', 'options', 'tokens', 'strictfp', 'false',
19 'null', 'super', 'this', 'true'
20 }))
21
22 -- Types.
23 lex:add_rule('type', token(lexer.TYPE, word_match(
24 'boolean byte char class double float int interface long short void')))
25
26 -- Functions.
27 lex:add_rule('func', token(lexer.FUNCTION, 'assert'))
28
29 -- Identifiers.
30 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
31
32 -- Comments.
33 local line_comment = lexer.to_eol('//')
34 local block_comment = lexer.range('/*', '*/')
35 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
36
37 -- Actions.
38 local open_brace = token(lexer.OPERATOR, '{')
39 local close_brace = token(lexer.OPERATOR, '}')
40 lex:add_rule('action', open_brace * token('action', (1 - P('}'))^0) * close_brace^-1)
41 lex:add_style('action', lexer.styles.nothing)
42
43 -- Strings.
44 lex:add_rule('string', token(lexer.STRING, lexer.range("'", true)))
45
46 -- Operators.
47 lex:add_rule('operator', token(lexer.OPERATOR, S('$@:;|.=+*?~!^>-()[]{}')))
48
49 -- Fold points.
50 lex:add_fold_point(lexer.OPERATOR, ':', ';')
51 lex:add_fold_point(lexer.OPERATOR, '(', ')')
52 lex:add_fold_point(lexer.OPERATOR, '{', '}')
53 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
54
55 lexer.property['scintillua.comment'] = '//'
56
57 return lex