Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add match and case syntax Python 3.10 #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cdce8p wants to merge 2 commits into MagicStack:master from cdce8p:add-match-case
Closed

Add match and case syntax Python 3.10 #237

cdce8p wants to merge 2 commits into MagicStack:master from cdce8p:add-match-case

Conversation

@cdce8p
Copy link

@cdce8p cdce8p commented May 1, 2021

Python 3.10 will add pattern matching with the match and case keywords.
This PR adds a regular expression to recognize them as keyword.control.flow.python if present in a match statement. Since they are only soft keywords and still allowed as normal variable names, it's not possible to simply add them to the list of the other keywords.

Closes: #235

Example

def http_error(status: int):
 match status:
 case 400:
 return "Bad request"
 case 404:
 return "Not found"
 case 418:
 return "I'm a teapot"
 case _:
 return "Default case, unreachable"

Screen Shot 2021年05月01日 at 15 21 44

Jaakkonen, segevfiner, xavdid, Apakottur, ruthus18, spagh-eddie, and wszqkzqk reacted with thumbs up emoji MrBenHacker reacted with rocket emoji
@cdce8p cdce8p changed the title (削除) Add match and case syntax (削除ここまで) (追記) Add match and case syntax Python 3.10 (追記ここまで) May 1, 2021
Copy link

It's no super clear to me why this PR is "failing" the CI tests. When I look at the logs it looks like none of the actual MagicPython tests fail, but later some bizarre error is thrown from inside Atom. When I clone this branch and run the tests myself, they all pass.

Can we try to get this merged soon? Does something need to be fixed with the CI?

Copy link

cgahr commented Mar 17, 2022

Hi, whats the current status of this pull request? I'd love to have match case statements.

Copy link

I am waiting for this pull request too.

Copy link

Any updates?

Copy link

I too would like this

Copy link

Nice feature!

Copy link
Author

cdce8p commented May 31, 2022

I no longer need this myself. VS Code and Pylance in particular have added syntax highlighting for match / case a while ago. If someone else would like to continue with the PR, feel free to reuse it.

Just to note, although I believe the regex is quite good already. There might still be some edge cases left which aren't covered. I just haven't used it in a long time.

@cdce8p cdce8p deleted the add-match-case branch May 31, 2022 18:37
Copy link

neumond commented Sep 12, 2022

This isn't true that built-in python highlighter of VS Code supports match/case.
I've built this pull request and it works for me. Haven't tested it thoroughly though.

For Ubuntu 22.04 and snap distribution of Codium:

sudo apt install nodejs npm
git clone 'https://github.com/MagicStack/MagicPython'
cd MagicPython/
git fetch origin pull/237/head
git checkout FETCH_HEAD
make
make release
npm install --dev cson
./node_modules/.bin/cson2json grammars/MagicPython.cson > grammars/MagicPython.tmLanguage.json
sudo mount --bind grammars/MagicPython.tmLanguage.json /snap/codium/current/usr/share/codium/resources/app/extensions/python/syntaxes/MagicPython.tmLanguage.json

This isn't permanent fix, it requires mounting patched json after each reboot. Json itself could be made more compact, as the one shipped with codium.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vpetrovykh vpetrovykh Awaiting requested review from vpetrovykh

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Tokenization for match-case

AltStyle によって変換されたページ (->オリジナル) /