Class: TreeHaver::Base::Tree Abstract
- Inherits:
-
Object
- Object
- TreeHaver::Base::Tree
- Defined in:
- lib/tree_haver/base/tree.rb
Overview
Subclasses must implement #root_node
Base class for all backend Tree implementations
This class defines the API contract for Tree objects across all backends. It provides shared implementation and documents required/optional methods.
Backend Architecture
TreeHaver supports two categories of backends:
Tree-sitter Backends (MRI, Rust, FFI, Java)
These backends use the native tree-sitter library (via different bindings). They return raw ::TreeSitter::Tree objects which are wrapped by TreeHaver::Tree (which inherits from this class).
-
Backend Parser returns:
::TreeSitter::Tree(raw) -
TreeHaver::Parser wraps it in:
TreeHaver::Tree -
These backends do NOT define their own Tree/Node classes
Pure-Ruby/Plugin Backends (Citrus, Prism, Psych, Commonmarker, Markly)
These backends define their own complete implementations:
-
Backend::X::Tree- wraps parser-specific tree objects -
Backend::X::Node- wraps parser-specific node objects
For consistency, these should also inherit from Base::Tree and Base::Node.
Direct Known Subclasses
TreeHaver::Backends::Prism::Tree, TreeHaver::Backends::Psych::Tree
Instance Attribute Summary collapse
-
#inner_tree ⇒ Object
readonly
The underlying backend-specific tree object.
-
#lines ⇒ Array<String>
readonly
Source lines for byte offset calculations.
-
#source ⇒ String
readonly
The source text.
Instance Method Summary collapse
-
#comments ⇒ Array
Get comments from the document.
-
#edit(start_byte:, old_end_byte:, new_end_byte:, start_point:, old_end_point:, new_end_point:) ⇒ void
Mark the tree as edited for incremental re-parsing.
-
#errors ⇒ Array
Get parse errors.
-
#has_error? ⇒ Boolean
Check if this tree has syntax errors.
-
#initialize(inner_tree = nil, source: nil, lines: nil) ⇒ Tree
constructor
Create a new Tree.
-
#inspect ⇒ String
Human-readable representation.
-
#root_node ⇒ Node
Get the root node of the tree.
-
#warnings ⇒ Array
Get parse warnings.
Constructor Details
#initialize(inner_tree = nil, source: nil, lines: nil) ⇒ Tree
Create a new Tree
53 54 55 56 57 |
# File 'lib/tree_haver/base/tree.rb', line 53 def initialize(inner_tree = nil, source: nil, lines: nil) @inner_tree = inner_tree @source = source @lines = lines || source&.lines || [] end |
Instance Attribute Details
#inner_tree ⇒ Object (readonly)
The underlying backend-specific tree object
38 39 40 |
# File 'lib/tree_haver/base/tree.rb', line 38 def inner_tree @inner_tree end |
#lines ⇒ Array<String> (readonly)
Source lines for byte offset calculations
46 47 48 |
# File 'lib/tree_haver/base/tree.rb', line 46 def lines @lines end |
#source ⇒ String (readonly)
The source text
42 43 44 |
# File 'lib/tree_haver/base/tree.rb', line 42 def source @source end |
Instance Method Details
#comments ⇒ Array
Get comments from the document
83 84 85 |
# File 'lib/tree_haver/base/tree.rb', line 83 def comments [] end |
#edit(start_byte:, old_end_byte:, new_end_byte:, start_point:, old_end_point:, new_end_point:) ⇒ void
This method returns an undefined value.
Mark the tree as edited for incremental re-parsing
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/tree_haver/base/tree.rb', line 89 def edit( start_byte:, old_end_byte:, new_end_byte:, start_point:, old_end_point:, new_end_point: ) # Default implementation: no-op (incremental parsing not supported) # Backends that support it should override this end |
#errors ⇒ Array
Get parse errors
71 72 73 |
# File 'lib/tree_haver/base/tree.rb', line 71 def errors [] end |
#has_error? ⇒ Boolean
Check if this tree has syntax errors
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tree_haver/base/tree.rb', line 103 def has_error? root = root_node return false unless root return true if root.has_error? # Deep check: traverse tree looking for error nodes # Use queue-based traversal to avoid deep recursion queue = [root] while (node = queue.shift) return true if node.has_error? || node.missing? # Add children to queue node.each { |child| queue.push(child) } end false end |
#inspect ⇒ String
Human-readable representation
123 124 125 |
# File 'lib/tree_haver/base/tree.rb', line 123 def inspect "#<#{self.class.name}>" end |
#root_node ⇒ Node
Get the root node of the tree
63 64 65 |
# File 'lib/tree_haver/base/tree.rb', line 63 def root_node raise NotImplementedError, "#{self.class}#root_node must be implemented" end |
#warnings ⇒ Array
Get parse warnings
77 78 79 |
# File 'lib/tree_haver/base/tree.rb', line 77 def warnings [] end |