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],
  "parser" => %i[PARSER parser_key],
  "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",
  parser_key: "a parser setting or end", parser_value: "a parser setting value"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extended: false) ⇒ DeclarationState

Returns a new instance of DeclarationState.



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

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

Instance Attribute Details

#conversion_nameObject (readonly)

: Hash[Symbol, String]



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

def conversion_name
  @conversion_name
end

#declarationObject (readonly)

: Symbol?



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

def declaration
  @declaration
end

#precedence_closerObject (readonly)

: String?



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

def precedence_closer
  @precedence_closer
end

#stateObject (readonly)

: Symbol



60
61
62
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 60

def state
  @state
end

Instance Method Details

#classify(token, remaining) ⇒ Object



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

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)


93
94
95
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 93

def cst_pragma?
  @pragmas.key?("cst")
end

#cst_pragma_locationObject



103
104
105
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 103

def cst_pragma_location
  @pragmas["cst"]
end

#expectation(token) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 108

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)


88
89
90
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 88

def extended_pragma?
  @pragmas.key?("extended")
end

#extended_pragma_locationObject



98
99
100
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 98

def extended_pragma_location
  @pragmas["extended"]
end

#rules?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/ibex/frontend/token_adapter/declaration_state.rb', line 83

def rules?
  @state == :rules
end