Class: TreeHaver::Backends::Commonmarker::Language

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

Overview

Commonmarker language wrapper

Commonmarker only parses Markdown. This class exists for API compatibility.

Examples:

language = TreeHaver::Backends::Commonmarker::Language.markdown
parser.language = language

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new Commonmarker language instance

Parameters:

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

    Language name (should be :markdown)

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

    Commonmarker parse options



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

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

Instance Attribute Details

#backendSymbol (readonly)

The backend this language is for

Returns:

  • (Symbol)


81
82
83
# File 'lib/tree_haver/backends/commonmarker.rb', line 81

def backend
  @backend
end

#nameSymbol (readonly)

The language name (always :markdown for Commonmarker)

Returns:

  • (Symbol)


77
78
79
# File 'lib/tree_haver/backends/commonmarker.rb', line 77

def name
  @name
end

#optionsHash (readonly)

Commonmarker parse options

Returns:

  • (Hash)


85
86
87
# File 'lib/tree_haver/backends/commonmarker.rb', line 85

def options
  @options
end

Class Method Details

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

Load language from library path (API compatibility)

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

Parameters:

  • _path (String) (defaults to: nil)

    Ignored - Commonmarker doesn’t load external grammars

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

    Ignored

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

    Language name hint (defaults to :markdown)

Returns:

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/tree_haver/backends/commonmarker.rb', line 117

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

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

  markdown
end

.markdown(options: {}) ⇒ Language

Create a Markdown language instance

Parameters:

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

    Commonmarker parse options

Returns:



102
103
104
# File 'lib/tree_haver/backends/commonmarker.rb', line 102

def markdown(options: {})
  new(:markdown, options: options)
end

Instance Method Details

#<=>(other) ⇒ Object

Comparison for sorting/equality



132
133
134
135
# File 'lib/tree_haver/backends/commonmarker.rb', line 132

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

#inspectObject



137
138
139
# File 'lib/tree_haver/backends/commonmarker.rb', line 137

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