Skip to content
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

Add match and case syntax Python 3.10 #237

Closed
wants to merge 2 commits into from
Closed

Add match and case syntax Python 3.10 #237

wants to merge 2 commits into from

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

@cdce8p cdce8p changed the title Add match and case syntax Add match and case syntax Python 3.10 May 1, 2021
@norabelrose
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?

@cgahr
Copy link

cgahr commented Mar 17, 2022

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

@tiararosebiezetta
Copy link

I am waiting for this pull request too.

@elprans elprans requested a review from vpetrovykh March 25, 2022 03:51
@ruthus18
Copy link

Any updates?

@spagh-eddie
Copy link

I too would like this

@wszqkzqk
Copy link

Nice feature!

@cdce8p
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 closed this May 31, 2022
@cdce8p cdce8p deleted the add-match-case branch May 31, 2022 18:37
@neumond
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tokenization for match-case
8 participants