Class: TreeHaver::Backends::Citrus::Language
- Inherits:
-
Object
- Object
- TreeHaver::Backends::Citrus::Language
- Includes:
- Comparable
- Defined in:
- lib/tree_haver/backends/citrus.rb
Overview
Citrus grammar wrapper
Unlike tree-sitter which loads compiled .so files, Citrus uses Ruby modules that define grammars. This class wraps a Citrus grammar module.
Instance Attribute Summary collapse
-
#backend ⇒ Symbol
readonly
The backend this language is for.
-
#grammar_module ⇒ Module
readonly
The Citrus grammar module.
Class Method Summary collapse
- .from_library(path, symbol: nil, name: nil) ⇒ Object (also: from_path)
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compare languages for equality.
-
#hash ⇒ Integer
Hash value for this language (for use in Sets/Hashes).
-
#initialize(grammar_module) ⇒ Language
constructor
A new instance of Language.
Constructor Details
#initialize(grammar_module) ⇒ Language
Returns a new instance of Language.
100 101 102 103 104 105 106 107 108 |
# File 'lib/tree_haver/backends/citrus.rb', line 100 def initialize(grammar_module) unless grammar_module.respond_to?(:parse) raise TreeHaver::NotAvailable, "Grammar module must respond to :parse. " \ "Expected a Citrus grammar module (e.g., TomlRB::Document)." end @grammar_module = grammar_module @backend = :citrus end |
Instance Attribute Details
#backend ⇒ Symbol (readonly)
The backend this language is for
97 98 99 |
# File 'lib/tree_haver/backends/citrus.rb', line 97 def backend @backend end |
#grammar_module ⇒ Module (readonly)
The Citrus grammar module
93 94 95 |
# File 'lib/tree_haver/backends/citrus.rb', line 93 def grammar_module @grammar_module end |
Class Method Details
.from_library(path, symbol: nil, name: nil) ⇒ Object Also known as: from_path
141 142 143 144 145 |
# File 'lib/tree_haver/backends/citrus.rb', line 141 def from_library(path, symbol: nil, name: nil) raise TreeHaver::NotAvailable, "Citrus backend doesn't use shared libraries. " \ "Use Citrus::Language.new(GrammarModule) instead." end |
Instance Method Details
#<=>(other) ⇒ Integer?
Compare languages for equality
Citrus languages are equal if they have the same backend and grammar_module. Grammar module uniquely identifies a Citrus language.
117 118 119 120 121 122 123 |
# File 'lib/tree_haver/backends/citrus.rb', line 117 def <=>(other) return unless other.is_a?(Language) return unless other.backend == @backend # Compare by grammar_module name (modules are compared by object_id by default) @grammar_module.name <=> other.grammar_module.name end |
#hash ⇒ Integer
Hash value for this language (for use in Sets/Hashes)
127 128 129 |
# File 'lib/tree_haver/backends/citrus.rb', line 127 def hash [@backend, @grammar_module.name].hash end |