vis

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

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

fantom.lua

(2786B)


      1 -- Copyright 2018-2025 Simeon Maryasin (MarSoft). See LICENSE.
      2 -- Fantom LPeg lexer.
      3 -- Based on Java LPeg lexer by Mitchell and Vim's Fantom syntax.
      4 
      5 local lexer = require('lexer')
      6 local token, word_match = lexer.token, lexer.word_match
      7 local P, S = lpeg.P, lpeg.S
      8 
      9 local lex = lexer.new('fantom')
     10 
     11 -- Whitespace.
     12 local ws = token(lexer.WHITESPACE, lexer.space^1)
     13 lex:add_rule('whitespace', ws)
     14 
     15 -- Classes.
     16 local type = token(lexer.TYPE, lexer.word)
     17 lex:add_rule('class_sequence',
     18 	token(lexer.KEYWORD, 'class') * ws * type * ( -- at most one inheritance spec
     19 	ws * token(lexer.OPERATOR, ':') * ws * type *
     20 		( -- at least 0 (i.e. any number) of additional classes
     21 		ws^-1 * token(lexer.OPERATOR, ',') * ws^-1 * type)^0)^-1)
     22 
     23 -- Keywords.
     24 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
     25 	'using', 'native', -- external
     26 	'goto', 'void', 'serializable', 'volatile', -- error
     27 	'if', 'else', 'switch', -- conditional
     28 	'do', 'while', 'for', 'foreach', 'each', -- repeat
     29 	'true', 'false', -- boolean
     30 	'null', -- constant
     31 	'this', 'super', -- typedef
     32 	'new', 'is', 'isnot', 'as', -- operator
     33 	'plus', 'minus', 'mult', 'div', 'mod', 'get', 'set', 'slice', 'lshift', 'rshift', 'and', 'or',
     34 	'xor', 'inverse', 'negate', --
     35 	'increment', 'decrement', 'equals', 'compare', -- long operator
     36 	'return', -- stmt
     37 	'static', 'const', 'final', -- storage class
     38 	'virtual', 'override', 'once', -- slot
     39 	'readonly', -- field
     40 	'throw', 'try', 'catch', 'finally', -- exceptions
     41 	'assert', -- assert
     42 	'class', 'enum', 'mixin', -- typedef
     43 	'break', 'continue', -- branch
     44 	'default', 'case', -- labels
     45 	'public', 'internal', 'protected', 'private', 'abstract' -- scope decl
     46 }))
     47 
     48 -- Types.
     49 lex:add_rule('type', token(lexer.TYPE, word_match(
     50 	'Void Bool Int Float Decimal Str Duration Uri Type Range List Map Obj Err Env')))
     51 
     52 -- Functions.
     53 -- lex:add_rule('function', token(lexer.FUNCTION, lexer.word) * #P('('))
     54 
     55 -- Identifiers.
     56 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     57 
     58 -- Strings.
     59 local sq_str = lexer.range("'", true)
     60 local dq_str = lexer.range('"', true)
     61 local bq_str = lexer.range('`', true)
     62 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str + bq_str))
     63 
     64 -- Comments.
     65 local line_comment = lexer.to_eol('//', true)
     66 local block_comment = lexer.range('/*', '*/')
     67 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     68 
     69 -- Numbers.
     70 lex:add_rule('number', token(lexer.NUMBER, lexer.number * S('LlFfDd')^-1))
     71 
     72 -- Operators.
     73 lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}#')))
     74 
     75 -- Annotations.
     76 lex:add_rule('facet', token(lexer.ANNOTATION, '@' * lexer.word))
     77 
     78 -- Fold points.
     79 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     80 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
     81 
     82 lexer.property['scintillua.comment'] = '//'
     83 
     84 return lex