vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
groovy.lua
(2812B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Groovy 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('groovy')
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', 'strictfp', 'package', 'import', 'as', 'assert', 'def', 'mixin', 'property',
19 'test', 'using', 'in', 'false', 'null', 'super', 'this', 'true', 'it'
20 }))
21
22 -- Functions.
23 lex:add_rule('function', token(lexer.FUNCTION, word_match{
24 'abs', 'any', 'append', 'asList', 'asWritable', 'call', 'collect', 'compareTo', 'count', 'div',
25 'dump', 'each', 'eachByte', 'eachFile', 'eachLine', 'every', 'find', 'findAll', 'flatten',
26 'getAt', 'getErr', 'getIn', 'getOut', 'getText', 'grep', 'immutable', 'inject', 'inspect',
27 'intersect', 'invokeMethods', 'isCase', 'join', 'leftShift', 'minus', 'multiply',
28 'newInputStream', 'newOutputStream', 'newPrintWriter', 'newReader', 'newWriter', 'next', 'plus',
29 'pop', 'power', 'previous', 'print', 'println', 'push', 'putAt', 'read', 'readBytes', 'readLines',
30 'reverse', 'reverseEach', 'round', 'size', 'sort', 'splitEachLine', 'step', 'subMap', 'times',
31 'toInteger', 'toList', 'tokenize', 'upto', 'waitForOrKill', 'withPrintWriter', 'withReader',
32 'withStream', 'withWriter', 'withWriterAppend', 'write', 'writeLine'
33 }))
34
35 -- Types.
36 lex:add_rule('type', token(lexer.TYPE, word_match(
37 'boolean byte char class double float int interface long short void')))
38
39 -- Identifiers.
40 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
41
42 -- Comments.
43 local line_comment = lexer.to_eol('//', true)
44 local block_comment = lexer.range('/*', '*/')
45 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
46
47 -- Strings.
48 local sq_str = lexer.range("'")
49 local dq_str = lexer.range('"')
50 local tq_str = lexer.range("'''") + lexer.range('"""')
51 local string = token(lexer.STRING, tq_str + sq_str + dq_str)
52 local regex_str = lexer.after_set('=~|!<>+-*?&,:;([{', lexer.range('/', true))
53 local regex = token(lexer.REGEX, regex_str)
54 lex:add_rule('string', string + regex)
55
56 -- Numbers.
57 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
58
59 -- Operators.
60 lex:add_rule('operator', token(lexer.OPERATOR, S('=~|!<>+-/*?&.,:;()[]{}')))
61
62 -- Fold points.
63 lex:add_fold_point(lexer.OPERATOR, '{', '}')
64 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
65
66 lexer.property['scintillua.comment'] = '//'
67
68 return lex