vis

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

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

tcl.lua

(1464B)


      1 -- Copyright 2014-2025 Joshua Krämer. See LICENSE.
      2 -- Tcl LPeg lexer.
      3 -- This lexer follows the TCL dodekalogue (http://wiki.tcl.tk/10259).
      4 -- It is based on the previous lexer by Mitchell.
      5 
      6 local lexer = require('lexer')
      7 local token, word_match = lexer.token, lexer.word_match
      8 local P, S = lpeg.P, lpeg.S
      9 
     10 local lex = lexer.new('tcl')
     11 
     12 -- Whitespace.
     13 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     14 
     15 -- Comment.
     16 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('#' * P(function(input, index)
     17 	local i = index - 2
     18 	while i > 0 and input:find('^[ \t]', i) do i = i - 1 end
     19 	if i < 1 or input:find('^[\r\n;]', i) then return true end
     20 end))))
     21 
     22 -- Separator (semicolon).
     23 lex:add_rule('separator', token(lexer.CLASS, ';'))
     24 
     25 -- Argument expander.
     26 lex:add_rule('expander', token(lexer.LABEL, '{*}'))
     27 
     28 -- Delimiters.
     29 lex:add_rule('braces', token(lexer.KEYWORD, S('{}')))
     30 lex:add_rule('quotes', token(lexer.FUNCTION, '"'))
     31 lex:add_rule('brackets', token(lexer.VARIABLE, S('[]')))
     32 
     33 -- Variable substitution.
     34 lex:add_rule('variable', token(lexer.STRING, '$' * (lexer.alnum + '_' + P(':')^2)^0))
     35 
     36 -- Backslash substitution.
     37 local oct = lexer.digit * lexer.digit^-2
     38 local hex = 'x' * lexer.xdigit^1
     39 local unicode = 'u' * lexer.xdigit * lexer.xdigit^-3
     40 lex:add_rule('backslash', token(lexer.TYPE, '\\' * (oct + hex + unicode + 1)))
     41 
     42 -- Fold points.
     43 lex:add_fold_point(lexer.KEYWORD, '{', '}')
     44 
     45 lexer.property['scintillua.comment'] = '#'
     46 
     47 return lex