From cda14ca3b8ff87781ef5036264826c281f5ea6bb Mon Sep 17 00:00:00 2001
From: Diego Restrepo
Date: Thu, 30 Jan 2025 00:54:33 +0000
Subject: [PATCH] fix(grammar): The TSG grammar for Java does not handle the
static_initializer node
- Add the static_initializer node for the Java grammar, this node may
contain multiple statements, such as initializing variables or
executing functions
---
.../src/stack-graphs.tsg | 14 ++++++++++++++
.../test/decl/static_initializer.java | 9 +++++++++
2 files changed, 23 insertions(+)
create mode 100644 languages/tree-sitter-stack-graphs-java/test/decl/static_initializer.java
diff --git a/languages/tree-sitter-stack-graphs-java/src/stack-graphs.tsg b/languages/tree-sitter-stack-graphs-java/src/stack-graphs.tsg
index 5f3c72466..6595e1ace 100644
--- a/languages/tree-sitter-stack-graphs-java/src/stack-graphs.tsg
+++ b/languages/tree-sitter-stack-graphs-java/src/stack-graphs.tsg
@@ -227,6 +227,20 @@ attribute node_symbol = node => symbol = (source-text node), source_n
edge @declaration.lexical_scope -> @class_body.lexical_scope
}
+(class_body
+ (static_initializer) @static_init
+) @class_body
+{
+ node @static_init.defs
+ node @static_init.static_defs
+ node @static_init.lexical_scope
+ node @static_init.before_scope
+
+ edge @static_init.before_scope -> @class_body.lexical_scope
+ edge @static_init.lexical_scope -> @static_init.before_scope
+}
+
+
(class_body (block) @block) {
node @block.defs
node @block.static_defs
diff --git a/languages/tree-sitter-stack-graphs-java/test/decl/static_initializer.java b/languages/tree-sitter-stack-graphs-java/test/decl/static_initializer.java
new file mode 100644
index 000000000..047cb3e61
--- /dev/null
+++ b/languages/tree-sitter-stack-graphs-java/test/decl/static_initializer.java
@@ -0,0 +1,9 @@
+class TestClass {
+ static {
+ System.out.println("Static initializer block executed!");
+ }
+
+ public void printMessage() {
+ System.out.println("Hello from TestClass!");
+ }
+}