Skip to content

Commit 388bee0

Browse files
authored
Merge pull request #194 from github/ça-va-java
Java: Define & reference labels
2 parents 69f9cff + a7f6cdf commit 388bee0

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

languages/tree-sitter-stack-graphs-java/src/stack-graphs.tsg

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,36 @@ attribute node_symbol = node => symbol = (source-text node), source_n
509509
edge @block.after_scope -> @last.after_scope
510510
}
511511

512-
(break_statement) @stmt {
513-
; FIXME: labels
512+
(break_statement (identifier) @name) @this {
513+
edge @this.after_scope -> @this.before_scope
514+
}
515+
516+
(break_statement (identifier) @name) @stmt {
517+
node ref
518+
node ns
519+
520+
attr (ref) node_reference = @name
521+
edge ref -> ns
522+
523+
attr (ns) push_symbol = "%Label"
524+
525+
edge ns -> @stmt.before_scope
514526
}
515527

516-
(continue_statement) @stmt {
517-
; FIXME: labels
528+
(continue_statement) @this {
529+
edge @this.after_scope -> @this.before_scope
530+
}
531+
532+
(continue_statement (identifier) @name) @this {
533+
node ref
534+
node ns
535+
536+
attr (ref) node_reference = @name
537+
edge ref -> ns
538+
539+
attr (ns) push_symbol = "%Label"
540+
541+
edge ns -> @this.before_scope
518542
}
519543

520544
(declaration) @decl {}
@@ -543,20 +567,26 @@ attribute node_symbol = node => symbol = (source-text node), source_n
543567
edge @stmt.after_scope -> @stmt.before_scope
544568
}
545569

570+
(for_statement) @this {
571+
edge @this.after_scope -> @this.before_scope
572+
}
573+
574+
(for_statement !init !condition !update body: (_) @body) @this {
575+
edge @body.before_scope -> @this.before_scope
576+
}
577+
546578
(for_statement init: (expression) @init condition: (_) @condition update: (_) @update body: (_) @body) @stmt {
547579
edge @init.lexical_scope -> @stmt.before_scope
548580
edge @condition.lexical_scope -> @stmt.before_scope
549581
edge @update.lexical_scope -> @stmt.before_scope
550582
edge @body.before_scope -> @stmt.before_scope
551-
edge @stmt.after_scope -> @stmt.before_scope
552583
}
553584

554585
(for_statement init: (local_variable_declaration) @init condition: (_) @condition update: (_) @update body: (_) @body) @stmt {
555586
edge @init.before_scope -> @stmt.before_scope
556587
edge @condition.lexical_scope -> @init.after_scope
557588
edge @update.lexical_scope -> @init.after_scope
558589
edge @body.before_scope -> @init.after_scope
559-
edge @stmt.after_scope -> @stmt.before_scope
560590
}
561591

562592
(if_statement condition: (_) @condition consequence: (_) @consequence) @stmt {
@@ -572,7 +602,14 @@ attribute node_symbol = node => symbol = (source-text node), source_n
572602
(labeled_statement (identifier) @name (statement) @child) @stmt {
573603
edge @child.before_scope -> @stmt.before_scope
574604
edge @stmt.after_scope -> @child.after_scope
575-
; FIXME: labels
605+
606+
node def
607+
attr (def) node_definition = @name
608+
node ns
609+
attr (ns) pop_symbol = "%Label"
610+
edge ns -> def
611+
612+
edge @stmt.before_scope -> ns
576613
}
577614

578615
(local_variable_declaration
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class A {
2+
void f() {
3+
foo: for(;;) {
4+
for(;;) {
5+
break foo;
6+
// ^ defined: 3
7+
}
8+
}
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class A {
2+
void f() {
3+
foo: for(;;) {
4+
for(;;) {
5+
continue foo;
6+
// ^ defined: 3
7+
}
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)