Skip to content

Instantly share code, notes, and snippets.

@rgieseke
Created June 18, 2011 11:12
Show Gist options
  • Save rgieseke/1033004 to your computer and use it in GitHub Desktop.
Save rgieseke/1033004 to your computer and use it in GitHub Desktop.
Latex lexer with only l.* token names
-- Copyright 2006-2011 Mitchell mitchell<att>caladbolg.net. See LICENSE.
-- Modified by Robert Gieseke.
-- LaTeX LPeg lexer.
local l = lexer
local token, style, color, word_match = l.token, l.style, l.color, l.word_match
local P, R, S = l.lpeg.P, l.lpeg.R, l.lpeg.S
local table = _G.table
module(...)
-- Whitespace.
local ws = token(l.WHITESPACE, l.space^1)
-- Comments.
local line_comment = '%' * l.nonnewline^0
local block_comment = '\\begin{comment}' * (l.any - '\\end{comment}')^0 *
P('\\end{comment}')^-1
local comment = token(l.COMMENT, line_comment + block_comment)
-- Sections.
local section_keywords = word_match { 'part', 'chapter', 'section',
'subsection', 'subsubsection',
'paragraph', 'subparagraph' }
local parts = token('parts', '\\' * section_keywords * P('*')^-1)
-- LaTeX environments.
local environment = token(l.FUNCTION, '\\' * (P('begin') + 'end'))
local tex = l.load('tex')
_rules = tex._rules
_rules[1] = { 'whitespace', ws }
_rules[2] = { 'comment', comment }
_rules[3] = { 'environment', environment }
table.insert(_rules, 4, { 'parts', parts })
_tokenstyles = {
{ 'parts', l.style_class },
}
_foldsymbols = {
_patterns = { '\\[a-z]+', '[{}]' },
[l.COMMENT] = { ['\\begin'] = 1, ['\\end'] = -1 },
[l.FUNCTION] = { ['\\begin'] = 1, ['\\end'] = -1 },
[l.OPERATOR] = { ['{'] = 1, ['}'] = -1 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment