Module: TreeHaver::Backends::Citrus

Defined in:
lib/tree_haver/backends/citrus.rb

Overview

Note:

This backend requires a Citrus grammar for the specific language

Citrus backend using pure Ruby PEG parser

This backend wraps Citrus-based parsers (like toml-rb) to provide a pure Ruby alternative to tree-sitter. Citrus is a PEG (Parsing Expression Grammar) parser generator written in Ruby.

Unlike tree-sitter backends which are language-agnostic runtime parsers, Citrus parsers are grammar-specific and compiled into Ruby code. Each language needs its own Citrus grammar (e.g., toml-rb for TOML).

Examples:

Using with toml-rb

require "toml-rb"

parser = TreeHaver::Parser.new
# For Citrus, "language" is actually a grammar module
parser.language = TomlRB::Document
tree = parser.parse(toml_source)

See Also:

Defined Under Namespace

Classes: Language, Node, Parser, Tree

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tree_haver/backends/citrus.rb', line 40

def available?
  return @loaded if @load_attempted # rubocop:disable ThreadSafety/ClassInstanceVariable
  @load_attempted = true # rubocop:disable ThreadSafety/ClassInstanceVariable
  begin
    require "citrus"

    @loaded = true # rubocop:disable ThreadSafety/ClassInstanceVariable
  rescue LoadError
    @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  end
  @loaded # rubocop:disable ThreadSafety/ClassInstanceVariable
end

.capabilitiesHash{Symbol => Object}

Get capabilities supported by this backend

Examples:

TreeHaver::Backends::Citrus.capabilities
# => { backend: :citrus, query: false, bytes_field: true, incremental: false }

Returns:

  • (Hash{Symbol => Object})

    capability map



68
69
70
71
72
73
74
75
76
77
# File 'lib/tree_haver/backends/citrus.rb', line 68

def capabilities
  return {} unless available?
  {
    backend: :citrus,
    query: false,          # Citrus doesn't have a query API like tree-sitter
    bytes_field: true,     # Citrus::Match provides offset and length
    incremental: false,    # Citrus doesn't support incremental parsing
    pure_ruby: true,       # Citrus is pure Ruby (portable)
  }
end

.reset!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Reset the load state (primarily for testing)



57
58
59
60
# File 'lib/tree_haver/backends/citrus.rb', line 57

def reset!
  @load_attempted = false # rubocop:disable ThreadSafety/ClassInstanceVariable
  @loaded = false # rubocop:disable ThreadSafety/ClassInstanceVariable
end