vis

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

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

context.lua

(1736B)


      1 -- Copyright 2006-2025 Robert Gieseke, Lars Otter. See LICENSE.
      2 -- ConTeXt 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('context')
      9 
     10 -- TeX and ConTeXt mkiv environment definitions.
     11 local beginend = (P('begin') + 'end')
     12 local startstop = (P('start') + 'stop')
     13 
     14 -- Whitespace.
     15 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     16 
     17 -- Comments.
     18 lex:add_rule('comment', token(lexer.COMMENT, lexer.to_eol('%')))
     19 
     20 -- Sections.
     21 local wm_section = word_match{
     22 	'chapter', 'part', 'section', 'subject', 'subsection', 'subsubject', 'subsubsection',
     23 	'subsubsubject', 'subsubsubsection', 'subsubsubsubject', 'title'
     24 }
     25 local section = token(lexer.CLASS, '\\' * startstop^-1 * wm_section)
     26 lex:add_rule('section', section)
     27 
     28 -- TeX and ConTeXt mkiv environments.
     29 local environment = token(lexer.STRING, '\\' * (beginend + startstop) * lexer.alpha^1)
     30 lex:add_rule('environment', environment)
     31 
     32 -- Commands.
     33 local command = token(lexer.KEYWORD, '\\' *
     34 	(lexer.alpha^1 * P('\\') * lexer.space^1 + lexer.alpha^1 + S('!"#$%&\',./;=[\\]_{|}~`^-')))
     35 lex:add_rule('command', command)
     36 
     37 -- Operators.
     38 local operator = token(lexer.OPERATOR, S('#$_[]{}~^'))
     39 lex:add_rule('operator', operator)
     40 
     41 -- Fold points.
     42 lex:add_fold_point('environment', '\\start', '\\stop')
     43 lex:add_fold_point('environment', '\\begin', '\\end')
     44 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     45 
     46 -- Embedded Lua.
     47 local luatex = lexer.load('lua')
     48 local luatex_start_rule = #P('\\startluacode') * environment
     49 local luatex_end_rule = #P('\\stopluacode') * environment
     50 lex:embed(luatex, luatex_start_rule, luatex_end_rule)
     51 
     52 lexer.property['scintillua.comment'] = '%'
     53 
     54 return lex