tree-sitter-stack-graphs-python 0.3.0

Stack graphs definition for Python using tree-sitter-python
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
;; -*- 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
    (_) @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
{
  edge @binop.new_bindings -> @left.new_bindings
  edge @binop.new_bindings -> @right.new_bindings
}

(primary_expression/identifier) @name {
  attr (@name.ref) node_reference = @name
}

(primary_expression/identifier) @name
{
  edge @name.output -> @name.local_scope
  edge @name.output -> @name.class_parent_scope
  edge @name.local_scope -> @name.input
  attr (@name.input) pop_node = @name
  attr (@name.output) node_reference = @name

  edge @name.new_bindings -> @name.def
}

(string) {}

(concatenated_string) {}

(integer) {}

(float) {}

(true) {}

(false) {}

(none) {}

(unary_operator) {}

(attribute
  object: (_) @object
  attribute: (identifier) @name) @expr
{
  node input_dot
  node output_dot

  edge @expr.output -> @name.output
  edge @name.output -> output_dot
  edge output_dot -> @object.output
  edge @object.input -> input_dot
  edge input_dot -> @name.input
  edge @name.input -> @expr.input
  attr (output_dot) push_symbol = "."
  attr (input_dot) pop_symbol = "."
  attr (@name.input) pop_node = @name
  attr (@name.output) push_node = @name
}

(primary_expression/attribute
  attribute: (identifier) @name)
{
  attr (@name.output) is_reference
}

(subscript) {}

(call
  function: (_) @fn
  arguments: (argument_list) @args) @call
{
  node output_args

  edge @call.output -> output_args
  edge output_args -> @fn.output
  attr (output_args) push_scoped_symbol = "()", scope = @args.args
}

(call
  function: (attribute
    object: (_) @receiver)
  arguments: (argument_list
    (expression) @arg) @args)
{
  node receiver_arg_index
  attr (receiver_arg_index) pop_symbol = "0"
  edge @args.args -> receiver_arg_index
  edge receiver_arg_index -> @receiver.output

  ;; FIXME the arguments will also exist with their unshifted indices because of the general
  ;;       rule below!
  node arg_index
  attr (arg_index) pop_symbol = (plus 1 (named-child-index @arg))
  edge arg_index -> @arg.output
}

(call
  arguments: (argument_list
    (keyword_argument
      name: (identifier) @name
      value: (_) @val)) @args)
{
  node arg_name
  edge @args.args -> arg_name
  attr (arg_name) pop_node = @name
  edge arg_name -> @val.output
}

(argument_list) @args {
  node @args.args
  attr (@args.args) is_exported
}

(argument_list
  (expression) @arg) @args
{
  node arg_index
  edge @args.args -> arg_index
  attr (arg_index) pop_symbol = (named-child-index @arg)
  edge arg_index -> @arg.output
}

(
  (call
    function: (identifier) @_fn_name) @call
  (#eq? @_fn_name "super")
)
{
  edge @call.output -> @call.class_super_scope
}

(list) @list
{
  node called

  edge @list.output -> called
  edge called -> @list.global_dot
  attr (called) push_symbol = "list"
}

(list (_) @el) @list
{
  edge @list.new_bindings -> @el.new_bindings
}

(list_comprehension) {}

(dictionary (pair) @pair) @dict
{
  edge @dict.new_bindings -> @pair.new_bindings
}

(pair
  value: (_) @value) @pair
{
  edge @pair.new_bindings -> @value.new_bindings
}

(dictionary_comprehension) {}

(set (_) @el) @set
{
  edge @set.new_bindings -> @el.new_bindings
}

(set_comprehension) {}

[
  (tuple (_) @element)
  (expression_list (_) @element)
] @tuple
{
  node el_index

  edge @tuple.output -> el_index
  attr (el_index) pop_symbol = (named-child-index @element)
  edge el_index -> @element.output

  edge @tuple.new_bindings -> @element.new_bindings
}

(parenthesized_expression) {}

(generator_expression) {}

(ellipsis) {}

(list_splat (_) @splatted) @splat
{
  edge @splat.new_bindings -> @splatted.new_bindings
}

;;
;; ######
;; #     #   ##   ##### ##### ###### #####  #    #  ####
;; #     #  #  #    #     #   #      #    # ##   # #
;; ######  #    #   #     #   #####  #    # # #  #  ####
;; #       ######   #     #   #      #####  #  # #      #
;; #       #    #   #     #   #      #   #  #   ## #    #
;; #       #    #   #     #   ###### #    # #    #  ####
;;
;; Patterns


[
  (pattern/identifier) @name @pattern
  ; identifiers in patterns
  (as_pattern (identifier) @name .) @pattern
  (keyword_pattern (identifier) @name) @pattern
  (splat_pattern (identifier) @name) @pattern
  ; every context where _simple_pattern is used, because matching
  ; pattern/dotted_name does not work
  (case_pattern (dotted_name . (_) @name .) @pattern)
  (keyword_pattern (dotted_name . (_) @name .) @pattern)
  (union_pattern (dotted_name . (_) @name .) @pattern)
] {
  attr (@name.def) node_definition = @name
  edge @pattern.output -> @pattern.local_scope
  edge @pattern.output -> @pattern.class_parent_scope
  edge @pattern.local_scope -> @pattern.input
  attr (@pattern.local_scope -> @pattern.input) precedence = 1
  attr (@pattern.input) node_definition = @name
  attr (@pattern.output) push_node = @name

  edge @pattern.new_bindings -> @name.def
}

(pattern/subscript) {}

(pattern/attribute
  attribute: (identifier) @name)
{
  attr (@name.input) is_definition
}


(list_splat_pattern (_) @pattern) @list {
  edge @list.new_bindings -> @pattern.new_bindings
}

[
  (pattern_list (_) @pattern)
  (tuple_pattern (_) @pattern)
  (list_pattern (_) @pattern)
] @list
{
  node pattern_index

  let statement_scope = @list.local_scope
  node @pattern.local_scope
  edge statement_scope -> @pattern.local_scope
  attr (statement_scope -> @pattern.local_scope) precedence = (plus 1 (named-child-index @pattern))

  edge pattern_index -> @list.input
  edge @pattern.input -> pattern_index
  attr (pattern_index) push_symbol = (named-child-index @pattern)
}

(class_pattern (case_pattern) @pattern) @class {
  edge @class.new_bindings -> @pattern.new_bindings
}

(splat_pattern (_) @pattern) @splat {
  edge @splat.new_bindings -> @pattern.new_bindings
}

(union_pattern (_) @pattern) @union {
  edge @union.new_bindings -> @pattern.new_bindings
}

(dict_pattern (_) @pattern) @dict {
  edge @dict.new_bindings -> @pattern.new_bindings
}

(complex_pattern) {}

(as_pattern
  (expression) @value
  alias: (as_pattern_target (identifier) @name)) @as_pattern
{
  attr (@name.local_scope -> @name.input) precedence = 1
  attr (@name.input) is_definition

  edge @as_pattern.new_bindings -> @value.new_bindings
  edge @as_pattern.new_bindings -> @name.new_bindings
}

(keyword_pattern) {}

(case_pattern (_) @pattern) @case
{
  edge @case.new_bindings -> @pattern.new_bindings
}

[
  (assignment
    left: (_) @pattern
    right: (_) @value)
  (with_item
    value:
      (as_pattern
        (_) @value
        alias: (as_pattern_target (_) @pattern)))
]
{
  edge @pattern.input -> @value.output
}

;;
;;  #####                                      #######
;; #     # #   # #    # #####   ##   #    #       #    #   # #####  ######  ####
;; #        # #  ##   #   #    #  #   #  #        #     # #  #    # #      #
;;  #####    #   # #  #   #   #    #   ##         #      #   #    # #####   ####
;;       #   #   #  # #   #   ######   ##         #      #   #####  #           #
;; #     #   #   #   ##   #   #    #  #  #        #      #   #      #      #    #
;;  #####    #   #    #   #   #    # #    #       #      #   #      ######  ####
;;
;; Syntax Types

;;
;; BEGIN BIG GNARLY DISJUNCTION
;;
;; The following pair of rules is intended to capture the following behavior:
;;
;; If a function definition is used to define a method, by being inside a class
;; definition, then we make its syntax type `method`. Otherwise, we make it's
;; syntax type `function`. Unfortunately, because of the limitations on negation
;; and binding in tree sitter queries, we cannot negate `class_definition` or
;; similar things directly. Instead, we have to manually push the negation down
;; to form the finite disjunction it corresponds to.
;;

[
  (class_definition (block (decorated_definition (function_definition name: (_)@name))))
  (class_definition (block (function_definition name: (_)@name)))
]
{
  attr (@name.def) syntax_type = "method"
}

[
  (module (decorated_definition (function_definition name: (_)@name)))
  (module (function_definition name: (_)@name))

  (if_statement (block (decorated_definition (function_definition name: (_)@name))))
  (if_statement (block (function_definition name: (_)@name)))

  (elif_clause (block (decorated_definition (function_definition name: (_)@name))))
  (elif_clause (block (function_definition name: (_)@name)))

  (else_clause (block (decorated_definition (function_definition name: (_)@name))))
  (else_clause (block (function_definition name: (_)@name)))

  (case_clause (block (decorated_definition (function_definition name: (_)@name))))
  (case_clause (block (function_definition name: (_)@name)))

  (for_statement (block (decorated_definition (function_definition name: (_)@name))))
  (for_statement (block (function_definition name: (_)@name)))

  (while_statement (block (decorated_definition (function_definition name: (_)@name))))
  (while_statement (block (function_definition name: (_)@name)))

  (try_statement (block (decorated_definition (function_definition name: (_)@name))))
  (try_statement (block (function_definition name: (_)@name)))

  (except_clause (block (decorated_definition (function_definition name: (_)@name))))
  (except_clause (block (function_definition name: (_)@name)))

  (finally_clause (block (decorated_definition (function_definition name: (_)@name))))
  (finally_clause (block (function_definition name: (_)@name)))

  (with_statement (block (decorated_definition (function_definition name: (_)@name))))
  (with_statement (block (function_definition name: (_)@name)))

  (function_definition (block (decorated_definition (function_definition name: (_)@name))))
  (function_definition (block (function_definition name: (_)@name)))
]
{
  attr (@name.def) syntax_type = "function"
}

;;
;; END BIG GNARLY DISJUNCTION
;;