Skip to content

Commit

Permalink
fix: parse all of @foo.bar as a Python decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
triallax authored and alecthomas committed Oct 6, 2022
1 parent 750df98 commit 396f572
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lexers/embedded/python.xml
Expand Up @@ -379,7 +379,7 @@
</rule>
</state>
<state name="name">
<rule pattern="@[_\p{L}][_\p{L}\p{N}]*">
<rule pattern="@[_\p{L}][_\p{L}\p{N}]*(\s*\.\s*[_\p{L}][_\p{L}\p{N}]*)*">
<token type="NameDecorator"/>
</rule>
<rule pattern="@">
Expand Down
8 changes: 2 additions & 6 deletions lexers/testdata/python/test_complex_file1.expected
Expand Up @@ -713,9 +713,7 @@
{"type":"Keyword","value":"else"},
{"type":"Punctuation","value":":"},
{"type":"Text","value":"\n "},
{"type":"NameDecorator","value":"@functools"},
{"type":"Operator","value":"."},
{"type":"Name","value":"wraps"},
{"type":"NameDecorator","value":"@functools.wraps"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"func"},
{"type":"Punctuation","value":")"},
Expand Down Expand Up @@ -863,9 +861,7 @@
{"type":"Keyword","value":"else"},
{"type":"Punctuation","value":":"},
{"type":"Text","value":"\n "},
{"type":"NameDecorator","value":"@functools"},
{"type":"Operator","value":"."},
{"type":"Name","value":"wraps"},
{"type":"NameDecorator","value":"@functools.wraps"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"func"},
{"type":"Punctuation","value":")"},
Expand Down
15 changes: 15 additions & 0 deletions lexers/testdata/python/test_decorators.actual
@@ -0,0 +1,15 @@
@decorator1
def foo():
print("foo")

@decorator2.member
def bar():
print("bar")

@decorator3 . member(param=1)
def baz():
print("baz")

@decorator4 . member1 .member2 (param=1)
def bat():
print("bat")
61 changes: 61 additions & 0 deletions lexers/testdata/python/test_decorators.expected
@@ -0,0 +1,61 @@
[
{"type":"NameDecorator","value":"@decorator1"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"def"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"foo"},
{"type":"Punctuation","value":"():"},
{"type":"Text","value":"\n "},
{"type":"NameBuiltin","value":"print"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"foo\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n\n"},
{"type":"NameDecorator","value":"@decorator2.member"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"def"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"bar"},
{"type":"Punctuation","value":"():"},
{"type":"Text","value":"\n "},
{"type":"NameBuiltin","value":"print"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"bar\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n\n"},
{"type":"NameDecorator","value":"@decorator3 . member"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"param"},
{"type":"Operator","value":"="},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"def"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"baz"},
{"type":"Punctuation","value":"():"},
{"type":"Text","value":"\n "},
{"type":"NameBuiltin","value":"print"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"baz\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n\n"},
{"type":"NameDecorator","value":"@decorator4 . member1 .member2"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"param"},
{"type":"Operator","value":"="},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n"},
{"type":"Keyword","value":"def"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"bat"},
{"type":"Punctuation","value":"():"},
{"type":"Text","value":"\n "},
{"type":"NameBuiltin","value":"print"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringDouble","value":"\"bat\""},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n"}
]

0 comments on commit 396f572

Please sign in to comment.