Skip to content

Commit cc69620

Browse files
committed
Updated version to 1.0.1 which includes compound declarations
1 parent 2b3f3fb commit cc69620

File tree

11 files changed

+90
-29
lines changed

11 files changed

+90
-29
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module Java
3939
| ModularUnit(Import* imports, Module body)
4040
4141
declaration = EmptyDecl()
42+
| CompoundDecl(declaration* body)
4243
| Package(Annotation* annotations, qname name)
4344
| Import(bool? static, qname name, bool? on_demand)
4445
| Module(bool? open, qname name, directive* directives)
@@ -90,7 +91,7 @@ module Java
9091
Block body, catch* catches, Block? final)
9192
| Assert(expr test, expr? msg)
9293
| Throw(expr exc)
93-
| Expression(expr value)
94+
| Expr(expr value)
9495
| Return(expr? value)
9596
| Yield(expr value)
9697
| Break(identifier? label)

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "java-ast"
10-
version = "1.0.0"
10+
version = "1.0.1"
1111
authors = [
1212
{ name = "Marius Smytzek", email = "marius.smytzek@cispa.de" },
1313
{ name = "Martin Eberlein", email = "martin.eberlein@hu-berlin.de" },
@@ -28,6 +28,9 @@ dependencies = [
2828
"antlr4-python3-runtime>=4.13.2",
2929
]
3030

31+
[tool.setuptools.packages.find]
32+
where = ["src"]
33+
3134
[project.urls]
3235
"Homepage" = "https://github.com/smythi93/jast"
3336
"Bug Tracker" = "https://github.com/smythi93/jast/issues"

specification.asdl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Java
77
| ModularUnit(Import* imports, Module body)
88

99
declaration = EmptyDecl()
10+
| CompoundDecl(declaration* body)
1011
| Package(Annotation* annotations, qname name)
1112
| Import(bool? static, qname name, bool? on_demand)
1213
| Module(bool? open, qname name, directive* directives)
@@ -58,7 +59,7 @@ module Java
5859
Block body, catch* catches, Block? final)
5960
| Assert(expr test, expr? msg)
6061
| Throw(expr exc)
61-
| Expression(expr value)
62+
| Expr(expr value)
6263
| Return(expr? value)
6364
| Yield(expr value)
6465
| Break(identifier? label)

src/jast/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
ModularUnit,
3232
declaration,
3333
EmptyDecl,
34+
CompoundDecl,
3435
Package,
3536
Import,
3637
Module,
@@ -67,7 +68,7 @@
6768
TryWithResources,
6869
Assert,
6970
Throw,
70-
Expression,
71+
Expr,
7172
Return,
7273
Yield,
7374
Break,
@@ -211,6 +212,7 @@
211212
"ModularUnit",
212213
"declaration",
213214
"EmptyDecl",
215+
"CompoundDecl",
214216
"Package",
215217
"Import",
216218
"Module",
@@ -247,7 +249,7 @@
247249
"TryWithResources",
248250
"Assert",
249251
"Throw",
250-
"Expression",
252+
"Expr",
251253
"Return",
252254
"Yield",
253255
"Break",

src/jast/_jast.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
19301930
yield "body", self.body
19311931

19321932

1933-
class Expression(stmt):
1933+
class Expr(stmt):
19341934
"""
19351935
Represents an exc statement in the Java AST.
19361936
@@ -1940,7 +1940,7 @@ class Expression(stmt):
19401940
def __init__(self, value: expr = None, *vargs, **kwargs):
19411941
super().__init__(*vargs, **kwargs)
19421942
if value is None:
1943-
raise JASTError("value is required for Expression")
1943+
raise JASTError("value is required for Expr")
19441944
self.value = value
19451945

19461946
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
@@ -2497,6 +2497,22 @@ class EmptyDecl(declaration):
24972497
"""
24982498

24992499

2500+
class CompoundDecl(declaration):
2501+
"""
2502+
Represents a compound decl in the Java AST.
2503+
2504+
<decl> <decl> ...
2505+
"""
2506+
2507+
def __init__(self, body: List[declaration] = None, *vargs, **kwargs):
2508+
super().__init__(*vargs, **kwargs)
2509+
self.body = body or []
2510+
2511+
def __iter__(self) -> Iterator[Tuple[str, JAST | List[JAST]]]:
2512+
if self.body:
2513+
yield "body", self.body
2514+
2515+
25002516
# Package decl
25012517

25022518

src/jast/_parser/_convert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ def visitStatement(self, ctx: JavaParser.StatementContext) -> jast.stmt:
13001300
**self._get_location_rule(ctx),
13011301
)
13021302
elif ctx.statementExpression:
1303-
return jast.Expression(
1303+
return jast.Expr(
13041304
value=self.visitExpression(ctx.statementExpression),
13051305
**self._get_location_rule(ctx),
13061306
)
@@ -1417,7 +1417,7 @@ def visitSwitchLabel(self, ctx: JavaParser.SwitchLabelContext) -> jast.switchlab
14171417

14181418
def visitForInit(
14191419
self, ctx: JavaParser.ForInitContext
1420-
) -> jast.LocalVariable | List[jast.Expression]:
1420+
) -> jast.LocalVariable | List[jast.Expr]:
14211421
if ctx.localVariableDeclaration():
14221422
return self.visitLocalVariableDeclaration(ctx.localVariableDeclaration())
14231423
else:
@@ -1428,7 +1428,7 @@ def visitParExpr(self, ctx: JavaParser.ParExprContext) -> jast.expr:
14281428

14291429
def visitExpressionList(
14301430
self, ctx: JavaParser.ExpressionListContext
1431-
) -> List[jast.Expression]:
1431+
) -> List[jast.Expr]:
14321432
return [self.visitExpression(expression) for expression in ctx.expression()]
14331433

14341434
def visitMethodCall(self, ctx: JavaParser.MethodCallContext) -> jast.Call:

src/jast/_unparse.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ def visit_Labeled(self, node: jast.Labeled):
755755
self.write(":")
756756
self.visit(node.body)
757757

758-
def visit_Expression(self, node: jast.Expression):
758+
def visit_Expr(self, node: jast.Expr):
759759
self.fill()
760760
self.visit(node.value)
761761
self.write(";")
@@ -961,6 +961,9 @@ def visit_Import(self, node: jast.Import):
961961
def visit_EmptyDecl(self, node: jast.EmptyDecl):
962962
self.fill(";")
963963

964+
def visit_CompoundDecl(self, node):
965+
self.traverse(node.body)
966+
964967
def visit_Field(self, node: jast.Field):
965968
self.fill()
966969
self.interleave(node.modifiers, " ", end=" ")

src/jast/_visitors.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class JNodeVisitor:
1414

1515
# Declarations
1616
def visit_EmptyDecl(self, node: jast.EmptyDecl): ...
17+
def visit_CompoundDecl(self, node: jast.CompoundDecl): ...
1718
def visit_Package(self, node: jast.Package): ...
1819
def visit_Import(self, node: jast.Import): ...
1920
def visit_Module(self, node: jast.Module): ...
@@ -52,7 +53,7 @@ class JNodeVisitor:
5253
def visit_TryWithResources(self, node: jast.TryWithResources): ...
5354
def visit_Assert(self, node: jast.Assert): ...
5455
def visit_Throw(self, node: jast.Throw): ...
55-
def visit_Expression(self, node: jast.Expression): ...
56+
def visit_Expr(self, node: jast.Expr): ...
5657
def visit_Return(self, node: jast.Return): ...
5758
def visit_Yield(self, node: jast.Yield): ...
5859
def visit_Break(self, node: jast.Break): ...

tests/test_jast.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,16 +1497,16 @@ def test_Labeled_error(self):
14971497
self.assertRaises(JASTError, jast.Labeled, label=jast.identifier("foo"))
14981498

14991499
def test_Expression(self):
1500-
expression = jast.Expression(
1500+
expression = jast.Expr(
15011501
value=jast.Constant(jast.IntLiteral(42)),
15021502
)
1503-
self.assertIsInstance(expression, jast.Expression)
1503+
self.assertIsInstance(expression, jast.Expr)
15041504
self.assertIsInstance(expression, jast.JAST)
15051505
self._test_int_constant(expression.value, 42)
15061506
self._test_iteration(expression)
15071507

15081508
def test_Expression_error(self):
1509-
self.assertRaises(JASTError, jast.Expression)
1509+
self.assertRaises(JASTError, jast.Expr)
15101510

15111511
def test_If(self):
15121512
if_ = jast.If(
@@ -2088,6 +2088,15 @@ def test_Yield(self):
20882088
def test_Yield_error(self):
20892089
self.assertRaises(JASTError, jast.Yield)
20902090

2091+
def test_CompoundDecl(self):
2092+
compound_decl = jast.CompoundDecl(body=[jast.EmptyDecl(), jast.EmptyDecl()])
2093+
self.assertIsInstance(compound_decl, jast.CompoundDecl)
2094+
self.assertIsInstance(compound_decl, jast.JAST)
2095+
self.assertEqual(2, len(compound_decl.body))
2096+
self.assertIsInstance(compound_decl.body[0], jast.EmptyDecl)
2097+
self.assertIsInstance(compound_decl.body[1], jast.EmptyDecl)
2098+
self._test_iteration(compound_decl)
2099+
20912100
def test_EmptyDecl(self):
20922101
empty_decl = jast.EmptyDecl()
20932102
self.assertIsInstance(empty_decl, jast.EmptyDecl)

tests/test_parse.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,7 @@ def test_Labeled(self):
17951795

17961796
def test_Expression(self):
17971797
tree = jast.parse("x;", jast.ParseMode.STMT)
1798-
self.assertIsInstance(tree, jast.Expression)
1798+
self.assertIsInstance(tree, jast.Expr)
17991799
self._test_name(tree.value, "x")
18001800

18011801
def test_If(self):
@@ -1863,7 +1863,7 @@ def test_Switch(self):
18631863
self.assertIsInstance(label, jast.Case)
18641864
self._test_int_constant(label.guard, 24)
18651865
self.assertEqual(2, len(group.body))
1866-
self.assertIsInstance(group.body[0], jast.Expression)
1866+
self.assertIsInstance(group.body[0], jast.Expr)
18671867
self.assertIsInstance(group.body[1], jast.Return)
18681868
group = block.groups[1]
18691869
self.assertIsInstance(group, jast.switchgroup)
@@ -2481,7 +2481,7 @@ def test_Constructor(self):
24812481
self.assertIsInstance(tree.body, jast.Block)
24822482
self.assertEqual(1, len(tree.body.body))
24832483
stmt = tree.body.body[0]
2484-
self.assertIsInstance(stmt, jast.Expression)
2484+
self.assertIsInstance(stmt, jast.Expr)
24852485
expr = stmt.value
24862486
self.assertIsInstance(expr, jast.Call)
24872487
self.assertIsInstance(expr.func, jast.Super)
@@ -2503,7 +2503,7 @@ def test_Constructor_compact(self):
25032503
self.assertIsInstance(tree.body, jast.Block)
25042504
self.assertEqual(1, len(tree.body.body))
25052505
stmt = tree.body.body[0]
2506-
self.assertIsInstance(stmt, jast.Expression)
2506+
self.assertIsInstance(stmt, jast.Expr)
25072507
expr = stmt.value
25082508
self.assertIsInstance(expr, jast.Call)
25092509
self.assertIsInstance(expr.func, jast.Super)
@@ -2519,7 +2519,7 @@ def test_Initializer(self):
25192519
self.assertIsInstance(tree.body, jast.Block)
25202520
self.assertEqual(1, len(tree.body.body))
25212521
stmt = tree.body.body[0]
2522-
self.assertIsInstance(stmt, jast.Expression)
2522+
self.assertIsInstance(stmt, jast.Expr)
25232523
self._test_name(stmt.value, "x")
25242524

25252525
def test_Initializer_static(self):
@@ -2532,7 +2532,7 @@ def test_Initializer_static(self):
25322532
self.assertIsInstance(tree.body, jast.Block)
25332533
self.assertEqual(1, len(tree.body.body))
25342534
stmt = tree.body.body[0]
2535-
self.assertIsInstance(stmt, jast.Expression)
2535+
self.assertIsInstance(stmt, jast.Expr)
25362536
self._test_name(stmt.value, "x")
25372537

25382538
def test_Interface(self):

0 commit comments

Comments
 (0)