vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

scala.lua

(2019B)


      1 -- Copyright 2006-2025 JMS. See LICENSE.
      2 -- Scala 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('scala')
      9 
     10 -- Whitespace.
     11 local ws = token(lexer.WHITESPACE, lexer.space^1)
     12 lex:add_rule('whitespace', ws)
     13 
     14 -- Classes.
     15 lex:add_rule('class', token(lexer.KEYWORD, 'class') * ws^1 * token(lexer.CLASS, lexer.word))
     16 
     17 -- Keywords.
     18 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
     19 	'abstract', 'case', 'catch', 'class', 'def', 'do', 'else', 'extends', 'false', 'final', 'finally',
     20 	'for', 'forSome', 'if', 'implicit', 'import', 'lazy', 'match', 'new', 'null', 'object',
     21 	'override', 'package', 'private', 'protected', 'return', 'sealed', 'super', 'this', 'throw',
     22 	'trait', 'try', 'true', 'type', 'val', 'var', 'while', 'with', 'yield'
     23 }))
     24 
     25 -- Types.
     26 lex:add_rule('type', token(lexer.TYPE, word_match{
     27 	'Array', 'Boolean', 'Buffer', 'Byte', 'Char', 'Collection', 'Double', 'Float', 'Int', 'Iterator',
     28 	'LinkedList', 'List', 'Long', 'Map', 'None', 'Option', 'Set', 'Short', 'SortedMap', 'SortedSet',
     29 	'String', 'TreeMap', 'TreeSet'
     30 }))
     31 
     32 -- Functions.
     33 lex:add_rule('function', token(lexer.FUNCTION, lexer.word) * #P('('))
     34 
     35 -- Identifiers.
     36 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     37 
     38 -- Strings.
     39 local symbol = "'" * lexer.word
     40 local dq_str = lexer.range('"', true)
     41 local tq_str = lexer.range('"""')
     42 lex:add_rule('string', token(lexer.STRING, tq_str + symbol + dq_str))
     43 
     44 -- Comments.
     45 local line_comment = lexer.to_eol('//', true)
     46 local block_comment = lexer.range('/*', '*/')
     47 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     48 
     49 -- Numbers.
     50 lex:add_rule('number', token(lexer.NUMBER, lexer.number * S('LlFfDd')^-1))
     51 
     52 -- Operators.
     53 lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}')))
     54 
     55 -- Fold points.
     56 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     57 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
     58 
     59 lexer.property['scintillua.comment'] = '//'
     60 
     61 return lex