Skip to content

Commit

Permalink
Merge pull request #503 from certik/ellipsis
Browse files Browse the repository at this point in the history
Parse ellipsis (...) in the old parser
  • Loading branch information
certik authored May 18, 2022
2 parents a9fa066 + c57fa7b commit 7fe6666
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions grammar/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ module LPython
| ConstantBool(bool value, string? kind)
| 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)
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/lpython_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ 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)
elif isinstance(node.value, None.__class__):
new_node = python_ast.ConstantNone(node.kind)
else:
print(type(node.value))
raise Exception("Unsupported Constant type")
Expand Down
37 changes: 37 additions & 0 deletions tests/parser/ellipsis1.py
Original file line number Diff line number Diff line change
@@ -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: ...
13 changes: 13 additions & 0 deletions tests/reference/ast-ellipsis1-4f6c4dd.json
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions tests/reference/ast-ellipsis1-4f6c4dd.stdout
Original file line number Diff line number Diff line change
@@ -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 ()) ())] [])] [])
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ ast_new = true
filename = "parser/slice1.py"
ast_new = true

[[test]]
filename = "parser/ellipsis1.py"
ast = true

# tests/errors

[[test]]
Expand Down

0 comments on commit 7fe6666

Please sign in to comment.