From 519b7328b66c4c84f03893a31e4be5ba6b1395f2 Mon Sep 17 00:00:00 2001 From: mitchell Date: Sun, 11 Mar 2018 23:04:41 -0400 Subject: Added optional Lua lexer support. This support is disabled by default and must be enabled via compile-time option. --- lexlua/django.lua | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lexlua/django.lua (limited to 'lexlua/django.lua') diff --git a/lexlua/django.lua b/lexlua/django.lua new file mode 100644 index 000000000..88406e1f8 --- /dev/null +++ b/lexlua/django.lua @@ -0,0 +1,55 @@ +-- Copyright 2006-2018 Mitchell mitchell.att.foicica.com. See License.txt. +-- Django LPeg lexer. + +local lexer = require('lexer') +local token, word_match = lexer.token, lexer.word_match +local P, R, S = lpeg.P, lpeg.R, lpeg.S + +local lex = lexer.new('django') + +-- Whitespace. +lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1)) + +-- Keywords. +lex:add_rule('keyword', token(lexer.KEYWORD, word_match[[ + as block blocktrans by endblock endblocktrans comment endcomment cycle date + debug else extends filter endfilter firstof for endfor if endif ifchanged + endifchanged ifnotequal endifnotequal in load not now or parsed regroup ssi + trans with widthratio +]])) + +-- Functions. +lex:add_rule('function', token(lexer.FUNCTION, word_match[[ + add addslashes capfirst center cut date default dictsort dictsortreversed + divisibleby escape filesizeformat first fix_ampersands floatformat get_digit + join length length_is linebreaks linebreaksbr linenumbers ljust lower + make_list phone2numeric pluralize pprint random removetags rjust slice slugify + stringformat striptags time timesince title truncatewords unordered_list upper + urlencode urlize urlizetrunc wordcount wordwrap yesno +]])) + +-- Identifiers. +lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word)) + +-- Strings. +lex:add_rule('string', token(lexer.STRING, + lexer.delimited_range('"', false, true))) + +-- Operators. +lex:add_rule('operator', token(lexer.OPERATOR, S(':,.|'))) + +-- Embed Django in HTML. +local html = lexer.load('html') +local html_comment = '')^0 * P('-->')^-1 +local django_comment = '{#' * (lexer.any - lexer.newline - '#}')^0 * P('#}')^-1 +html:modify_rule('comment', token(lexer.COMMENT, html_comment + django_comment)) +local django_start_rule = token('django_tag', '{' * S('{%')) +local django_end_rule = token('django_tag', S('%}') * '}') +html:embed(lex, django_start_rule, django_end_rule) +lex:add_style('django_tag', lexer.STYLE_EMBEDDED) + +-- Fold points. +lex:add_fold_point('django_tag', '{{', '}}') +lex:add_fold_point('django_tag', '{%', '%}') + +return lex -- cgit v1.2.3