vis

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

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

xtend.lua

(2922B)


      1 -- Copyright (c) 2014-2025 Piotr Orzechowski [drzewo.org]. See LICENSE.
      2 -- Xtend 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('xtend')
      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 	-- General.
     20 	'abstract', 'annotation', 'as', 'case', 'catch', 'class', 'create', 'def', 'default', 'dispatch',
     21 	'do', 'else', 'enum', 'extends', 'extension', 'final', 'finally', 'for', 'if', 'implements',
     22 	'import', 'interface', 'instanceof', 'it', 'new', 'override', 'package', 'private', 'protected',
     23 	'public', 'return', 'self', 'static', 'super', 'switch', 'synchronized', 'this', 'throw',
     24 	'throws', 'try', 'typeof', 'val', 'var', 'while',
     25 	-- Templates.
     26 	'AFTER', 'BEFORE', 'ENDFOR', 'ENDIF', 'FOR', 'IF', 'SEPARATOR',
     27 	-- Literals.
     28 	'true', 'false', 'null'
     29 }))
     30 
     31 -- Types.
     32 lex:add_rule('type', token(lexer.TYPE, word_match{
     33 	'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'short', 'void', 'Boolean', 'Byte',
     34 	'Character', 'Double', 'Float', 'Integer', 'Long', 'Short', 'String'
     35 }))
     36 
     37 -- Functions.
     38 lex:add_rule('function', token(lexer.FUNCTION, lexer.word) * #P('('))
     39 
     40 -- Identifiers.
     41 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     42 
     43 -- Templates.
     44 lex:add_rule('template', token(lexer.EMBEDDED, lexer.range("'''")))
     45 
     46 -- Strings.
     47 local sq_str = lexer.range("'", true)
     48 local dq_str = lexer.range('"', true)
     49 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
     50 
     51 -- Comments.
     52 local line_comment = lexer.to_eol('//', true)
     53 local block_comment = lexer.range('/*', '*/')
     54 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     55 
     56 -- Numbers.
     57 local small_suff = S('lL')
     58 local med_suff = S('bB') * S('iI')
     59 local large_suff = S('dD') + S('fF') + S('bB') * S('dD')
     60 local exp = S('eE') * lexer.digit^1
     61 
     62 local dec_inf = ('_' * lexer.digit^1)^0
     63 local hex_inf = ('_' * lexer.xdigit^1)^0
     64 local float_pref = lexer.digit^1 * '.' * lexer.digit^1
     65 local float_suff = exp^-1 * med_suff^-1 * large_suff^-1
     66 
     67 local dec = lexer.digit * dec_inf * (small_suff^-1 + float_suff)
     68 local hex = lexer.hex_num * hex_inf * P('#' * (small_suff + med_suff))^-1
     69 local float = float_pref * dec_inf * float_suff
     70 
     71 lex:add_rule('number', token(lexer.NUMBER, float + hex + dec))
     72 
     73 -- Annotations.
     74 lex:add_rule('annotation', token(lexer.ANNOTATION, '@' * lexer.word))
     75 
     76 -- Operators.
     77 lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}#')))
     78 
     79 -- Error.
     80 lex:add_rule('error', token(lexer.ERROR, lexer.any))
     81 
     82 -- Fold points.
     83 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     84 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
     85 
     86 lexer.property['scintillua.comment'] = '//'
     87 
     88 return lex