This repository has been archived on 2024-06-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
coffee.pygments/tests/examplefiles/python/switch_case.txt
amitkummer 40de02ec6f 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.
2021-12-20 20:22:50 +02:00

23 lines
433 B
Text

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