-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlexer.py
62 lines (56 loc) · 1.77 KB
/
lexer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import re
from pygments.lexer import bygroups, default, include, inherit
from pygments.lexers.javascript import JavascriptLexer
from pygments.token import Name, Operator, Punctuation, String, Text
# Use same tokens as `JavascriptLexer`, but with tags and attributes support
TOKENS = {
"root": [
include("jsx"),
inherit,
],
"jsx": [
(
r"(<)(/?)(>)",
bygroups(Punctuation, Punctuation, Punctuation),
), # JSXFragment <>|</>
(r"(<)([\w]+)(\.?)", bygroups(Punctuation, Name.Tag, Punctuation), "tag"),
(
r"(<)(/)([\w]+)(>)",
bygroups(Punctuation, Punctuation, Name.Tag, Punctuation),
),
(
r"(<)(/)([\w]+)",
bygroups(Punctuation, Punctuation, Name.Tag),
"fragment",
), # Same for React.Context
],
"tag": [
(r"\s+", Text),
(r"([\w-]+\s*)(=)(\s*)", bygroups(Name.Attribute, Operator, Text), "attr"),
(r"[{}]+", Punctuation),
(r"[\w\.]+", Name.Attribute),
(r"(/?)(\s*)(>)", bygroups(Punctuation, Text, Punctuation), "#pop"),
],
"fragment": [
(r"(.)([\w]+)", bygroups(Punctuation, Name.Attribute)),
(r"(>)", bygroups(Punctuation), "#pop"),
],
"attr": [
("{", Punctuation, "expression"),
('".*?"', String, "#pop"),
("'.*?'", String, "#pop"),
default("#pop"),
],
"expression": [
("{", Punctuation, "#push"),
("}", Punctuation, "#pop"),
include("root"),
],
}
class JsxLexer(JavascriptLexer):
name = "react"
aliases = ["jsx", "react"]
filenames = ["*.jsx", "*.react"]
mimetypes = ["text/jsx", "text/typescript-jsx"]
flags = re.MULTILINE | re.DOTALL | re.UNICODE
tokens = TOKENS