vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
wsf.lua
(3123B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- WSF LPeg lexer (based on XML).
3 -- Contributed by Jeff Stone.
4
5 local lexer = lexer
6 local P, S = lpeg.P, lpeg.S
7
8 local lex = lexer.new(...)
9
10 -- Comments.
11 lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.range('<!--', '-->')))
12
13 -- Elements.
14 local identifier = (lexer.alpha + S('_-')) * (lexer.alnum + S('_-'))^0
15 local tag = lex:tag(lexer.TAG, '<' * P('/')^-1 * identifier)
16 lex:add_rule('tag', tag)
17
18 -- Closing tags.
19 local tag_close = lex:tag(lexer.TAG, P('/')^-1 * '>')
20 lex:add_rule('tag_close', tag_close)
21
22 -- Equals.
23 -- TODO: performance is terrible on large files.
24 local in_tag = P(function(input, index)
25 local before = input:sub(1, index - 1)
26 local s, e = before:find('<[^>]-$'), before:find('>[^<]-$')
27 if s and e then return s > e end
28 if s then return true end
29 return input:find('^[^<]->', index) ~= nil
30 end)
31
32 local equals = lex:tag(lexer.OPERATOR, '=') -- * in_tag
33 -- lex:add_rule('equals', equals)
34
35 -- Attributes.
36 local ws = lex:get_rule('whitespace')
37 local attribute_eq = lex:tag(lexer.ATTRIBUTE, identifier) * ws^-1 * equals
38 lex:add_rule('attribute', attribute_eq)
39
40 -- Strings.
41 local sq_str = lexer.range("'", false, false)
42 local dq_str = lexer.range('"', false, false)
43 local string = lex:tag(lexer.STRING, lexer.after_set('=', sq_str + dq_str))
44 lex:add_rule('string', string)
45
46 -- Numbers.
47 local number = lex:tag(lexer.NUMBER, lexer.dec_num * P('%')^-1)
48 lex:add_rule('number', lexer.after_set('=', number)) -- * in_tag)
49
50 -- Entities.
51 local predefined = lex:tag(lexer.CONSTANT_BUILTIN .. '.entity',
52 '&' * lexer.word_match('lt gt amp apos quot') * ';')
53 local general = lex:tag(lexer.CONSTANT .. '.entity', '&' * identifier * ';')
54 lex:add_rule('entity', predefined + general)
55
56 -- Fold points.
57 local function disambiguate_lt(text, pos, line, s) return not line:find('^</', s) and 1 or -1 end
58 lex:add_fold_point(lexer.TAG, '<', disambiguate_lt)
59 lex:add_fold_point(lexer.TAG, '/>', -1)
60 lex:add_fold_point(lexer.COMMENT, '<!--', '-->')
61
62 -- Finally, add JavaScript and VBScript as embedded languages
63
64 -- Tags that start embedded languages.
65 local embed_start_tag = tag * (ws * attribute_eq * ws^-1 * string)^0 * ws^-1 * tag_close
66 local embed_end_tag = tag * tag_close
67
68 -- Embedded JavaScript.
69 local js = lexer.load('javascript')
70 local js_start_rule = #(P('<script') * (P(function(input, index)
71 if input:find('^%s+language%s*=%s*(["\'])[jJ][ava]*[sS]cript%1', index) then return true end
72 end) + '>')) * embed_start_tag -- <script language="javascript">
73 local js_end_rule = #P('</script>') * embed_end_tag -- </script>
74 lex:embed(js, js_start_rule, js_end_rule)
75
76 -- Embedded VBScript.
77 local vbs = lexer.load('vb', 'vbscript')
78 local vbs_start_rule = #(P('<script') * (P(function(input, index)
79 if input:find('^%s+language%s*=%s*(["\'])[vV][bB][sS]cript%1', index) then return true end
80 end) + '>')) * embed_start_tag -- <script language="vbscript">
81 local vbs_end_rule = #P('</script>') * embed_end_tag -- </script>
82 lex:embed(vbs, vbs_start_rule, vbs_end_rule)
83
84 lexer.property['scintillua.comment'] = '<!--|-->'
85 lexer.property['scintillua.angle.braces'] = '1'
86
87 return lex