Class: TreeHaver::Backends::Citrus::Parser
- Inherits:
-
Object
- Object
- TreeHaver::Backends::Citrus::Parser
- Defined in:
- lib/tree_haver/backends/citrus.rb
Overview
Citrus parser wrapper
Wraps Citrus grammar modules to provide a tree-sitter-like API.
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
Create a new Citrus parser instance.
-
#language=(grammar) ⇒ void
Set the grammar for this parser.
-
#parse(source) ⇒ Tree
Parse source code.
-
#parse_string(old_tree, source) ⇒ Tree
Parse source code (compatibility with tree-sitter API).
Constructor Details
#initialize ⇒ Parser
Create a new Citrus parser instance
158 159 160 161 |
# File 'lib/tree_haver/backends/citrus.rb', line 158 def initialize raise TreeHaver::NotAvailable, "citrus gem not available" unless Citrus.available? @grammar = nil end |
Instance Method Details
#language=(grammar) ⇒ void
This method returns an undefined value.
Set the grammar for this parser
Note: TreeHaver::Parser unwraps language objects before calling this method. This backend receives the raw Citrus grammar module (unwrapped), not the Language wrapper.
174 175 176 177 178 179 180 181 182 |
# File 'lib/tree_haver/backends/citrus.rb', line 174 def language=(grammar) # grammar is already unwrapped by TreeHaver::Parser unless grammar.respond_to?(:parse) raise ArgumentError, "Expected Citrus grammar module with parse method, " \ "got #{grammar.class}" end @grammar = grammar end |
#parse(source) ⇒ Tree
Parse source code
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/tree_haver/backends/citrus.rb', line 190 def parse(source) raise TreeHaver::NotAvailable, "No grammar loaded" unless @grammar begin citrus_match = @grammar.parse(source) # Return raw Citrus::Tree - TreeHaver::Parser will wrap it Tree.new(citrus_match, source) rescue ::Citrus::ParseError => e # Re-raise with more context raise TreeHaver::Error, "Parse error: #{e.message}" end end |
#parse_string(old_tree, source) ⇒ Tree
Parse source code (compatibility with tree-sitter API)
Citrus doesn’t support incremental parsing, so old_tree is ignored.
210 211 212 |
# File 'lib/tree_haver/backends/citrus.rb', line 210 def parse_string(old_tree, source) # rubocop:disable Lint/UnusedMethodArgument parse(source) # Citrus doesn't support incremental parsing end |