forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
antlr.lua
74 lines (59 loc) · 1.99 KB
/
antlr.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
-- ANTLR 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 M = {_NAME = 'antlr'}
-- Whitespace.
local ws = token(l.WHITESPACE, l.space^1)
-- Comments.
local line_comment = '//' * l.nonnewline^0
local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
local comment = token(l.COMMENT, line_comment + block_comment)
-- Strings.
local string = token(l.STRING, l.delimited_range("'", true))
-- Keywords.
local keyword = token(l.KEYWORD, word_match{
'abstract', 'break', 'case', 'catch', 'continue', 'default', 'do', 'else',
'extends', 'final', 'finally', 'for', 'if', 'implements', 'instanceof',
'native', 'new', 'private', 'protected', 'public', 'return', 'static',
'switch', 'synchronized', 'throw', 'throws', 'transient', 'try', 'volatile',
'while', 'package', 'import', 'header', 'options', 'tokens', 'strictfp',
'false', 'null', 'super', 'this', 'true'
})
-- Types.
local type = token(l.TYPE, word_match{
'boolean', 'byte', 'char', 'class', 'double', 'float', 'int', 'interface',
'long', 'short', 'void'
})
-- Functions.
local func = token(l.FUNCTION, 'assert')
-- Identifiers.
local identifier = token(l.IDENTIFIER, l.word)
-- Operators.
local operator = token(l.OPERATOR, S('$@:;|.=+*?~!^>-()[]{}'))
-- Actions.
local action = #P('{') * operator * token('action', (1 - P('}'))^0) *
(#P('}') * operator)^-1
M._rules = {
{'whitespace', ws},
{'keyword', keyword},
{'type', type},
{'function', func},
{'identifier', identifier},
{'string', string},
{'comment', comment},
{'action', action},
{'operator', operator},
}
M._tokenstyles = {
action = l.STYLE_NOTHING
}
M._foldsymbols = {
_patterns = {'[:;%(%){}]', '/%*', '%*/', '//'},
[l.OPERATOR] = {
[':'] = 1, [';'] = -1, ['('] = 1, [')'] = -1, ['{'] = 1, ['}'] = -1
},
[l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}
}
return M