Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add Typescript #8

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 66 additions & 48 deletions jsx/lexer.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,81 @@
import re

from pygments.lexer import bygroups, include, default
from pygments.lexers.javascript import JavascriptLexer
from pygments.lexers.javascript import JavascriptLexer, TypeScriptLexer
from pygments.token import Name, Operator, Punctuation, String, Text


# Use same tokens as `JavascriptLexer`, but with tags and attributes support
TOKENS = JavascriptLexer.tokens
TOKENS.update(
{
"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"),
],
}
)
TOKENS["root"].insert(0, include("jsx"))
# Add tags and attributes support to existing tokens
def build_react_tokens(tokens):
tokens.update(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be fixed to avoid mutating dictionaries that don’t belong to us (see #20).

{
"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"),
],
}
)
tokens["root"].insert(0, include("jsx"))
return tokens


JSX_TOKENS = build_react_tokens(JavascriptLexer.tokens)
TSX_TOKENS = build_react_tokens(TypeScriptLexer.tokens)


class JsxLexer(JavascriptLexer):
name = "react"
aliases = ["jsx", "react"]
filenames = ["*.jsx", "*.react"]
mimetypes = ["text/jsx", "text/typescript-jsx"]
mimetypes = ["text/jsx"]

flags = re.MULTILINE | re.DOTALL | re.UNICODE

tokens = JSX_TOKENS


class TsxLexer(TypeScriptLexer):
name = "react-typescript"
aliases = ["tsx", "react-typescript"]
filenames = ["*.tsx"]
mimetypes = ["text/tsx", "text/typescript-jsx"]

flags = re.MULTILINE | re.DOTALL | re.UNICODE

tokens = TOKENS
tokens = TSX_TOKENS