vis

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

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

myrddin.lua

(1832B)


      1 -- Copyright 2017-2025 Michael Forney. See LICENSE
      2 -- Myrddin 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('myrddin')
      9 
     10 -- Whitespace.
     11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     12 
     13 -- Keywords.
     14 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
     15 	'break', 'const', 'continue', 'elif', 'else', 'extern', 'false', 'for', 'generic', 'goto', 'if',
     16 	'impl', 'in', 'match', 'pkg', 'pkglocal', 'sizeof', 'struct', 'trait', 'true', 'type', 'union',
     17 	'use', 'var', 'while'
     18 }))
     19 
     20 -- Types.
     21 lex:add_rule('type', token(lexer.TYPE, word_match{
     22 	'void', 'bool', 'char', 'byte', 'int', 'uint', 'int8', 'uint8', 'int16', 'uint16', 'int32',
     23 	'uint32', 'int64', 'uint64', 'flt32', 'flt64'
     24 } + '@' * lexer.word))
     25 
     26 -- Identifiers.
     27 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     28 
     29 -- Comments.
     30 local line_comment = lexer.to_eol('//', true)
     31 local block_comment = lexer.range('/*', '*/', false, false, true)
     32 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     33 
     34 -- Strings.
     35 local sq_str = lexer.range("'", true)
     36 local dq_str = lexer.range('"', true)
     37 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
     38 
     39 -- Numbers.
     40 local digit = lexer.digit + '_'
     41 local bdigit = S('01') + '_'
     42 local xdigit = lexer.xdigit + '_'
     43 local odigit = lpeg.R('07') + '_'
     44 local integer = '0x' * xdigit^1 + '0o' * odigit^1 + '0b' * bdigit^1 + digit^1
     45 local float = digit^1 * ((('.' * digit^1) * (S('eE') * S('+-')^-1 * digit^1)^-1) +
     46 	(('.' * digit^1)^-1 * S('eE') * S('+-')^-1 * digit^1))
     47 lex:add_rule('number', token(lexer.NUMBER, float + integer))
     48 
     49 -- Operators.
     50 lex:add_rule('operator', token(lexer.OPERATOR, S('`#_+-/*%<>~!=^&|~:;,.()[]{}')))
     51 
     52 lexer.property['scintillua.comment'] = '//'
     53 
     54 return lex