build: set version to 0.5
[vis.git] / lua / lexers / antlr.lua
blob28fb2e1c7b48bec4cb55c65c8fe3cd7f75fb7f50
1 -- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- ANTLR LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S = lpeg.P, lpeg.R, lpeg.S
8 local M = {_NAME = 'antlr'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local line_comment = '//' * l.nonnewline^0
15 local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
16 local comment = token(l.COMMENT, line_comment + block_comment)
18 -- Strings.
19 local string = token(l.STRING, l.delimited_range("'", true))
21 -- Keywords.
22 local keyword = token(l.KEYWORD, word_match{
23 'abstract', 'break', 'case', 'catch', 'continue', 'default', 'do', 'else',
24 'extends', 'final', 'finally', 'for', 'if', 'implements', 'instanceof',
25 'native', 'new', 'private', 'protected', 'public', 'return', 'static',
26 'switch', 'synchronized', 'throw', 'throws', 'transient', 'try', 'volatile',
27 'while', 'package', 'import', 'header', 'options', 'tokens', 'strictfp',
28 'false', 'null', 'super', 'this', 'true'
31 -- Types.
32 local type = token(l.TYPE, word_match{
33 'boolean', 'byte', 'char', 'class', 'double', 'float', 'int', 'interface',
34 'long', 'short', 'void'
37 -- Functions.
38 local func = token(l.FUNCTION, 'assert')
40 -- Identifiers.
41 local identifier = token(l.IDENTIFIER, l.word)
43 -- Operators.
44 local operator = token(l.OPERATOR, S('$@:;|.=+*?~!^>-()[]{}'))
46 -- Actions.
47 local action = #P('{') * operator * token('action', (1 - P('}'))^0) *
48 (#P('}') * operator)^-1
50 M._rules = {
51 {'whitespace', ws},
52 {'keyword', keyword},
53 {'type', type},
54 {'function', func},
55 {'identifier', identifier},
56 {'string', string},
57 {'comment', comment},
58 {'action', action},
59 {'operator', operator},
62 M._tokenstyles = {
63 action = l.STYLE_NOTHING
66 M._foldsymbols = {
67 _patterns = {'[:;%(%){}]', '/%*', '%*/', '//'},
68 [l.OPERATOR] = {
69 [':'] = 1, [';'] = -1, ['('] = 1, [')'] = -1, ['{'] = 1, ['}'] = -1
71 [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}
74 return M