vis: move text object definitions to separate file
[vis.git] / lexers / context.lua
blob30ec1b0c9dd7434221bb38ac9bff664a872c6580
1 -- Copyright 2006-2013 Robert Gieseke. See LICENSE.
2 -- ConTeXt LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S = lpeg.P, lpeg.R, lpeg.S
8 local M = {_NAME = 'context'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local comment = token(l.COMMENT, '%' * l.nonnewline^0)
16 -- Commands.
17 local command = token(l.KEYWORD, '\\' * (l.alpha^1 + S('#$&~_^%{}')))
19 -- Sections.
20 local section = token('section', '\\' * word_match{
21 'part', 'chapter', 'section', 'subsection', 'subsubsection', 'title',
22 'subject', 'subsubject', 'subsubsubject'
25 -- ConTeXt environments.
26 local environment = token('environment', '\\' * (P('start') + 'stop') * l.word)
28 -- Operators.
29 local operator = token(l.OPERATOR, S('$&#{}[]'))
31 M._rules = {
32 {'whitespace', ws},
33 {'comment', comment},
34 {'environment', environment},
35 {'section', section},
36 {'keyword', command},
37 {'operator', operator},
40 M._tokenstyles = {
41 environment = l.STYLE_KEYWORD,
42 section = l.STYLE_CLASS
45 M._foldsymbols = {
46 _patterns = {'\\start', '\\stop', '[{}]', '%%'},
47 ['environment'] = {['\\start'] = 1, ['\\stop'] = -1},
48 [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
49 [l.COMMENT] = {['%'] = l.fold_line_comments('%')}
52 -- Embedded Lua.
53 local luatex = l.load('lua')
54 local luatex_start_rule = #P('\\startluacode') * environment
55 local luatex_end_rule = #P('\\stopluacode') * environment
56 l.embed_lexer(M, luatex, luatex_start_rule, luatex_end_rule)
59 return M