diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -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; |