diff options
author | amitkummer <amit.kummer@gmail.com> | 2021-12-20 19:49:33 +0200 |
---|---|---|
committer | amitkummer <amit.kummer@gmail.com> | 2021-12-20 20:22:50 +0200 |
commit | 40de02ec6f1ee25efaeec80ad2964c84caa43b92 (patch) | |
tree | a3edf107449c6ce9a7431e74d42462d01f124c0b /tests/examplefiles/python | |
parent | 8630e033313647d9579e314f3e8e2882f4558933 (diff) | |
download | pygments-git-40de02ec6f1ee25efaeec80ad2964c84caa43b92.tar.gz |
Python: lex soft keywords
Some notes:
- This approach is not perfect, but it's rather simple and I can't
think of an edge case.
- I did not use the `words` function to create the regex matching the
keywords list, because it returns a capturing group (`()`) and it
needs to be non-capturing here (because of `bygroups` usage).
- I chose to go to the 'soft-keywords-inner' state after both
`match` and `case`, even though it's unnecessary for `match`
(the inner state catches the `_` wildcard keyword which appears
only after a `case`).
This is mostly harmless and saves us from writing the 'soft-keywords'
regex twice each for `match` and `case` with the extra inner state
just for `case`.
The only piece of code this will lex incorrectly is `match _:`
(`_` will be lexed as keyword). I doubt though that pattern mathcing
will be used like this.
Diffstat (limited to 'tests/examplefiles/python')
-rw-r--r-- | tests/examplefiles/python/switch_case.txt | 23 | ||||
-rw-r--r-- | tests/examplefiles/python/switch_case.txt.output | 196 |
2 files changed, 219 insertions, 0 deletions
diff --git a/tests/examplefiles/python/switch_case.txt b/tests/examplefiles/python/switch_case.txt new file mode 100644 index 00000000..36c34fbd --- /dev/null +++ b/tests/examplefiles/python/switch_case.txt @@ -0,0 +1,23 @@ +match command.split(): + case ['to', direction] if direction in destination.route: + return 1 + case 'foo' | 'bar': + return 2 + case 'raz' as name: + return 3 + case ['to', _]: + return 4 + case else_bar: + return 5 + case _: + return 6 + +match command.split(): + case _Direction(): + return 1 + case _ as default: + return 2 + +case = 1 +match = 1 +match if True else bar diff --git a/tests/examplefiles/python/switch_case.txt.output b/tests/examplefiles/python/switch_case.txt.output new file mode 100644 index 00000000..2abf65a5 --- /dev/null +++ b/tests/examplefiles/python/switch_case.txt.output @@ -0,0 +1,196 @@ +'match' Keyword +' ' Text +'command' Name +'.' Operator +'split' Name +'(' Punctuation +')' Punctuation +':' Punctuation +'\n' Text + +' ' Text +'case' Keyword +' ' Text +'[' Punctuation +"'" Literal.String.Single +'to' Literal.String.Single +"'" Literal.String.Single +',' Punctuation +' ' Text +'direction' Name +']' Punctuation +' ' Text +'if' Keyword +' ' Text +'direction' Name +' ' Text +'in' Operator.Word +' ' Text +'destination' Name +'.' Operator +'route' Name +':' Punctuation +'\n' Text + +' ' Text +'return' Keyword +' ' Text +'1' Literal.Number.Integer +'\n' Text + +' ' Text +'case' Keyword +' ' Text +"'" Literal.String.Single +'foo' Literal.String.Single +"'" Literal.String.Single +' ' Text +'|' Operator +' ' Text +"'" Literal.String.Single +'bar' Literal.String.Single +"'" Literal.String.Single +':' Punctuation +'\n' Text + +' ' Text +'return' Keyword +' ' Text +'2' Literal.Number.Integer +'\n' Text + +' ' Text +'case' Keyword +' ' Text +"'" Literal.String.Single +'raz' Literal.String.Single +"'" Literal.String.Single +' ' Text +'as' Keyword +' ' Text +'name' Name +':' Punctuation +'\n' Text + +' ' Text +'return' Keyword +' ' Text +'3' Literal.Number.Integer +'\n' Text + +' ' Text +'case' Keyword +' ' Text +'[' Punctuation +"'" Literal.String.Single +'to' Literal.String.Single +"'" Literal.String.Single +',' Punctuation +' ' Text +'_' Keyword +']' Punctuation +':' Punctuation +'\n' Text + +' ' Text +'return' Keyword +' ' Text +'4' Literal.Number.Integer +'\n' Text + +' ' Text +'case' Keyword +' ' Text +'else_bar' Name +':' Punctuation +'\n' Text + +' ' Text +'return' Keyword +' ' Text +'5' Literal.Number.Integer +'\n' Text + +' ' Text +'case' Keyword +' ' Text +'_' Keyword +':' Punctuation +'\n' Text + +' ' Text +'return' Keyword +' ' Text +'6' Literal.Number.Integer +'\n' Text + +'\n' Text + +'match' Keyword +' ' Text +'command' Name +'.' Operator +'split' Name +'(' Punctuation +')' Punctuation +':' Punctuation +'\n' Text + +' ' Text +'case' Keyword +' ' Text +'_Direction' Name +'(' Punctuation +')' Punctuation +':' Punctuation +'\n' Text + +' ' Text +'return' Keyword +' ' Text +'1' Literal.Number.Integer +'\n' Text + +' ' Text +'case' Keyword +' ' Text +'_' Keyword +' ' Text +'as' Keyword +' ' Text +'default' Name +':' Punctuation +'\n' Text + +' ' Text +'return' Keyword +' ' Text +'2' Literal.Number.Integer +'\n' Text + +'\n' Text + +'case' Name +' ' Text +'=' Operator +' ' Text +'1' Literal.Number.Integer +'\n' Text + +'match' Name +' ' Text +'=' Operator +' ' Text +'1' Literal.Number.Integer +'\n' Text + +'match' Name +' ' Text +'if' Keyword +' ' Text +'True' Keyword.Constant +' ' Text +'else' Keyword +' ' Text +'bar' Name +'\n' Text |