Class: Ibex::Frontend::TokenAdapter::DeclarationState

Inherits:
Object
  • Object
show all
Includes:
DeclarationDocumentState, DeclarationLexerState
Defined in:
lib/ibex/frontend/token_adapter/declaration_state.rb

Overview

Classifies tokens through the class header and declaration section. rubocop:disable Metrics/ClassLength, Metrics/CyclomaticComplexity One state machine owns declaration-boundary classification.

Constant Summary collapse

DECLARATIONS =
{
  "include" => %i[INCLUDE include_path],
  "import" => %i[IMPORT include_path],
  "token" => %i[TOKEN token_symbols], "options" => %i[OPTIONS options_identifiers],
  "expect" => %i[EXPECT expect_integer], "start" => %i[START start_first_symbol],
  "expect_rr" => %i[EXPECT_RR expect_rr_integer],
  "recover" => %i[RECOVER recover_kind],
  "on_error_reduce" => %i[ON_ERROR_REDUCE on_error_reduce_first_symbol],
  "test" => %i[TEST test_expectation],
  "lexer" => %i[LEXER lexer_entries],
  "convert" => %i[CONVERT convert_name], "pragma" => %i[PRAGMA pragma_value],
  "display" => %i[DISPLAY display_symbol], "type" => %i[TYPE type_symbol],
  "param" => %i[PARAM param_name],
  "printer" => %i[PRINTER printer_symbol],
  "rule" => %i[RULE rules]
}.freeze
ASSOCIATIONS =

: Hash[String, [external_token, Symbol]]

{
  "left" => :LEFT, "right" => :RIGHT, "nonassoc" => :NONASSOC, "precedence" => :PRECEDENCE
}.freeze
SCALAR_TYPES =

: Hash[String, external_token]

{
  literal: :LITERAL, regexp: :REGEXP, integer: :INTEGER, action: :ACTION, user_code: :USER_CODE
}.freeze
EXPECTATIONS =

: Hash[Symbol, external_token]

{
  class_keyword: "class", class_name: "identifier", superclass_name: "identifier",
  expect_integer: "integer", expect_rr_integer: "integer",
  start_first_symbol: "a grammar symbol", start_symbols: "a grammar symbol",
  include_path: "a double-quoted relative path",
  display_symbol: "a grammar symbol", type_symbol: "a grammar symbol",
  display_value: "a quoted string", type_value: "a quoted string",
  param_name: "an identifier", param_type: "a quoted type or declaration",
  printer_symbol: "a grammar symbol", printer_action: "an action",
  recover_kind: "sync", recovery_colon: ":", recovery_first_symbol: "a grammar symbol",
  recovery_symbols: "a grammar symbol", on_error_reduce_first_symbol: "a grammar symbol",
  on_error_reduce_symbols: "a grammar symbol", test_expectation: "accept or reject",
  test_source: "a double-quoted string",
  lexer_entries: "a lexer rule, state, or end", lexer_state_name: "a state name",
  lexer_state_do: "do", lexer_pattern: "a regular expression or quoted literal",
  lexer_action_or_entry: "an action, lexer rule, state, or end"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extended: false) ⇒ DeclarationState

Returns a new instance of DeclarationState.



66
67
68
69
70
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 66

def initialize(extended: false)
  @extended_mode = extended
  @pragmas = {} #: Hash[String, bool]
  @state = :class_keyword
end

Instance Attribute Details

#conversion_nameObject (readonly)

: Hash[Symbol, String]



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

def conversion_name
  @conversion_name
end

#declarationObject (readonly)

: Symbol?



56
57
58
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 56

def declaration
  @declaration
end

#precedence_closerObject (readonly)

: String?



57
58
59
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 57

def precedence_closer
  @precedence_closer
end

#stateObject (readonly)

: Symbol



58
59
60
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 58

def state
  @state
end

Instance Method Details

#classify(token, remaining) ⇒ Object



73
74
75
76
77
78
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 73

def classify(token, remaining)
  return classify_identifier(token, remaining) if token.type == :identifier
  return classify_scalar(token, remaining) if SCALAR_TYPES.key?(token.type)

  classify_punctuation(token)
end

#cst_pragma?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 91

def cst_pragma?
  @pragmas["cst"] == true
end

#expectation(token) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 96

def expectation(token)
  return "identifier" if @state == :pragma_value && token&.type != :identifier

  expected = EXPECTATIONS[@state]
  return expected if expected

  if @declaration == :precedence
    precedence_expectation(token)
  elsif @declaration == :convert
    "end"
  elsif @state == :declaration
    token&.type == :eof ? "rule" : "a declaration or rule"
  end
end

#extended_pragma?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 86

def extended_pragma?
  @pragmas["extended"] == true
end

#rules?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 81

def rules?
  @state == :rules
end