1 package j
2
3 import (
4 . "github.com/alecthomas/chroma"
5 "github.com/alecthomas/chroma/lexers/internal"
6 )
7
8
9
10
11 var JSX = internal.Register(MustNewLazyLexer(
12 &Config{
13 Name: "react",
14 Aliases: []string{"jsx", "react"},
15 Filenames: []string{"*.jsx", "*.react"},
16 MimeTypes: []string{"text/jsx", "text/typescript-jsx"},
17 DotAll: true,
18 },
19 jsxRules,
20 ))
21
22 func jsxRules() Rules {
23 return Rules{
24 "commentsandwhitespace": {
25 {`\s+`, Text, nil},
26 {`<!--`, Comment, nil},
27 {`//.*?\n`, CommentSingle, nil},
28 {`/\*.*?\*/`, CommentMultiline, nil},
29 },
30 "slashstartsregex": {
31 Include("commentsandwhitespace"),
32 {`/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/([gimuy]+\b|\B)`, LiteralStringRegex, Pop(1)},
33 {`(?=/)`, Text, Push("#pop", "badregex")},
34 Default(Pop(1)),
35 },
36 "badregex": {
37 {`\n`, Text, Pop(1)},
38 },
39 "root": {
40 Include("jsx"),
41 {`\A#! ?/.*?\n`, CommentHashbang, nil},
42 {`^(?=\s|/|<!--)`, Text, Push("slashstartsregex")},
43 Include("commentsandwhitespace"),
44 {`(\.\d+|[0-9]+\.[0-9]*)([eE][-+]?[0-9]+)?`, LiteralNumberFloat, nil},
45 {`0[bB][01]+`, LiteralNumberBin, nil},
46 {`0[oO][0-7]+`, LiteralNumberOct, nil},
47 {`0[xX][0-9a-fA-F]+`, LiteralNumberHex, nil},
48 {`[0-9]+`, LiteralNumberInteger, nil},
49 {`\.\.\.|=>`, Punctuation, nil},
50 {`\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?`, Operator, Push("slashstartsregex")},
51 {`[{(\[;,]`, Punctuation, Push("slashstartsregex")},
52 {`[})\].]`, Punctuation, nil},
53 {`(for|in|while|do|break|return|continue|switch|case|default|if|else|throw|try|catch|finally|new|delete|typeof|instanceof|void|yield|this|of)\b`, Keyword, Push("slashstartsregex")},
54 {`(var|let|with|function)\b`, KeywordDeclaration, Push("slashstartsregex")},
55 {`(abstract|async|await|boolean|byte|char|class|const|debugger|double|enum|export|extends|final|float|goto|implements|import|int|interface|long|native|package|private|protected|public|short|static|super|synchronized|throws|transient|volatile)\b`, KeywordReserved, nil},
56 {`(true|false|null|NaN|Infinity|undefined)\b`, KeywordConstant, nil},
57 {`(Array|Boolean|Date|Error|Function|Math|netscape|Number|Object|Packages|RegExp|String|Promise|Proxy|sun|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|Error|eval|isFinite|isNaN|isSafeInteger|parseFloat|parseInt|document|this|window)\b`, NameBuiltin, nil},
58 {`(?:[$_\p{L}\p{N}]|\\u[a-fA-F0-9]{4})(?:(?:[$\p{L}\p{N}]|\\u[a-fA-F0-9]{4}))*`, NameOther, nil},
59 {`"(\\\\|\\"|[^"])*"`, LiteralStringDouble, nil},
60 {`'(\\\\|\\'|[^'])*'`, LiteralStringSingle, nil},
61 {"`", LiteralStringBacktick, Push("interp")},
62 },
63 "interp": {
64 {"`", LiteralStringBacktick, Pop(1)},
65 {`\\\\`, LiteralStringBacktick, nil},
66 {"\\\\`", LiteralStringBacktick, nil},
67 {`\$\{`, LiteralStringInterpol, Push("interp-inside")},
68 {`\$`, LiteralStringBacktick, nil},
69 {"[^`\\\\$]+", LiteralStringBacktick, nil},
70 },
71 "interp-inside": {
72 {`\}`, LiteralStringInterpol, Pop(1)},
73 Include("root"),
74 },
75 "jsx": {
76 {`(<)(/?)(>)`, ByGroups(Punctuation, Punctuation, Punctuation), nil},
77 {`(<)([\w\.]+)`, ByGroups(Punctuation, NameTag), Push("tag")},
78 {`(<)(/)([\w\.]+)(>)`, ByGroups(Punctuation, Punctuation, NameTag, Punctuation), nil},
79 },
80 "tag": {
81 {`\s+`, Text, nil},
82 {`([\w]+\s*)(=)(\s*)`, ByGroups(NameAttribute, Operator, Text), Push("attr")},
83 {`[{}]+`, Punctuation, nil},
84 {`[\w\.]+`, NameAttribute, nil},
85 {`(/?)(\s*)(>)`, ByGroups(Punctuation, Text, Punctuation), Pop(1)},
86 },
87 "attr": {
88 {`{`, Punctuation, Push("expression")},
89 {`".*?"`, LiteralString, Pop(1)},
90 {`'.*?'`, LiteralString, Pop(1)},
91 Default(Pop(1)),
92 },
93 "expression": {
94 {`{`, Punctuation, Push()},
95 {`}`, Punctuation, Pop(1)},
96 Include("root"),
97 },
98 }
99 }
100
View as plain text