vis: move text object definitions to separate file
[vis.git] / lexers / latex.lua
blobf1febe990e0d2a66655dbcd1f3d0b13683e8b340
1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- Latex LPeg lexer.
3 -- Modified by Brian Schott.
4 -- Modified by Robert Gieseke.
6 local l = require('lexer')
7 local token, word_match = l.token, l.word_match
8 local P, R, S = lpeg.P, lpeg.R, lpeg.S
10 local M = {_NAME = 'latex'}
12 -- Whitespace.
13 local ws = token(l.WHITESPACE, l.space^1)
15 -- Comments.
16 local line_comment = '%' * l.nonnewline^0
17 local block_comment = '\\begin' * P(' ')^0 * '{comment}' *
18 (l.any - '\\end' * P(' ')^0 * '{comment}')^0 *
19 P('\\end' * P(' ')^0 * '{comment}')^-1
20 -- Note: need block_comment before line_comment or LPeg cannot compile rule.
21 local comment = token(l.COMMENT, block_comment + line_comment)
23 -- Sections.
24 local section = token('section', '\\' * word_match{
25 'part', 'chapter', 'section', 'subsection', 'subsubsection', 'paragraph',
26 'subparagraph'
27 } * P('*')^-1)
29 -- Math environments.
30 local math_word = word_match{
31 'align', 'displaymath', 'eqnarray', 'equation', 'gather', 'math', 'multline'
33 local math_begin_end = (P('begin') + P('end')) * P(' ')^0 *
34 '{' * math_word * P('*')^-1 * '}'
35 local math = token('math', '$' + '\\' * (S('[]()') + math_begin_end))
37 -- LaTeX environments.
38 local environment = token('environment', '\\' * (P('begin') + P('end')) *
39 P(' ')^0 *
40 '{' * l.word * P('*')^-1 * '}')
42 -- Commands.
43 local command = token(l.KEYWORD, '\\' * (l.alpha^1 + S('#$&~_^%{}')))
45 -- Operators.
46 local operator = token(l.OPERATOR, S('&#{}[]'))
48 M._rules = {
49 {'whitespace', ws},
50 {'comment', comment},
51 {'math', math},
52 {'environment', environment},
53 {'section', section},
54 {'keyword', command},
55 {'operator', operator},
58 M._tokenstyles = {
59 environment = l.STYLE_KEYWORD,
60 math = l.STYLE_FUNCTION,
61 section = l.STYLE_CLASS
64 M._foldsymbols = {
65 _patterns = {'\\[a-z]+', '[{}]', '%%'},
66 [l.COMMENT] = {
67 ['\\begin'] = 1, ['\\end'] = -1, ['%'] = l.fold_line_comments('%')
69 ['environment'] = {['\\begin'] = 1, ['\\end'] = -1},
70 [l.OPERATOR] = {['{'] = 1, ['}'] = -1}
73 return M