Class: TreeHaver::Base::Language Abstract

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tree_haver/base/language.rb

Overview

This class is abstract.

Subclasses must implement #name and #backend at minimum

Base class for backend Language implementations

This class defines the API contract for all language implementations. Backend-specific Language classes should inherit from this and implement the required interface.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new Language instance



32
33
34
35
36
# File 'lib/tree_haver/base/language.rb', line 32

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

Instance Attribute Details

#backendSymbol (readonly)

The backend this language is for



21
22
23
# File 'lib/tree_haver/base/language.rb', line 21

def backend
  @backend
end

#nameSymbol (readonly) Also known as: language_name

The language name (e.g., :markdown, :ruby, :json)



17
18
19
# File 'lib/tree_haver/base/language.rb', line 17

def name
  @name
end

#optionsHash (readonly)

Language-specific options



25
26
27
# File 'lib/tree_haver/base/language.rb', line 25

def options
  @options
end

Class Method Details

.from_library(_path = nil, symbol: nil, name: nil) ⇒ Language

Load a language from a library path (factory method)

For pure-Ruby backends (Commonmarker, Markly, Prism, Psych), this typically ignores the path and returns the single supported language.

For tree-sitter backends (MRI, Rust, FFI, Java), this loads the language from the shared library file.

Raises:

  • (NotImplementedError)

    If not implemented by subclass



92
93
94
# File 'lib/tree_haver/base/language.rb', line 92

def from_library(_path = nil, symbol: nil, name: nil)
  raise NotImplementedError, "#{self}.from_library must be implemented"
end

Instance Method Details

#<=>(other) ⇒ Integer?

Comparison based on backend then name



46
47
48
49
50
51
# File 'lib/tree_haver/base/language.rb', line 46

def <=>(other)
  return unless other.is_a?(Language)
  return unless other.respond_to?(:backend) && other.backend == backend

  name <=> other.name
end

#eql?(other) ⇒ Boolean

Equality check for Hash keys



62
63
64
65
66
# File 'lib/tree_haver/base/language.rb', line 62

def eql?(other)
  return false unless other.is_a?(Language)

  backend == other.backend && name == other.name && options == other.options
end

#hashInteger

Hash value for use in Sets/Hashes



55
56
57
# File 'lib/tree_haver/base/language.rb', line 55

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

#inspectString

Human-readable representation



70
71
72
73
74
# File 'lib/tree_haver/base/language.rb', line 70

def inspect
  opts = options.empty? ? "" : " options=#{options}"
  class_name = self.class.name || "#{self.class.superclass.name}(anonymous)"
  "#<#{class_name} name=#{name} backend=#{backend}#{opts}>"
end