This repository was archived by the owner on Sep 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathlua.rs
More file actions
1046 lines (999 loc) · 38.5 KB
/
lua.rs
File metadata and controls
1046 lines (999 loc) · 38.5 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.
// ------------------------------------------------------------------------------------------------
#![cfg_attr(docsrs, doc(cfg(feature = "lua")))]
//! Provides access to `StackGraph` instances from Lua.
//!
//! With the `lua` feature enabled, you can add [`StackGraph`] instances to a [`Lua`][mlua::Lua]
//! interpreter. You might typically use this to _create_ stack graphs from Lua, by calling a Lua
//! function with an empty stack graph as a parameter. Note that you'll almost certainly need to
//! use `mlua`'s [scoped values](mlua::Lua::scope) mechanism so that you can still use the
//! [`StackGraph`] on the Rust side once the Lua function has finished.
//!
//! ```
//! # use mlua::Lua;
//! # use stack_graphs::graph::StackGraph;
//! # fn main() -> Result<(), mlua::Error> {
//! let lua = Lua::new();
//! let chunk = r#"
//! function process_graph(graph)
//! local file = graph:file("test.py")
//! local def = file:definition_node("foo")
//! def:add_edge_from(graph:root_node())
//! end
//! "#;
//! lua.load(chunk).set_name("stack graph chunk").exec()?;
//! let process_graph: mlua::Function = lua.globals().get("process_graph")?;
//!
//! let mut graph = StackGraph::new();
//! lua.scope(|scope| {
//! let graph = graph.lua_ref_mut(&scope)?;
//! process_graph.call(graph)
//! })?;
//! assert_eq!(graph.iter_nodes().count(), 3);
//! # Ok(())
//! # }
//! ```
//!
//! ## Building
//!
//! Lua support is only enabled if you compile with the `lua` feature. This feature is not enough
//! on its own, because the `mlua` crate supports multiple Lua versions, and can either link
//! against a system-installed copy of Lua, or build its own copy from vendored Lua source. These
//! choices are all controlled via additional features on the `mlua` crate.
//!
//! When building and testing this crate, make sure to provide all necessary features on the
//! command line:
//!
//! ``` console
//! $ cargo test --features lua,mlua/lua54,mlua/vendored
//! ```
//!
//! When building a crate that depends on this crate, add a dependency on `mlua` so that you can
//! set its feature flags:
//!
//! ``` toml
//! [dependencies]
//! stack-graphs = { version="0.13", features=["lua"] }
//! mlua = { version="0.9", features=["lua54", "vendored"] }
//! ```
//!
//! ## Lua API
//!
//! ### Stack graphs
//!
//! The following Lua methods are available on a stack graph instance:
//!
//! #### `edges`
//!
//! ``` lua
//! let edges = graph:edges()
//! ```
//!
//! Returns an array containing all of the edges in the graph.
//!
//! #### `file`
//!
//! ``` lua
//! local file = graph:file(name)
//! ```
//!
//! Returns the file in the stack graph with the given name, creating it if necessary.
//!
//! #### `jump_to_node`
//!
//! ``` lua
//! local node = graph:jump_to_node()
//! ```
//!
//! Returns the graph's jump-to node.
//!
//! #### `nodes`
//!
//! ``` lua
//! for node in graph:nodes() do
//! -- whatever
//! end
//! ```
//!
//! Returns an iterator of every node in the stack graph.
//!
//! #### `root_node`
//!
//! ``` lua
//! local node = graph:root_node()
//! ```
//!
//! Returns the graph's root node.
//!
//! ### Files
//!
//! The following Lua methods are available on a file instance:
//!
//! #### `definition_node`
//!
//! ``` lua
//! local node = file:definition_node(symbol)
//! ```
//!
//! Adds a new definition node to this file. `symbol` must be a string, or an instance that can be
//! converted to a string via its `tostring` method.
//!
//! #### `drop_scopes_node`
//!
//! ``` lua
//! local node = file:drop_scopes_node()
//! ```
//!
//! Adds a new drop scopes node to this file.
//!
//! #### `edges`
//!
//! ``` lua
//! let edges = file:edges()
//! ```
//!
//! Returns an array containing all of the edges starting from or leaving a node in this file.
//!
//! #### `exported_scope_node`
//!
//! ``` lua
//! local node = file:exported_scope_node()
//! ```
//!
//! Adds a new exported scope node to this file.
//!
//! #### `internal_scope_node`
//!
//! ``` lua
//! local node = file:internal_scope_node()
//! ```
//!
//! Adds a new internal scope node to this file.
//!
//! #### `jump_to_node`
//!
//! ``` lua
//! local node = file:jump_to_node()
//! ```
//!
//! Returns the root node of the graph containing this file.
//!
//! #### `pop_scoped_symbol_node`
//!
//! ``` lua
//! local node = file:pop_scoped_symbol_node(symbol)
//! ```
//!
//! Adds a new pop scoped symbol node to this file. `symbol` must be a string, or an instance that
//! can be converted to a string via its `tostring` method.
//!
//! #### `pop_symbol_node`
//!
//! ``` lua
//! local node = file:pop_symbol_node(symbol)
//! ```
//!
//! Adds a new pop symbol node to this file. `symbol` must be a string, or an instance that can be
//! converted to a string via its `tostring` method.
//!
//! #### `push_scoped_symbol_node`
//!
//! ``` lua
//! local node = file:push_scoped_symbol_node(symbol, scope)
//! ```
//!
//! Adds a new push scoped symbol node to this file. `symbol` must be a string, or an instance
//! that can be converted to a string via its `tostring` method. `scope` must be an exported scope
//! node.
//!
//! #### `push_symbol_node`
//!
//! ``` lua
//! local node = file:push_symbol_node(symbol)
//! ```
//!
//! Adds a new push symbol node to this file. `symbol` must be a string, or an instance that can
//! be converted to a string via its `tostring` method.
//!
//! #### `reference_node`
//!
//! ``` lua
//! local node = file:reference_node(symbol)
//! ```
//!
//! Adds a new definition node to this file. `symbol` must be a string, or an instance that can be
//! converted to a string via its `tostring` method.
//!
//! #### `root_node`
//!
//! ``` lua
//! local node = file:root_node()
//! ```
//!
//! Returns the root node of the graph containing this file.
//!
//! #### `scoped_definition_node`
//!
//! ``` lua
//! local node = file:scoped_definition_node(symbol)
//! ```
//!
//! Adds a new scoped definition node to this file. `symbol` must be a string, or an instance that
//! can be converted to a string via its `tostring` method.
//!
//! #### `scoped_reference_node`
//!
//! ``` lua
//! local node = file:scoped_reference_node(symbol)
//! ```
//!
//! Adds a new scoped reference node to this file. `symbol` must be a string, or an instance that
//! can be converted to a string via its `tostring` method.
//!
//! ### Nodes
//!
//! The following Lua methods are available on a node instance:
//!
//! #### `add_edge_from`
//!
//! ``` lua
//! node:add_edge_from(other, precedence)
//! ```
//!
//! Adds an edge from another node to this node. `precedence` is optional; it defaults to 0 if not
//! given.
//!
//! #### `add_edge_to`
//!
//! ``` lua
//! node:add_edge_to(other, precedence)
//! ```
//!
//! Adds an edge from this node to another node. `precedence` is optional; it defaults to 0 if not
//! given.
//!
//! #### `debug_info`
//!
//! ``` lua
//! let info = node:debug_info()
//! ```
//!
//! Returns a Lua table containing all of the debug info added to this node.
//!
//! #### `definiens_span`
//!
//! ``` lua
//! let span = node:definiens_span()
//! ```
//!
//! Returns the definiens span of this node. (See [`set_definiens_span`](#set_definiens_span) for
//! the structure of a span.)
//!
//! #### `local_id`
//!
//! ``` lua
//! let local_id = node:local_id()
//! ```
//!
//! Returns the local ID of this node within its file.
//!
//! #### `outgoing_edges`
//!
//! ``` lua
//! let edges = node:outgoing_edges()
//! ```
//!
//! Returns an array containing all of the edges leaving this node.
//!
//! #### `set_debug_info`
//!
//! ``` lua
//! node:add_debug_info(key, value)
//! ```
//!
//! Adds a new debug info to this node. `key` and `value` must each be a string, or an instance
//! that can be converted to a string via its `tostring` method.
//!
//! #### `set_definiens_span`
//!
//! ``` lua
//! node:set_definiens_span {
//! start = {
//! line = 1,
//! column = { utf8_offset = 1, utf16_offset = 1, grapheme_offset = 1 },
//! -- UTF-8 offsets within the source file of the line containing the span
//! containing_line = { start = 1, end = 14 },
//! -- UTF-8 offsets within the source file of the line containing the span, with leading and
//! -- trailing whitespace removed
//! trimmed_line = { start = 2, end = 12 },
//! },
//! end = {
//! line = 2,
//! column = { utf8_offset = 12, utf16_offset = 10, grapheme_offset = 8 },
//! containing_line = { start = 1, end = 14 },
//! trimmed_line = { start = 1, end = 14 },
//! },
//! }
//! ```
//!
//! Sets the definiens span of this node. All entries in the table are optional, and default to 0
//! if not provided.
//!
//! #### `set_span`
//!
//! ``` lua
//! node:set_span {
//! start = {
//! line = 1,
//! column = { utf8_offset = 1, utf16_offset = 1, grapheme_offset = 1 },
//! -- UTF-8 offsets within the source file of the line containing the span
//! containing_line = { start = 1, end = 14 },
//! -- UTF-8 offsets within the source file of the line containing the span, with leading and
//! -- trailing whitespace removed
//! trimmed_line = { start = 2, end = 12 },
//! },
//! end = {
//! line = 2,
//! column = { utf8_offset = 12, utf16_offset = 10, grapheme_offset = 8 },
//! containing_line = { start = 1, end = 14 },
//! trimmed_line = { start = 1, end = 14 },
//! },
//! }
//! ```
//!
//! Sets the span of this node. All entries in the table are optional, and default to 0 if not
//! provided.
//!
//! #### `set_syntax_type`
//!
//! ``` lua
//! node:set_syntax_type(syntax_type)
//! ```
//!
//! Sets the syntax type of this node. `syntax_type` must be a string, or an instance that can be
//! converted to a string via its `tostring` method.
//!
//! #### `span`
//!
//! ``` lua
//! let span = node:span()
//! ```
//!
//! Returns the span of this node. (See [`set_span`](#set_span) for the structure of a span.)
//!
//! #### `syntax_type`
//!
//! ``` lua
//! let syntax_type = node:syntax_type()
//! ```
//!
//! Returns the syntax type of this node.
// Implementation notes: Stack graphs, files, and nodes can live inside the Lua interpreter as
// objects. They are each wrapped in a userdata, with a metatable defining the methods that are
// available. With mlua, the UserData trait is the way to define these metatables and methods.
//
// Complicating matters is that files and nodes need to be represented by a _pair_ of Lua values:
// the handle of the file or node, and a reference to the StackGraph that the file or node lives
// in. We need both because some of the methods need to dereference the handle to get e.g. the
// `Node` instance. It's not safe to dereference the handle when we create the userdata, because
// the resulting pointer is not guaranteed to be stable. (If you add another node, the arena's
// storage might get resized, moving the node instances around in memory.)
//
// To handle this, we leverage Lua's ability to associate “user values” with each userdata. For
// files and nodes, we store the graph's userdata (i.e. its Lua representation) as the user value
// of each file and node userdata.
//
// That, in turn, means that we must use `add_function` to define each metatable method, since that
// gives us an `mlua::AnyUserData`, which lets us access the userdata's underlying Rust value _and_
// its user value. (Typically, you would use the more ergonomic `add_method` or `add_method_mut`,
// which take care of unwrapping the userdata and giving you a &ref or &mut ref to the underlying
// Rust type. But then you don't have access to the userdata's user value.)
use std::fmt::Write;
use std::num::NonZeroU32;
use controlled_option::ControlledOption;
use lsp_positions::Span;
use mlua::AnyUserData;
use mlua::Lua;
use mlua::Scope;
use mlua::UserData;
use mlua::UserDataMethods;
use crate::arena::Handle;
use crate::graph::Edge;
use crate::graph::File;
use crate::graph::Node;
use crate::graph::StackGraph;
impl StackGraph {
// Returns a Lua wrapper for this stack graph. Takes ownership of the stack graph. If you
// want to access the stack graph after your Lua code is done with it, use [`lua_ref_mut`]
// instead.
pub fn lua_value<'lua>(self, lua: &'lua Lua) -> Result<AnyUserData<'lua>, mlua::Error> {
lua.create_userdata(self)
}
// Returns a scoped Lua wrapper for this stack graph.
pub fn lua_ref_mut<'lua, 'scope>(
&'scope mut self,
scope: &Scope<'lua, 'scope>,
) -> Result<AnyUserData<'lua>, mlua::Error> {
scope.create_userdata_ref_mut(self)
}
// Returns a scoped Lua wrapper for a file in this stack graph.
pub fn file_lua_ref_mut<'lua, 'scope>(
&'scope mut self,
file: Handle<File>,
scope: &Scope<'lua, 'scope>,
) -> Result<AnyUserData<'lua>, mlua::Error> {
let graph_ud = self.lua_ref_mut(scope)?;
let file_ud = scope.create_userdata(file)?;
file_ud.set_user_value(graph_ud)?;
Ok(file_ud)
}
}
impl UserData for StackGraph {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_function("edges", |l, graph_ud: AnyUserData| {
let graph = graph_ud.borrow::<StackGraph>()?;
let mut edges = Vec::new();
for node in graph.iter_nodes() {
for edge in graph.outgoing_edges(node) {
let edge_ud = l.create_userdata(edge)?;
edge_ud.set_user_value(graph_ud.clone())?;
edges.push(edge_ud);
}
}
Ok(edges)
});
methods.add_function("file", |l, (graph_ud, name): (AnyUserData, String)| {
let file = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
graph.get_or_create_file(&name)
};
let file_ud = l.create_userdata(file)?;
file_ud.set_user_value(graph_ud)?;
Ok(file_ud)
});
methods.add_function("jump_to_node", |l, graph_ud: AnyUserData| {
let node = StackGraph::jump_to_node();
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
});
methods.add_function("nodes", |l, graph_ud: AnyUserData| {
let iter = l.create_function(
|l, (graph_ud, prev_node_ud): (AnyUserData, Option<AnyUserData>)| {
let prev_index = match prev_node_ud {
Some(prev_node_ud) => {
let prev_node = prev_node_ud.borrow::<Handle<Node>>()?;
prev_node.as_u32()
}
None => 0,
};
let node_index = {
let graph = graph_ud.borrow::<StackGraph>()?;
let node_count = graph.nodes.len() as u32;
if prev_index == node_count - 1 {
return Ok(None);
}
unsafe { NonZeroU32::new_unchecked(prev_index + 1) }
};
let node = Handle::new(node_index);
let node_ud = l.create_userdata::<Handle<Node>>(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(Some(node_ud))
},
)?;
Ok((iter, graph_ud, None::<AnyUserData>))
});
methods.add_function("root_node", |l, graph_ud: AnyUserData| {
let node = StackGraph::root_node();
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
});
}
}
impl UserData for Handle<File> {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_function(
"definition_node",
|l, (file_ud, symbol): (AnyUserData, String)| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let symbol = graph.add_symbol(&symbol);
let node_id = graph.new_node_id(file);
graph
.add_pop_symbol_node(node_id, symbol, true)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
},
);
methods.add_function("drop_scopes_node", |l, file_ud: AnyUserData| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let node_id = graph.new_node_id(file);
graph
.add_drop_scopes_node(node_id)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
});
methods.add_function("edges", |l, file_ud: AnyUserData| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let graph = graph_ud.borrow::<StackGraph>()?;
let mut edges = Vec::new();
// First find any edges from the singleton nodes _to_ a node in this file.
for edge in graph.outgoing_edges(StackGraph::root_node()) {
if !graph[edge.sink].file().map(|f| f == file).unwrap_or(false) {
continue;
}
let edge_ud = l.create_userdata(edge)?;
edge_ud.set_user_value(graph_ud.clone())?;
edges.push(edge_ud);
}
for edge in graph.outgoing_edges(StackGraph::jump_to_node()) {
if !graph[edge.sink].file().map(|f| f == file).unwrap_or(false) {
continue;
}
let edge_ud = l.create_userdata(edge)?;
edge_ud.set_user_value(graph_ud.clone())?;
edges.push(edge_ud);
}
// Then find any edges _starting_ from a node in this file.
for node in graph.nodes_for_file(file) {
for edge in graph.outgoing_edges(node) {
let edge_ud = l.create_userdata(edge)?;
edge_ud.set_user_value(graph_ud.clone())?;
edges.push(edge_ud);
}
}
Ok(edges)
});
methods.add_function("exported_scope_node", |l, file_ud: AnyUserData| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let node_id = graph.new_node_id(file);
graph
.add_scope_node(node_id, true)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
});
methods.add_function("internal_scope_node", |l, file_ud: AnyUserData| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let node_id = graph.new_node_id(file);
graph
.add_scope_node(node_id, false)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
});
methods.add_function("jump_to_node", |l, file_ud: AnyUserData| {
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = StackGraph::jump_to_node();
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
});
methods.add_function("nodes", |l, file_ud: AnyUserData| {
let iter = l.create_function(
|l, (file_ud, prev_node_ud): (AnyUserData, Option<AnyUserData>)| {
// Pull out the node handle from the previous iteration.
let prev_index = match prev_node_ud {
Some(prev_node_ud) => {
let prev_node = prev_node_ud.borrow::<Handle<Node>>()?;
prev_node.as_u32()
}
None => 0,
};
// Loop through the next node handles until we find one belonging to the file.
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph = graph_ud.borrow::<StackGraph>()?;
let node_count = graph.nodes.len() as u32;
let mut node_index = unsafe { NonZeroU32::new_unchecked(prev_index + 1) };
loop {
let handle = Handle::<Node>::new(node_index);
// If we reach the end without finding a matching node, return nil
// to terminate the iterator.
if node_index.get() == node_count {
return Ok(None);
}
// If the node belongs to the file, break out of the loop to use this
// node as the next result of the iterator.
if graph[handle].file().map(|f| f == file).unwrap_or(false) {
break handle;
}
// Otherwise try the next node.
node_index = node_index.checked_add(1).unwrap();
}
};
// Wrap up the node handle that we just found.
let node_ud = l.create_userdata::<Handle<Node>>(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(Some(node_ud))
},
)?;
Ok((iter, file_ud, None::<AnyUserData>))
});
methods.add_function(
"pop_scoped_symbol_node",
|l, (file_ud, symbol): (AnyUserData, String)| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let symbol = graph.add_symbol(&symbol);
let node_id = graph.new_node_id(file);
graph
.add_pop_scoped_symbol_node(node_id, symbol, false)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
},
);
methods.add_function(
"pop_symbol_node",
|l, (file_ud, symbol): (AnyUserData, String)| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let symbol = graph.add_symbol(&symbol);
let node_id = graph.new_node_id(file);
graph
.add_pop_symbol_node(node_id, symbol, false)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
},
);
methods.add_function(
"push_scoped_symbol_node",
|l, (file_ud, symbol, scope_ud): (AnyUserData, String, AnyUserData)| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let scope = *scope_ud.borrow::<Handle<Node>>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let scope_id = {
let scope = &graph[scope];
if !scope.is_exported_scope() {
return Err(mlua::Error::RuntimeError(
"Can only push exported scope nodes".to_string(),
));
}
scope.id()
};
let symbol = graph.add_symbol(&symbol);
let node_id = graph.new_node_id(file);
graph
.add_push_scoped_symbol_node(node_id, symbol, scope_id, false)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
},
);
methods.add_function(
"push_symbol_node",
|l, (file_ud, symbol): (AnyUserData, String)| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let symbol = graph.add_symbol(&symbol);
let node_id = graph.new_node_id(file);
graph
.add_push_symbol_node(node_id, symbol, false)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
},
);
methods.add_function(
"reference_node",
|l, (file_ud, symbol): (AnyUserData, String)| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let symbol = graph.add_symbol(&symbol);
let node_id = graph.new_node_id(file);
graph
.add_push_symbol_node(node_id, symbol, true)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
},
);
methods.add_function("root_node", |l, file_ud: AnyUserData| {
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = StackGraph::root_node();
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
});
methods.add_function(
"scoped_definition_node",
|l, (file_ud, symbol): (AnyUserData, String)| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let symbol = graph.add_symbol(&symbol);
let node_id = graph.new_node_id(file);
graph
.add_pop_scoped_symbol_node(node_id, symbol, true)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
},
);
methods.add_function(
"scoped_reference_node",
|l, (file_ud, symbol, scope_ud): (AnyUserData, String, AnyUserData)| {
let file = *file_ud.borrow::<Handle<File>>()?;
let graph_ud = file_ud.user_value::<AnyUserData>()?;
let scope = *scope_ud.borrow::<Handle<Node>>()?;
let node = {
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let scope_id = {
let scope = &graph[scope];
if !scope.is_exported_scope() {
return Err(mlua::Error::RuntimeError(
"Can only push exported scope nodes".to_string(),
));
}
scope.id()
};
let symbol = graph.add_symbol(&symbol);
let node_id = graph.new_node_id(file);
graph
.add_push_scoped_symbol_node(node_id, symbol, scope_id, true)
.expect("Node ID collision")
};
let node_ud = l.create_userdata(node)?;
node_ud.set_user_value(graph_ud)?;
Ok(node_ud)
},
);
}
}
impl UserData for Handle<Node> {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_function(
"add_edge_from",
|l, (this_ud, from_ud, precedence): (AnyUserData, AnyUserData, Option<i32>)| {
let this = *this_ud.borrow::<Handle<Node>>()?;
let from = *from_ud.borrow::<Handle<Node>>()?;
let graph_ud = this_ud.user_value::<AnyUserData>()?;
let precedence = precedence.unwrap_or(0);
{
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
graph.add_edge(from, this, precedence);
}
let edge = Edge {
source: from,
sink: this,
precedence,
};
let edge_ud = l.create_userdata(edge)?;
edge_ud.set_user_value(graph_ud)?;
Ok(edge_ud)
},
);
methods.add_function(
"add_edge_to",
|l, (this_ud, to_ud, precedence): (AnyUserData, AnyUserData, Option<i32>)| {
let this = *this_ud.borrow::<Handle<Node>>()?;
let to = *to_ud.borrow::<Handle<Node>>()?;
let graph_ud = this_ud.user_value::<AnyUserData>()?;
let precedence = precedence.unwrap_or(0);
{
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
graph.add_edge(this, to, precedence);
}
let edge = Edge {
source: this,
sink: to,
precedence,
};
let edge_ud = l.create_userdata(edge)?;
edge_ud.set_user_value(graph_ud)?;
Ok(edge_ud)
},
);
methods.add_function("debug_info", |l, node_ud: AnyUserData| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let graph = graph_ud.borrow::<StackGraph>()?;
let debug_info = match graph.node_debug_info(node) {
Some(debug_info) => debug_info,
None => return Ok(None),
};
let result = l.create_table()?;
for entry in debug_info.iter() {
result.set(&graph[entry.key], &graph[entry.value])?;
}
Ok(Some(result))
});
methods.add_function("definiens_span", |_, node_ud: AnyUserData| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let graph = graph_ud.borrow::<StackGraph>()?;
let source_info = match graph.source_info(node) {
Some(source_info) => source_info,
None => return Ok(None),
};
Ok(Some(source_info.definiens_span.clone()))
});
methods.add_function("local_id", |_, node_ud: AnyUserData| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let graph = graph_ud.borrow::<StackGraph>()?;
Ok(graph[node].id().local_id())
});
methods.add_function("outgoing_edges", |l, node_ud: AnyUserData| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let graph = graph_ud.borrow::<StackGraph>()?;
let mut edges = Vec::new();
for edge in graph.outgoing_edges(node) {
let edge_ud = l.create_userdata(edge)?;
edge_ud.set_user_value(graph_ud.clone())?;
edges.push(edge_ud);
}
Ok(edges)
});
methods.add_function(
"set_debug_info",
|_, (node_ud, k, v): (AnyUserData, String, String)| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let k = graph.add_string(&k);
let v = graph.add_string(&v);
graph.node_debug_info_mut(node).add(k, v);
Ok(())
},
);
methods.add_function(
"set_definiens_span",
|_, (node_ud, definiens_span): (AnyUserData, Span)| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
graph.source_info_mut(node).definiens_span = definiens_span;
Ok(())
},
);
methods.add_function("set_span", |_, (node_ud, span): (AnyUserData, Span)| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
graph.source_info_mut(node).span = span;
Ok(())
});
methods.add_function(
"set_syntax_type",
|_, (node_ud, syntax_type): (AnyUserData, String)| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let mut graph = graph_ud.borrow_mut::<StackGraph>()?;
let syntax_type = graph.add_string(&syntax_type);
graph.source_info_mut(node).syntax_type = ControlledOption::some(syntax_type);
Ok(())
},
);
methods.add_function("span", |_, node_ud: AnyUserData| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let graph = graph_ud.borrow::<StackGraph>()?;
let source_info = match graph.source_info(node) {
Some(source_info) => source_info,
None => return Ok(None),
};
Ok(Some(source_info.span.clone()))
});
methods.add_function("syntax_type", |_, node_ud: AnyUserData| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let graph = graph_ud.borrow::<StackGraph>()?;
let source_info = match graph.source_info(node) {
Some(source_info) => source_info,
None => return Ok(None),
};
let syntax_type = match source_info.syntax_type.into_option() {
Some(syntax_type) => syntax_type,
None => return Ok(None),
};
Ok(Some(graph[syntax_type].to_string()))
});
methods.add_meta_function(mlua::MetaMethod::ToString, |_, node_ud: AnyUserData| {
let node = *node_ud.borrow::<Handle<Node>>()?;
let graph_ud = node_ud.user_value::<AnyUserData>()?;
let graph = graph_ud.borrow::<StackGraph>()?;
let mut display = graph[node].display(&graph).to_string();
if let Some(source_info) = graph.source_info(node) {
display.pop(); // remove the trailing ]
if let Some(syntax_type) = source_info.syntax_type.into_option() {
write!(&mut display, " ({})", syntax_type.display(&graph)).unwrap();