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

Parameters:

  • name (Symbol, String)

    Language name

  • backend (Symbol)

    Backend identifier

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

    Backend-specific options



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

Returns:

  • (Symbol)

    Backend identifier (e.g., :commonmarker, :markly, :prism)



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)

Returns:

  • (Symbol)

    Language name



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

def name
  @name
end

#optionsHash (readonly)

Language-specific options

Returns:

  • (Hash)

    Options hash



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.

Parameters:

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

    Path to shared library (optional for pure-Ruby)

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

    Symbol name to load (optional)

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

    Language name hint (optional)

Returns:

  • (Language)

    Loaded language instance

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

Parameters:

  • other (Object)

Returns:

  • (Integer, nil)


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

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

Returns:

  • (Integer)


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

Returns:

  • (String)


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