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