1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
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'}
13 local ws
= token(l
.WHITESPACE
, l
.space^
1)
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
)
24 local section
= token('section', '\\' * word_match
{
25 'part', 'chapter', 'section', 'subsection', 'subsubsection', 'paragraph',
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')) *
40 '{' * l
.word
* P('*')^
-1 * '}')
43 local command
= token(l
.KEYWORD
, '\\' * (l
.alpha^
1 + S('#$&~_^%{}')))
46 local operator
= token(l
.OPERATOR
, S('&#{}[]'))
52 {'environment', environment
},
55 {'operator', operator
},
59 environment
= l
.STYLE_KEYWORD
,
60 math
= l
.STYLE_FUNCTION
,
61 section
= l
.STYLE_CLASS
65 _patterns
= {'\\[a-z]+', '[{}]', '%%'},
67 ['\\begin'] = 1, ['\\end'] = -1, ['%'] = l
.fold_line_comments('%')
69 ['environment'] = {['\\begin'] = 1, ['\\end'] = -1},
70 [l
.OPERATOR
] = {['{'] = 1, ['}'] = -1}