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)



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

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)


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

def backend
  @backend
end

#nameSymbol (readonly)

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)


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

def options
  @options
end

Class Method Details

.from_library(path, symbol: nil, name: nil) ⇒ Object Also known as: from_path

Not applicable for Prism (tree-sitter-specific)

Prism is Ruby-only and doesn’t load external grammar libraries. This method exists for API compatibility but will raise an error.

Raises:



162
163
164
165
166
# File 'lib/tree_haver/backends/prism.rb', line 162

def from_library(path, symbol: nil, name: nil)
  raise TreeHaver::NotAvailable,
    "Prism backend doesn't use shared libraries. " \
      "Use Prism::Language.ruby instead."
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:



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

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



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

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)


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

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