vis: templ: add (implement) templ lexer

This commit is contained in:
2025-09-22 22:46:16 +02:00
parent fddce86eca
commit 1e7c5902ea

View File

@@ -1,71 +1,65 @@
-- Copyright 2006-2025 Mitchell. See LICENSE. -- Copyright 2006-2025 Mitchell. See LICENSE.
-- Go LPeg lexer. -- templ LPeg lexer.
local lexer = lexer local lexer = lexer
local starts_line = lexer.starts_line
local P, S = lpeg.P, lpeg.S local P, S = lpeg.P, lpeg.S
local lex = lexer.new(...) local lex = lexer.new(..., {inherit = lexer.load('go')})
-- Keywords. local close_paren = lex:tag(lexer.OPERATOR, P(')'))
lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD))) local open_brace = lex:tag(lexer.OPERATOR, P('{'))
local close_brace = lex:tag(lexer.OPERATOR, P('}'))
local colon = lex:tag(lexer.OPERATOR, P(':'))
local at = lex:tag(lexer.OPERATOR, P('@'))
local ws = lex:get_rule('whitespace')
local ident = lex:get_rule('identifier')
-- Constants. local html = lexer.load('html')
lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN))) local go_expr = lexer.load('go', 'go.expr')
local go_stmt1 = lexer.load('go', 'go.stmt1')
local go_stmt2 = lexer.load('go', 'go.stmt2')
local go_stmt3 = lexer.load('go', 'go.stmt3')
local for_ = lex:tag(lexer.KEYWORD, lexer.word_match('for if switch'))
local else_ = close_brace * ws^0 * lex:tag(lexer.KEYWORD, P('else'))
local case_ = lex:tag(lexer.KEYWORD, lexer.word_match('case default'))
html:embed(go_expr, open_brace, close_brace * close_brace^-1)
html:embed(go_stmt1, starts_line(for_ + else_, true), open_brace * #lexer.newline)
html:embed(go_stmt2, starts_line(case_, true), colon * #lexer.newline)
html:embed(go_stmt3, starts_line(at, true), (ident + open_brace + close_paren) * #lexer.newline)
-- Types. local func_block_start = close_paren * ws^0 * open_brace
lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
-- Functions. lex:set_word_list(lexer.KEYWORD, 'templ css script', true)
local builtin_func = -lpeg.B('.') *
lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
local func = lex:tag(lexer.FUNCTION, lexer.word)
local method = lpeg.B('.') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
lex:add_rule('function', (builtin_func + method + func) * #(lexer.space^0 * '('))
-- Identifiers. local function starts_with(keyword)
lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word)) local prefix = '\n' .. keyword .. '%s'
return function(input, index)
local i = index - 1024
if i < 1 then
i = 1
end
local s = input:sub(i, index)
local k = 0
repeat
i = s:find('\n%w', i)
if i then
k = i
i = i + 2
end
until not i
return k > 0 and s:find(prefix, k) and true
end
end
-- Strings. lex:embed(html, func_block_start * P(starts_with("templ")), starts_line(close_brace))
local sq_str = lexer.range("'", true)
local dq_str = lexer.range('"', true)
local raw_str = lexer.range('`', false, false)
lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str + raw_str))
-- Comments. local css = lexer.load('css')
local line_comment = lexer.to_eol('//') local css_go_expr = lexer.load('go', 'go.expr2')
local block_comment = lexer.range('/*', '*/') css:embed(css_go_expr, open_brace, close_brace)
lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment)) lex:embed(css, func_block_start * P(starts_with("css")), starts_line(close_brace))
-- Numbers. local js = lexer.load('javascript')
lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number * P('i')^-1)) lex:embed(js, func_block_start * P(starts_with("script")), starts_line(close_brace))
-- Operators.
lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-*/%&|^<>=!~:;.,()[]{}@')))
-- Fold points.
lex:add_fold_point(lexer.OPERATOR, '{', '}')
lex:add_fold_point(lexer.COMMENT, '/*', '*/')
-- Word lists.
lex:set_word_list(lexer.KEYWORD, {
'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else', 'fallthrough', 'for',
'func', 'go', 'goto', 'if', 'import', 'interface', 'map', 'package', 'range', 'return', 'select',
'struct', 'switch', 'type', 'var', 'templ'
})
lex:set_word_list(lexer.CONSTANT_BUILTIN, 'true false iota nil')
lex:set_word_list(lexer.TYPE, {
'any', 'bool', 'byte', 'comparable', 'complex64', 'complex128', 'error', 'float32', 'float64',
'int', 'int8', 'int16', 'int32', 'int64', 'rune', 'string', 'uint', 'uint8', 'uint16', 'uint32',
'uint64', 'uintptr'
})
lex:set_word_list(lexer.FUNCTION_BUILTIN, {
'append', 'cap', 'close', 'complex', 'copy', 'delete', 'imag', 'len', 'make', 'new', 'panic',
'print', 'println', 'real', 'recover'
})
lexer.property['scintillua.comment'] = '//'
return lex return lex