summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-07-16 18:06:18 +0800
committerJSDurand <mmemmew@gmail.com>2023-07-16 18:06:18 +0800
commit780f3cc80cadf87ecfdb702ef90fcb606f2783fd (patch)
tree7d978d43b1c6f58c358e6f8e8d9f30c0303a7a98 /src/lib.rs
parent6a24e0a805c597b8f835c5c72a0e4dcdd64ca39b (diff)
Fix the bug of forgetting to check cloned nodes.
In the process of splitting, cloning, and planting the forest, I forgot to check whether some cloned node of the node inquestion satisfy the condition. This used to cause forests that violate some fundamental assumptions. Now this is supposed to be fixed, but more tests await us.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 685c66e..5412ed7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -8,6 +8,11 @@ extern crate grammar;
use chain::{atom::DefaultAtom, default::DefaultChain, Chain};
use grammar::Grammar;
+// For printing forests
+use chain::item::{default::DefaultForest, ForestLabel};
+use grammar::GrammarLabel;
+use graph::LabelGraph;
+
/// This struct is the representation of a parser.
///
/// When the user constructs a parser, an instance of this struct will
@@ -545,4 +550,41 @@ extern "C" fn parser_parse(
}
}
+// TODO: Write a function to print the node label of a forest and
+// expose it to C ABI.
+//
+// This can be used in LLDB.
+
+/// This struct is a wrapper around the forest.
+///
+/// This is used so that we can call a C function receiving a pointer
+/// to a forest struct.
+#[derive(Debug, Clone)]
+#[repr(C)]
+pub struct CForest {
+ forest: DefaultForest<ForestLabel<GrammarLabel>>,
+}
+
+/// Print the label of the node with id `node` in the forest `forest`.
+///
+/// The parameter `node` should point to 8 bytes of unsigned
+/// characters, which forms a number in 64 bits, in the *big endian*
+/// format.
+#[no_mangle]
+extern "C" fn print_forest_node(forest: *mut CForest, node: *mut std::os::raw::c_uchar) {
+ let node = usize::from_be_bytes(
+ unsafe { std::slice::from_raw_parts(node, 8) }
+ .try_into()
+ .unwrap(),
+ );
+
+ let forest = unsafe { (*forest).forest.clone() };
+
+ let Ok(Some(label)) = forest.vertex_label(node) else {
+ return;
+ };
+
+ println!("node {node} has label {label}");
+}
+
pub mod bytes;