antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.15k stars 3.7k forks source link

Do you have plans to support python 3.10 (for example, assignment expressions)? #2462

Open dours opened 2 years ago

dours commented 2 years ago
assignment_expression ::=  [identifier ":="] expression

An assignment expression (sometimes also called a “named expression” or “walrus”) assigns an expression to an identifier, while also returning the value of the expression.

One common use case is when handling matched regular expressions:

if matching := pattern.search(data):
    do_something(matching)

Or, when processing a file stream in chunks:

while chunk := file.read(9000):
    process(chunk)

From https://docs.python.org/3/reference/expressions.html#assignment-expressions

RobEin commented 2 years ago

Yes, I have an implementation for Python 3.10 in ANTLR4. I'll share in a few days.

RobEin commented 2 years ago

uploaded: https://github.com/RobEin/ANTLR4-Python-grammar-by-PEG

I'm not uploading it to the ANTLR4 repository yet because it has yet to be tested. I have passed all the old tests, but I don't have many files about the latest features yet.

dours commented 2 years ago

@RobEin Thank you very much! I'll try to use it soon.

RobEin commented 2 years ago

I still have to work on it. The parser is extremely slow but looks like it works. The grammar was tested with the latest Python standard library. I found a mistake.

RobEin commented 2 years ago

I have also uploaded another version (3.8.12) which can also handle the named expressions. It's fast enough, but it's still being tested. I hope it helps.

hasii2011 commented 1 year ago

The above grammars generate Java code is that true? Do you know of what that generates Python code? Actually, I tried to generate python code with these. I got a serialization error

RobEin commented 1 year ago

A Python 3.11.4 parser for ANTLR4 is ready. It looks like it's working fine, although it's still very slow. If you are interested, please test it. https://github.com/RobEin/ANTLR4-Python-grammar-by-PEG