vis

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

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

xs.lua

(1989B)


      1 -- Copyright 2017-2025 David B. Lamkins. See LICENSE.
      2 -- xs LPeg lexer.
      3 -- Adapted from rc lexer by Michael Forney.
      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('xs')
     10 
     11 -- Whitespace.
     12 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     13 
     14 -- Keywords.
     15 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
     16 	'access', 'alias', 'catch', 'cd', 'dirs', 'echo', 'else', 'escape', 'eval', 'exec', 'exit',
     17 	'false', 'fn-', 'fn', 'for', 'forever', 'fork', 'history', 'if', 'jobs', 'let', 'limit', 'local',
     18 	'map', 'omap', 'popd', 'printf', 'pushd', 'read', 'result', 'set-', 'switch', 'throw', 'time',
     19 	'true', 'umask', 'until', 'unwind-protect', 'var', 'vars', 'wait', 'whats', 'while', ':lt', ':le',
     20 	':gt', ':ge', ':eq', ':ne', '~', '~~', '...', '.'
     21 }))
     22 
     23 -- Identifiers.
     24 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     25 
     26 -- Strings.
     27 local str = lexer.range("'", false, true)
     28 local herestr = '<<<' * str
     29 local heredoc = '<<' * P(function(input, index)
     30 	local s, e, _, delimiter = input:find('[ \t]*(["\']?)([%w!"%%+,-./:?@_~]+)%1', index)
     31 	if s == index and delimiter then
     32 		delimiter = delimiter:gsub('[%%+-.?]', '%%%1')
     33 		e = select(2, input:find('[\n\r]' .. delimiter .. '[\n\r]', e))
     34 		return e and e + 1 or #input + 1
     35 	end
     36 end)
     37 lex:add_rule('string', token(lexer.STRING, str + herestr + heredoc))
     38 
     39 -- Comments.
     40 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#')))
     41 
     42 -- Numbers.
     43 -- lex:add_rule('number', token(lexer.NUMBER, lexer.number))
     44 
     45 -- Constants.
     46 lex:add_rule('constant', token(lexer.CONSTANT, '$&' * lexer.word))
     47 
     48 -- Variables.
     49 lex:add_rule('variable',
     50 	token(lexer.VARIABLE, '$' * S('"#')^-1 * ('*' + lexer.digit^1 + lexer.word)))
     51 
     52 -- Operators.
     53 lex:add_rule('operator', token(lexer.OPERATOR, S('@`=!<>*&^|;?()[]{}') + '\\\n'))
     54 
     55 -- Fold points.
     56 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     57 
     58 lexer.property['scintillua.comment'] = '#'
     59 
     60 return lex