Class: TreeHaver::Backends::Prism::Language

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tree_haver/backends/prism.rb

Overview

Prism language wrapper

Unlike tree-sitter which supports many languages via grammar files, Prism only parses Ruby. This class exists for API compatibility with other tree_haver backends.

Examples:

language = TreeHaver::Backends::Prism::Language.ruby
parser.language = language

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :ruby, options: {}) ⇒ Language

Returns a new instance of Language.

Parameters:

  • name (Symbol) (defaults to: :ruby)

    language name (should be :ruby)

  • options (Hash) (defaults to: {})

    Prism parsing options (e.g., frozen_string_literal, version)



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tree_haver/backends/prism.rb', line 108

def initialize(name = :ruby, options: {})
  @name = name.to_sym
  @backend = :prism
  @options = options

  unless @name == :ruby
    raise TreeHaver::NotAvailable,
      "Prism only supports Ruby parsing. " \
        "Got language: #{name.inspect}"
  end
end

Instance Attribute Details

#backendSymbol (readonly)

The backend this language is for

Returns:

  • (Symbol)


100
101
102
# File 'lib/tree_haver/backends/prism.rb', line 100

def backend
  @backend
end

#nameSymbol (readonly) Also known as: language_name

The language name (always :ruby for Prism)

Returns:

  • (Symbol)


95
96
97
# File 'lib/tree_haver/backends/prism.rb', line 95

def name
  @name
end

#optionsHash (readonly)

Prism parsing options

Returns:

  • (Hash)


104
105
106
# File 'lib/tree_haver/backends/prism.rb', line 104

def options
  @options
end

Class Method Details

.from_library(_path = nil, symbol: nil, name: nil) ⇒ Language Also known as: from_path

Load language from library path (API compatibility)

Prism only supports Ruby, so path and symbol parameters are ignored. This method exists for API consistency with tree-sitter backends, allowing ‘TreeHaver.parser_for(:ruby)` to work regardless of backend.

Parameters:

  • _path (String) (defaults to: nil)

    Ignored - Prism doesn’t load external grammars

  • symbol (String, nil) (defaults to: nil)

    Ignored

  • name (String, nil) (defaults to: nil)

    Language name hint (defaults to :ruby)

Returns:

Raises:



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/tree_haver/backends/prism.rb', line 168

def from_library(_path = nil, symbol: nil, name: nil)
  # Derive language name from symbol if provided
  lang_name = name || symbol&.to_s&.sub(/^tree_sitter_/, "")&.to_sym || :ruby

  unless lang_name == :ruby
    raise TreeHaver::NotAvailable,
      "Prism backend only supports Ruby, not #{lang_name}. " \
        "Use a tree-sitter backend for #{lang_name} support."
  end

  ruby
end

.ruby(options = {}) ⇒ Language

Create a Ruby language instance (convenience method)

Examples:

lang = TreeHaver::Backends::Prism::Language.ruby
lang = TreeHaver::Backends::Prism::Language.ruby(frozen_string_literal: true)

Parameters:

  • options (Hash) (defaults to: {})

    Prism parsing options

Options Hash (options):

  • :frozen_string_literal (Boolean)

    frozen string literal pragma

  • :version (String)

    Ruby version to parse as (e.g., “3.3.0”)

  • :command_line (Symbol)

    command line option (-e, -n, etc.)

Returns:



153
154
155
# File 'lib/tree_haver/backends/prism.rb', line 153

def ruby(options = {})
  new(:ruby, options: options)
end

Instance Method Details

#<=>(other) ⇒ Integer?

Compare languages for equality

Prism languages are equal if they have the same backend and options.

Parameters:

  • other (Object)

    object to compare with

Returns:

  • (Integer, nil)

    -1, 0, 1, or nil if not comparable



126
127
128
129
130
131
# File 'lib/tree_haver/backends/prism.rb', line 126

def <=>(other)
  return unless other.is_a?(Language)
  return unless other.backend == @backend

  @options.to_a.sort <=> other.options.to_a.sort
end

#hashInteger

Hash value for this language (for use in Sets/Hashes)

Returns:

  • (Integer)


135
136
137
# File 'lib/tree_haver/backends/prism.rb', line 135

def hash
  [@backend, @name, @options.to_a.sort].hash
end