diff options
Diffstat (limited to 'lua/lexers/antlr.lua')
| -rw-r--r-- | lua/lexers/antlr.lua | 90 |
1 files changed, 36 insertions, 54 deletions
diff --git a/lua/lexers/antlr.lua b/lua/lexers/antlr.lua index 28fb2e1..32c9a77 100644 --- a/lua/lexers/antlr.lua +++ b/lua/lexers/antlr.lua @@ -1,74 +1,56 @@ --- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE. +-- Copyright 2006-2022 Mitchell. 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 lexer = require('lexer') +local token, word_match = lexer.token, lexer.word_match +local P, S = lpeg.P, lpeg.S -local M = {_NAME = 'antlr'} +local lex = lexer.new('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)) +lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1)) -- 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' -}) +lex:add_rule('keyword', token(lexer.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' -}) +lex:add_rule('type', token(lexer.TYPE, word_match( + 'boolean byte char class double float int interface long short void'))) -- Functions. -local func = token(l.FUNCTION, 'assert') +lex:add_rule('func', token(lexer.FUNCTION, 'assert')) -- Identifiers. -local identifier = token(l.IDENTIFIER, l.word) +lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word)) --- Operators. -local operator = token(l.OPERATOR, S('$@:;|.=+*?~!^>-()[]{}')) +-- Comments. +local line_comment = lexer.to_eol('//') +local block_comment = lexer.range('/*', '*/') +lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment)) -- Actions. -local action = #P('{') * operator * token('action', (1 - P('}'))^0) * - (#P('}') * operator)^-1 +local open_brace = token(lexer.OPERATOR, '{') +local close_brace = token(lexer.OPERATOR, '}') +lex:add_rule('action', open_brace * token('action', (1 - P('}'))^0) * close_brace^-1) +lex:add_style('action', lexer.styles.nothing) -M._rules = { - {'whitespace', ws}, - {'keyword', keyword}, - {'type', type}, - {'function', func}, - {'identifier', identifier}, - {'string', string}, - {'comment', comment}, - {'action', action}, - {'operator', operator}, -} +-- Strings. +lex:add_rule('string', token(lexer.STRING, lexer.range("'", true))) -M._tokenstyles = { - action = l.STYLE_NOTHING -} +-- Operators. +lex:add_rule('operator', token(lexer.OPERATOR, S('$@:;|.=+*?~!^>-()[]{}'))) -M._foldsymbols = { - _patterns = {'[:;%(%){}]', '/%*', '%*/', '//'}, - [l.OPERATOR] = { - [':'] = 1, [';'] = -1, ['('] = 1, [')'] = -1, ['{'] = 1, ['}'] = -1 - }, - [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')} -} +-- Fold points. +lex:add_fold_point(lexer.OPERATOR, ':', ';') +lex:add_fold_point(lexer.OPERATOR, '(', ')') +lex:add_fold_point(lexer.OPERATOR, '{', '}') +lex:add_fold_point(lexer.COMMENT, '/*', '*/') +lex:add_fold_point(lexer.COMMENT, lexer.fold_consecutive_lines('//')) -return M +return lex |
