Class: Ibex::Frontend::TokenAdapter::RuleState

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/frontend/token_adapter/rule_state.rb

Overview

Classifies context-dependent tokens in the grammar rule section.

Constant Summary collapse

SCALAR_TYPES =
{
  literal: :LITERAL, integer: :INTEGER, action: :ACTION, user_code: :USER_CODE
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuleState

Returns a new instance of RuleState.



21
22
23
24
25
# File 'lib/ibex/frontend/token_adapter/rule_state.rb', line 21

def initialize
  @state = :rules_lhs
  @section = :rules
  @delimiters = DelimiterTracker.new
end

Instance Attribute Details

#sectionObject (readonly)

: Hash[Symbol, external_token]



12
13
14
# File 'lib/ibex/frontend/token_adapter/rule_state.rb', line 12

def section
  @section
end

#stateObject (readonly)

: Symbol



13
14
15
# File 'lib/ibex/frontend/token_adapter/rule_state.rb', line 13

def state
  @state
end

Instance Method Details

#classify(token, remaining, last_external:, previous_external:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ibex/frontend/token_adapter/rule_state.rb', line 29

def classify(token, remaining, last_external:, previous_external:)
  external = if token.type == :identifier
               classify_identifier(token, remaining, last_external, previous_external)
             elsif token.type == :node
               @node_annotation = true
               :NODE
             elsif token.type == :inline
               classify_inline
             elsif token.type == :empty
               :EMPTY
             elsif SCALAR_TYPES.key?(token.type)
               SCALAR_TYPES.fetch(token.type)
             else
               classify_punctuation(token)
             end
  @delimiters.observe(token, external, last_external)
  @node_annotation = false if token.type == :")" && @node_annotation
  external
end

#expectation(token) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/ibex/frontend/token_adapter/rule_state.rb', line 60

def expectation(token)
  if @state == :rules_lhs && @section == :rules
    rules_lhs_expectation(token)
  elsif @state == :rule_colon
    ":"
  elsif @state == :rule_rhs && %w[) ,].include?(token&.value)
    "a grammar symbol"
  end
end

#group_openingObject



50
51
52
# File 'lib/ibex/frontend/token_adapter/rule_state.rb', line 50

def group_opening
  @delimiters.group_opening
end

#open_delimiter_kindObject



55
56
57
# File 'lib/ibex/frontend/token_adapter/rule_state.rb', line 55

def open_delimiter_kind
  @delimiters.open_kind
end