Skip to content

Commit 5d0cdcd

Browse files
committed
added tests for jast decls and mods
1 parent 134b411 commit 5d0cdcd

File tree

6 files changed

+796
-51
lines changed

6 files changed

+796
-51
lines changed

src/jast/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
jast
33
~~~
44
5-
The `jast` module helps Python applications to process trees of the Java
5+
The `jast` body helps Python applications to process trees of the Java
66
abstract syntax grammar.
77
88
An abstract syntax tree can be generated by using the `parse()`
9-
func from this module. The result will be a tree of objects whose
9+
func from this body. The result will be a tree of objects whose
1010
classes all inherit from `jast.JAST`.
1111
1212
A modified abstract syntax tree can be written back to Java source code
@@ -15,7 +15,7 @@
1515
1616
Additionally various helper functions are provided that make working with
1717
the trees simpler. The main intention of the helper functions and this
18-
module in general is to provide an easy to use interface for libraries
18+
body in general is to provide an easy to use interface for libraries
1919
that work tightly with Java.
2020
2121

src/jast/_jast.py

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This module contains the abstract syntax tree (AST) classes for the Java AST (JAST).
2+
This body contains the abstract syntax tree (AST) classes for the Java AST (JAST).
33
"""
44

55
import abc
@@ -2485,7 +2485,7 @@ def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
24852485
# noinspection PyPep8Naming
24862486
class declaration(_JAST, abc.ABC):
24872487
"""
2488-
Abstract base class for all declarations in the Java AST.
2488+
Abstract base class for all body in the Java AST.
24892489
"""
24902490

24912491

@@ -2496,9 +2496,6 @@ class EmptyDecl(declaration):
24962496
;
24972497
"""
24982498

2499-
def __init__(self, *vargs, **kwargs):
2500-
super().__init__(*vargs, **kwargs)
2501-
25022499

25032500
# Package decl
25042501

@@ -2522,7 +2519,7 @@ def __init__(
25222519
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
25232520
if self.annotations:
25242521
yield "annotations", self.annotations
2525-
yield "qname", self.name
2522+
yield "name", self.name
25262523

25272524

25282525
# Import Declarations
@@ -2554,15 +2551,15 @@ def __init__(
25542551
self.on_demand = on_demand
25552552

25562553
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
2557-
yield "qname", self.name
2554+
yield "name", self.name
25582555

25592556

25602557
# Module decl
25612558

25622559

25632560
class directive(_JAST, abc.ABC):
25642561
"""
2565-
Abstract base class for all module directives in the Java AST.
2562+
Abstract base class for all body body in the Java AST.
25662563
"""
25672564

25682565

@@ -2589,7 +2586,7 @@ def __init__(
25892586
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
25902587
if self.modifiers:
25912588
yield "modifiers", self.modifiers
2592-
yield "qname", self.name
2589+
yield "name", self.name
25932590

25942591

25952592
class Exports(directive):
@@ -2613,7 +2610,7 @@ def __init__(
26132610
self.to = to
26142611

26152612
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
2616-
yield "qname", self.name
2613+
yield "name", self.name
26172614
if self.to:
26182615
yield "to", self.to
26192616

@@ -2639,7 +2636,7 @@ def __init__(
26392636
self.to = to
26402637

26412638
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
2642-
yield "qname", self.name
2639+
yield "name", self.name
26432640
if self.to:
26442641
yield "to", self.to
26452642

@@ -2663,7 +2660,7 @@ def __init__(
26632660
self.name = name
26642661

26652662
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
2666-
yield "qname", self.name
2663+
yield "name", self.name
26672664

26682665

26692666
class Provides(directive):
@@ -2683,43 +2680,43 @@ def __init__(
26832680
super().__init__(*vargs, **kwargs)
26842681
if name is None:
26852682
raise JASTError("type is required for Provides")
2686-
if not with_:
2683+
if with_ is None:
26872684
raise JASTError("with_ is required for Provides")
26882685
self.name = name
26892686
self.with_ = with_
26902687

26912688
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
2692-
yield "qname", self.name
2693-
yield "with", self.with_
2689+
yield "name", self.name
2690+
yield "with_", self.with_
26942691

26952692

26962693
class Module(declaration):
26972694
"""
2698-
Represents a module decl in the Java AST.
2695+
Represents a body decl in the Java AST.
26992696
2700-
module <qname> { <directive> <directive> ... }
2697+
body <qname> { <directive> <directive> ... }
27012698
"""
27022699

27032700
# noinspection PyShadowingBuiltins
27042701
def __init__(
27052702
self,
27062703
open: bool = False,
27072704
name: qname = None,
2708-
directives: List[directive] = None,
2705+
body: List[directive] = None,
27092706
*vargs,
27102707
**kwargs,
27112708
):
27122709
super().__init__(*vargs, **kwargs)
2713-
if not name:
2710+
if name is None:
27142711
raise JASTError("qname is required for Module")
27152712
self.open = open
27162713
self.name = name
2717-
self.directives = directives or []
2714+
self.body = body or []
27182715

27192716
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
2720-
yield "open", self.open
2721-
if self.directives:
2722-
yield "directives", self.directives
2717+
yield "name", self.name
2718+
if self.body:
2719+
yield "body", self.body
27232720

27242721

27252722
# Field Declarations
@@ -2834,7 +2831,7 @@ def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
28342831
yield "return_type", self.return_type
28352832
yield "id", self.id
28362833
if self.parameters:
2837-
yield "args", self.parameters
2834+
yield "parameters", self.parameters
28382835
if self.dims:
28392836
yield "dims", self.dims
28402837
if self.throws:
@@ -2880,7 +2877,7 @@ def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
28802877
yield "type_params", self.type_params
28812878
yield "id", self.id
28822879
if self.parameters:
2883-
yield "args", self.parameters
2880+
yield "parameters", self.parameters
28842881
if self.body:
28852882
yield "body", self.body
28862883

@@ -3011,15 +3008,15 @@ def __init__(
30113008
if id is None:
30123009
raise JASTError("qname is required for AnnotationInterfaceDeclaration")
30133010
self.modifiers = modifiers or []
3014-
self.name = id
3011+
self.id = id
30153012
self.extends = extends or []
30163013
self.permits = permits or []
30173014
self.body = body or []
30183015

30193016
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
30203017
if self.modifiers:
30213018
yield "modifiers", self.modifiers
3022-
yield "qname", self.name
3019+
yield "id", self.id
30233020
if self.extends:
30243021
yield "extends", self.extends
30253022
if self.permits:
@@ -3082,7 +3079,7 @@ class enumconstant(JAST):
30823079
"""
30833080
Represents an enum constant in the Java AST.
30843081
3085-
<annotation>* <qname> [(<argument>, <argument>, ...)] [<body>]
3082+
<annotation>* <id> [(<argument>, <argument>, ...)] [<body>]
30863083
"""
30873084

30883085
# noinspection PyShadowingBuiltins
@@ -3099,14 +3096,14 @@ def __init__(
30993096
if id is None:
31003097
raise JASTError("qname is required for enumconstant")
31013098
self.annotations = annotations or []
3102-
self.name = id
3099+
self.id = id
31033100
self.args = args
31043101
self.body = body
31053102

31063103
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
31073104
if self.annotations:
31083105
yield "annotations", self.annotations
3109-
yield "qname", self.name
3106+
yield "id", self.id
31103107
if self.args:
31113108
yield "args", self.args
31123109
if self.body:
@@ -3135,15 +3132,15 @@ def __init__(
31353132
if id is None:
31363133
raise JASTError("qname is required for Enum")
31373134
self.modifiers = modifiers or []
3138-
self.name = id
3135+
self.id = id
31393136
self.implements = implements or []
31403137
self.constants = constants or []
31413138
self.body = body or []
31423139

31433140
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
31443141
if self.modifiers:
31453142
yield "modifiers", self.modifiers
3146-
yield "qname", self.name
3143+
yield "id", self.id
31473144
if self.implements:
31483145
yield "implements", self.implements
31493146
if self.constants:
@@ -3240,44 +3237,44 @@ def __init__(
32403237
self,
32413238
package: Package = None,
32423239
imports: List[Import] = None,
3243-
declarations: List[declaration] = None,
3240+
body: List[declaration] = None,
32443241
*vargs,
32453242
**kwargs,
32463243
):
32473244
super().__init__(*vargs, **kwargs)
32483245
self.package = package
32493246
self.imports = imports or []
3250-
self.declarations = declarations or []
3247+
self.body = body or []
32513248

32523249
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
32533250
if self.package:
32543251
yield "package", self.package
32553252
if self.imports:
32563253
yield "imports", self.imports
3257-
yield "declarations", self.declarations
3254+
yield "body", self.body
32583255

32593256

32603257
class ModularUnit(mod):
32613258
"""
32623259
Represents a modular compilation unit in the Java AST.
32633260
3264-
[<import> <import> ...] <module>
3261+
[<import> <import> ...] <body>
32653262
"""
32663263

32673264
def __init__(
32683265
self,
32693266
imports: List[Import] = None,
3270-
module: Module = None,
3267+
body: Module = None,
32713268
*vargs,
32723269
**kwargs,
32733270
):
32743271
super().__init__(*vargs, **kwargs)
3275-
if not module:
3276-
raise JASTError("module is required for ModularUnit")
3272+
if not body:
3273+
raise JASTError("body is required for ModularUnit")
32773274
self.imports = imports or []
3278-
self.module = module
3275+
self.body = body
32793276

32803277
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
32813278
if self.imports:
32823279
yield "imports", self.imports
3283-
yield "module", self.module
3280+
yield "body", self.body

src/jast/_parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def parse(src: str, mode: ParseMode | str | int = ParseMode.UNIT) -> JAST:
8383
:param mode: The parse mode used to identify the java code.
8484
The default is `ParseMode.UNIT` which is used to parse a complete Java compilation unit.
8585
Other modes are `ParseMode.DECL`, `ParseMode.STMT`, and `ParseMode.EXPR`, for parsing
86-
Java declarations, body, and expressions, respectively.
86+
Java body, body, and expressions, respectively.
8787
:return: The jAST representing the Java source code.
8888
"""
8989
return _parser.parse(src, mode)

src/jast/_parser/_convert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def visitOrdinaryCompilationUnit(
6060
return jast.CompilationUnit(
6161
package=package,
6262
imports=imports,
63-
declarations=declarations,
63+
body=declarations,
6464
**self._get_location_rule(ctx),
6565
)
6666

@@ -73,7 +73,7 @@ def visitModularCompilationUnit(
7373
]
7474
module = self.visitModuleDeclaration(ctx.moduleDeclaration())
7575
return jast.ModularUnit(
76-
imports=imports, module=module, **self._get_location_rule(ctx)
76+
imports=imports, body=module, **self._get_location_rule(ctx)
7777
)
7878

7979
def visitPackageDeclaration(
@@ -972,7 +972,7 @@ def visitModuleDeclaration(self, ctx: JavaParser.ModuleDeclarationContext):
972972
return jast.Module(
973973
open=open_,
974974
name=name,
975-
directives=directives,
975+
body=directives,
976976
**self._get_location_rule(ctx),
977977
)
978978

0 commit comments

Comments
 (0)