tex.lua (1303B)
1 local l = require("lexer") 2 local lex = l.new(...) 3 local P, R, S = lpeg.P, lpeg.R, lpeg.S 4 local T = function(name, patt) return lex:tag(name, patt) end 5 local any = P(1) 6 local function I(s) -- Case-insensitive string match 7 local p = P(true) 8 for i = 1, #s do 9 local c = s:sub(i, i) 10 p = p * (P(c:lower()) + P(c:upper())) 11 end 12 return p 13 end 14 local function N(p, min, max) 15 max = max or min 16 return p^min - p^(max+1) 17 end 18 19 local whitespace = T("whitespace", S"\n\r\t "^1) 20 21 local comment_keyword = T("comment_keyword", I"todo" + I"xxx" + I"fixme") 22 local comment_text = T("comment_text", (any - P"\n" - comment_keyword)^1) 23 local comment = T("comment_text", P"%") * (comment_text + comment_keyword)^0 * T("whitespace", P"\n") 24 25 local upup = T("tex_upup", P"^^" * (N(R("09", "af"), 2) + any)) 26 27 local delimiter = T("delimiter", S"{}") 28 29 local math_ = T("tex_math", P"$") 30 31 local operator = T("operator", S"^_~&" + (P"#" * (R"19" + P"#" + #P"{")) + P"---" + P"--") 32 33 local command = T("function", P"\\") * (upup + T("function", (R("AZ", "az") + P"@")^1 + any)) 34 35 lex:add_rule("whitespace", whitespace) 36 lex:add_rule("comment", comment) 37 lex:add_rule("tex_upup", upup) 38 lex:add_rule("delimiter", delimiter) 39 lex:add_rule("tex_math", math_) 40 lex:add_rule("operator", operator) 41 lex:add_rule("function", command) 42 43 return lex