From 40de02ec6f1ee25efaeec80ad2964c84caa43b92 Mon Sep 17 00:00:00 2001 From: amitkummer Date: Mon, 20 Dec 2021 19:49:33 +0200 Subject: 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. --- tests/examplefiles/python/switch_case.txt | 23 +++ tests/examplefiles/python/switch_case.txt.output | 196 +++++++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 tests/examplefiles/python/switch_case.txt create mode 100644 tests/examplefiles/python/switch_case.txt.output (limited to 'tests/examplefiles/python') 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 -- cgit v1.2.1