Class: Ibex::Frontend::GeneratedParserBase

Inherits:
Runtime::Parser show all
Includes:
GeneratedParserIncludes, GeneratedParserMetadata, GeneratedParserParameters
Defined in:
lib/ibex/frontend/generated_parser_base.rb

Overview

Semantic support and token delivery for the generated grammar parser. rubocop:disable Metrics/ClassLength -- generated grammar semantics share one parser support surface.

Constant Summary

Constants inherited from Runtime::Parser

Runtime::Parser::EMPTY_GREEN_TRIVIA, Runtime::Parser::EMPTY_LOCATIONS, Runtime::Parser::EMPTY_LOCATION_NAMES, Runtime::Parser::EMPTY_ROW, Runtime::Parser::EOF_TOKEN, Runtime::Parser::ERROR_TOKEN, Runtime::Parser::GENERATED_ACTION_NAME, Runtime::Parser::NO_LOOKAHEAD, Runtime::Parser::ParseError, Runtime::Parser::RECOVERY_SHIFTS

Instance Attribute Summary collapse

Attributes inherited from Runtime::Parser

#incremental_reused_descendants, #syntax_parse_memo

Instance Method Summary collapse

Methods inherited from Runtime::Parser

#do_parse, #expected_tokens, #expected_tokens_exact, #finish, incremental_session, #loc, #on_discard, #on_error_recover, #on_error_recover_location, #on_reduce, #on_reduce_location, #on_repair, #on_shift, #on_shift_location, #parse_with_syntax, #push, #repair_policy, #repair_policy=, #reset_push, #resource_limits, #resource_limits=, #result_loc, #syntax_root, #token_to_str, #trace_value_printer=, #yyaccept, #yydebug, #yydebug=, #yydebug_output=, #yyerrok, #yyerror, #yyparse

Methods included from Runtime::Observation

#observe, #unobserve

Constructor Details

#initialize(tokens, mode: :default) ⇒ GeneratedParserBase

Returns a new instance of GeneratedParserBase.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
# File 'lib/ibex/frontend/generated_parser_base.rb', line 22

def initialize(tokens, mode: :default)
  raise ArgumentError, "mode must be :default or :extended" unless %i[default extended].include?(mode)

  super()
  @adapter = TokenAdapter.new(tokens, extended: mode == :extended)
  @mode = mode
end

Instance Attribute Details

#diagnostic_tokenObject (readonly)

: Token?



16
17
18
# File 'lib/ibex/frontend/generated_parser_base.rb', line 16

def diagnostic_token
  @diagnostic_token
end

Instance Method Details

#next_tokenObject



36
37
38
39
40
41
42
43
# File 'lib/ibex/frontend/generated_parser_base.rb', line 36

def next_token
  delivered = @adapter.next_token
  @diagnostic_token = delivered ? delivered.fetch(1) : @adapter.eof_token
  delivered
rescue Ibex::Error
  @diagnostic_token = @adapter.last_token || @adapter.eof_token
  raise
end

#on_error(_token_id, value, _value_stack) ⇒ Object

Raises:



46
47
48
49
50
51
52
53
54
55
# File 'lib/ibex/frontend/generated_parser_base.rb', line 46

def on_error(_token_id, value, _value_stack)
  token = value || @adapter.eof_token
  @diagnostic_token = token
  raise_contextual_error(token)

  received = token&.value || token&.type || :eof
  location = token&.location || Location.new(file: "(grammar)", line: 1, column: 1)
  expected = expected_description(token)
  raise Ibex::Error, "#{location}: expected #{expected}, got #{received}"
end

#parseObject



31
32
33
# File 'lib/ibex/frontend/generated_parser_base.rb', line 31

def parse
  do_parse
end