From 2382ef2c97819d086aec1102db3944ae3a33ee42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Zu=CC=88rn?= Date: Fri, 7 Feb 2020 18:06:56 +0100 Subject: [PATCH 1/2] Added typescript lexer --- jsx/lexer.py | 114 +++++++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 48 deletions(-) diff --git a/jsx/lexer.py b/jsx/lexer.py index b3fec2c..eacd7ac 100644 --- a/jsx/lexer.py +++ b/jsx/lexer.py @@ -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( + { + "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 From 44d96a438c709e14f17fdf61d0709404f816d677 Mon Sep 17 00:00:00 2001 From: Pier Carlo Cadoppi Date: Tue, 11 Apr 2023 14:50:06 +0200 Subject: [PATCH 2/2] tsx lexer gets actually added --- jsx/__init__.py | 3 ++- setup.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/jsx/__init__.py b/jsx/__init__.py index e380014..17ebe26 100644 --- a/jsx/__init__.py +++ b/jsx/__init__.py @@ -1,4 +1,5 @@ from .lexer import JsxLexer # noqa +from .lexer import TsxLexer -__all__ = ["JsxLexer"] +__all__ = ["JsxLexer", "TsxLexer"] diff --git a/setup.py b/setup.py index 8f14184..3450846 100644 --- a/setup.py +++ b/setup.py @@ -30,5 +30,6 @@ entry_points=""" [pygments.lexers] jsx=jsx:JsxLexer + tsx=jsx:TsxLexer """, )