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
187 188 189 190 |
# File 'lib/tree_haver/backends/citrus.rb', line 187 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.
203 204 205 206 207 208 209 210 211 |
# File 'lib/tree_haver/backends/citrus.rb', line 203 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
219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/tree_haver/backends/citrus.rb', line 219 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.
239 240 241 |
# File 'lib/tree_haver/backends/citrus.rb', line 239 def parse_string(old_tree, source) # rubocop:disable Lint/UnusedMethodArgument parse(source) # Citrus doesn't support incremental parsing end |