|
| 1 | +// -*- coding: utf-8 -*- |
| 2 | +// ------------------------------------------------------------------------------------------------ |
| 3 | +// Copyright © 2023, stack-graphs authors. |
| 4 | +// Licensed under either of Apache License, Version 2.0, or MIT license, at your option. |
| 5 | +// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details. |
| 6 | +// ------------------------------------------------------------------------------------------------ |
| 7 | + |
| 8 | +//! Construct stack graphs using a Lua script that consumes a tree-sitter parse tree |
| 9 | +
|
| 10 | +use std::borrow::Cow; |
| 11 | + |
| 12 | +use mlua::Lua; |
| 13 | +use mlua_tree_sitter::Module; |
| 14 | +use mlua_tree_sitter::WithSource; |
| 15 | +use stack_graphs::arena::Handle; |
| 16 | +use stack_graphs::graph::File; |
| 17 | +use stack_graphs::graph::StackGraph; |
| 18 | + |
| 19 | +use crate::parse_file; |
| 20 | +use crate::BuildError; |
| 21 | +use crate::CancellationFlag; |
| 22 | + |
| 23 | +/// Holds information about how to construct stack graphs for a particular language. |
| 24 | +pub struct StackGraphLanguageLua { |
| 25 | + language: tree_sitter::Language, |
| 26 | + lua_source: Cow<'static, [u8]>, |
| 27 | + lua_source_name: String, |
| 28 | +} |
| 29 | + |
| 30 | +impl StackGraphLanguageLua { |
| 31 | + /// Creates a new stack graph language for the given language, loading the Lua stack graph |
| 32 | + /// construction rules from a static string. |
| 33 | + pub fn from_static_str( |
| 34 | + language: tree_sitter::Language, |
| 35 | + lua_source: &'static [u8], |
| 36 | + lua_source_name: &str, |
| 37 | + ) -> StackGraphLanguageLua { |
| 38 | + StackGraphLanguageLua { |
| 39 | + language, |
| 40 | + lua_source: Cow::from(lua_source), |
| 41 | + lua_source_name: lua_source_name.to_string(), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// Creates a new stack graph language for the given language, loading the Lua stack graph |
| 46 | + /// construction rules from a string. |
| 47 | + pub fn from_str( |
| 48 | + language: tree_sitter::Language, |
| 49 | + lua_source: &[u8], |
| 50 | + lua_source_name: &str, |
| 51 | + ) -> StackGraphLanguageLua { |
| 52 | + StackGraphLanguageLua { |
| 53 | + language, |
| 54 | + lua_source: Cow::from(lua_source.to_vec()), |
| 55 | + lua_source_name: lua_source_name.to_string(), |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + pub fn language(&self) -> tree_sitter::Language { |
| 60 | + self.language |
| 61 | + } |
| 62 | + |
| 63 | + pub fn lua_source_name(&self) -> &str { |
| 64 | + &self.lua_source_name |
| 65 | + } |
| 66 | + |
| 67 | + pub fn lua_source(&self) -> &Cow<'static, [u8]> { |
| 68 | + &self.lua_source |
| 69 | + } |
| 70 | + |
| 71 | + /// Executes the graph construction rules for this language against a source file, creating new |
| 72 | + /// nodes and edges in `stack_graph`. Any new nodes that we create will belong to `file`. |
| 73 | + /// (The source file must be implemented in this language, otherwise you'll probably get a |
| 74 | + /// parse error.) |
| 75 | + pub fn build_stack_graph_into<'a>( |
| 76 | + &'a self, |
| 77 | + stack_graph: &'a mut StackGraph, |
| 78 | + file: Handle<File>, |
| 79 | + source: &'a str, |
| 80 | + cancellation_flag: &'a dyn CancellationFlag, |
| 81 | + ) -> Result<(), BuildError> { |
| 82 | + // Create a Lua environment and load the language's stack graph rules. |
| 83 | + // TODO: Sandbox the Lua environment |
| 84 | + let mut lua = Lua::new(); |
| 85 | + lua.open_ltreesitter(false)?; |
| 86 | + lua.load(self.lua_source.as_ref()) |
| 87 | + .set_name(&self.lua_source_name) |
| 88 | + .exec()?; |
| 89 | + let process: mlua::Function = lua.globals().get("process")?; |
| 90 | + |
| 91 | + // Parse the source using the requested grammar. |
| 92 | + let tree = parse_file(self.language, source, cancellation_flag)?; |
| 93 | + let tree = tree.with_source(source.as_bytes()); |
| 94 | + |
| 95 | + // Invoke the Lua `process` function with the parsed tree and the stack graph file. |
| 96 | + // TODO: Add a debug hook that checks the cancellation flag during execution |
| 97 | + lua.scope(|scope| { |
| 98 | + let file = stack_graph.file_lua_ref_mut(file, scope)?; |
| 99 | + process.call((tree, file)) |
| 100 | + })?; |
| 101 | + Ok(()) |
| 102 | + } |
| 103 | +} |
0 commit comments