Skip to content

Commit ea9e7a4

Browse files
committed
added tests for parsing stmts
1 parent 9049f96 commit ea9e7a4

File tree

10 files changed

+580
-166
lines changed

10 files changed

+580
-166
lines changed

specification.asdl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ module Java
3939
| Block(stmt* body)
4040
| Compound(stmt* body)
4141

42-
| LocalClass(Class decl)
43-
| LocalInterface(Interface decl)
44-
| LocalRecord(Record decl)
42+
| LocalType(Class | Interface | Record decl)
4543
| LocalVariable(modifier* modifiers, jtype type, declarator* variables)
4644

4745
| Labeled(identifier label, stmt body)

src/jast/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@
5454
Empty,
5555
Block,
5656
Compound,
57-
LocalClass,
58-
LocalInterface,
59-
LocalRecord,
57+
LocalType,
6058
LocalVariable,
6159
Labeled,
6260
If,
@@ -236,9 +234,7 @@
236234
"Empty",
237235
"Block",
238236
"Compound",
239-
"LocalClass",
240-
"LocalInterface",
241-
"LocalRecord",
237+
"LocalType",
242238
"LocalVariable",
243239
"Labeled",
244240
"If",

src/jast/_jast.py

Lines changed: 48 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
370370

371371
class elementarrayinit(JAST):
372372
"""
373-
Represents an element-value array initializer in the Java AST.
373+
Represents an element-value array init in the Java AST.
374374
375375
{ <value>, <value>, ... }
376376
"""
@@ -1343,7 +1343,7 @@ class NewArray(expr):
13431343
Represents a new array creation in the Java AST.
13441344
13451345
new <jtype><expr_dim><expr_dim>...[<dim><dim>...]
1346-
new <jtype><dim><dim>... [<initializer>]
1346+
new <jtype><dim><dim>... [<init>]
13471347
"""
13481348

13491349
# noinspection PyShadowingBuiltins
@@ -1352,30 +1352,30 @@ def __init__(
13521352
type: jtype = None,
13531353
expr_dims: List[expr] = None,
13541354
dims: List[dim] = None,
1355-
initializer: "arrayinit" = None,
1355+
init: "arrayinit" = None,
13561356
*vargs,
13571357
**kwargs,
13581358
):
13591359
super().__init__(*vargs, **kwargs)
13601360
if type is None:
13611361
raise JASTError("jtype is required for ArrayCreation")
1362-
if expr_dims and initializer:
1362+
if expr_dims and init:
13631363
raise JASTError(
1364-
"expr_dims and initializer are mutually exclusive for ArrayCreation"
1364+
"expr_dims and init are mutually exclusive for ArrayCreation"
13651365
)
13661366
self.type = type
13671367
self.expr_dims = expr_dims or []
13681368
self.dims = dims or []
1369-
self.initializer = initializer
1369+
self.init = init
13701370

13711371
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
13721372
yield "type", self.type
13731373
if self.expr_dims:
13741374
yield "expr_dims", self.expr_dims
13751375
if self.dims:
13761376
yield "dims", self.dims
1377-
if self.initializer:
1378-
yield "initializer", self.initializer
1377+
if self.init:
1378+
yield "init", self.init
13791379

13801380

13811381
class switchexplabel(JAST, abc.ABC):
@@ -1677,7 +1677,7 @@ def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
16771677

16781678
class arrayinit(JAST):
16791679
"""
1680-
Represents an array initializer in the Java AST.
1680+
Represents an array init in the Java AST.
16811681
16821682
{ <value>, <value>, ... }
16831683
"""
@@ -1857,55 +1857,23 @@ def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
18571857
yield "body", self.body
18581858

18591859

1860-
class LocalClass(stmt):
1861-
"""
1862-
Represents a local class decl in the Java AST.
1863-
1864-
class { <decl> <decl> ... }
1865-
"""
1866-
1867-
def __init__(self, decl: "Class" = None, *vargs, **kwargs):
1868-
super().__init__(*vargs, **kwargs)
1869-
if decl is None:
1870-
raise JASTError("decl is required for LocalClass")
1871-
self.declaration = decl
1872-
1873-
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
1874-
yield "declaration", self.declaration
1875-
1876-
1877-
class LocalInterface(stmt):
1878-
"""
1879-
Represents a local interface decl in the Java AST.
1880-
1881-
interface { <decl> <decl> ... }
1882-
"""
1883-
1884-
def __init__(self, decl: "Interface" = None, *vargs, **kwargs):
1885-
super().__init__(*vargs, **kwargs)
1886-
if decl is None:
1887-
raise JASTError("decl is required for LocalInterface")
1888-
self.declaration = decl
1889-
1890-
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
1891-
yield "declaration", self.declaration
1892-
1893-
1894-
class LocalRecord(stmt):
1860+
class LocalType(stmt):
18951861
"""
1896-
Represents a local record decl in the Java AST.
1862+
Represents a local type (class, interface, or record) decl in the Java AST.
18971863
1898-
record { <decl> <decl> ... }
1864+
<decl>
18991865
"""
19001866

1901-
def __init__(self, decl: "Record" = None, *vargs, **kwargs):
1867+
def __init__(
1868+
self, decl: Union["Class", "Interface", "Record"] = None, *vargs, **kwargs
1869+
):
19021870
super().__init__(*vargs, **kwargs)
19031871
if decl is None:
1904-
raise JASTError("decl is required for LocalRecord")
1905-
self.declaration = decl
1872+
raise JASTError("decl is required for LocalType")
1873+
self.decl = decl
19061874

19071875
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
1908-
yield "declaration", self.declaration
1876+
yield "decl", self.decl
19091877

19101878

19111879
class LocalVariable(stmt):
@@ -2020,7 +1988,7 @@ class Assert(stmt):
20201988
def __init__(self, test: expr = None, msg: expr = None, *vargs, **kwargs):
20211989
super().__init__(*vargs, **kwargs)
20221990
if test is None:
2023-
raise JASTError("condition is required for Assert")
1991+
raise JASTError("test is required for Assert")
20241992
self.test = test
20251993
self.msg = msg
20261994

@@ -2081,23 +2049,6 @@ class DefaultCase(switchlabel):
20812049
"""
20822050

20832051

2084-
class Throw(stmt):
2085-
"""
2086-
Represents a throw statement in the Java AST.
2087-
2088-
throw <exc>;
2089-
"""
2090-
2091-
def __init__(self, exc: expr = None, *vargs, **kwargs):
2092-
super().__init__(*vargs, **kwargs)
2093-
if exc is None:
2094-
raise JASTError("value is required for Throw")
2095-
self.exc = exc
2096-
2097-
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
2098-
yield "exc", self.exc
2099-
2100-
21012052
class switchgroup(JAST):
21022053
"""
21032054
Represents a group of switch labels in the Java AST.
@@ -2169,6 +2120,23 @@ def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
21692120
yield "body", self.body
21702121

21712122

2123+
class Throw(stmt):
2124+
"""
2125+
Represents a throw statement in the Java AST.
2126+
2127+
throw <exc>;
2128+
"""
2129+
2130+
def __init__(self, exc: expr = None, *vargs, **kwargs):
2131+
super().__init__(*vargs, **kwargs)
2132+
if exc is None:
2133+
raise JASTError("value is required for Throw")
2134+
self.exc = exc
2135+
2136+
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
2137+
yield "exc", self.exc
2138+
2139+
21722140
class While(stmt):
21732141
"""
21742142
Represents a while statement in the Java AST.
@@ -2447,7 +2415,7 @@ def __init__(
24472415
if type is None:
24482416
raise JASTError("type is required for resource")
24492417
if variable is None:
2450-
raise JASTError("declarator is required for resource")
2418+
raise JASTError("variable is required for resource")
24512419
self.modifiers = modifiers or []
24522420
self.type = type
24532421
self.variable = variable
@@ -2761,27 +2729,27 @@ class declarator(JAST):
27612729
"""
27622730
Represents a variable declarator in the Java AST.
27632731
2764-
<label> [= <initializer>]
2732+
<label> [= <init>]
27652733
"""
27662734

27672735
# noinspection PyShadowingBuiltins
27682736
def __init__(
27692737
self,
27702738
id: variabledeclaratorid = None,
2771-
initializer: expr | arrayinit = None,
2739+
init: expr | arrayinit = None,
27722740
*vargs,
27732741
**kwargs,
27742742
):
27752743
super().__init__(*vargs, **kwargs)
27762744
if id is None:
27772745
raise JASTError("id_ is required for declarator")
27782746
self.id = id
2779-
self.initializer = initializer
2747+
self.init = init
27802748

27812749
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
27822750
yield "id", self.id
2783-
if self.initializer:
2784-
yield "initializer", self.initializer
2751+
if self.init:
2752+
yield "init", self.init
27852753

27862754

27872755
class Field(declaration):
@@ -2922,7 +2890,7 @@ def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
29222890

29232891
class Initializer(declaration):
29242892
"""
2925-
Represents an initializer in the Java AST.
2893+
Represents an init in the Java AST.
29262894
29272895
{ <statement> <statement> ... }
29282896
static { <statement> <statement> ... }
@@ -2966,7 +2934,7 @@ def __init__(
29662934
raise JASTError("qname is required for Interface")
29672935
self.modifiers = modifiers or []
29682936
self.id = id
2969-
self.type_params = type_params or []
2937+
self.type_params = type_params
29702938
self.extends = extends or []
29712939
self.permits = permits or []
29722940
self.body = body or []
@@ -3229,18 +3197,18 @@ def __init__(
32293197
):
32303198
super().__init__(*vargs, **kwargs)
32313199
if id is None:
3232-
raise JASTError("qname is required for Record")
3200+
raise JASTError("id is required for Record")
32333201
self.modifiers = modifiers or []
3234-
self.name = id
3235-
self.type_params = type_params or []
3202+
self.id = id
3203+
self.type_params = type_params
32363204
self.components = components or []
32373205
self.implements = implements or []
32383206
self.body = body or []
32393207

32403208
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
32413209
if self.modifiers:
32423210
yield "modifiers", self.modifiers
3243-
yield "qname", self.name
3211+
yield "id", self.id
32443212
if self.type_params:
32453213
yield "type_params", self.type_params
32463214
if self.components:

0 commit comments

Comments
 (0)