diff options
Diffstat (limited to 'lua/lexers/scala.lua')
| -rw-r--r-- | lua/lexers/scala.lua | 101 |
1 files changed, 43 insertions, 58 deletions
diff --git a/lua/lexers/scala.lua b/lua/lexers/scala.lua index 96fe344..87c9095 100644 --- a/lua/lexers/scala.lua +++ b/lua/lexers/scala.lua @@ -1,75 +1,60 @@ --- Copyright 2006-2017 JMS. See LICENSE. --- Scala LPeg Lexer. +-- Copyright 2006-2022 JMS. See LICENSE. +-- Scala 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 = 'scala'} +local lex = lexer.new('scala') -- Whitespace. -local ws = token(l.WHITESPACE, l.space^1) +local ws = token(lexer.WHITESPACE, lexer.space^1) +lex:add_rule('whitespace', ws) --- Comments. -local line_comment = '//' * l.nonnewline_esc^0 -local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1 -local comment = token(l.COMMENT, line_comment + block_comment) - --- Strings. -local symbol = "'" * l.word -local dq_str = l.delimited_range('"', true) -local tq_str = '"""' * (l.any - '"""')^0 * P('"""')^-1 -local string = token(l.STRING, tq_str + symbol + dq_str) - --- Numbers. -local number = token(l.NUMBER, (l.float + l.integer) * S('LlFfDd')^-1) +-- Classes. +lex:add_rule('class', token(lexer.KEYWORD, 'class') * ws^1 * token(lexer.CLASS, lexer.word)) -- Keywords. -local keyword = token(l.KEYWORD, word_match{ - 'abstract', 'case', 'catch', 'class', 'def', 'do', 'else', 'extends', 'false', - 'final', 'finally', 'for', 'forSome', 'if', 'implicit', 'import', 'lazy', - 'match', 'new', 'null', 'object', 'override', 'package', 'private', - 'protected', 'return', 'sealed', 'super', 'this', 'throw', 'trait', 'try', - 'true', 'type', 'val', 'var', 'while', 'with', 'yield' -}) +lex:add_rule('keyword', token(lexer.KEYWORD, word_match{ + 'abstract', 'case', 'catch', 'class', 'def', 'do', 'else', 'extends', 'false', 'final', 'finally', + 'for', 'forSome', 'if', 'implicit', 'import', 'lazy', 'match', 'new', 'null', 'object', + 'override', 'package', 'private', 'protected', 'return', 'sealed', 'super', 'this', 'throw', + 'trait', 'try', 'true', 'type', 'val', 'var', 'while', 'with', 'yield' +})) -- Types. -local type = token(l.TYPE, word_match{ - 'Array', 'Boolean', 'Buffer', 'Byte', 'Char', 'Collection', 'Double', 'Float', - 'Int', 'Iterator', 'LinkedList', 'List', 'Long', 'Map', 'None', 'Option', - 'Set', 'Short', 'SortedMap', 'SortedSet', 'String', 'TreeMap', 'TreeSet' -}) +lex:add_rule('type', token(lexer.TYPE, word_match{ + 'Array', 'Boolean', 'Buffer', 'Byte', 'Char', 'Collection', 'Double', 'Float', 'Int', 'Iterator', + 'LinkedList', 'List', 'Long', 'Map', 'None', 'Option', 'Set', 'Short', 'SortedMap', 'SortedSet', + 'String', 'TreeMap', 'TreeSet' +})) + +-- Functions. +lex:add_rule('function', token(lexer.FUNCTION, lexer.word) * #P('(')) -- Identifiers. -local identifier = token(l.IDENTIFIER, l.word) +lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word)) --- Operators. -local operator = token(l.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}')) +-- Strings. +local symbol = "'" * lexer.word +local dq_str = lexer.range('"', true) +local tq_str = lexer.range('"""') +lex:add_rule('string', token(lexer.STRING, tq_str + symbol + dq_str)) --- Functions. -local func = token(l.FUNCTION, l.word) * #P('(') +-- Comments. +local line_comment = lexer.to_eol('//', true) +local block_comment = lexer.range('/*', '*/') +lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment)) --- Classes. -local class_sequence = token(l.KEYWORD, P('class')) * ws^1 * - token(l.CLASS, l.word) +-- Numbers. +lex:add_rule('number', token(lexer.NUMBER, lexer.number * S('LlFfDd')^-1)) -M._rules = { - {'whitespace', ws}, - {'class', class_sequence}, - {'keyword', keyword}, - {'type', type}, - {'function', func}, - {'identifier', identifier}, - {'string', string}, - {'comment', comment}, - {'number', number}, - {'operator', operator}, -} +-- Operators. +lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}'))) -M._foldsymbols = { - _patterns = {'[{}]', '/%*', '%*/', '//'}, - [l.OPERATOR] = {['{'] = 1, ['}'] = -1}, - [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')} -} +-- Fold points. +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 |
