vis

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

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

troff.lua

(1809B)


      1 -- Copyright 2023-2025 Mitchell. See LICENSE.
      2 -- troff/man LPeg lexer.
      3 -- Based on original Man lexer by David B. Lamkins and modified by Eolien55.
      4 
      5 local lexer = lexer
      6 local P, R, S = lpeg.P, lpeg.R, lpeg.S
      7 
      8 local lex = lexer.new(...)
      9 
     10 -- Registers and groff's structured programming.
     11 lex:add_rule('keywords', lex:tag(lexer.KEYWORD, (lexer.starts_line('.') * (lexer.space - '\n')^0 *
     12 	(P('while') + 'break' + 'continue' + 'nr' + 'rr' + 'rnn' + 'aln' + '\\}')) + '\\{'))
     13 
     14 -- Markup.
     15 lex:add_rule('escape_sequences', lex:tag(lexer.VARIABLE,
     16 	'\\' * (('s' * S('+-')^-1) + S('*fgmnYV'))^-1 * (P('(') * 2 + lexer.range('[', ']') + 1)))
     17 
     18 lex:add_rule('headings', lex:tag(lexer.NUMBER,
     19 	lexer.starts_line('.') * (lexer.space - '\n')^0 * (S('STN') * 'H') * (lexer.space - '\n') *
     20 		lexer.nonnewline^0))
     21 lex:add_rule('man_alignment', lex:tag(lexer.KEYWORD,
     22 	lexer.starts_line('.') * (lexer.space - '\n')^0 * (P('br') + 'DS' + 'RS' + 'RE' + 'PD' + 'PP') *
     23 		lexer.space))
     24 lex:add_rule('font', lex:tag(lexer.VARIABLE,
     25 	lexer.starts_line('.') * (lexer.space - '\n')^0 * ('B' * P('R')^-1 + 'I' * S('PR')^-1) *
     26 		lexer.space))
     27 
     28 -- Lowercase troff macros are plain macros (like .so or .nr).
     29 lex:add_rule('troff_plain_macros', lex:tag(lexer.VARIABLE, lexer.starts_line('.') *
     30 	(lexer.space - '\n')^0 * lexer.lower^1))
     31 lex:add_rule('any_macro', lex:tag(lexer.PREPROCESSOR,
     32 	lexer.starts_line('.') * (lexer.space - '\n')^0 * (lexer.any - lexer.space)^0))
     33 lex:add_rule('comment', lex:tag(lexer.COMMENT,
     34 	(lexer.starts_line('.\\"') + '\\"' + '\\#') * lexer.nonnewline^0))
     35 lex:add_rule('string', lex:tag(lexer.STRING, lexer.range('"', true)))
     36 
     37 -- Usually used by eqn, and mandoc in some way.
     38 lex:add_rule('in_dollars', lex:tag(lexer.EMBEDDED, lexer.range('$', false, false)))
     39 
     40 -- TODO: a lexer for each preprocessor?
     41 
     42 return lex