Class: TreeHaver::Backends::Parslet::Parser
- Inherits:
-
Object
- Object
- TreeHaver::Backends::Parslet::Parser
- Defined in:
- lib/tree_haver/backends/parslet.rb
Overview
Parslet parser wrapper
Wraps Parslet grammar classes to provide a tree-sitter-like API.
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
Create a new Parslet 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 Parslet parser instance
225 226 227 228 229 230 |
# File 'lib/tree_haver/backends/parslet.rb', line 225 def initialize raise TreeHaver::NotAvailable, 'parslet gem not available' unless Parslet.available? @grammar = nil @backend = :parslet end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
232 233 234 |
# File 'lib/tree_haver/backends/parslet.rb', line 232 def backend @backend end |
Instance Method Details
#language=(grammar) ⇒ void
This method returns an undefined value.
Set the grammar for this parser
Accepts either a Parslet::Language wrapper or a raw Parslet grammar class. When passed a Language wrapper, extracts the grammar_class from it. When passed a raw grammar class, uses it directly.
This flexibility allows both patterns:
parser.language = TreeHaver::Backends::Parslet::Language.new(TOML::Parslet)
parser.language = TOML::Parslet # Also works
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/tree_haver/backends/parslet.rb', line 246 def language=(grammar) # Accept Language wrapper or raw grammar class actual_grammar = case grammar when Language grammar.grammar_class else grammar end unless actual_grammar.respond_to?(:new) raise ArgumentError, 'Expected Parslet grammar class with new method or Language wrapper, ' \ "got #{grammar.class}" end @grammar = actual_grammar end |
#parse(source) ⇒ Tree
Parse source code
269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/tree_haver/backends/parslet.rb', line 269 def parse(source) raise TreeHaver::NotAvailable, 'No grammar loaded' unless @grammar begin parser_instance = @grammar.new parslet_result = parser_instance.parse(source) # Return raw Parslet result wrapped in Tree - TreeHaver::Parser will wrap it Tree.new(parslet_result, source) rescue ::Parslet::ParseFailed => e # Re-raise with more context raise TreeHaver::Error, "Parse error: #{e.}" end end |
#parse_string(old_tree, source) ⇒ Tree
Parse source code (compatibility with tree-sitter API)
Parslet doesn't support incremental parsing, so old_tree is ignored.
290 291 292 |
# File 'lib/tree_haver/backends/parslet.rb', line 290 def parse_string(old_tree, source) # rubocop:disable Lint/UnusedMethodArgument parse(source) # Parslet doesn't support incremental parsing end |