lrpeg icon indicating copy to clipboard operation
lrpeg copied to clipboard

NodeAPI suggestion

Open oovm opened this issue 4 years ago • 1 comments

This is a relatively private request, only for my project.

All the interfaces I need are listed here

/// It's a node contained in the Lossless Concrete Syntax Tree
/// All subsequent required information will be retained
/// Including spaces, line breaks, and comments or other semantically irrelevant content.
/// Macros and formatting can start at this level
pub trait CSTNode where Self: Sized {
    /// get str of the node
    fn get_str(&self) -> &str;
    /// Provide basic location information
    /// (start_offset, end_offset)
    fn get_span(&self) -> (usize, usize);
    /// Provide detailed row and column ranges
    /// (start_line, start_col, end_line, end_col)
    fn get_range(&self) -> (usize, usize, usize, usize);
    /// Get the tag of the current node
    fn get_node_tag(&self) -> Option<&'static str>;
    /// Get the tag of the current branch
    fn get_branch_tag(&self) -> Option<&'static str>;
    /// Find node tags in all of the children
    /// Then collect then into the vec, and store in hashmap with tag name
    fn get_tag_map(&self) -> HashMap<String, Vec<Self>>;
}

This version is used in pest, lrpeg itself does not store the input reference, so it may look like this

pub trait CSTNode where Self: Sized {
    fn get_str<'i>(&self,input: &'i str) -> &'i str;
    fn get_span(&self, input:&str) -> (usize, usize);
    fn get_range(&self, input:&str) -> (usize, usize, usize, usize);
    fn get_node_tag(&self) -> Option<&'static str>;
    fn get_branch_tag(&self) -> Option<&'static str>;
    fn get_tag_map(&self) -> HashMap<String, Vec<Self>>;
}

Hope this helps refer to which field in Node should keep.


Why do I need two ranges (span, range)?

because the language server protocol is like this, most interfaces are rows and columns, and a few cases are offsets.

oovm avatar Oct 20 '21 16:10 oovm

All good stuff. Yes I know about language servers, this makes sense.

I think we should store reference to the input.

seanyoung avatar Oct 20 '21 17:10 seanyoung