Class: Woods::Ast::Parser

Inherits:
Object
  • Object
show all
Includes:
SourceSpan
Defined in:
lib/woods/ast/parser.rb

Overview

Parser adapter that normalizes Prism and parser gem ASTs to a common Node structure. Auto-detects parser availability at load time.

Examples:

Parsing Ruby source

parser = Ast::Parser.new
root = parser.parse("class Foo; def bar; end; end")
root.find_all(:def).first.method_name #=> "bar"

Instance Method Summary collapse

Instance Method Details

#parse(source) ⇒ Ast::Node

Parse Ruby source into a normalized AST.

Parameters:

  • source (String)

    Ruby source code

Returns:

  • (Ast::Node)

    Root node of the normalized tree

Raises:



25
26
27
28
29
30
31
32
33
# File 'lib/woods/ast/parser.rb', line 25

def parse(source)
  if prism_available?
    parse_with_prism(source)
  else
    parse_with_parser_gem(source)
  end
rescue StandardError => e
  raise Woods::ExtractionError, "Failed to parse source: #{e.message}"
end

#prism_available?Boolean

Check if Prism is available.

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/woods/ast/parser.rb', line 38

def prism_available?
  return @prism_available unless @prism_available.nil?

  require 'prism'
  @prism_available = true
rescue LoadError
  @prism_available = false
end