Class: TreeHaver::Backends::Psych::Language

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

Overview

Psych language wrapper

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

Examples:

language = TreeHaver::Backends::Psych::Language.yaml
parser.language = language

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :yaml) ⇒ Language

Create a new Psych language instance

Parameters:

  • (defaults to: :yaml)

    Language name (should be :yaml)



91
92
93
94
# File 'lib/tree_haver/backends/psych.rb', line 91

def initialize(name = :yaml)
  @name = name.to_sym
  @backend = :psych
end

Instance Attribute Details

#backendSymbol (readonly)

The backend this language is for

Returns:



86
87
88
# File 'lib/tree_haver/backends/psych.rb', line 86

def backend
  @backend
end

#nameSymbol (readonly)

The language name (always :yaml for Psych)

Returns:



82
83
84
# File 'lib/tree_haver/backends/psych.rb', line 82

def name
  @name
end

Class Method Details

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

Load language from library path (API compatibility)

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

Parameters:

  • (defaults to: nil)

    Ignored - Psych doesn’t load external grammars

  • (defaults to: nil)

    Ignored

  • (defaults to: nil)

    Language name hint (defaults to :yaml)

Returns:

  • YAML language

Raises:

  • if requested language is not YAML



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tree_haver/backends/psych.rb', line 115

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 || :yaml

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

  yaml
end

.yamlLanguage

Create a YAML language instance

Returns:

  • YAML language



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

def yaml
  new(:yaml)
end

Instance Method Details

#<=>(other) ⇒ Integer?

Comparison for sorting/equality

Parameters:

  • other language

Returns:

  • comparison result



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

def <=>(other)
  return unless other.is_a?(Language)
  name <=> other.name
end

#inspectString

Returns human-readable representation.

Returns:

  • human-readable representation



139
140
141
# File 'lib/tree_haver/backends/psych.rb', line 139

def inspect
  "#<TreeHaver::Backends::Psych::Language name=#{name}>"
end