1 package lexers
2
3 import (
4 . "github.com/alecthomas/chroma/v2"
5 )
6
7
8 var Svelte = Register(DelegatingLexer(HTML, MustNewLexer(
9 &Config{
10 Name: "Svelte",
11 Aliases: []string{"svelte"},
12 Filenames: []string{"*.svelte"},
13 MimeTypes: []string{"application/x-svelte"},
14 DotAll: true,
15 },
16 svelteRules,
17 )))
18
19 func svelteRules() Rules {
20 return Rules{
21 "root": {
22
23 {`<!--`, Other, Push("comment")},
24 {
25
26
27 `(<\s*(?:script|style).*?lang\s*=\s*['"])` +
28 `(.+?)(['"].*?>)` +
29 `(.+?)` +
30 `(<\s*/\s*(?:script|style)\s*>)`,
31 UsingByGroup(2, 4, Other, Other, Other, Other, Other),
32 nil,
33 },
34 {
35
36 `(?<!<\s*(?:script|style)(?:(?!(?:script|style)\s*>).)*?)` +
37 `{` +
38 `(?!(?:(?!<\s*(?:script|style)).)*?(?:script|style)\s*>)`,
39 Punctuation,
40 Push("templates"),
41 },
42
43 {`(?<=\s+on:\w+(?:\|\w+)*)\|(?=\w+)`, Operator, nil},
44 {`.+?`, Other, nil},
45 },
46 "comment": {
47 {`-->`, Other, Pop(1)},
48 {`.+?`, Other, nil},
49 },
50 "templates": {
51 {`}`, Punctuation, Pop(1)},
52
53 {`(?<!(?<!\\)\\)(['"` + "`])" + `.*?(?<!(?<!\\)\\)\1`, Using("TypeScript"), nil},
54
55 {"{", Punctuation, Push("templates")},
56 {`@(debug|html)\b`, Keyword, nil},
57 {
58 `(#await)(\s+)(\w+)(\s+)(then|catch)(\s+)(\w+)`,
59 ByGroups(Keyword, Text, Using("TypeScript"), Text,
60 Keyword, Text, Using("TypeScript"),
61 ),
62 nil,
63 },
64 {`(#|/)(await|each|if|key)\b`, Keyword, nil},
65 {`(:else)(\s+)(if)?\b`, ByGroups(Keyword, Text, Keyword), nil},
66 {`:(catch|then)\b`, Keyword, nil},
67 {`[^{}]+`, Using("TypeScript"), nil},
68 },
69 }
70 }
71
View as plain text