grammars-v4 icon indicating copy to clipboard operation
grammars-v4 copied to clipboard

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

Open dours opened this issue 3 years ago • 7 comments

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

dours avatar Dec 30 '21 14:12 dours

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

RobEin avatar Dec 30 '21 15:12 RobEin

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.

RobEin avatar Jan 10 '22 02:01 RobEin

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

dours avatar Jan 17 '22 19:01 dours

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 avatar Jan 19 '22 03:01 RobEin

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.

RobEin avatar Jan 27 '22 22:01 RobEin

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

hasii2011 avatar Oct 21 '22 20:10 hasii2011

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

RobEin avatar Jul 31 '23 08:07 RobEin