From fa455430774939321666d6ed6f0ad3879a70f85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aksh=C4=81nsh?= <53227127+akshanshbhatt@users.noreply.github.com> Date: Sun, 29 May 2022 19:56:32 +0530 Subject: [PATCH] Add parsing rules for `...` and `None` with tests --- src/lpython/parser/parser.yy | 2 ++ src/lpython/parser/semantics.h | 4 +++ tests/parser/ellipsis2.py | 35 +++++++++++++++++++ .../reference/ast_new-ellipsis2-3a9750b.json | 13 +++++++ .../ast_new-ellipsis2-3a9750b.stdout | 1 + tests/tests.toml | 4 +++ 6 files changed, 59 insertions(+) create mode 100644 tests/parser/ellipsis2.py create mode 100644 tests/reference/ast_new-ellipsis2-3a9750b.json create mode 100644 tests/reference/ast_new-ellipsis2-3a9750b.stdout diff --git a/src/lpython/parser/parser.yy b/src/lpython/parser/parser.yy index 1c94e848bd..0875d3b61a 100644 --- a/src/lpython/parser/parser.yy +++ b/src/lpython/parser/parser.yy @@ -681,6 +681,8 @@ expr | TK_IMAG_NUM { $$ = COMPLEX($1, @$); } | TK_TRUE { $$ = BOOL(true, @$); } | TK_FALSE { $$ = BOOL(false, @$); } + | KW_NONE { $$ = NONE(@$); } + | TK_ELLIPSIS { $$ = ELLIPSIS(@$); } | "(" expr ")" { $$ = $2; } | function_call { $$ = $1; } | "[" expr_list_opt "]" { $$ = LIST($2, @$); } diff --git a/src/lpython/parser/semantics.h b/src/lpython/parser/semantics.h index cc0539c193..0ceb5afee2 100644 --- a/src/lpython/parser/semantics.h +++ b/src/lpython/parser/semantics.h @@ -457,6 +457,10 @@ expr_t* CHECK_TUPLE(expr_t *x) { } } +#define ELLIPSIS(l) make_ConstantEllipsis_t(p.m_a, l, nullptr) + +#define NONE(l) make_ConstantNone_t(p.m_a, l, nullptr) + #define TUPLE(elts, l) make_Tuple_t(p.m_a, l, \ EXPRS(elts), elts.size(), expr_contextType::Load) #define SUBSCRIPT_01(value, slice, l) make_Subscript_t(p.m_a, l, \ diff --git a/tests/parser/ellipsis2.py b/tests/parser/ellipsis2.py new file mode 100644 index 0000000000..489d16fcd4 --- /dev/null +++ b/tests/parser/ellipsis2.py @@ -0,0 +1,35 @@ +import numpy as np +from typing import Callable + + +print(...) + +# TODO: Make this work +# def bar(x = ...): +# return x + +array = np.random.rand(2, 2, 2, 2) +print(array[..., 0]) +print(array[Ellipsis, 0]) + + +def test1(): + ... + + +def test2() -> None: + x = [1, [2, [...], 3]] + l = [..., 1, 2, 3] + ... + + +array = np.random.rand(2, 2, 2, 2) +print(array[..., 0]) + + +def foo(x: ...) -> None: + ... + + +def inject(get_next_item: Callable[..., str]) -> None: + ... diff --git a/tests/reference/ast_new-ellipsis2-3a9750b.json b/tests/reference/ast_new-ellipsis2-3a9750b.json new file mode 100644 index 0000000000..8247382fd5 --- /dev/null +++ b/tests/reference/ast_new-ellipsis2-3a9750b.json @@ -0,0 +1,13 @@ +{ + "basename": "ast_new-ellipsis2-3a9750b", + "cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}", + "infile": "tests/parser/ellipsis2.py", + "infile_hash": "2e6669bafe4247887d3cd6d9f479ef9c02de96d2a018df4a716ae259", + "outfile": null, + "outfile_hash": null, + "stdout": "ast_new-ellipsis2-3a9750b.stdout", + "stdout_hash": "fea13952d8c4d5fda5c9ceb115f42765ff0855c332776a5589037bad", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/ast_new-ellipsis2-3a9750b.stdout b/tests/reference/ast_new-ellipsis2-3a9750b.stdout new file mode 100644 index 0000000000..cc4da52a66 --- /dev/null +++ b/tests/reference/ast_new-ellipsis2-3a9750b.stdout @@ -0,0 +1 @@ +(Module [(Import [(numpy np)]) (ImportFrom typing [(Callable ())] 0) (Expr (Call (Name print Load) [(ConstantEllipsis ())] [])) (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 test1 ([] [] [] [] [] [] []) [(Expr (ConstantEllipsis ()))] [] () ()) (FunctionDef test2 ([] [] [] [] [] [] []) [(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) ()) (Expr (ConstantEllipsis ()))] [] (ConstantNone ()) ()) (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)] [])) (FunctionDef foo ([] [(x (ConstantEllipsis ()) ())] [] [] [] [] []) [(Expr (ConstantEllipsis ()))] [] (ConstantNone ()) ()) (FunctionDef inject ([] [(get_next_item (Subscript (Name Callable Load) (Tuple [(ConstantEllipsis ()) (Name str Load)] Load) Load) ())] [] [] [] [] []) [(Expr (ConstantEllipsis ()))] [] (ConstantNone ()) ())] []) diff --git a/tests/tests.toml b/tests/tests.toml index a1e8fbe03e..2f54e0d2dc 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -335,6 +335,10 @@ ast_new = true filename = "parser/ellipsis1.py" ast = true +[[test]] +filename = "parser/ellipsis2.py" +ast_new = true + # tests/errors [[test]]