From f0e48f0736efac883776f4e7a6087a5cd8fccc4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Tue, 17 May 2022 09:57:00 -0600 Subject: [PATCH 1/3] Parse ellipsis (...) in the old parser --- grammar/Python.asdl | 1 + src/runtime/lpython_parser.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/grammar/Python.asdl b/grammar/Python.asdl index b0720ded01..1cdf8b5e5f 100644 --- a/grammar/Python.asdl +++ b/grammar/Python.asdl @@ -83,6 +83,7 @@ module LPython | ConstantBool(bool value, string? kind) | ConstantFloat(float value, string? kind) | ConstantComplex(float re, float im, string? kind) + | ConstantEllipsis(string? kind) -- the following expression can appear in assignment context | Attribute(expr value, identifier attr, expr_context ctx) diff --git a/src/runtime/lpython_parser.py b/src/runtime/lpython_parser.py index 2ad5ad90a3..d369e8b8b9 100644 --- a/src/runtime/lpython_parser.py +++ b/src/runtime/lpython_parser.py @@ -50,6 +50,8 @@ def visit_Constant(self, node): elif isinstance(node.value, complex): new_node = python_ast.ConstantComplex(node.value.real, node.value.imag, node.kind) + elif isinstance(node.value, Ellipsis.__class__): + new_node = python_ast.ConstantEllipsis(node.kind) else: print(type(node.value)) raise Exception("Unsupported Constant type") From 68e9f58a41c591969197b5c75ac9e33bd6a92703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Tue, 17 May 2022 09:59:24 -0600 Subject: [PATCH 2/3] Parse None in the old parser --- grammar/Python.asdl | 1 + src/runtime/lpython_parser.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/grammar/Python.asdl b/grammar/Python.asdl index 1cdf8b5e5f..85fb170651 100644 --- a/grammar/Python.asdl +++ b/grammar/Python.asdl @@ -84,6 +84,7 @@ module LPython | ConstantFloat(float value, string? kind) | ConstantComplex(float re, float im, string? kind) | ConstantEllipsis(string? kind) + | ConstantNone(string? kind) -- the following expression can appear in assignment context | Attribute(expr value, identifier attr, expr_context ctx) diff --git a/src/runtime/lpython_parser.py b/src/runtime/lpython_parser.py index d369e8b8b9..02e9c37bd0 100644 --- a/src/runtime/lpython_parser.py +++ b/src/runtime/lpython_parser.py @@ -52,6 +52,8 @@ def visit_Constant(self, node): node.value.imag, node.kind) elif isinstance(node.value, Ellipsis.__class__): new_node = python_ast.ConstantEllipsis(node.kind) + elif isinstance(node.value, None.__class__): + new_node = python_ast.ConstantNone(node.kind) else: print(type(node.value)) raise Exception("Unsupported Constant type") From c57fa7b398ccd3cbda125f59e93bbeaa7fee3749 Mon Sep 17 00:00:00 2001 From: Thirumalai-Shaktivel Date: Wed, 18 May 2022 11:54:46 +0530 Subject: [PATCH 3/3] Add tests and Update the references --- tests/parser/ellipsis1.py | 37 ++++++++++++++++++++ tests/reference/ast-ellipsis1-4f6c4dd.json | 13 +++++++ tests/reference/ast-ellipsis1-4f6c4dd.stdout | 1 + tests/tests.toml | 4 +++ 4 files changed, 55 insertions(+) create mode 100644 tests/parser/ellipsis1.py create mode 100644 tests/reference/ast-ellipsis1-4f6c4dd.json create mode 100644 tests/reference/ast-ellipsis1-4f6c4dd.stdout diff --git a/tests/parser/ellipsis1.py b/tests/parser/ellipsis1.py new file mode 100644 index 0000000000..23f4436da8 --- /dev/null +++ b/tests/parser/ellipsis1.py @@ -0,0 +1,37 @@ +import numpy as np +from typing import Callable + +array = np.random.rand(2, 2, 2, 2) +print(array[..., 0]) +print(array[Ellipsis, 0]) + +def inject(get_next_item: Callable[..., str]) -> None: + ... + +def foo(x: ...) -> None: + ... + +class flow: + def __understand__(self, name: str, value: ...) -> None: ... + +def foo(x = ...): + return x + +def test(): + ... + +class Todo: + ... + +x = [1, [2, [...], 3]] +l = [..., 1, 2, 3] + +if x is ...: + pass + +def partial(func: Callable[..., str], *args) -> Callable[..., str]: + pass + +class xyz: + abc: str = ... + def __init__(self, name: str=...) -> None: ... diff --git a/tests/reference/ast-ellipsis1-4f6c4dd.json b/tests/reference/ast-ellipsis1-4f6c4dd.json new file mode 100644 index 0000000000..0e042fd21c --- /dev/null +++ b/tests/reference/ast-ellipsis1-4f6c4dd.json @@ -0,0 +1,13 @@ +{ + "basename": "ast-ellipsis1-4f6c4dd", + "cmd": "lpython --show-ast --no-color {infile} -o {outfile}", + "infile": "tests/parser/ellipsis1.py", + "infile_hash": "24df29cba718c679016f3758a2eccafbeb9cfebd56265fd8da16bee1", + "outfile": null, + "outfile_hash": null, + "stdout": "ast-ellipsis1-4f6c4dd.stdout", + "stdout_hash": "99ae0e12fd8efcd71433e66f10ba7ba5b59e165ddd5ded0072593b12", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/ast-ellipsis1-4f6c4dd.stdout b/tests/reference/ast-ellipsis1-4f6c4dd.stdout new file mode 100644 index 0000000000..51aa9eb229 --- /dev/null +++ b/tests/reference/ast-ellipsis1-4f6c4dd.stdout @@ -0,0 +1 @@ +(Module [(Import [(numpy np)]) (ImportFrom typing [(Callable ())] 0) (Assign [(Name array Store)] (Call (Attribute (Attribute (Name np Load) random Load) rand Load) [(ConstantInt 2 ()) (ConstantInt 2 ()) (ConstantInt 2 ()) (ConstantInt 2 ())] []) ()) (Expr (Call (Name print Load) [(Subscript (Name array Load) (Tuple [(ConstantEllipsis ()) (ConstantInt 0 ())] Load) Load)] [])) (Expr (Call (Name print Load) [(Subscript (Name array Load) (Tuple [(Name Ellipsis Load) (ConstantInt 0 ())] Load) Load)] [])) (FunctionDef inject ([] [(get_next_item (Subscript (Name Callable Load) (Tuple [(ConstantEllipsis ()) (Name str Load)] Load) Load) ())] [] [] [] [] []) [(Expr (ConstantEllipsis ()))] [] (ConstantNone ()) ()) (FunctionDef foo ([] [(x (ConstantEllipsis ()) ())] [] [] [] [] []) [(Expr (ConstantEllipsis ()))] [] (ConstantNone ()) ()) (ClassDef flow [] [] [(FunctionDef __understand__ ([] [(self () ()) (name (Name str Load) ()) (value (ConstantEllipsis ()) ())] [] [] [] [] []) [(Expr (ConstantEllipsis ()))] [] (ConstantNone ()) ())] []) (FunctionDef foo ([] [(x () ())] [] [] [] [] [(ConstantEllipsis ())]) [(Return (Name x Load))] [] () ()) (FunctionDef test ([] [] [] [] [] [] []) [(Expr (ConstantEllipsis ()))] [] () ()) (ClassDef Todo [] [] [(Expr (ConstantEllipsis ()))] []) (Assign [(Name x Store)] (List [(ConstantInt 1 ()) (List [(ConstantInt 2 ()) (List [(ConstantEllipsis ())] Load) (ConstantInt 3 ())] Load)] Load) ()) (Assign [(Name l Store)] (List [(ConstantEllipsis ()) (ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load) ()) (If (Compare (Name x Load) Is [(ConstantEllipsis ())]) [(Pass)] []) (FunctionDef partial ([] [(func (Subscript (Name Callable Load) (Tuple [(ConstantEllipsis ()) (Name str Load)] Load) Load) ())] [(args () ())] [] [] [] []) [(Pass)] [] (Subscript (Name Callable Load) (Tuple [(ConstantEllipsis ()) (Name str Load)] Load) Load) ()) (ClassDef xyz [] [] [(AnnAssign (Name abc Store) (Name str Load) (ConstantEllipsis ()) 1) (FunctionDef __init__ ([] [(self () ()) (name (Name str Load) ())] [] [] [] [] [(ConstantEllipsis ())]) [(Expr (ConstantEllipsis ()))] [] (ConstantNone ()) ())] [])] []) diff --git a/tests/tests.toml b/tests/tests.toml index e0f45a8a2a..d2b205f55d 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -318,6 +318,10 @@ ast_new = true filename = "parser/slice1.py" ast_new = true +[[test]] +filename = "parser/ellipsis1.py" +ast = true + # tests/errors [[test]]