forked from github/stack-graphs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-graphs.tsg
More file actions
1380 lines (1160 loc) · 36.2 KB
/
stack-graphs.tsg
File metadata and controls
1380 lines (1160 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; -*- coding: utf-8 -*-
;; ------------------------------------------------------------------------------------------------
;; Copyright © 2023, stack-graphs authors.
;; Licensed under either of Apache License, Version 2.0, or MIT license, at your option.
;; Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
;; ------------------------------------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stack graphs definition for Python
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Global Variables
;; ^^^^^^^^^^^^^^^^
global FILE_PATH
global ROOT_PATH = ""
global ROOT_NODE
global JUMP_TO_SCOPE_NODE
;; Attribute Shorthands
;; ^^^^^^^^^^^^^^^^^^^^
attribute node_definition = node => type = "pop_symbol", node_symbol = node, is_definition
attribute node_reference = node => type = "push_symbol", node_symbol = node, is_reference
attribute pop_node = node => type = "pop_symbol", node_symbol = node
attribute pop_scoped_node = node => type = "pop_scoped_symbol", node_symbol = node
attribute pop_scoped_symbol = symbol => type = "pop_scoped_symbol", symbol = symbol
attribute pop_symbol = symbol => type = "pop_symbol", symbol = symbol
attribute push_node = node => type = "push_symbol", node_symbol = node
attribute push_scoped_node = node => type = "push_scoped_symbol", node_symbol = node
attribute push_scoped_symbol = symbol => type = "push_scoped_symbol", symbol = symbol
attribute push_symbol = symbol => type = "push_symbol", symbol = symbol
attribute scoped_node_definition = node => type = "pop_scoped_symbol", node_symbol = node, is_definition
attribute scoped_node_reference = node => type = "push_scoped_symbol", node_symbol = node, is_reference
attribute symbol_definition = symbol => type = "pop_symbol", symbol = symbol, is_definition
attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, is_reference
attribute node_symbol = node => symbol = (source-text node), source_node = node
;; Nodes
;; ^^^^^
(module) @node {
node @node.after_scope
node @node.before_scope
}
[
; _statement
; _simple_statement
(future_import_statement)
(import_statement)
(import_from_statement)
(print_statement)
(assert_statement)
(expression_statement)
(return_statement)
(delete_statement)
(raise_statement)
(pass_statement)
(break_statement)
(continue_statement)
(global_statement)
(nonlocal_statement)
(exec_statement)
(type_alias_statement)
; _compound_statement
(if_statement)
(for_statement)
(while_statement)
(try_statement)
(with_statement)
(function_definition)
(class_definition)
(decorated_definition)
(match_statement)
; block
(block)
; statement clauses
(if_clause)
(elif_clause)
(else_clause)
(except_group_clause)
(except_clause)
(finally_clause)
(with_clause)
(case_clause)
] @node {
node @node.after_scope
node @node.before_scope
}
[
(parameters)
(lambda_parameters)
] @node {
node @node.after_scope
node @node.before_scope
}
[
(identifier)
] @node {
node @node.def
node @node.def_dot
node @node.ref
node @node.ref_dot
}
[
(dotted_name)
(aliased_import)
(relative_import)
(wildcard_import)
(import_prefix)
] @node {
node @node.after_scope
node @node.before_scope
node @node.def
node @node.ref
}
[
; expressions
(comparison_operator)
(not_operator)
(boolean_operator)
(lambda)
;(primary_expression) ; unfolded below
(conditional_expression)
(named_expression)
(as_pattern)
; primary_expression
(await)
(binary_operator)
(identifier)
;(keyword_identifier) ; invalid query pattern?
(string)
(concatenated_string)
(integer)
(float)
(true)
(false)
(none)
(unary_operator)
(attribute)
(subscript)
(call)
(list)
(list_comprehension)
(dictionary)
(dictionary_comprehension)
(set)
(set_comprehension)
(tuple)
(pair)
(parenthesized_expression)
(generator_expression)
(ellipsis)
(list_splat)
; expression list
(expression_list)
; pattern
(pattern/identifier)
;(keyword_identifier) ; invalid query pattern?
;(subscript)
;(attribute)
(list_splat_pattern)
(tuple_pattern)
(list_pattern)
; _simple_patterns
(class_pattern)
(splat_pattern)
(union_pattern)
;(list_pattern) ; already in pattern
;(tuple_pattern) ; already in pattern
(dict_pattern)
;(string) ; already in primary_expression
;(concatenated_string) ; already in primary_expression
;(true) ; already in primary_expression
;(false) ; already in primary_expression
;(none) ; already in primary_expression
;(integer) ; already in primary_expression
;(float) ; already in primary_expression
(complex_pattern)
(dotted_name)
; _as_attern
(as_pattern)
; keyword pattern
(keyword_pattern)
; case pattern
(case_pattern)
; with item
(with_item)
; pattern list
(pattern_list)
; parameter
;(identifier) ; already in expressions
(typed_parameter)
(default_parameter)
(typed_default_parameter)
;(list_splat_pattern) ; already in patterns
;(tuple_pattern) ; already in patterns
(keyword_separator)
(positional_separator)
(dictionary_splat_pattern)
; parameters
(parameters)
] @node {
node @node.input
node @node.new_bindings
node @node.output
}
(comment) @node {
node @node.after_scope
node @node.before_scope
node @node.def
node @node.def_dot
node @node.input
node @node.new_bindings
node @node.output
node @node.ref
node @node.ref_dot
}
;; Inherited Variables
;; ^^^^^^^^^^^^^^^^^^^
inherit .bottom
inherit .class_member_attr_scope
inherit .class_parent_scope
inherit .class_self_scope
inherit .class_super_scope
inherit .function_returns
inherit .global
inherit .global_dot
inherit .grandparent_module
inherit .local_scope
inherit .parent_module
;;
;; # #
;; ## ## #### ##### # # # ###### ####
;; # # # # # # # # # # # # #
;; # # # # # # # # # # ##### ####
;; # # # # # # # # # # #
;; # # # # # # # # # # # #
;; # # #### ##### #### ###### ###### ####
;;
;; Modules
(module) @mod
{
node mod_file_def
node mod_file_ref
var module_def = mod_file_def
node parent_module_def_node
var parent_module_def = parent_module_def_node
var module_ref = mod_file_ref
node parent_module_ref_node
var parent_module_ref = parent_module_ref_node
node grandparent_module_ref_node
var grandparent_module_ref = grandparent_module_ref_node
; get the file path relative to the root path
let rel_path = (replace FILE_PATH ROOT_PATH "")
scan rel_path {
"([^/]+)/"
{
node def_dot
attr (def_dot) pop_symbol = "."
node next_def
;
edge module_def -> def_dot
edge def_dot -> next_def
;
attr (module_def) pop_symbol = $1
;
set parent_module_def = module_def
set module_def = next_def
node ref_dot
attr (ref_dot) push_symbol = "."
node next_ref
;
edge next_ref -> ref_dot
edge ref_dot -> module_ref
;
attr (module_ref) push_symbol = $1
;
set grandparent_module_ref = parent_module_ref
set parent_module_ref = module_ref
set module_ref = next_ref
}
"__init__\.py$"
{
attr (parent_module_def) is_definition, source_node = @mod, empty_source_span
}
"([^/]+)\.py$"
{
node def_dot
attr (def_dot) pop_symbol = "."
node next_def
;
edge module_def -> def_dot
edge def_dot -> next_def
;
attr (module_def) pop_symbol = $1, is_definition, source_node = @mod, empty_source_span
;
set module_def = next_def
}
}
edge ROOT_NODE -> mod_file_def
edge mod_file_ref -> ROOT_NODE
edge module_def -> @mod.after_scope
node global
node global_dot
edge @mod.before_scope -> global_dot
edge global -> ROOT_NODE
attr (global) push_symbol = "<builtins>"
edge global_dot -> global
attr (global_dot) push_symbol = "."
let @mod.parent_module = parent_module_ref
let @mod.grandparent_module = grandparent_module_ref
let @mod.bottom = @mod.after_scope
let @mod.global = global
let @mod.global_dot = global_dot
;; add a dummy nodes for inherited variables
node @mod.class_member_attr_scope
node @mod.class_parent_scope
node @mod.class_self_scope
node @mod.class_super_scope
node @mod.function_returns
node @mod.local_scope
}
;;
;; ###
;; # # # ##### #### ##### ##### ####
;; # ## ## # # # # # # # #
;; # # ## # # # # # # # # ####
;; # # # ##### # # ##### # #
;; # # # # # # # # # # #
;; ### # # # #### # # # ####
;;
;; Imports
;; Import References
;; ^^^^^^^^^^^^^^^^^
;;;; Dotted Names
;;
;; (dotted_name).ref node to connect to to use the reference
;; (dotted_name).before_scope node to connect from to ensure the reference resolves
;; all names are references
[
(import_statement name: (dotted_name (identifier) @name))
(future_import_statement name: (dotted_name (identifier) @name))
(import_from_statement name: (dotted_name (identifier) @name))
(import_statement name: (aliased_import name: (dotted_name (identifier) @name)))
(future_import_statement name: (aliased_import name: (dotted_name (identifier) @name)))
(import_from_statement name: (aliased_import name: (dotted_name (identifier) @name)))
(import_from_statement module_name: (dotted_name (identifier) @name))
(import_from_statement module_name: (relative_import (dotted_name (identifier) @name)))
] {
attr (@name.ref) node_reference = @name
}
;; references are chained
[
(import_statement name: (dotted_name (identifier) @left . (identifier) @right))
(future_import_statement name: (dotted_name (identifier) @left . (identifier) @right))
(import_from_statement name: (dotted_name (identifier) @left . (identifier) @right))
(import_statement name: (aliased_import name: (dotted_name (identifier) @left . (identifier) @right)))
(future_import_statement name: (aliased_import name: (dotted_name (identifier) @left . (identifier) @right)))
(import_from_statement name: (aliased_import name: (dotted_name (identifier) @left . (identifier) @right)))
(import_from_statement module_name: (dotted_name (identifier) @left . (identifier) @right))
(import_from_statement module_name: (relative_import (dotted_name (identifier) @left . (identifier) @right)))
] {
node push_dot
attr (push_dot) push_symbol = "."
edge @right.ref -> push_dot
edge push_dot -> @left.ref
}
;; lookup first reference
[
(import_statement name: (dotted_name . (identifier) @first) @dotted)
(future_import_statement name: (dotted_name . (identifier) @first) @dotted)
(import_from_statement name: (dotted_name . (identifier) @first) @dotted)
(import_statement name: (aliased_import name: (dotted_name . (identifier) @first) @dotted))
(future_import_statement name: (aliased_import name: (dotted_name . (identifier) @first) @dotted))
(import_from_statement name: (aliased_import name: (dotted_name . (identifier) @first) @dotted))
(import_from_statement module_name: (dotted_name . (identifier) @first) @dotted)
(import_from_statement module_name: (relative_import (dotted_name . (identifier) @first) @dotted))
] {
edge @first.ref -> @dotted.before_scope
}
;; expose last reference
[
(import_statement name: (dotted_name (identifier) @last .) @dotted)
(future_import_statement name: (dotted_name (identifier) @last .) @dotted)
(import_from_statement name: (dotted_name (identifier) @last .) @dotted)
(import_statement name: (aliased_import name: (dotted_name (identifier) @last .) @dotted))
(future_import_statement name: (aliased_import name: (dotted_name (identifier) @last .) @dotted))
(import_from_statement name: (aliased_import name: (dotted_name (identifier) @last .) @dotted))
(import_from_statement module_name: (dotted_name (identifier) @last .) @dotted)
(import_from_statement module_name: (relative_import (dotted_name (identifier) @last .) @dotted))
] {
edge @dotted.ref -> @last.ref
}
;;;; Aliased Import
;;
;; An aliased import behaves like its wrapped dotted_name as a reference
;;
;; (aliased_import).ref node to connect to, to use the reference
;; (aliased_import).before_scope node to connect from to ensure the reference resolves
(aliased_import name: (dotted_name) @dotted) @aliased {
edge @aliased.ref -> @dotted.ref
edge @dotted.before_scope -> @aliased.before_scope
}
;;;; Relative Import
;;
;; A relative import behaves like its wrapped dotted_name as a reference
;;
;; (relative_import).ref node to connect to, to use the reference
;; (relative_import).before_scope node to connect from to ensure the reference resolves
(relative_import (import_prefix) . (dotted_name) @dotted) @relative {
edge @relative.ref -> @dotted.ref
node push_dot
attr (push_dot) push_symbol = "."
edge @dotted.before_scope -> push_dot
edge push_dot -> @relative.before_scope
}
(relative_import (import_prefix) .) @relative {
edge @relative.ref -> @relative.before_scope
}
;;;; Wildcard Import
;;
;; A wildcard import simply passes through
;;
;; (wildcard_import).ref node to connect to, to use the reference
;; (wildcard_import).before_scope node to connect from to ensure the reference resolves
(wildcard_import) @wildcard {
edge @wildcard.ref -> @wildcard.before_scope
}
;;;; Import from
;;
;; The imported references are resolved in the from module
[
(import_from_statement module_name: (_) @left name: (_) @right)
(import_from_statement module_name: (_) @left (wildcard_import) @right)
] {
node push_dot
attr (push_dot) push_symbol = "."
edge @right.before_scope -> push_dot
edge push_dot -> @left.ref
}
;;;; Non-relative Imports
;;
;; Non-relative imports are resolved in the root scope
[
(import_statement name: (_) @name)
(import_from_statement module_name: (dotted_name) @name)
] {
edge @name.before_scope -> ROOT_NODE
}
;;;; Relative Imports
;;
;; Relative imports are resolved in scopes related to the current module
;; . imports resolve in parent module scope
(import_from_statement module_name: (relative_import (import_prefix) @prefix (#eq? @prefix ".")) @relative) {
edge @relative.before_scope -> @prefix.parent_module
}
;; .. imports resolve in grandparent module scope
(import_from_statement module_name: (relative_import (import_prefix) @prefix (#eq? @prefix "..")) @relative) {
edge @relative.before_scope -> @prefix.grandparent_module
}
;;;; Future Imports
;;
;; We don't know the future, so we cannot connect references of future imports anywhere. Maybe one day?
;; Import Definitions
;; ^^^^^^^^^^^^^^^^^^
;;;; Dotted Names & Aliased Imports
;;
;; (dotted_name).after_scope node to connect to, to expose the definition
;; (dotted_name).def node to connect from, to give definition content
;; unaliased names and aliases are definitions
[
(import_statement name: (dotted_name (identifier) @name))
(future_import_statement name: (dotted_name (identifier) @name))
(import_from_statement name: (dotted_name (identifier) @name))
(import_statement name: (aliased_import alias: (identifier) @name))
(future_import_statement name: (aliased_import alias: (identifier) @name))
(import_from_statement name: (aliased_import alias: (identifier) @name))
] {
attr (@name.def) node_definition = @name
}
;; definitions are chained
[
(import_statement name: (dotted_name (identifier) @left . (identifier) @right))
(future_import_statement name: (dotted_name (identifier) @left . (identifier) @right))
(import_from_statement name: (dotted_name (identifier) @left . (identifier) @right))
] {
node pop_dot
attr (pop_dot) pop_symbol = "."
edge @left.def -> pop_dot
edge pop_dot -> @right.def
}
;; connect last definition
[
(import_statement name: (dotted_name (identifier) @last .) @outer)
(future_import_statement name: (dotted_name (identifier) @last .) @outer)
(import_from_statement name: (dotted_name (identifier) @last .) @outer)
(import_statement name: (aliased_import alias: (identifier) @last ) @outer)
(future_import_statement name: (aliased_import alias: (identifier) @last ) @outer)
(import_from_statement name: (aliased_import alias: (identifier) @last ) @outer)
] {
edge @last.def -> @outer.def
}
;; expose first definition
[
(import_statement name: (dotted_name . (identifier) @first) @outer)
(future_import_statement name: (dotted_name . (identifier) @first) @outer)
(import_from_statement name: (dotted_name . (identifier) @first) @outer)
(import_statement name: (aliased_import alias: (identifier) @first ) @outer)
(future_import_statement name: (aliased_import alias: (identifier) @first ) @outer)
(import_from_statement name: (aliased_import alias: (identifier) @first ) @outer)
] {
edge @outer.after_scope -> @first.def
}
;;;; Wildcard Import
;;
;; Wildcard imports simply pass through
(wildcard_import) @wildcard {
edge @wildcard.after_scope -> @wildcard.def
}
;;;; Import Definitions -> References
;;
;; The definitions introduced by imports are connected to the corresponding references
[
(import_statement name: (_) @name)
(future_import_statement name: (_) @name)
(import_from_statement name: (_) @name)
(import_from_statement (wildcard_import) @name)
] {
edge @name.def -> @name.ref
}
;;;; Imports
;;
;; The definitions introduced by imports are visible after the import statement
[
(import_statement name: (_) @name)
(future_import_statement name: (_) @name)
(import_from_statement name: (_) @name)
(import_from_statement (wildcard_import) @name)
] @stmt {
edge @stmt.after_scope -> @name.after_scope
attr (@stmt.after_scope -> @name.after_scope) precedence = 1
}
;;
;; ######
;; # # # #### #### # # ####
;; # # # # # # # # # #
;; ###### # # # # #### ####
;; # # # # # # # # #
;; # # # # # # # # # # #
;; ###### ###### #### #### # # ####
;;
;; Blocks
[
(module (_) @last_stmt .)
(block (_) @last_stmt .)
] @block
{
edge @block.after_scope -> @last_stmt.after_scope
}
[
(module (_) @stmt1 . (_) @stmt2)
(block (_) @stmt1 . (_) @stmt2)
]
{
edge @stmt2.before_scope -> @stmt1.after_scope
}
[
(module (_) @stmt)
(block (_) @stmt)
]
{
edge @stmt.after_scope -> @stmt.before_scope
let @stmt.local_scope = @stmt.before_scope
}
[
(block . (_) @stmt)
(module . (_) @stmt)
] @block
{
edge @stmt.before_scope -> @block.before_scope
}
(function_definition (block) @block)
{
edge @block.before_scope -> @block.local_scope
}
[
(while_statement (block) @block)
(if_statement (block) @block)
(with_statement (block) @block)
(try_statement (block) @block)
(for_statement (block) @block)
(_ [
(else_clause (block) @block)
(elif_clause (block) @block)
(except_clause (block) @block)
(finally_clause (block) @block)
])
] @stmt
{
edge @block.before_scope -> @block.local_scope
edge @stmt.after_scope -> @block.after_scope
}
(match_statement body: (_) @block) @stmt
{
let @block.local_scope = @block.before_scope
edge @block.before_scope -> @stmt.before_scope
edge @stmt.after_scope -> @block.after_scope
}
[
(for_statement)
(while_statement)
] @stmt
{
edge @stmt.before_scope -> @stmt.after_scope
}
;;
;; #####
;; # # ##### ## ##### ###### # # ###### # # ##### ####
;; # # # # # # ## ## # ## # # #
;; ##### # # # # ##### # ## # ##### # # # # ####
;; # # ###### # # # # # # # # # #
;; # # # # # # # # # # # ## # # #
;; ##### # # # # ###### # # ###### # # # ####
;;
;; Statements
;;;; Simple Statements
(print_statement) {}
(assert_statement) {}
(expression_statement) {}
(return_statement (_) @expr) @stmt
{
edge @stmt.function_returns -> @expr.output
}
(delete_statement) {}
(raise_statement) {}
(pass_statement) {}
(break_statement) {}
(continue_statement) {}
(global_statement) {}
(nonlocal_statement) {}
(exec_statement) {}
(type_alias_statement) {}
;;;; Compound Statements
(if_statement) {}
(if_clause) {}
(elif_clause) {}
(else_clause) {}
(for_statement) {}
(while_statement) {}
(try_statement) {}
(except_group_clause) {}
(except_clause) {}
(finally_clause) {}
(with_statement) {}
(with_clause) {}
[
(function_definition
parameters: (_) @params
body: (_) @body
) @func
(lambda
parameters: (_) @params
body: (_) @body
)@func
] {
node @func.call
node return_value
node drop_scope
edge @func.call -> return_value
edge @body.before_scope -> @params.after_scope
edge @body.before_scope -> drop_scope
edge drop_scope -> @func.bottom
attr (drop_scope) type = "drop_scopes"
attr (@func.call) pop_scoped_symbol = "()"
edge @params.before_scope -> JUMP_TO_SCOPE_NODE
attr (return_value) is_exported
let @func.function_returns = return_value
}
(function_definition
name: (identifier) @name
body: (_) @body
) @func {
attr (@name.def) node_definition = @name
attr (@name.def) definiens_node = @func
edge @func.after_scope -> @name.def
edge @name.def -> @func.call
; Prevent functions defined inside of method bodies from being treated like methods
let @body.class_self_scope = #null
let @body.class_member_attr_scope = #null
}
; method definition
(class_definition
body: (block
(function_definition
parameters:
(parameters . (identifier) @first_param)
body: (block) @method_body
)
)
)
{
edge @first_param.def -> @first_param.class_self_scope
edge @first_param.class_member_attr_scope -> @first_param.output
edge @first_param.output -> @method_body.after_scope
attr (@first_param.output) push_node = @first_param
}
[
(parameters (_) @param) @params
(lambda_parameters (_) @param) @params
]
{
node @param.param_index
node @param.param_name
attr (@param.param_index) push_symbol = (named-child-index @param)
edge @param.param_index -> @params.before_scope
edge @params.after_scope -> @param.input
edge @param.param_name -> @params.before_scope
}
(parameter/identifier) @param @name
{
attr (@name.def) node_definition = @name
attr (@param.param_name) push_node = @param
edge @name.def -> @param.param_name
edge @name.def -> @param.param_index
edge @param.input -> @name.def
}
[
(parameter/default_parameter
name: (_) @name
value: (_) @value) @param
(parameter/typed_default_parameter
name: (_) @name
value: (_) @value) @param
] {
attr (@name.def) node_definition = @name
attr (@param.param_name) push_node = @name
edge @name.def -> @param.param_name
edge @name.def -> @param.param_index
edge @param.input -> @name.def
edge @name.def -> @value.output
}
[
(parameter/typed_parameter
. (_) @name) @param
(parameter/list_splat_pattern
(_) @name) @param
(parameter/dictionary_splat_pattern
(_) @name) @param
] {
attr (@name.def) node_definition = @name
attr (@param.param_name) push_node = @name
edge @name.def -> @param.param_name
edge @name.def -> @param.param_index
edge @param.input -> @name.def
}
(class_definition) @class {
node @class.call_drop
node @class.member_attrs
node @class.members
node @class.super_scope
}
(class_definition
name: (identifier) @name) {
attr (@name.def) node_definition = @name
}
(class_definition
name: (identifier) @name) @class
{
node call
node self_dot
node self_scope
node members_dot
attr (@name.def) definiens_node = @class
attr (@name.def) syntax_type = "class"
edge @class.after_scope -> @name.def
edge @name.def -> call
edge @name.def -> members_dot
edge members_dot -> @class.members
edge call -> @class.call_drop
edge @class.call_drop -> self_scope
edge self_scope -> @class.super_scope
edge self_scope -> self_dot
edge self_dot -> @class.members
edge @class.members -> @class.member_attrs
attr (call) pop_scoped_symbol = "()"
attr (@class.call_drop) type = "drop_scopes"
attr (members_dot) pop_symbol = "."
attr (self_dot) pop_symbol = "."
attr (@class.member_attrs) push_symbol = "."
attr (self_scope) is_exported
}
(class_definition
body: (_) @body) @class
{
let @body.class_member_attr_scope = @class.member_attrs
node @body.class_parent_scope
edge @body.class_parent_scope -> @class.class_parent_scope
edge @body.class_parent_scope -> @class.local_scope
let @body.class_self_scope = @class.call_drop
let @body.class_super_scope = @class.super_scope
}
(class_definition
body: (block
(_) @last_stmt .)) @class
{
edge @class.members -> @last_stmt.after_scope
}
(class_definition
superclasses: (argument_list
[
(identifier)
(attribute)
] @superclass)) @class
{
edge @class.super_scope -> @superclass.output
}
(decorated_definition
definition: (_) @def) @stmt
{
edge @def.before_scope -> @stmt.before_scope
edge @stmt.after_scope -> @def.after_scope
}
(match_statement) {}
(case_clause
(case_pattern) @pattern
consequence: (_) @consequence)
{
edge @consequence.before_scope -> @pattern.new_bindings
}
(case_clause
consequence: (_) @consequence) @clause
{
edge @consequence.before_scope -> @clause.before_scope
edge @clause.after_scope -> @consequence.after_scope
}
;;
;; #######
;; # # # ##### ##### ###### #### #### # #### # # ####
;; # # # # # # # # # # # # # ## # #
;; ##### ## # # # # ##### #### #### # # # # # # ####
;; # ## ##### ##### # # # # # # # # # #
;; # # # # # # # # # # # # # # # ## # #
;; ####### # # # # # ###### #### #### # #### # # ####
;;
;; Expressions
(lambda
body: (_) @body
)@lam {
;; TODO Unify .before_scope, .local_scope, and .input to simplify
;; uniform treatment of lambdas and function definitions.
node @body.before_scope
let @body.local_scope = @body.before_scope
edge @body.input -> @body.before_scope
edge @lam.output -> @lam.call
}
(conditional_expression) {}
(named_expression) {}
(as_pattern) {}
(await) {}
(binary_operator
(_) @left
(_) @right) @binop