Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lpython/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -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, @$); }
Expand Down
4 changes: 4 additions & 0 deletions src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
35 changes: 35 additions & 0 deletions tests/parser/ellipsis2.py
Original file line number Diff line number Diff line change
@@ -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:
...
13 changes: 13 additions & 0 deletions tests/reference/ast_new-ellipsis2-3a9750b.json
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions tests/reference/ast_new-ellipsis2-3a9750b.stdout
Original file line number Diff line number Diff line change
@@ -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 ()) ())] [])
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ ast_new = true
filename = "parser/ellipsis1.py"
ast = true

[[test]]
filename = "parser/ellipsis2.py"
ast_new = true

# tests/errors

[[test]]
Expand Down