Class: Ibex::Runtime::CST::Kind

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/runtime/cst/kind.rb

Overview

Grammar-specific integer kind names and interval predicates.

Instance Method Summary collapse

Constructor Details

#initialize(metadata, slots: {}) ⇒ Kind

Returns a new instance of Kind.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ibex/runtime/cst/kind.rb', line 43

def initialize(, slots: {})
  @names = .fetch(:names).dup.freeze
  @terminal_range = .fetch(:terminal_range).dup.freeze
  @nonterminal_range = .fetch(:nonterminal_range).dup.freeze
  @named = .fetch(:named).dup.freeze
  @named_nonterminals = .fetch(:named_nonterminals).dup.freeze
  @trivia = .fetch(:trivia).dup.freeze
  @synthetic = .fetch(:synthetic).dup.freeze
  @field_slots = build_field_slots(slots)
  freeze
end

Instance Method Details

#error?(kind) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/ibex/runtime/cst/kind.rb', line 78

def error?(kind)
  kind == @synthetic["lexical_error_token"] || kind == @synthetic["error_node"]
end

#fetch(name) ⇒ Object



61
62
63
64
# File 'lib/ibex/runtime/cst/kind.rb', line 61

def fetch(name)
  key = name.to_s
  @named[key] || @trivia[key] || @synthetic.fetch(key)
end

#fields(kind) ⇒ Object

Return the named field-to-physical-slot mapping for a kind.



90
# File 'lib/ibex/runtime/cst/kind.rb', line 90

def fields(kind) = @field_slots.fetch(kind, EMPTY_FIELDS)

#name(kind) ⇒ Object



56
57
58
# File 'lib/ibex/runtime/cst/kind.rb', line 56

def name(kind)
  @names.fetch(kind)
end

#nonterminal?(kind) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ibex/runtime/cst/kind.rb', line 70

def nonterminal?(kind)
  within?(@nonterminal_range, kind) || @named_nonterminals.key?(kind)
end

#nonterminal_of(kind) ⇒ Object

Map a named node kind to its physical nonterminal kind.



84
85
86
# File 'lib/ibex/runtime/cst/kind.rb', line 84

def nonterminal_of(kind)
  @named_nonterminals.fetch(kind, kind)
end

#terminal?(kind) ⇒ Boolean

Returns:

  • (Boolean)


67
# File 'lib/ibex/runtime/cst/kind.rb', line 67

def terminal?(kind) = within?(@terminal_range, kind)

#to_hObject

Return the serialization-safe kind metadata.



94
95
96
97
98
99
100
101
# File 'lib/ibex/runtime/cst/kind.rb', line 94

def to_h
  value = {
    names: @names, terminal_range: @terminal_range, nonterminal_range: @nonterminal_range,
    named: @named, named_nonterminals: @named_nonterminals,
    trivia: @trivia, synthetic: @synthetic
  } #: metadata
  value.freeze
end

#trivia?(kind) ⇒ Boolean

Returns:

  • (Boolean)


75
# File 'lib/ibex/runtime/cst/kind.rb', line 75

def trivia?(kind) = @trivia.value?(kind)