Module: TreeHaver::Backends::Prism

Defined in:
lib/tree_haver/backends/prism.rb

Overview

Note:

This backend only parses Ruby source code

Prism backend using Ruby’s built-in Prism parser

This backend wraps Prism, Ruby’s official parser (stdlib in Ruby 3.4+, available as a gem for 3.2+). Unlike tree-sitter backends which are language-agnostic runtime parsers, Prism is specifically designed for parsing Ruby source code.

Prism provides excellent error recovery, detailed location information, and is the future of Ruby parsing (used by CRuby, JRuby, TruffleRuby).

Examples:

Basic usage

parser = TreeHaver::Parser.new
parser.language = TreeHaver::Backends::Prism::Language.ruby
tree = parser.parse(ruby_source)
root = tree.root_node
puts root.type  # => "program_node"

See Also:

Defined Under Namespace

Classes: Language, Node, Parser, Tree

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tree_haver/backends/prism.rb', line 39

def available?
  return @loaded if @load_attempted # rubocop:disable ThreadSafety/ClassInstanceVariable
  @load_attempted = true # rubocop:disable ThreadSafety/ClassInstanceVariable
  begin
    require "prism"

    @loaded = true # rubocop:disable ThreadSafety/ClassInstanceVariable
  rescue LoadError
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  end
  @loaded # rubocop:disable ThreadSafety/ClassInstanceVariable
end

.capabilitiesHash{Symbol => Object}

Get capabilities supported by this backend

Examples:

TreeHaver::Backends::Prism.capabilities
# => { backend: :prism, query: false, bytes_field: true, incremental: false, ruby_only: true }

Returns:

  • (Hash{Symbol => Object})

    capability map



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tree_haver/backends/prism.rb', line 67

def capabilities
  return {} unless available?
  {
    backend: :prism,
    query: false,           # Prism doesn't have tree-sitter-style queries (has pattern matching)
    bytes_field: true,      # Prism provides byte offsets via Location
    incremental: false,     # Prism doesn't support incremental parsing (yet)
    pure_ruby: false,       # Prism has native C extension (but also pure Ruby mode)
    ruby_only: true,        # Prism only parses Ruby source code
    error_tolerant: true,   # Prism has excellent error recovery
  }
end

.reset!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Reset the load state (primarily for testing)



56
57
58
59
# File 'lib/tree_haver/backends/prism.rb', line 56

def reset!
  @load_attempted = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
end