Class: Ibex::Runtime::Parser
- Inherits:
-
Object
- Object
- Ibex::Runtime::Parser
- Includes:
- Observation, ParserSyncRecovery
- Defined in:
- lib/ibex/runtime/parser.rb,
sig/ibex/runtime/parser.rbs
Overview
Drives a table-defined LR parser without native extensions.
Subclasses provide .parser_tables, returning :tokens, :token_names,
:actions, :gotos, and :productions, with optional
:default_actions, :error_messages, and :recovery_sync_tokens.
Actions are represented by
[:shift, state], [:reduce, production], [:accept], or [:error].
Format-v2 and newer generated production entries mark their five-argument
semantic methods with location_action: true. Format-v3 and newer
composed actions additionally use composition_action: true for the
six-argument contract carrying the lookahead location. Format-v4
location-free generated methods use values_action: true for a
one-argument values contract. Format-v5 additionally marks proven-safe
zero-to-four-value methods with positional_action: true. V1 and
unmarked application actions retain the historical two-argument
contract. Markers are honored only for the generated _ibex_action_N
Symbol shape, never for callables.
Constant Summary collapse
- ParseError =
Ibex::Runtime::ParseError
- EOF_TOKEN =
0- ERROR_TOKEN =
1- GENERATED_ACTION_NAME =
/\A_ibex_action_\d+\z/- NO_LOOKAHEAD =
Object.new.freeze
- RECOVERY_SHIFTS =
3- FAST_PATH_HOOK_NAMES =
%i[ on_shift on_shift_location on_reduce on_reduce_location token_to_str ].freeze
- FAST_PATH_HOOK_REFERENCES =
{ on_shift: :__ibex_fast_path_on_shift, on_shift_location: :__ibex_fast_path_on_shift_location, on_reduce: :__ibex_fast_path_on_reduce, on_reduce_location: :__ibex_fast_path_on_reduce_location, token_to_str: :__ibex_fast_path_token_to_str }.freeze
- ERROR_ACTION =
[:error].freeze
- SYNC_RECOVER_ACTION =
[:sync_recover].freeze
- CONTINUE_OUTCOME =
[:continue].freeze
- REPAIR_PENDING_OUTCOME =
[:repair_pending].freeze
- TERMINAL_OUTCOMES =
%i[accepted done].freeze
- COMPACT_ACCEPTED =
Object.new.freeze
- EMPTY_ROW =
empty_row.freeze
- EMPTY_LOCATION_NAMES =
empty_location_names.freeze
- EMPTY_LOCATIONS =
empty_locations.freeze
- EMPTY_GREEN_TRIVIA =
empty_green_trivia.freeze
Instance Attribute Summary collapse
Class Method Summary collapse
-
.incremental_session(source_text, resource_limits: nil, blender: true) ⇒ CST::IncrementalParseSession
Start a syntax-only incremental session backed by a generated lexer.
Instance Method Summary collapse
- #accept_reduction(result) ⇒ [ :accepted, untyped ]
- #action_for_current_state ⇒ Object
- #actionless_reduction_value(_production_id, _production, values, _locations, _location) ⇒ Object
- #append_repair_edits(output, input, edits) ⇒ Boolean
- #apply_repair(plan) ⇒ void
- #assign_repair_input(input) ⇒ void
- #begin_recovery(report) ⇒ Object
- #build_reduce_event_data(observers, production_id, production, length, pre_state, post_state, next_state, result, location) ⇒ Hash[String, untyped]?
- #cache_generated_action_contracts!(tables) ⇒ void
- #cached_runtime_fast_path_hooks_eligibility ⇒ Boolean?
- #capture_cst_error(token_id, value, location, reason) ⇒ void
- #clear_repair_lookahead ⇒ void
- #commit_green_token_shift(builder, kinds, token_id, from_state, trailing, location_leading, repair, token_text, lexer_state) ⇒ void
- #commit_repair(repair) ⇒ [ :continue ]
- #compact_fast_driver_eligible?(tables) ⇒ Boolean
- #configured_error_message(configured) ⇒ [ String?, String? ]
- #consume_recovery_attempt! ⇒ void
- #continue_recovery ⇒ Object
- #cst_enabled? ⇒ Boolean
- #cst_lexical_failure(error) ⇒ nil
- #cst_location_value(location, key) ⇒ Object
- #cst_repair_location(location, kind) ⇒ Hash[Symbol, untyped]
- #current_repair_input ⇒ RepairInput
- #default_action(state) ⇒ Object
- #discard_green_lookahead ⇒ void
-
#do_parse ⇒ Object
Pull tokens from
next_tokenand parse them. -
#drive_compact_fast_parser(tables) ⇒ Object, ...
Generated compact tables can keep the unobserved, location-free pull loop on their frozen displacement arrays.
- #drive_parser(source, initial_state: nil) ⇒ Object
- #edit_distance(left, right) ⇒ Integer
- #emit_accept_event(result_summary, reason) ⇒ void
- #emit_incremental_event(type, data) ⇒ void
- #emit_reject_event(reason, token_data, observers: runtime_observer_snapshot) ⇒ void
- #enqueue_or_assign_repair_input(input) ⇒ void
- #ensure_driver_available_without_lock! ⇒ void
-
#ensure_runtime_initialized! ⇒ void
Racc-generated parsers commonly define an application initializer without calling super.
- #ensure_stack_capacity! ⇒ void
- #error_action?(action) ⇒ Boolean
- #exact_lookahead_accepted?(token_id) ⇒ Boolean
- #exact_lookahead_step_budget(stack_depth) ⇒ Integer
- #exact_reduction_applied?(stack, production_id) ⇒ Boolean
-
#expected_tokens ⇒ Array[String]
Return token names accepted in the current parser state.
-
#expected_tokens_exact ⇒ Array[String]
Return token names that survive all required default reductions.
- #failed_cst ⇒ nil
-
#fallback_recovery(context, token_data, recovery_observers) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity Green restoration mirrors the existing state/value/location transactional fallback.
- #fast_reduce(production_id, production, length) ⇒ [ :continue ]
- #fast_reduction_eligible?(production, length) ⇒ Boolean
- #fast_shift(next_state) ⇒ [ :continue ]
- #fast_values_reduce(production_id, production, length, action) ⇒ Object
- #finalize_cst(value) ⇒ Object
- #finalize_failed_green_cst ⇒ void
- #finalize_lexical_green_cst(error) ⇒ void
- #finalize_red_green_cst(value) ⇒ Object
-
#finish(location: nil) ⇒ Object
Supply EOF to a caller-driven parser session and return its result.
- #finish_push_session ⇒ void
- #finish_recovery(token_id, token_display, value, location, original_state, value_stack, token_data, reason, observers) ⇒ [ :continue ]
-
#generated_action_contract_cache_accessible? ⇒ Boolean
Ruby 3.0-3.3 expose the main Ractor as an object instead of exposing Ractor.main?.
-
#generated_action_contracts_validated?(tables) ⇒ Boolean
Generated parser tables are deeply frozen before they can cross a Ractor boundary.
- #generated_action_symbol?(action) ⇒ Boolean
- #generated_composition_action?(production, action) ⇒ Boolean
- #generated_location_action?(production, action) ⇒ Boolean
- #generated_positional_action?(production, action) ⇒ Boolean
- #generated_values_action?(production, action) ⇒ Boolean
- #green_lexer_state(location) ⇒ Symbol
- #green_location_trivia(location, key) ⇒ Array[CST::GreenTrivia]
- #green_token_text(value, location) ⇒ String
-
#initialize(resource_limits: ResourceLimits.new) ⇒ Parser
constructor
A new instance of Parser.
- #initialize_runtime_fast_path(tables) ⇒ void
-
#initialize_runtime_state(resource_limits, preserve_existing:) ⇒ void
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity The explicit branches keep every runtime ivar typed while preserving application-owned values.
- #install_parse_memo(green) ⇒ void
- #install_runtime_fast_path_class_tracker! ⇒ void
- #install_runtime_fast_path_tracker!(singleton = nil) ⇒ void
- #install_syntax_root(green, kinds, file: nil) ⇒ void
-
#install_value_stack(values) ⇒ void
Keep the two historical value-stack names as read-compatible aliases.
- #internal_token_id(external_token) ⇒ Integer
- #load_parser_tables ⇒ Hash[Symbol, untyped]
-
#loc(reference) ⇒ Object
Return the location of a one-based RHS position or named reference while a semantic action is running.
- #materialize_compatible_lookahead ⇒ void
- #materialize_lookahead_token_display! ⇒ String
-
#next_token ⇒ [ untyped, untyped ], ...
Override in pull parsers.
- #normalized_token_word(token_name) ⇒ String?
- #notify_error_handler(context, repair) ⇒ void
-
#on_discard(_token_id, _value, _location, _reason) ⇒ void
Called when yacc recovery discards an application token.
-
#on_error(token_id, value, _value_stack) ⇒ Object
Override to recover from syntax errors.
-
#on_error_recover(_token_id, _value, _value_stack) ⇒ void
Called after the synthetic error token enters a recovery state.
-
#on_error_recover_location(_token_id, _value, _value_stack, _location, _state) ⇒ void
Location-aware recovery observer.
-
#on_reduce(_production_id, _values, _result) ⇒ void
(also: #__ibex_fast_path_on_reduce)
Called after a production's semantic action and goto are committed.
-
#on_reduce_location(_production_id, _values, _result, _locations, _result_location) ⇒ void
(also: #__ibex_fast_path_on_reduce_location)
Location-aware reduction observer.
-
#on_repair(_plan) ⇒ void
Called once after a repair is selected and before its edited token prefix is replayed through normal parser actions.
-
#on_shift(_token_id, _value, _state) ⇒ void
(also: #__ibex_fast_path_on_shift)
Called after an ordinary input token is shifted.
-
#on_shift_location(_token_id, _value, _state, _location) ⇒ void
(also: #__ibex_fast_path_on_shift_location)
Location-aware shift observer.
- #parse_syntax_token_source(source, cache) ⇒ CST::SyntaxResult
-
#parse_with_syntax ⇒ CST::ParseResult
Parse through
next_tokenand return both the semantic value and Red root. - #parser_state_count(tables) ⇒ Integer
- #parser_tables ⇒ Hash[Symbol, untyped]
- #pending_green_trivia ⇒ Array[CST::GreenTrivia]
- #perform(action) ⇒ Object
- #pop_reduction_locations(length) ⇒ Array[untyped]
- #positional_action_contract?(production) ⇒ Boolean
- #prepare_green_cst(tables) ⇒ void
- #prepare_parse(source, initial_state: nil) ⇒ void
-
#preserve_deleted_repair_input(input) ⇒ void
Retain a repair deletion in the concrete tree even though it is absent from the repaired token stream.
- #publish_error_context(context) ⇒ [ Hash[String, untyped]?, Array[Proc]? ]
-
#push(token, value = nil, location = nil) ⇒ :need_more, ...
Supply one token to a caller-driven parser session.
-
#push_location(location) ⇒ void
Keep allocation out of the ordinary two-element lexer path.
- #push_reduction_result(next_state, result, location) ⇒ void
- #push_reusable_green_subtree(subtree) ⇒ void
- #read_compatible_lookahead ⇒ void
- #read_external_token ⇒ Object
- #read_lookahead ⇒ void
- #read_repair_lookahead ⇒ void
- #recover(report: true) ⇒ Object
- #recovery_context(report) ⇒ Hash[Symbol, untyped]
- #reduce(production_id, prefetched_production = nil) ⇒ Object
- #reduce_green(production_id, production, length, left_state) ⇒ void
- #reduction_value(production_id, production, values, locations, location) ⇒ Object
-
#refresh_runtime_fast_path_after_user_code! ⇒ void
Re-check after lexer and semantic-action callbacks because those are supported points at which an application can install instrumentation.
- #reject_recovery_eof ⇒ [ :done, nil ]
- #reject_without_recovery(token_id, token_display, value, location, original_state, token_data, observers) ⇒ [ :done, nil ]
- #release_driver ⇒ void
- #repair_input(external_token, value, location) ⇒ RepairInput
- #repair_input_from_external(token) ⇒ RepairInput
- #repair_policy ⇒ RepairPolicy?
-
#repair_policy=(policy) ⇒ RepairPolicy?
Enable bounded automatic repair for the next parser session.
- #repair_search_tokens(policy) ⇒ [ Array[RepairInput], bool ]
- #repair_trace(plan) ⇒ String
- #replay_repair_edits(source, edits) ⇒ Array[RepairInput]
- #reset_cst_results(tables) ⇒ void
- #reset_parse_recovery_state ⇒ void
-
#reset_push ⇒ nil
Discard a caller-driven session so this parser can accept a new one.
- #resolve_initial_state(tables, requested) ⇒ Integer
- #resource_limits ⇒ ResourceLimits
-
#resource_limits=(limits) ⇒ ResourceLimits
Replace the limits used by future sessions.
-
#result_loc ⇒ Object
Return the synthesized span of the reduction being evaluated.
- #run_push_driver { ... } ⇒ Object
- #run_push_lookahead ⇒ :need_more, ...
-
#runtime_core_method(lookup, name) ⇒ Method?
Bypass an application-defined
methodhelper while retaining Ruby's complete singleton/prepend/subclass lookup semantics. - #runtime_current_token_data ⇒ Hash[String, untyped]
- #runtime_discard_data(token_display) ⇒ Hash[String, untyped]
- #runtime_fast_path_class_hooks_eligible? ⇒ Boolean
-
#runtime_fast_path_eligible?(tables) ⇒ Boolean
The generic driver remains authoritative whenever a public runtime extension can observe a committed shift or reduction.
- #runtime_fast_path_hooks_eligible? ⇒ Boolean
- #runtime_method_unchanged?(lookup, name, reference) ⇒ Boolean
-
#runtime_methods_equivalent?(implementation, expected) ⇒ Boolean
Ruby 3.0 and 3.1 do not preserve UnboundMethod#== for an inherited method and its alias even though both still identify the same body.
- #runtime_original_token_data(token_id, token_display, value, location, state) ⇒ Hash[String, untyped]
- #runtime_reduce_data(production_id, production, length, pre_state, post_state, next_state, result, location) ⇒ Hash[String, untyped]
- #runtime_token_data(token_id:, token_display:, value:, location:, state:) ⇒ Hash[String, untyped]
- #selected_repair ⇒ RepairPlan, ...
- #shift(next_state) ⇒ Object
- #shift_error_token ⇒ Boolean
-
#shift_green_token(token_id, value, location, from_state) ⇒ void
rubocop:disable Metrics/PerceivedComplexity -- generated locations have a deliberate allocation-free path.
- #start_push_session ⇒ void
- #syntax_parse_result(value) ⇒ CST::ParseResult
-
#syntax_root ⇒ CST::SyntaxNode?
Return the Red source-file root built by the most recent CST parse.
- #syntax_token_states ⇒ Array[Symbol]
- #synthetic_repair_input(edit, value:, location:) ⇒ RepairInput
- #table_lookup(table, row, column) ⇒ Object
- #token_suggestions(actual, expected) ⇒ Array[String]
-
#token_to_str(token_id) ⇒ String
(also: #__ibex_fast_path_token_to_str)
Return a human-readable name for an internal token id.
- #trace(message) ⇒ void
- #trace_reduction(production_id, length, lhs, result, next_state) ⇒ void
-
#trace_value_printer=(printer) ⇒ ^(untyped) -> untyped?
Install an opt-in value formatter for human-readable yydebug traces.
- #trace_value_suffix(value, symbol_id) ⇒ String
- #track_locations?(tables) ⇒ Boolean
- #validate_composition_action_contract!(production, index, version) ⇒ void
- #validate_current_cst_tables!(tables, actual) ⇒ void
- #validate_generated_action_contracts!(tables, version) ⇒ void
- #validate_parser_table_format! ⇒ Hash[Symbol, untyped]
- #validate_positional_action_contract!(production, index, version) ⇒ void
- #validate_resource_limits!(resource_limits) ⇒ void
- #validate_values_action_contract!(production, index, version) ⇒ void
- #values_reduction_value(production, action, values, locations, location) ⇒ Object
- #with_syntax_only(cache) { ... } ⇒ Object
-
#yyaccept ⇒ nil
Accept immediately after the current semantic action completes.
- #yydebug ⇒ Boolean
- #yydebug=(enabled) ⇒ Boolean
- #yydebug_output=(output) ⇒ IO
-
#yyerrok ⇒ nil
Leave error recovery immediately.
-
#yyerror ⇒ nil
Enter error recovery from a semantic action without calling
on_error. -
#yyparse(receiver, method_id) ⇒ Object
Parse tokens yielded by
receiver.method_id.
Methods included from ParserSyncRecovery
#begin_sync_recovery, #clear_sync_recovery, #continue_sync_recovery, #finish_sync_recovery, #reject_sync_recovery_eof, #sync_action, #sync_recovery_active?, #sync_recovery_configured?, #sync_token?, #synchronize_for_current_token
Methods included from Observation
#emit_runtime_event, #ensure_observation_thread!, #observe, #runtime_observer_snapshot, #unobserve
Constructor Details
#initialize(resource_limits: ResourceLimits.new) ⇒ Parser
Returns a new instance of Parser.
335 336 337 338 |
# File 'lib/ibex/runtime/parser.rb', line 335 def initialize(resource_limits: ResourceLimits.new) validate_resource_limits!(resource_limits) initialize_runtime_state(resource_limits, preserve_existing: false) end |
Instance Attribute Details
#incremental_reused_descendants ⇒ Integer (readonly)
323 324 325 |
# File 'lib/ibex/runtime/parser.rb', line 323 def incremental_reused_descendants @incremental_reused_descendants end |
#syntax_parse_memo ⇒ CST::ParseMemo? (readonly)
322 323 324 |
# File 'lib/ibex/runtime/parser.rb', line 322 def syntax_parse_memo @syntax_parse_memo end |
Class Method Details
.incremental_session(source_text, resource_limits: nil, blender: true) ⇒ CST::IncrementalParseSession
Start a syntax-only incremental session backed by a generated lexer.
328 329 330 331 332 |
# File 'lib/ibex/runtime/parser.rb', line 328 def self.incremental_session(source_text, resource_limits: nil, blender: true) CST::IncrementalParseSession.new( self, source_text, resource_limits: resource_limits, blender: blender ) end |
Instance Method Details
#accept_reduction(result) ⇒ [ :accepted, untyped ]
1669 1670 1671 1672 1673 |
# File 'lib/ibex/runtime/parser.rb', line 1669 def accept_reduction(result) result = finalize_cst(result) emit_accept_event(EventSanitizer.value(result), "semantic") if @runtime_observers [:accepted, result] end |
#action_for_current_state ⇒ Object
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 |
# File 'lib/ibex/runtime/parser.rb', line 1420 def action_for_current_state loop do if @lookahead.equal?(NO_LOOKAHEAD) state = @state_stack.last eager = parser_tables.fetch(:eager_reductions, EMPTY_ROW)[state] return eager if eager read_lookahead next if @lookahead.equal?(NO_LOOKAHEAD) end return SYNC_RECOVER_ACTION if sync_recovery_active? state = @state_stack.last return ERROR_ACTION unless parser_tables.fetch(:token_names).key?(@lookahead) explicit = table_lookup(parser_tables.fetch(:actions), state, @lookahead) return explicit if explicit return default_action(state) || ERROR_ACTION end end |
#actionless_reduction_value(_production_id, _production, values, _locations, _location) ⇒ Object
1742 1743 1744 |
# File 'lib/ibex/runtime/parser.rb', line 1742 def actionless_reduction_value(_production_id, _production, values, _locations, _location) values.first end |
#append_repair_edits(output, input, edits) ⇒ Boolean
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 |
# File 'lib/ibex/runtime/parser.rb', line 2538 def append_repair_edits(output, input, edits) preserve_deleted_repair_input(input) if edits.any? { |edit| edit.kind == :delete } edits.each do |edit| if edit.kind == :insert output << synthetic_repair_input(edit, value: nil, location: input.location) elsif edit.kind == :replace output << synthetic_repair_input(edit, value: input.value, location: input.location) end end edits.any? { |edit| %i[delete replace].include?(edit.kind) } end |
#apply_repair(plan) ⇒ void
This method returns an undefined value.
2505 2506 2507 2508 2509 2510 2511 |
# File 'lib/ibex/runtime/parser.rb', line 2505 def apply_repair(plan) source = [current_repair_input] + (@repair_input_buffer || []) @repair_input_buffer = replay_repair_edits(source, plan.edits) clear_repair_lookahead @recovery_shifts = 0 trace("repair cost #{plan.cost}: #{repair_trace(plan)}") if @yydebug end |
#assign_repair_input(input) ⇒ void
This method returns an undefined value.
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 |
# File 'lib/ibex/runtime/parser.rb', line 2714 def assign_repair_input(input) @lookahead = input.token_id @lookahead_value = input.value @lookahead_location = input.location @runtime_lookahead_token_display = input.token_name if input.token_id.negative? @unknown_token_id = input.token_id @unknown_token_name = input.token_name end trace("read #{input.token_name}") if @yydebug end |
#begin_recovery(report) ⇒ Object
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 |
# File 'lib/ibex/runtime/parser.rb', line 2229 def begin_recovery(report) consume_recovery_attempt! repair = report ? selected_repair : nil return REPAIR_PENDING_OUTCOME if repair.equal?(RepairSearch::NEED_INPUT) context = recovery_context(report) capture_cst_error(context[:token_id], context[:value], context[:location], context[:reason].to_sym) token_data, recovery_observers = publish_error_context(context) notify_error_handler(context, repair) if report return commit_repair(repair) if repair.is_a?(RepairPlan) fallback_recovery(context, token_data, recovery_observers) end |
#build_reduce_event_data(observers, production_id, production, length, pre_state, post_state, next_state, result, location) ⇒ Hash[String, untyped]?
1642 1643 1644 1645 1646 1647 1648 1649 1650 |
# File 'lib/ibex/runtime/parser.rb', line 1642 def build_reduce_event_data( observers, production_id, production, length, pre_state, post_state, next_state, result, location ) return unless observers runtime_reduce_data( production_id, production, length, pre_state, post_state, next_state, result, location ) end |
#cache_generated_action_contracts!(tables) ⇒ void
This method returns an undefined value.
1331 1332 1333 1334 1335 1336 1337 1338 1339 |
# File 'lib/ibex/runtime/parser.rb', line 1331 def cache_generated_action_contracts!(tables) shareable = defined?(Ractor) && Ractor.respond_to?(:shareable?) && Ractor.shareable?(tables) return unless shareable return unless generated_action_contract_cache_accessible? self.class.instance_variable_set(:@__ibex_validated_parser_tables, tables) rescue FrozenError nil end |
#cached_runtime_fast_path_hooks_eligibility ⇒ Boolean?
2893 2894 2895 2896 2897 2898 2899 2900 2901 |
# File 'lib/ibex/runtime/parser.rb', line 2893 def cached_runtime_fast_path_hooks_eligibility return nil unless @runtime_fast_path_tracker_installed && !@runtime_fast_path_hooks_mutated singleton = Object.instance_method(:singleton_class).bind_call(self) return false if singleton.frozen? return nil unless @runtime_fast_path_singleton_ancestors == singleton.ancestors runtime_fast_path_class_hooks_eligible? end |
#capture_cst_error(token_id, value, location, reason) ⇒ void
This method returns an undefined value.
1785 1786 1787 1788 1789 1790 1791 1792 |
# File 'lib/ibex/runtime/parser.rb', line 1785 def capture_cst_error(token_id, value, location, reason) return unless cst_enabled? return if token_id == EOF_TOKEN @syntax_diagnostics << { token_id: token_id, value: value, location: location, reason: reason }.freeze end |
#clear_repair_lookahead ⇒ void
This method returns an undefined value.
2574 2575 2576 2577 2578 2579 |
# File 'lib/ibex/runtime/parser.rb', line 2574 def clear_repair_lookahead @lookahead = NO_LOOKAHEAD @lookahead_value = nil @lookahead_location = nil @runtime_lookahead_token_display = nil end |
#commit_green_token_shift(builder, kinds, token_id, from_state, trailing, location_leading, repair, token_text, lexer_state) ⇒ void
This method returns an undefined value.
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 |
# File 'lib/ibex/runtime/parser.rb', line 1894 def commit_green_token_shift( builder, kinds, token_id, from_state, trailing, location_leading, repair, token_text, lexer_state ) builder.append_trailing_to_last_token(trailing) if !@green_reused_right_edge && !trailing.empty? if repair == :insert builder.missing(token_id) @green_token_states << lexer_state @green_memo_stack << CST::ParseMemo::Entry.new(from_state) return end if @green_pending_skipped.empty? leading = location_leading flags = 0 else leading = @green_pending_skipped + location_leading @green_pending_skipped.clear flags = CST::Flags::CONTAINS_SKIPPED end flags |= CST::Flags::CONTAINS_ERROR if repair == :replace || token_id == ERROR_TOKEN kind = token_id.negative? ? kinds.fetch(:lexical_error_token) : token_id builder.token(kind, token_text, leading: leading, flags: flags) @green_token_states << lexer_state @green_memo_stack << CST::ParseMemo::Entry.new(from_state) @green_reused_right_edge = false end |
#commit_repair(repair) ⇒ [ :continue ]
2281 2282 2283 2284 2285 |
# File 'lib/ibex/runtime/parser.rb', line 2281 def commit_repair(repair) on_repair(repair) apply_repair(repair) CONTINUE_OUTCOME end |
#compact_fast_driver_eligible?(tables) ⇒ Boolean
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 |
# File 'lib/ibex/runtime/parser.rb', line 1136 def compact_fast_driver_eligible?(tables) return false unless @runtime_fast_path && tables return false unless tables[:compact_fast_driver] == true return false unless tables.fetch(:eager_reductions, EMPTY_ROW).empty? return false unless defined?(Ibex::Tables::Compact) tables.fetch(:actions).instance_of?(Ibex::Tables::CompactActions) && tables.fetch(:gotos).instance_of?(Ibex::Tables::Compact) && tables.fetch(:productions).instance_of?(Ibex::Tables::CompactProductions) && tables.fetch(:productions).direct_values? end |
#configured_error_message(configured) ⇒ [ String?, String? ]
2767 2768 2769 2770 2771 2772 2773 |
# File 'lib/ibex/runtime/parser.rb', line 2767 def (configured) return [nil, configured] unless configured.is_a?(Hash) error_id = configured[:id] || configured["id"] = configured[:message] || configured["message"] [error_id, ] end |
#consume_recovery_attempt! ⇒ void
This method returns an undefined value.
2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 |
# File 'lib/ibex/runtime/parser.rb', line 2466 def consume_recovery_attempt! @recovery_attempts += 1 limit = @resource_limits.max_recovery_attempts return if @recovery_attempts <= limit raise ResourceLimitError.new( resource: :recovery_attempts, limit: limit, observed: @recovery_attempts, state: @state_stack.last, location: @lookahead_location ) end |
#continue_recovery ⇒ Object
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 |
# File 'lib/ibex/runtime/parser.rb', line 2191 def continue_recovery return reject_recovery_eof if @lookahead == EOF_TOKEN token_display = token_to_str(@lookahead) event_observers = runtime_observer_snapshot if @runtime_observers event_data = runtime_discard_data(token_display) if event_observers trace("discard #{token_display} during recovery") if @yydebug on_discard(@lookahead, @lookahead_value, @lookahead_location, :recovery) discard_green_lookahead @lookahead = NO_LOOKAHEAD @lookahead_location = nil @runtime_lookahead_token_display = nil emit_runtime_event(:discard, event_data, observers: event_observers) if event_data CONTINUE_OUTCOME end |
#cst_enabled? ⇒ Boolean
1780 1781 1782 |
# File 'lib/ibex/runtime/parser.rb', line 1780 def cst_enabled? parser_tables[:cst].is_a?(Hash) end |
#cst_lexical_failure(error) ⇒ nil
1808 1809 1810 1811 1812 |
# File 'lib/ibex/runtime/parser.rb', line 1808 def cst_lexical_failure(error) @syntax_diagnostics << error finalize_lexical_green_cst(error) nil end |
#cst_location_value(location, key) ⇒ Object
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 |
# File 'lib/ibex/runtime/parser.rb', line 1815 def cst_location_value(location, key) if location.is_a?(Hash) value = location[key] return value if value || location.key?(key) return if location.key?(:ibex_lexer_start_state) return location[key.name] end return location.public_send(key) if location.respond_to?(key) nil end |
#cst_repair_location(location, kind) ⇒ Hash[Symbol, untyped]
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 |
# File 'lib/ibex/runtime/parser.rb', line 2588 def cst_repair_location(location, kind) return location.merge(ibex_repair: kind).freeze if location.is_a?(Hash) { file: cst_location_value(location, :file), line: cst_location_value(location, :line), column: cst_location_value(location, :column), end_line: cst_location_value(location, :end_line), end_column: cst_location_value(location, :end_column), origin: location, ibex_repair: kind }.freeze end |
#current_repair_input ⇒ RepairInput
2514 2515 2516 2517 2518 2519 2520 2521 |
# File 'lib/ibex/runtime/parser.rb', line 2514 def current_repair_input RepairInput.new( token_id: @lookahead, token_name: @runtime_lookahead_token_display || token_to_str(@lookahead), value: @lookahead_value, location: @lookahead_location ) end |
#default_action(state) ⇒ Object
2811 2812 2813 |
# File 'lib/ibex/runtime/parser.rb', line 2811 def default_action(state) parser_tables.fetch(:default_actions, EMPTY_ROW)[state] end |
#discard_green_lookahead ⇒ void
This method returns an undefined value.
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 |
# File 'lib/ibex/runtime/parser.rb', line 2161 def discard_green_lookahead @green_parse_memo_valid = false builder = @green_builder kinds = @green_kinds return unless builder && kinds kind = @lookahead.is_a?(Integer) && @lookahead >= 0 ? @lookahead : kinds.fetch(:lexical_error_token) token = builder.make_token( kind, green_token_text(@lookahead_value, @lookahead_location), leading: green_location_trivia(@lookahead_location, :leading_trivia), flags: CST::Flags::CONTAINS_ERROR ) return if builder.append_to_last_error(token) @green_pending_skipped << CST::GreenTrivia.new( kind: kinds.fetch(:skipped_tokens), text: token.to_source ) end |
#do_parse ⇒ Object
Pull tokens from next_token and parse them.
408 409 410 |
# File 'lib/ibex/runtime/parser.rb', line 408 def do_parse drive_parser(nil) end |
#drive_compact_fast_parser(tables) ⇒ Object, ...
Generated compact tables can keep the unobserved, location-free pull loop on their frozen displacement arrays. Any extension boundary or exceptional table shape returns control to the generic driver without replaying a committed action. rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity Direct comparisons avoid predicate-method dispatch inside the parser loop. rubocop:disable Style/BitwisePredicate, Style/NumericPredicate
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 |
# File 'lib/ibex/runtime/parser.rb', line 896 def drive_compact_fast_parser(tables) actions = tables.fetch(:actions) gotos = tables.fetch(:gotos) productions = tables.fetch(:productions) default_codes = tables.fetch(:compact_default_actions) token_ids = tables.fetch(:tokens) dense_action_codes = actions.dense_codes action_column_count = actions.column_count dense_goto_values = gotos.dense_values goto_dense_width = gotos.dense_width return unless dense_action_codes && action_column_count && dense_goto_values && goto_dense_width && (tables[:compact_action_encoding] == :signed || default_codes.empty?) production_lhs_ids = productions.lhs_ids production_lengths = productions.lengths production_actions = productions.actions production_flags = productions.flags borrowed_values_flag = Ibex::Tables::CompactProductions::BORROWED_VALUES_ACTION positional_action_flag = Ibex::Tables::CompactProductions::POSITIONAL_ACTION states = @state_stack values = @value_stack stack_limit = @resource_limits.max_stack_depth accept_code = Ibex::Tables::CompactActions::ACCEPT_CODE error_code = Ibex::Tables::CompactActions::ERROR_CODE shift_base = Ibex::Tables::CompactActions::SHIFT_BASE reduce_base = Ibex::Tables::CompactActions::REDUCE_BASE while @runtime_fast_path if @lookahead.equal?(NO_LOOKAHEAD) source = @source token = begin source ? source.call : next_token rescue StopIteration false end @runtime_fast_path = false if @yydebug || @runtime_observers || @repair_policy || @location_stack || @semantic_error || @accept_requested if token.nil? || token == false @lookahead = EOF_TOKEN @lookahead_value = nil @lookahead_location = nil else external_token, @lookahead_value, @lookahead_location = token if external_token.nil? || external_token == false @lookahead = EOF_TOKEN else token_id = token_ids[external_token] if token_id @lookahead = token_id else @unknown_token_name = external_token.inspect @unknown_token_id = -external_token.object_id.abs @lookahead = @unknown_token_id end end end @runtime_fast_path = false unless nil.equal?(@lookahead_location) unless @runtime_fast_path materialize_compatible_lookahead return end end return if @unknown_token_id == @lookahead state = states[-1] code = dense_action_codes[(state * action_column_count) + @lookahead] || default_codes[state] return unless code if code == accept_code return COMPACT_ACCEPTED elsif code == error_code return elsif code > 0 ensure_stack_capacity! if states.length >= stack_limit states << (code - shift_base) values << @lookahead_value @lookahead = NO_LOOKAHEAD @lookahead_location = nil @runtime_lookahead_token_display = nil else production_id = reduce_base - code lhs = production_lhs_ids[production_id] length = production_lengths[production_id] return unless length <= values.length && length < states.length semantic_action = production_actions[production_id] if !semantic_action && length == 1 goto_row = states[-2] next_state = dense_goto_values[(goto_row * goto_dense_width) + lhs] raise ParseError, "(tables):1:1: missing goto for production #{production_id}" if next_state.nil? states[-1] = next_state next end hook_values = EMPTY_LOCATIONS if semantic_action flags = production_flags[production_id] positional_action = (flags & positional_action_flag) != 0 if positional_action case length when 1 value0 = values[-1] when 2 value0 = values[-2] value1 = values[-1] when 3 value0 = values[-3] value1 = values[-2] value2 = values[-1] when 4 value0 = values[-4] value1 = values[-3] value2 = values[-2] value3 = values[-1] end else reduction_values = values.last(length) hook_values = if (flags & borrowed_values_flag) == 0 reduction_values.dup else reduction_values end end else positional_action = false result = length.zero? ? nil : values[-length] end case length when 0 nil when 1 states.pop values.pop when 2 states.pop states.pop values.pop values.pop when 3 states.pop states.pop states.pop values.pop values.pop values.pop when 4 states.pop states.pop states.pop states.pop values.pop values.pop values.pop values.pop else remaining = length while remaining > 0 states.pop values.pop remaining -= 1 end end if semantic_action previous_locations = @semantic_locations previous_names = @semantic_location_names previous_result_location = @semantic_result_location @semantic_locations = EMPTY_LOCATIONS @semantic_location_names = EMPTY_LOCATION_NAMES @semantic_result_location = nil begin result = if positional_action case length when 0 then __send__(semantic_action) when 1 then __send__(semantic_action, value0) when 2 then __send__(semantic_action, value0, value1) when 3 then __send__(semantic_action, value0, value1, value2) when 4 then __send__(semantic_action, value0, value1, value2, value3) end else __send__(semantic_action, reduction_values) end ensure @semantic_locations = previous_locations @semantic_location_names = previous_names @semantic_result_location = previous_result_location end @runtime_fast_path = false if @yydebug || @runtime_observers || @repair_policy || @location_stack || @semantic_error || @accept_requested end goto_row = states[-1] next_state = dense_goto_values[(goto_row * goto_dense_width) + lhs] raise ParseError, "(tables):1:1: missing goto for production #{production_id}" if next_state.nil? ensure_stack_capacity! if states.length >= stack_limit states << next_state location_stack = @location_stack location_stack << nil if location_stack values << result if semantic_action trace_reduction(production_id, length, lhs, result, next_state) if @yydebug unless @runtime_fast_path if positional_action case length when 0 then hook_values = EMPTY_LOCATIONS when 1 then hook_values = [value0] when 2 then hook_values = [value0, value1] when 3 then hook_values = [value0, value1, value2] when 4 then hook_values = [value0, value1, value2, value3] end end lookup = Object.instance_method(:method) unless runtime_method_unchanged?(lookup, :on_reduce, :__ibex_fast_path_on_reduce) on_reduce(production_id, hook_values, result) end unless runtime_method_unchanged?(lookup, :on_reduce_location, :__ibex_fast_path_on_reduce_location) on_reduce_location(production_id, hook_values, result, Array.new(length), nil) end end return accept_reduction(result) if @accept_requested return recover(report: false) if @semantic_error next end end end nil end |
#drive_parser(source, initial_state: nil) ⇒ Object
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 |
# File 'lib/ibex/runtime/parser.rb', line 852 def drive_parser(source, initial_state: nil) ensure_runtime_initialized! @runtime_observation_mutex.synchronize do ensure_driver_available_without_lock! if @push_status == :active raise ParseError, "(input):1:1: cannot start another parser driver during an active push session" end @driver_status = :pull @runtime_driver_thread = Thread.current end begin prepare_parse(source, initial_state: initial_state) tables = @runtime_parser_tables if tables && compact_fast_driver_eligible?(tables) fast_outcome = drive_compact_fast_parser(tables) return @value_stack.last if fast_outcome.equal?(COMPACT_ACCEPTED) return fast_outcome.fetch(1) if fast_outcome.is_a?(Array) && TERMINAL_OUTCOMES.include?(fast_outcome[0]) end loop do outcome = perform(action_for_current_state) case outcome[0] when :accepted, :done then return outcome[1] end end ensure @source = nil @runtime_parser_tables = nil @runtime_fast_path = false release_driver end end |
#edit_distance(left, right) ⇒ Integer
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 |
# File 'lib/ibex/runtime/parser.rb', line 2794 def edit_distance(left, right) previous = (0..right.length).to_a left.each_char.with_index(1) do |left_character, row| current = [row] right.each_char.with_index(1) do |right_character, column| current[column] = [ current[column - 1] + 1, previous[column] + 1, previous[column - 1] + (left_character == right_character ? 0 : 1) ].min end previous = current end previous.last end |
#emit_accept_event(result_summary, reason) ⇒ void
This method returns an undefined value.
2409 2410 2411 2412 2413 2414 |
# File 'lib/ibex/runtime/parser.rb', line 2409 def emit_accept_event(result_summary, reason) emit_runtime_event( :accept, { "state" => @state_stack.last, "result" => result_summary, "reason" => reason } ) end |
#emit_incremental_event(type, data) ⇒ void
This method returns an undefined value.
2156 2157 2158 |
# File 'lib/ibex/runtime/parser.rb', line 2156 def emit_incremental_event(type, data) emit_runtime_event(type, data) if @runtime_observers end |
#emit_reject_event(reason, token_data, observers: runtime_observer_snapshot) ⇒ void
This method returns an undefined value.
2417 2418 2419 2420 2421 2422 2423 |
# File 'lib/ibex/runtime/parser.rb', line 2417 def emit_reject_event(reason, token_data, observers: runtime_observer_snapshot) emit_runtime_event( :reject, token_data.merge("reason" => reason), observers: observers ) end |
#enqueue_or_assign_repair_input(input) ⇒ void
This method returns an undefined value.
2704 2705 2706 2707 2708 2709 2710 2711 |
# File 'lib/ibex/runtime/parser.rb', line 2704 def enqueue_or_assign_repair_input(input) buffer = @repair_input_buffer || raise(ParseError, "(repair):1:1: repair input buffer is unavailable") if @lookahead.equal?(NO_LOOKAHEAD) && buffer.empty? assign_repair_input(input) else buffer << input end end |
#ensure_driver_available_without_lock! ⇒ void
This method returns an undefined value.
1201 1202 1203 1204 1205 |
# File 'lib/ibex/runtime/parser.rb', line 1201 def ensure_driver_available_without_lock! return if @driver_status == :idle raise ParseError, "(input):1:1: parser driver is already running" end |
#ensure_runtime_initialized! ⇒ void
This method returns an undefined value.
Racc-generated parsers commonly define an application initializer without calling super. Complete only missing runtime state so those initializers retain values they deliberately configured.
689 690 691 692 693 694 695 696 697 698 |
# File 'lib/ibex/runtime/parser.rb', line 689 def ensure_runtime_initialized! return if defined?(@runtime_observation_mutex) && @runtime_observation_mutex limits = if defined?(@resource_limits) && @resource_limits.is_a?(ResourceLimits) @resource_limits else ResourceLimits.new end initialize_runtime_state(limits, preserve_existing: true) end |
#ensure_stack_capacity! ⇒ void
This method returns an undefined value.
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 |
# File 'lib/ibex/runtime/parser.rb', line 2454 def ensure_stack_capacity! observed = @state_stack.length + 1 limit = @resource_limits.max_stack_depth return if observed <= limit raise ResourceLimitError.new( resource: :stack_depth, limit: limit, observed: observed, state: @state_stack.last, location: @lookahead_location ) end |
#error_action?(action) ⇒ Boolean
2762 2763 2764 |
# File 'lib/ibex/runtime/parser.rb', line 2762 def error_action?(action) action.nil? || action.first == :error end |
#exact_lookahead_accepted?(token_id) ⇒ Boolean
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
# File 'lib/ibex/runtime/parser.rb', line 780 def exact_lookahead_accepted?(token_id) stack = @state_stack.dup seen = {} #: Hash[Array[Integer], bool] remaining = exact_lookahead_step_budget(stack.length) loop do return false unless remaining.positive? return false if seen.key?(stack) remaining -= 1 seen[stack.dup.freeze] = true state = stack.last action = table_lookup(parser_tables.fetch(:actions), state, token_id) || default_action(state) || ERROR_ACTION case action.first when :shift, :accept then return true when :error then return false end return false unless action.first == :reduce return false unless exact_reduction_applied?(stack, action.fetch(1)) end end |
#exact_lookahead_step_budget(stack_depth) ⇒ Integer
802 803 804 805 806 807 808 809 810 |
# File 'lib/ibex/runtime/parser.rb', line 802 def exact_lookahead_step_budget(stack_depth) tables = parser_tables production_count = tables.fetch(:productions).length unless production_count.is_a?(Integer) raise ParseError, "(tables):1:1: parser table production count must be an Integer" end (stack_depth + parser_state_count(tables) + 1) * (production_count + 1) end |
#exact_reduction_applied?(stack, production_id) ⇒ Boolean
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
# File 'lib/ibex/runtime/parser.rb', line 825 def exact_reduction_applied?(stack, production_id) production = parser_tables.fetch(:productions).fetch(production_id) length = production.fetch(:length) return false if length >= stack.length if length.is_a?(Integer) if length.negative? stack.pop(length) return false end remaining = length while remaining.positive? stack.pop remaining -= 1 end else stack.pop(length) end target = table_lookup(parser_tables.fetch(:gotos), stack.last, production.fetch(:lhs)) return false unless target stack << target true end |
#expected_tokens ⇒ Array[String]
Return token names accepted in the current parser state.
621 622 623 624 625 626 627 628 629 630 631 |
# File 'lib/ibex/runtime/parser.rb', line 621 def expected_tokens ensure_runtime_initialized! return expected_tokens_exact if parser_tables[:exact_expected_tokens] return [] if @state_stack.empty? state = @state_stack.last parser_tables.fetch(:token_names).keys.filter_map do |token_id| action = table_lookup(parser_tables.fetch(:actions), state, token_id) || default_action(state) || ERROR_ACTION token_to_str(token_id) unless error_action?(action) || token_id == ERROR_TOKEN end end |
#expected_tokens_exact ⇒ Array[String]
Return token names that survive all required default reductions. Semantic actions are not evaluated during this lookahead correction.
636 637 638 639 640 641 642 643 644 645 |
# File 'lib/ibex/runtime/parser.rb', line 636 def expected_tokens_exact ensure_runtime_initialized! return [] if @state_stack.empty? parser_tables.fetch(:token_names).keys.filter_map do |token_id| next if token_id == ERROR_TOKEN token_to_str(token_id) if exact_lookahead_accepted?(token_id) end end |
#failed_cst ⇒ nil
1802 1803 1804 1805 |
# File 'lib/ibex/runtime/parser.rb', line 1802 def failed_cst finalize_failed_green_cst nil end |
#fallback_recovery(context, token_data, recovery_observers) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity Green restoration mirrors the existing state/value/location transactional fallback.
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 |
# File 'lib/ibex/runtime/parser.rb', line 2291 def fallback_recovery(context, token_data, recovery_observers) state_stack = @state_stack.dup if sync_recovery_configured? value_stack = @value_stack.dup if state_stack location_stack = @location_stack&.dup if state_stack green_stack = @green_builder&.snapshot if state_stack unless shift_error_token if state_stack @state_stack = state_stack install_value_stack(value_stack || []) @location_stack = location_stack @green_builder&.restore(green_stack || []) return begin_sync_recovery(context, token_data, recovery_observers) end return reject_without_recovery( context[:token_id], context[:token_display], context[:value], context[:location], context[:state], token_data, recovery_observers ) end @recovery_shifts = RECOVERY_SHIFTS finish_recovery( context[:token_id], context[:token_display], context[:value], context[:location], context[:state], context[:value_stack], token_data, context[:reason], recovery_observers ) end |
#fast_reduce(production_id, production, length) ⇒ [ :continue ]
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 |
# File 'lib/ibex/runtime/parser.rb', line 1494 def fast_reduce(production_id, production, length) action = production[:action] return fast_values_reduce(production_id, production, length, action) if action result = length.zero? ? nil : @value_stack.fetch(@value_stack.length - length) remaining = length while remaining.positive? @state_stack.pop @value_stack.pop remaining -= 1 end next_state = table_lookup(parser_tables.fetch(:gotos), @state_stack.last, production.fetch(:lhs)) raise ParseError, "(tables):1:1: missing goto for production #{production_id}" if next_state.nil? ensure_stack_capacity! @state_stack << next_state @value_stack << result CONTINUE_OUTCOME end |
#fast_reduction_eligible?(production, length) ⇒ Boolean
1485 1486 1487 1488 1489 1490 1491 |
# File 'lib/ibex/runtime/parser.rb', line 1485 def fast_reduction_eligible?(production, length) action = production[:action] return false if action && !generated_values_action?(production, action) length.is_a?(Integer) && !length.negative? && length <= @value_stack.length && length < @state_stack.length end |
#fast_shift(next_state) ⇒ [ :continue ]
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 |
# File 'lib/ibex/runtime/parser.rb', line 1472 def fast_shift(next_state) value = @lookahead_value ensure_stack_capacity! @state_stack << next_state @value_stack << value @lookahead = NO_LOOKAHEAD @lookahead_location = nil @runtime_lookahead_token_display = nil @recovery_shifts -= 1 if @recovery_shifts.positive? CONTINUE_OUTCOME end |
#fast_values_reduce(production_id, production, length, action) ⇒ Object
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 |
# File 'lib/ibex/runtime/parser.rb', line 1515 def fast_values_reduce(production_id, production, length, action) hook_values = @value_stack.last(length) values = hook_values.dup remaining = length while remaining.positive? @state_stack.pop @value_stack.pop remaining -= 1 end result = values_reduction_value(production, action, values, EMPTY_LOCATIONS, nil) refresh_runtime_fast_path_after_user_code! next_state = table_lookup(parser_tables.fetch(:gotos), @state_stack.last, production.fetch(:lhs)) raise ParseError, "(tables):1:1: missing goto for production #{production_id}" if next_state.nil? ensure_stack_capacity! push_reduction_result(next_state, result, nil) trace_reduction(production_id, length, production.fetch(:lhs), result, next_state) unless @runtime_fast_path lookup = Object.instance_method(:method) unless runtime_method_unchanged?(lookup, :on_reduce, :__ibex_fast_path_on_reduce) on_reduce(production_id, hook_values, result) end unless runtime_method_unchanged?(lookup, :on_reduce_location, :__ibex_fast_path_on_reduce_location) on_reduce_location(production_id, hook_values, result, Array.new(length), nil) end end return accept_reduction(result) if @accept_requested return recover(report: false) if @semantic_error CONTINUE_OUTCOME end |
#finalize_cst(value) ⇒ Object
1795 1796 1797 1798 1799 |
# File 'lib/ibex/runtime/parser.rb', line 1795 def finalize_cst(value) return value unless cst_enabled? finalize_red_green_cst(value) end |
#finalize_failed_green_cst ⇒ void
This method returns an undefined value.
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 |
# File 'lib/ibex/runtime/parser.rb', line 1995 def finalize_failed_green_cst builder = @green_builder || raise(ParseError, "(cst):1:1: Green builder is unavailable") kinds = @green_kinds || raise(ParseError, "(cst):1:1: kind metadata is unavailable") trailing = [] #: Array[CST::GreenNode | CST::GreenToken] builder.append_trailing_to_last_token(green_location_trivia(@lookahead_location, :cst_previous_trailing)) leading = @green_pending_skipped + green_location_trivia(@lookahead_location, :leading_trivia) @green_pending_skipped.clear if @lookahead.equal?(NO_LOOKAHEAD) trailing << builder.make_token(EOF_TOKEN, "", leading: leading) unless leading.empty? elsif @lookahead == EOF_TOKEN trailing << builder.make_token(EOF_TOKEN, "", leading: leading) else kind = @lookahead.is_a?(Integer) && @lookahead >= 0 ? @lookahead : kinds.fetch(:lexical_error_token) trailing << builder.make_token( kind, green_token_text(@lookahead_value, @lookahead_location), leading: leading, flags: CST::Flags::CONTAINS_ERROR ) end install_syntax_root(builder.finish_synthetic_root(trailing), kinds) end |
#finalize_lexical_green_cst(error) ⇒ void
This method returns an undefined value.
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 |
# File 'lib/ibex/runtime/parser.rb', line 2018 def finalize_lexical_green_cst(error) builder = @green_builder || raise(ParseError, "(cst):1:1: Green builder is unavailable") kinds = @green_kinds || raise(ParseError, "(cst):1:1: kind metadata is unavailable") unmatched_text = cst_location_value(error.location, :ibex_cst_unmatched_text) text = if unmatched_text.is_a?(String) unmatched_text elsif error.token_value.is_a?(String) error.token_value else error.token_value.to_s end token = builder.make_token( kinds.fetch(:lexical_error_token), text, leading: pending_green_trivia + green_location_trivia(error.location, :leading_trivia), flags: CST::Flags::CONTAINS_ERROR ) file = cst_location_value(error.location, :file) install_syntax_root( builder.finish_synthetic_root([token]), kinds, file: file.is_a?(String) ? file : nil ) end |
#finalize_red_green_cst(value) ⇒ Object
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 |
# File 'lib/ibex/runtime/parser.rb', line 1967 def finalize_red_green_cst(value) builder = @green_builder || raise(ParseError, "(cst):1:1: Green builder is unavailable") kinds = @green_kinds || raise(ParseError, "(cst):1:1: kind metadata is unavailable") incomplete = @lookahead != EOF_TOKEN unless incomplete || @green_reused_right_edge builder.append_trailing_to_last_token(green_location_trivia(@lookahead_location, :cst_previous_trailing)) end empty_trivia = [] #: Array[CST::GreenTrivia] eof_leading = incomplete ? empty_trivia : green_location_trivia(@lookahead_location, :leading_trivia) if @green_pending_skipped.empty? leading = eof_leading else leading = @green_pending_skipped + eof_leading @green_pending_skipped.clear end eof = builder.make_token(EOF_TOKEN, "", leading: leading) @green_token_states << green_lexer_state(@lookahead_location) green = if builder.size == 1 builder.finish_source_file(eof, incomplete: incomplete) else builder.finish_synthetic_root([eof], incomplete: incomplete) end install_syntax_root(green, kinds) install_parse_memo(green) value end |
#finish(location: nil) ⇒ Object
Supply EOF to a caller-driven parser session and return its result.
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/ibex/runtime/parser.rb', line 461 def finish(location: nil) run_push_driver do start_push_session refresh_runtime_fast_path_after_user_code! if @repair_policy enqueue_or_assign_repair_input( RepairInput.new(token_id: EOF_TOKEN, token_name: token_to_str(EOF_TOKEN), value: nil, location: location) ) else @lookahead = EOF_TOKEN @lookahead_value = nil @lookahead_location = location @runtime_fast_path = false unless nil.equal?(location) materialize_compatible_lookahead end outcome = run_push_lookahead return outcome.fetch(1) if outcome.is_a?(Array) raise ParseError, "(input):1:1: parser requested input after EOF" end end |
#finish_push_session ⇒ void
This method returns an undefined value.
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 |
# File 'lib/ibex/runtime/parser.rb', line 1216 def finish_push_session @push_status = :finished @source = nil @lookahead = NO_LOOKAHEAD @lookahead_location = nil @repair_input_buffer = nil @repair_selected = false @runtime_parser_tables = nil @runtime_fast_path = false clear_sync_recovery end |
#finish_recovery(token_id, token_display, value, location, original_state, value_stack, token_data, reason, observers) ⇒ [ :continue ]
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 |
# File 'lib/ibex/runtime/parser.rb', line 2351 def finish_recovery( token_id, token_display, value, location, original_state, value_stack, token_data, reason, observers ) recover_data = if observers token_data || runtime_original_token_data( token_id, token_display, value, location, original_state ) end on_error_recover(token_id, value, value_stack) on_error_recover_location(token_id, value, value_stack, location, @state_stack.last) if recover_data emit_runtime_event( :recover, recover_data.merge( "from_state" => original_state, "state" => @state_stack.last, "reason" => reason ), observers: observers ) end CONTINUE_OUTCOME end |
#generated_action_contract_cache_accessible? ⇒ Boolean
Ruby 3.0-3.3 expose the main Ractor as an object instead of exposing Ractor.main?. Class instance variables cannot be read or written from another Ractor on those versions.
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 |
# File 'lib/ibex/runtime/parser.rb', line 1345 def generated_action_contract_cache_accessible? return true unless Object.const_defined?(:Ractor, false) ractor = Object.const_get(:Ractor) return ractor.__send__(:main?) if ractor.respond_to?(:main?) return ractor.__send__(:current).equal?(ractor.__send__(:main)) if ractor.respond_to?(:current) && ractor.respond_to?(:main) true end |
#generated_action_contracts_validated?(tables) ⇒ Boolean
Generated parser tables are deeply frozen before they can cross a Ractor boundary. Their action-marker contract only needs one scan per parser class and exact table object, including across parser instances. Mutable application tables remain intentionally uncached.
1323 1324 1325 1326 1327 1328 |
# File 'lib/ibex/runtime/parser.rb', line 1323 def generated_action_contracts_validated?(tables) return false unless generated_action_contract_cache_accessible? cached = self.class.instance_variable_get(:@__ibex_validated_parser_tables) cached.equal?(tables) end |
#generated_action_symbol?(action) ⇒ Boolean
1775 1776 1777 |
# File 'lib/ibex/runtime/parser.rb', line 1775 def generated_action_symbol?(action) action.is_a?(Symbol) && action.name.match?(GENERATED_ACTION_NAME) end |
#generated_composition_action?(production, action) ⇒ Boolean
1768 1769 1770 1771 1772 |
# File 'lib/ibex/runtime/parser.rb', line 1768 def generated_composition_action?(production, action) parser_tables.fetch(:format_version) >= 3 && production[:composition_action] == true && generated_location_action?(production, action) end |
#generated_location_action?(production, action) ⇒ Boolean
1747 1748 1749 1750 1751 |
# File 'lib/ibex/runtime/parser.rb', line 1747 def generated_location_action?(production, action) parser_tables.fetch(:format_version) >= 2 && production[:location_action] == true && generated_action_symbol?(action) end |
#generated_positional_action?(production, action) ⇒ Boolean
1761 1762 1763 1764 1765 |
# File 'lib/ibex/runtime/parser.rb', line 1761 def generated_positional_action?(production, action) parser_tables.fetch(:format_version) >= 5 && production[:positional_action] == true && generated_action_symbol?(action) end |
#generated_values_action?(production, action) ⇒ Boolean
1754 1755 1756 1757 1758 |
# File 'lib/ibex/runtime/parser.rb', line 1754 def generated_values_action?(production, action) parser_tables.fetch(:format_version) >= 4 && production[:values_action] == true && generated_action_symbol?(action) end |
#green_lexer_state(location) ⇒ Symbol
2099 2100 2101 2102 2103 2104 2105 2106 |
# File 'lib/ibex/runtime/parser.rb', line 2099 def green_lexer_state(location) state = if location.is_a?(Hash) && location.key?(:ibex_lexer_start_state) location[:ibex_lexer_start_state] else cst_location_value(location, :ibex_lexer_start_state) end state ? state.to_sym : :INITIAL end |
#green_location_trivia(location, key) ⇒ Array[CST::GreenTrivia]
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 |
# File 'lib/ibex/runtime/parser.rb', line 1953 def green_location_trivia(location, key) trivia = if location.is_a?(Hash) && location.key?(:ibex_lexer_start_state) location[key] else cst_location_value(location, key) end return EMPTY_GREEN_TRIVIA unless trivia.is_a?(Array) return trivia if trivia.frozen? return trivia if trivia.all?(CST::GreenTrivia) trivia.grep(CST::GreenTrivia) end |
#green_token_text(value, location) ⇒ String
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 |
# File 'lib/ibex/runtime/parser.rb', line 1940 def green_token_text(value, location) text = if location.is_a?(Hash) && location.key?(:ibex_lexer_start_state) location[:ibex_cst_text] else cst_location_value(location, :ibex_cst_text) end return text if text.is_a?(String) return value if value.is_a?(String) "" end |
#initialize_runtime_fast_path(tables) ⇒ void
This method returns an undefined value.
2821 2822 2823 2824 2825 2826 2827 2828 |
# File 'lib/ibex/runtime/parser.rb', line 2821 def initialize_runtime_fast_path(tables) @location_stack = track_locations?(tables) ? [] : nil @runtime_fast_path = runtime_fast_path_eligible?(tables) return unless @runtime_fast_path return if @runtime_fast_path_tracker_installed install_runtime_fast_path_tracker! end |
#initialize_runtime_state(resource_limits, preserve_existing:) ⇒ void
This method returns an undefined value.
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity The explicit branches keep every runtime ivar typed while preserving application-owned values.
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 |
# File 'lib/ibex/runtime/parser.rb', line 703 def initialize_runtime_state(resource_limits, preserve_existing:) @resource_limits = resource_limits unless preserve_existing && defined?(@resource_limits) @yydebug = false unless preserve_existing && defined?(@yydebug) @yydebug_output = $stderr unless preserve_existing && defined?(@yydebug_output) @source = nil unless preserve_existing && defined?(@source) @state_stack = [] unless preserve_existing && defined?(@state_stack) install_value_stack([]) unless preserve_existing && defined?(@value_stack) @location_stack = nil unless preserve_existing && defined?(@location_stack) @lookahead = NO_LOOKAHEAD unless preserve_existing && defined?(@lookahead) @lookahead_value = nil unless preserve_existing && defined?(@lookahead_value) @lookahead_location = nil unless preserve_existing && defined?(@lookahead_location) @recovery_shifts = 0 unless preserve_existing && defined?(@recovery_shifts) @semantic_error = false unless preserve_existing && defined?(@semantic_error) @accept_requested = false unless preserve_existing && defined?(@accept_requested) @unknown_token_id = nil unless preserve_existing && defined?(@unknown_token_id) @push_status = :idle unless preserve_existing && defined?(@push_status) @driver_status = :idle unless preserve_existing && defined?(@driver_status) @runtime_driver_thread = nil unless preserve_existing && defined?(@runtime_driver_thread) @runtime_observers = nil unless preserve_existing && defined?(@runtime_observers) @runtime_event_sequence = 0 unless preserve_existing && defined?(@runtime_event_sequence) @runtime_lookahead_token_display = nil unless preserve_existing && defined?(@runtime_lookahead_token_display) @runtime_observation_mutex = Mutex.new unless preserve_existing && defined?(@runtime_observation_mutex) @repair_policy = nil unless preserve_existing && defined?(@repair_policy) @repair_input_buffer = nil unless preserve_existing && defined?(@repair_input_buffer) @repair_selected = false unless preserve_existing && defined?(@repair_selected) @semantic_locations = nil unless preserve_existing && defined?(@semantic_locations) @semantic_location_names = nil unless preserve_existing && defined?(@semantic_location_names) @semantic_result_location = nil unless preserve_existing && defined?(@semantic_result_location) @trace_value_printer = nil unless preserve_existing && defined?(@trace_value_printer) @sync_recovery_context = nil unless preserve_existing && defined?(@sync_recovery_context) @sync_recovery_token_data = nil unless preserve_existing && defined?(@sync_recovery_token_data) @sync_recovery_observers = nil unless preserve_existing && defined?(@sync_recovery_observers) @green_builder = nil unless preserve_existing && defined?(@green_builder) @green_kinds = nil unless preserve_existing && defined?(@green_kinds) @green_cache = nil unless preserve_existing && defined?(@green_cache) @syntax_root = nil unless preserve_existing && defined?(@syntax_root) @syntax_diagnostics = [] unless preserve_existing && defined?(@syntax_diagnostics) @green_pending_skipped = [] unless preserve_existing && defined?(@green_pending_skipped) @recovery_attempts = 0 unless preserve_existing && defined?(@recovery_attempts) @runtime_parser_tables = nil unless preserve_existing && defined?(@runtime_parser_tables) @runtime_fast_path = false unless preserve_existing && defined?(@runtime_fast_path) @runtime_fast_path_tracker_installed = false unless preserve_existing && defined?(@runtime_fast_path_tracker_installed) @runtime_fast_path_hooks_mutated = false unless preserve_existing && defined?(@runtime_fast_path_hooks_mutated) @runtime_fast_path_singleton_ancestors = nil unless preserve_existing && defined?(@runtime_fast_path_singleton_ancestors) @syntax_only = false unless preserve_existing && defined?(@syntax_only) @green_cache_override = nil unless preserve_existing && defined?(@green_cache_override) @green_token_states = [] unless preserve_existing && defined?(@green_token_states) @green_memo_stack = [] unless preserve_existing && defined?(@green_memo_stack) @green_parse_memo_valid = false unless preserve_existing && defined?(@green_parse_memo_valid) @green_initial_state = 0 unless preserve_existing && defined?(@green_initial_state) @syntax_parse_memo = nil unless preserve_existing && defined?(@syntax_parse_memo) @green_reused_right_edge = false unless preserve_existing && defined?(@green_reused_right_edge) @incremental_reused_descendants = 0 unless preserve_existing && defined?(@incremental_reused_descendants) end |
#install_parse_memo(green) ⇒ void
This method returns an undefined value.
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 |
# File 'lib/ibex/runtime/parser.rb', line 2082 def install_parse_memo(green) return unless @green_parse_memo_valid && @green_memo_stack.one? states = [@green_initial_state] #: Array[Integer?] @green_memo_stack.fetch(0).append_to(states) states << @state_stack.last return unless states.length == green.descendant_count @syntax_parse_memo = CST::ParseMemo.new( left_states: states, grammar_digest: parser_tables.fetch(:grammar_digest), state_count: parser_tables.fetch(:state_count), production_count: parser_tables.fetch(:production_count) ) end |
#install_runtime_fast_path_class_tracker! ⇒ void
This method returns an undefined value.
2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 |
# File 'lib/ibex/runtime/parser.rb', line 2842 def install_runtime_fast_path_class_tracker! parser_class = self.class return if parser_class.instance_variable_get(:@__ibex_fast_path_class_tracker_installed) == true singleton = parser_class.singleton_class singleton.prepend(FastPathClassMutationTracker) parser_class.instance_variable_set(:@__ibex_fast_path_hook_version, 0) parser_class.instance_variable_set(:@__ibex_fast_path_class_tracker_installed, true) rescue FrozenError, TypeError nil end |
#install_runtime_fast_path_tracker!(singleton = nil) ⇒ void
This method returns an undefined value.
2831 2832 2833 2834 2835 2836 2837 2838 2839 |
# File 'lib/ibex/runtime/parser.rb', line 2831 def install_runtime_fast_path_tracker!(singleton = nil) singleton ||= Object.instance_method(:singleton_class).bind_call(self) Module.instance_method(:prepend).bind_call(singleton, FastPathMutationTracker) @runtime_fast_path_tracker_installed = true @runtime_fast_path_hooks_mutated = false @runtime_fast_path_singleton_ancestors = singleton.ancestors.freeze rescue FrozenError, TypeError @runtime_fast_path = false end |
#install_syntax_root(green, kinds, file: nil) ⇒ void
This method returns an undefined value.
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 |
# File 'lib/ibex/runtime/parser.rb', line 2049 def install_syntax_root(green, kinds, file: nil) config = parser_tables.fetch(:cst) policy = config.fetch(:trivia_policy) location_file = cst_location_value(@lookahead_location, :file) source_file = file || (location_file if location_file.is_a?(String)) source = CST::SourceText.new(green.to_source.freeze, file: source_file) @syntax_root = CST::SyntaxNode.new( green: green, kinds: kinds, trivia_policy: policy, source_text: source ) emit_incremental_event( :cst_built, { "descendant_count" => green.descendant_count, "full_width" => green.full_width, "contains_error" => green.flags.anybits?(CST::Flags::CONTAINS_ERROR) } ) end |
#install_value_stack(values) ⇒ void
This method returns an undefined value.
Keep the two historical value-stack names as read-compatible aliases. Applications must not mutate or replace these internal arrays.
773 774 775 776 777 |
# File 'lib/ibex/runtime/parser.rb', line 773 def install_value_stack(values) @value_stack = values @vstack = values @racc_vstack = values end |
#internal_token_id(external_token) ⇒ Integer
2738 2739 2740 2741 2742 2743 2744 |
# File 'lib/ibex/runtime/parser.rb', line 2738 def internal_token_id(external_token) token_id = parser_tables.fetch(:tokens)[external_token] return token_id if token_id @unknown_token_name = external_token.inspect @unknown_token_id = -external_token.object_id.abs end |
#load_parser_tables ⇒ Hash[Symbol, untyped]
2755 2756 2757 2758 2759 |
# File 'lib/ibex/runtime/parser.rb', line 2755 def load_parser_tables self.class.__send__(:parser_tables) rescue NoMethodError raise ParseError, "(tables):1:1: #{self.class} must define .parser_tables" end |
#loc(reference) ⇒ Object
Return the location of a one-based RHS position or named reference while a semantic action is running.
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
# File 'lib/ibex/runtime/parser.rb', line 650 def loc(reference) locations = @semantic_locations raise ParseError, "(runtime):1:1: loc is only available inside a semantic action" unless locations index = if reference.is_a?(Integer) raise ArgumentError, "location index must be positive" unless reference.positive? reference - 1 else names = @semantic_location_names || EMPTY_LOCATION_NAMES names.fetch(reference.to_sym) do raise ArgumentError, "unknown named location #{reference.inspect}" end end locations.fetch(index) { raise ArgumentError, "location index #{reference.inspect} is outside the RHS" } end |
#materialize_compatible_lookahead ⇒ void
This method returns an undefined value.
2651 2652 2653 2654 2655 2656 2657 2658 |
# File 'lib/ibex/runtime/parser.rb', line 2651 def materialize_compatible_lookahead if @runtime_fast_path @runtime_lookahead_token_display = nil else token_display = materialize_lookahead_token_display! trace("read #{token_display}") if @yydebug end end |
#materialize_lookahead_token_display! ⇒ String
2661 2662 2663 2664 2665 2666 |
# File 'lib/ibex/runtime/parser.rb', line 2661 def materialize_lookahead_token_display! token_display = @runtime_lookahead_token_display return token_display if token_display @runtime_lookahead_token_display = token_to_str(@lookahead) end |
#next_token ⇒ [ untyped, untyped ], ...
Override in pull parsers. Return [token, value],
[token, value, location], false, or nil.
509 510 511 |
# File 'lib/ibex/runtime/parser.rb', line 509 def next_token raise NotImplementedError, "(input):1:1: next_token must be implemented" end |
#normalized_token_word(token_name) ⇒ String?
2788 2789 2790 2791 |
# File 'lib/ibex/runtime/parser.rb', line 2788 def normalized_token_word(token_name) word = token_name.delete_prefix(":") word.upcase if word.match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/) end |
#notify_error_handler(context, repair) ⇒ void
This method returns an undefined value.
2271 2272 2273 2274 2275 2276 2277 2278 |
# File 'lib/ibex/runtime/parser.rb', line 2271 def notify_error_handler(context, repair) @repair_selected = repair.is_a?(RepairPlan) begin on_error(context[:token_id], context[:value], context.fetch(:value_stack).dup) ensure @repair_selected = false end end |
#on_discard(_token_id, _value, _location, _reason) ⇒ void
This method returns an undefined value.
Called when yacc recovery discards an application token.
570 |
# File 'lib/ibex/runtime/parser.rb', line 570 def on_discard(_token_id, _value, _location, _reason); end |
#on_error(token_id, value, _value_stack) ⇒ Object
Override to recover from syntax errors. The default raises unless a bounded automatic repair has already been selected.
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/ibex/runtime/parser.rb', line 516 def on_error(token_id, value, _value_stack) return if @repair_selected return if cst_enabled? expected = expected_tokens token_name = token_to_str(token_id) state = @state_stack.last configured = parser_tables.fetch(:error_messages, EMPTY_ROW)[state] error_id, detail = (configured) raise ParseError.new( token_id: token_id, token_name: token_name, token_value: value, expected_tokens: expected, location: @lookahead_location, state: state, suggestions: token_suggestions(token_name, expected), error_id: error_id, detail: detail ) end |
#on_error_recover(_token_id, _value, _value_stack) ⇒ void
This method returns an undefined value.
Called after the synthetic error token enters a recovery state. The payload describes the original error before recovery popped stacks.
561 |
# File 'lib/ibex/runtime/parser.rb', line 561 def on_error_recover(_token_id, _value, _value_stack); end |
#on_error_recover_location(_token_id, _value, _value_stack, _location, _state) ⇒ void
This method returns an undefined value.
Location-aware recovery observer.
566 |
# File 'lib/ibex/runtime/parser.rb', line 566 def on_error_recover_location(_token_id, _value, _value_stack, _location, _state); end |
#on_reduce(_production_id, _values, _result) ⇒ void Also known as: __ibex_fast_path_on_reduce
This method returns an undefined value.
Called after a production's semantic action and goto are committed. Override to observe its id, RHS values, and semantic result.
551 |
# File 'lib/ibex/runtime/parser.rb', line 551 def on_reduce(_production_id, _values, _result); end |
#on_reduce_location(_production_id, _values, _result, _locations, _result_location) ⇒ void Also known as: __ibex_fast_path_on_reduce_location
This method returns an undefined value.
Location-aware reduction observer.
556 |
# File 'lib/ibex/runtime/parser.rb', line 556 def on_reduce_location(_production_id, _values, _result, _locations, _result_location); end |
#on_repair(_plan) ⇒ void
This method returns an undefined value.
Called once after a repair is selected and before its edited token prefix is replayed through normal parser actions.
586 |
# File 'lib/ibex/runtime/parser.rb', line 586 def on_repair(_plan); end |
#on_shift(_token_id, _value, _state) ⇒ void Also known as: __ibex_fast_path_on_shift
This method returns an undefined value.
Called after an ordinary input token is shifted. Override to observe the internal token id, semantic value, and destination state.
541 |
# File 'lib/ibex/runtime/parser.rb', line 541 def on_shift(_token_id, _value, _state); end |
#on_shift_location(_token_id, _value, _state, _location) ⇒ void Also known as: __ibex_fast_path_on_shift_location
This method returns an undefined value.
Location-aware shift observer. The compatible hook above retains its original signature and runs first.
546 |
# File 'lib/ibex/runtime/parser.rb', line 546 def on_shift_location(_token_id, _value, _state, _location); end |
#parse_syntax_token_source(source, cache) ⇒ CST::SyntaxResult
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 |
# File 'lib/ibex/runtime/parser.rb', line 2122 def parse_syntax_token_source(source, cache) with_syntax_only(cache) do value = drive_parser(source) parsed = syntax_parse_result(value) CST::SyntaxResult.new( syntax_root: parsed.syntax_root, diagnostics: parsed.diagnostics, reused_ratio: 0.0 ) end end |
#parse_with_syntax ⇒ CST::ParseResult
Parse through next_token and return both the semantic value and Red root.
414 415 416 |
# File 'lib/ibex/runtime/parser.rb', line 414 def parse_with_syntax syntax_parse_result(do_parse) end |
#parser_state_count(tables) ⇒ Integer
813 814 815 816 817 818 819 820 821 822 |
# File 'lib/ibex/runtime/parser.rb', line 813 def parser_state_count(tables) state_count = tables[:state_count] return state_count if state_count.is_a?(Integer) actions = tables.fetch(:actions) state_count = actions.respond_to?(:row_count) ? actions.row_count : actions.length return state_count if state_count.is_a?(Integer) raise ParseError, "(tables):1:1: parser table state count must be an Integer" end |
#parser_tables ⇒ Hash[Symbol, untyped]
2747 2748 2749 2750 2751 2752 |
# File 'lib/ibex/runtime/parser.rb', line 2747 def parser_tables tables = @runtime_parser_tables return tables if tables load_parser_tables end |
#pending_green_trivia ⇒ Array[CST::GreenTrivia]
2042 2043 2044 2045 2046 |
# File 'lib/ibex/runtime/parser.rb', line 2042 def pending_green_trivia return [] unless respond_to?(:take_cst_pending_green_trivia, true) __send__(:take_cst_pending_green_trivia) end |
#perform(action) ⇒ Object
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 |
# File 'lib/ibex/runtime/parser.rb', line 1443 def perform(action) case action.first when :shift next_state = action.fetch(1) @runtime_fast_path ? fast_shift(next_state) : shift(next_state) when :reduce production_id = action.fetch(1) if @runtime_fast_path production = parser_tables.fetch(:productions).fetch(production_id) length = production.fetch(:length) return fast_reduce(production_id, production, length) if fast_reduction_eligible?(production, length) end reduce(production_id, production) when :accept result = finalize_cst(@value_stack.last) emit_accept_event(EventSanitizer.value(result), "table") if @runtime_observers [:accepted, result] when :error materialize_lookahead_token_display! recover when :sync_recover materialize_lookahead_token_display! continue_sync_recovery else raise ParseError, "(tables):1:1: unknown parser action #{action.inspect}" end end |
#pop_reduction_locations(length) ⇒ Array[untyped]
2986 2987 2988 2989 2990 2991 2992 2993 |
# File 'lib/ibex/runtime/parser.rb', line 2986 def pop_reduction_locations(length) stack = @location_stack return Array.new(length) unless stack locations = stack.last(length) stack.pop(length) locations end |
#positional_action_contract?(production) ⇒ Boolean
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 |
# File 'lib/ibex/runtime/parser.rb', line 1408 def positional_action_contract?(production) production[:length].is_a?(Integer) && production[:length].between?(0, 4) && generated_action_symbol?(production[:action]) && production[:values_action] != true && production[:borrowed_values_action] != true && production[:location_action] != true && production[:composition_action] != true && production.fetch(:location_context_length, 0).zero? end |
#prepare_green_cst(tables) ⇒ void
This method returns an undefined value.
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 |
# File 'lib/ibex/runtime/parser.rb', line 1829 def prepare_green_cst(tables) @syntax_root = nil @syntax_parse_memo = nil @syntax_diagnostics.clear @green_pending_skipped.clear config = tables[:cst] unless config.is_a?(Hash) @green_builder = nil @green_kinds = nil @green_cache = nil return end kinds = tables[:cst_kinds] kinds = CST::Kind.new(config.fetch(:kinds), slots: config.fetch(:slots)) unless kinds.is_a?(CST::Kind) cache = @green_cache_override || CST::NodeCache.new @green_kinds = kinds @green_cache = cache @green_token_states.clear @green_memo_stack.clear @green_parse_memo_valid = true @green_initial_state = @state_stack.first || 0 @green_reused_right_edge = false @incremental_reused_descendants = 0 @green_builder = CST::GreenBuilder.new(kinds: kinds, cache: cache) end |
#prepare_parse(source, initial_state: nil) ⇒ void
This method returns an undefined value.
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 |
# File 'lib/ibex/runtime/parser.rb', line 1229 def prepare_parse(source, initial_state: nil) @runtime_parser_tables = load_parser_tables tables = validate_parser_table_format! initial_state = resolve_initial_state(tables, initial_state) @source = source @state_stack.clear << initial_state @value_stack.clear initialize_runtime_fast_path(tables) @lookahead = NO_LOOKAHEAD @lookahead_value = nil @lookahead_location = nil @runtime_lookahead_token_display = nil reset_parse_recovery_state @accept_requested = false @unknown_token_id = nil reset_cst_results(tables) @recovery_attempts = 0 trace("start state #{initial_state}") if @yydebug @runtime_event_sequence = 0 return unless @runtime_observers emit_runtime_event( :start, { "driver" => @driver_status.to_s, "initial_state" => initial_state, "table_format_version" => tables.fetch(:format_version), "grammar_digest" => tables[:grammar_digest], "state_count" => tables[:state_count], "production_count" => tables[:production_count] } ) end |
#preserve_deleted_repair_input(input) ⇒ void
This method returns an undefined value.
Retain a repair deletion in the concrete tree even though it is absent from the repaired token stream.
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 |
# File 'lib/ibex/runtime/parser.rb', line 2553 def preserve_deleted_repair_input(input) return unless cst_enabled? builder = @green_builder kinds = @green_kinds return unless builder && kinds previous = green_location_trivia(input.location, :cst_previous_trailing) empty = [] #: Array[CST::GreenTrivia] previous = empty if builder.append_trailing_to_last_token(previous) leading = previous + green_location_trivia(input.location, :leading_trivia) source = leading.each_with_object(String.new(encoding: Encoding::BINARY)) do |trivia, text| text << trivia.text end source << green_token_text(input.value, input.location) @green_pending_skipped << CST::GreenTrivia.new( kind: kinds.fetch(:skipped_tokens), text: source ) end |
#publish_error_context(context) ⇒ [ Hash[String, untyped]?, Array[Proc]? ]
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 |
# File 'lib/ibex/runtime/parser.rb', line 2257 def publish_error_context(context) error_observers = runtime_observer_snapshot if @runtime_observers if error_observers token_data = runtime_original_token_data( context[:token_id], context[:token_display], context[:value], context[:location], context[:state] ) emit_runtime_event( :error, token_data.merge("reason" => context[:reason]), observers: error_observers ) end [token_data, (runtime_observer_snapshot if @runtime_observers)] end |
#push(token, value = nil, location = nil) ⇒ :need_more, ...
Supply one token to a caller-driven parser session.
Returns :need_more after consuming it, [:accepted, result] after
acceptance, or [:rejected, result] after recovery terminates.
rubocop:disable Layout/LineLength
rubocop:enable Layout/LineLength
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/ibex/runtime/parser.rb', line 440 def push(token, value = nil, location = nil) raise ParseError, "(input):1:1: push requires a token; call finish for EOF" if token.nil? || token == false run_push_driver do start_push_session refresh_runtime_fast_path_after_user_code! if @repair_policy enqueue_or_assign_repair_input(repair_input(token, value, location)) else @lookahead = internal_token_id(token) @lookahead_value = value @lookahead_location = location @runtime_fast_path = false unless nil.equal?(location) materialize_compatible_lookahead end run_push_lookahead end end |
#push_location(location) ⇒ void
This method returns an undefined value.
Keep allocation out of the ordinary two-element lexer path. If a location first appears after values have already shifted, backfill the parallel prefix with nil exactly once.
2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 |
# File 'lib/ibex/runtime/parser.rb', line 2974 def push_location(location) stack = @location_stack if stack stack << location elsif location new_stack = Array.new(@value_stack.length) new_stack << location @location_stack = new_stack end end |
#push_reduction_result(next_state, result, location) ⇒ void
This method returns an undefined value.
1626 1627 1628 1629 1630 |
# File 'lib/ibex/runtime/parser.rb', line 1626 def push_reduction_result(next_state, result, location) @state_stack << next_state push_location(location) @value_stack << result end |
#push_reusable_green_subtree(subtree) ⇒ void
This method returns an undefined value.
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 |
# File 'lib/ibex/runtime/parser.rb', line 2135 def push_reusable_green_subtree(subtree) unless @syntax_only && @state_stack.last == subtree.left_state raise ParseError, "(incremental):1:1: reusable subtree state mismatch" end builder = @green_builder || raise(ParseError, "(incremental):1:1: Green builder is unavailable") builder.append_trailing_to_last_token(subtree.previous_trailing) unless @green_reused_right_edge next_state = table_lookup(parser_tables.fetch(:gotos), @state_stack.last, subtree.lhs) raise ParseError, "(incremental):1:1: reusable subtree has no goto" unless next_state ensure_stack_capacity! @state_stack << next_state push_location(nil) @value_stack << nil builder.subtree(subtree.green) @green_memo_stack << CST::ParseMemo::Entry.new(nil, segment: subtree.left_states) @green_reused_right_edge = true @incremental_reused_descendants += subtree.green.descendant_count end |
#read_compatible_lookahead ⇒ void
This method returns an undefined value.
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 |
# File 'lib/ibex/runtime/parser.rb', line 2626 def read_compatible_lookahead token = read_external_token refresh_runtime_fast_path_after_user_code! if token.is_a?(CST::ReusableSubtree) push_reusable_green_subtree(token) return end if token.nil? || token == false @lookahead = EOF_TOKEN @lookahead_value = nil @lookahead_location = nil else external_token, @lookahead_value, @lookahead_location = token @lookahead = if external_token.nil? || external_token == false EOF_TOKEN else internal_token_id(external_token) end end @runtime_fast_path = false unless nil.equal?(@lookahead_location) materialize_compatible_lookahead end |
#read_external_token ⇒ Object
2727 2728 2729 2730 2731 2732 2733 2734 2735 |
# File 'lib/ibex/runtime/parser.rb', line 2727 def read_external_token source = @source return next_token unless source return source.next_for_state(@state_stack.last) if source.respond_to?(:next_for_state) source.call rescue StopIteration false end |
#read_lookahead ⇒ void
This method returns an undefined value.
2608 2609 2610 2611 2612 |
# File 'lib/ibex/runtime/parser.rb', line 2608 def read_lookahead return read_repair_lookahead if @repair_policy read_compatible_lookahead end |
#read_repair_lookahead ⇒ void
This method returns an undefined value.
2615 2616 2617 2618 2619 2620 2621 2622 2623 |
# File 'lib/ibex/runtime/parser.rb', line 2615 def read_repair_lookahead buffer = @repair_input_buffer input = if buffer&.any? buffer.shift else repair_input_from_external(read_external_token) end assign_repair_input(input) end |
#recover(report: true) ⇒ Object
2181 2182 2183 2184 2185 2186 2187 2188 |
# File 'lib/ibex/runtime/parser.rb', line 2181 def recover(report: true) @runtime_fast_path = false materialize_lookahead_token_display! unless @lookahead.equal?(NO_LOOKAHEAD) @semantic_error = false return continue_recovery if @recovery_shifts.positive? begin_recovery(report) end |
#recovery_context(report) ⇒ Hash[Symbol, untyped]
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 |
# File 'lib/ibex/runtime/parser.rb', line 2244 def recovery_context(report) { token_id: @lookahead, value: @lookahead_value, token_display: @runtime_lookahead_token_display, location: @lookahead_location, value_stack: @value_stack.dup, state: @state_stack.last, reason: report ? "syntax" : "semantic" } end |
#reduce(production_id, prefetched_production = nil) ⇒ Object
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 |
# File 'lib/ibex/runtime/parser.rb', line 1581 def reduce(production_id, prefetched_production = nil) # rubocop:disable Metrics -- allocation-free hot path. production = prefetched_production || parser_tables.fetch(:productions).fetch(production_id) length = production.fetch(:length) event_observers = runtime_observer_snapshot if @runtime_observers pre_state = @state_stack.last if event_observers values = @value_stack.last(length) locations = pop_reduction_locations(length) hook_values = values.dup # Array#last above preserves the native negative-length exception before either stack mutates. if length.is_a?(Integer) && !length.negative? && length <= @value_stack.length && length < @state_stack.length remaining = length while remaining.positive? @state_stack.pop @value_stack.pop remaining -= 1 end else @state_stack.pop(length) @value_stack.pop(length) end post_state = @state_stack.last if event_observers location = LocationSpan.for_reduction(locations, lookahead: @lookahead_location) result = reduction_value(production_id, production, values, locations, location) refresh_runtime_fast_path_after_user_code! reduce_green(production_id, production, length, @state_stack.last) next_state = table_lookup(parser_tables.fetch(:gotos), @state_stack.last, production.fetch(:lhs)) raise ParseError, "(tables):1:1: missing goto for production #{production_id}" if next_state.nil? ensure_stack_capacity! push_reduction_result(next_state, result, location) trace_reduction(production_id, length, production.fetch(:lhs), result, next_state) event_data = build_reduce_event_data( event_observers, production_id, production, length, pre_state, post_state, next_state, result, location ) on_reduce(production_id, hook_values, result) on_reduce_location(production_id, hook_values, result, locations.dup, location) emit_runtime_event(:reduce, event_data, observers: event_observers) if event_data return accept_reduction(result) if @accept_requested return recover(report: false) if @semantic_error CONTINUE_OUTCOME end |
#reduce_green(production_id, production, length, left_state) ⇒ void
This method returns an undefined value.
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 |
# File 'lib/ibex/runtime/parser.rb', line 1922 def reduce_green(production_id, production, length, left_state) builder = @green_builder return unless builder config = parser_tables.fetch(:cst) slot = config.fetch(:slots).fetch(production_id, nil) kind = slot ? slot.fetch(:node_kind) : production.fetch(:lhs) flags = length.zero? ? CST::Flags::SYNTHETIC : 0 builder.node(kind, length, flags: flags) entry = if length.zero? CST::ParseMemo::Entry.new(left_state) else CST::ParseMemo::Entry.new(left_state, children: @green_memo_stack.pop(length).freeze) end @green_memo_stack << entry end |
#reduction_value(production_id, production, values, locations, location) ⇒ Object
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 |
# File 'lib/ibex/runtime/parser.rb', line 1677 def reduction_value(production_id, production, values, locations, location) # rubocop:disable Metrics -- explicit hot path. previous_locations = @semantic_locations previous_names = @semantic_location_names previous_result_location = @semantic_result_location return values.first if @syntax_only action = production[:action] return actionless_reduction_value(production_id, production, values, locations, location) unless action context_length = production.fetch(:location_context_length, 0) action_locations = if context_length.positive? (@location_stack || []).last(context_length) else locations end @semantic_locations = action_locations @semantic_location_names = production.fetch(:location_names, EMPTY_LOCATION_NAMES) @semantic_result_location = location return __send__(action, *values) if generated_positional_action?(production, action) return __send__(action, values) if generated_values_action?(production, action) value_stack = @value_stack.dup callable = action.respond_to?(:call) if generated_location_action?(production, action) location_stack = (@location_stack || []).dup if generated_composition_action?(production, action) if callable return instance_exec( values, value_stack, locations, location_stack, location, @lookahead_location, &action ) end return __send__(action, values, value_stack, locations, location_stack, location, @lookahead_location) end return instance_exec(values, value_stack, locations, location_stack, location, &action) if callable return __send__(action, values, value_stack, locations, location_stack, location) end callable ? instance_exec(values, value_stack, &action) : __send__(action, values, value_stack) ensure @semantic_locations = previous_locations @semantic_location_names = previous_names @semantic_result_location = previous_result_location end |
#refresh_runtime_fast_path_after_user_code! ⇒ void
This method returns an undefined value.
Re-check after lexer and semantic-action callbacks because those are supported points at which an application can install instrumentation. A disabled fast path never becomes active again within the session.
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 |
# File 'lib/ibex/runtime/parser.rb', line 2958 def refresh_runtime_fast_path_after_user_code! return unless @runtime_fast_path @runtime_fast_path = false unless !@yydebug && @runtime_observers.nil? && @repair_policy.nil? && @location_stack.nil? && @semantic_error == false && @accept_requested == false end |
#reject_recovery_eof ⇒ [ :done, nil ]
2208 2209 2210 2211 2212 2213 2214 2215 |
# File 'lib/ibex/runtime/parser.rb', line 2208 def reject_recovery_eof if @runtime_observers observers = runtime_observer_snapshot data = runtime_current_token_data emit_reject_event("eof_during_recovery", data, observers: observers) end [:done, cst_enabled? ? failed_cst : nil] end |
#reject_without_recovery(token_id, token_display, value, location, original_state, token_data, observers) ⇒ [ :done, nil ]
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 |
# File 'lib/ibex/runtime/parser.rb', line 2334 def reject_without_recovery(token_id, token_display, value, location, original_state, token_data, observers) if observers reject_data = token_data || runtime_original_token_data( token_id, token_display, value, location, original_state ) emit_reject_event( "no_recovery_state", reject_data.merge("state" => @state_stack.last), observers: observers ) end [:done, cst_enabled? ? failed_cst : nil] end |
#release_driver ⇒ void
This method returns an undefined value.
1208 1209 1210 1211 1212 1213 |
# File 'lib/ibex/runtime/parser.rb', line 1208 def release_driver @runtime_observation_mutex.synchronize do @driver_status = :idle @runtime_driver_thread = nil end end |
#repair_input(external_token, value, location) ⇒ RepairInput
2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 |
# File 'lib/ibex/runtime/parser.rb', line 2669 def repair_input(external_token, value, location) token_id = parser_tables.fetch(:tokens)[external_token] if token_id return RepairInput.new( token_id: token_id, token_name: token_to_str(token_id), value: value, location: location ) end RepairInput.new( token_id: -external_token.object_id.abs, token_name: external_token.inspect, value: value, location: location ) end |
#repair_input_from_external(token) ⇒ RepairInput
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 |
# File 'lib/ibex/runtime/parser.rb', line 2686 def repair_input_from_external(token) if token.nil? || token == false return RepairInput.new(token_id: EOF_TOKEN, token_name: token_to_str(EOF_TOKEN), value: nil, location: nil) end external_token, value, location = token if external_token.nil? || external_token == false return RepairInput.new( token_id: EOF_TOKEN, token_name: token_to_str(EOF_TOKEN), value: nil, location: location ) end repair_input(external_token, value, location) end |
#repair_policy ⇒ RepairPolicy?
360 361 362 363 |
# File 'lib/ibex/runtime/parser.rb', line 360 def repair_policy ensure_runtime_initialized! @repair_policy end |
#repair_policy=(policy) ⇒ RepairPolicy?
Enable bounded automatic repair for the next parser session. Assign nil to restore the compatible yacc-only behavior.
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/ibex/runtime/parser.rb', line 374 def repair_policy=(policy) ensure_runtime_initialized! unless policy.nil? || policy.is_a?(RepairPolicy) raise ArgumentError, "repair_policy must be an Ibex::Runtime::RepairPolicy or nil" end @runtime_observation_mutex.synchronize do ensure_driver_available_without_lock! if @push_status == :active raise ParseError, "(repair):1:1: repair_policy cannot change during an active push session" end @repair_policy = policy end end |
#repair_search_tokens(policy) ⇒ [ Array[RepairInput], bool ]
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 |
# File 'lib/ibex/runtime/parser.rb', line 2487 def repair_search_tokens(policy) current = RepairInput.new( token_id: @lookahead, token_name: @runtime_lookahead_token_display || token_to_str(@lookahead), value: @lookahead_value, location: @lookahead_location ) buffer = @repair_input_buffer || raise(ParseError, "(repair):1:1: repair input buffer is unavailable") while @driver_status == :pull && buffer.length + 1 < policy.max_lookahead && !buffer.last&.eof? buffer << repair_input_from_external(read_external_token) end tokens = ([current] + buffer).take(policy.max_lookahead) complete = tokens.any?(&:eof?) || tokens.length >= policy.max_lookahead [tokens.freeze, complete] end |
#repair_trace(plan) ⇒ String
2603 2604 2605 |
# File 'lib/ibex/runtime/parser.rb', line 2603 def repair_trace(plan) plan.edits.map { |edit| "#{edit.kind}(#{edit.token_name}@#{edit.position})" }.join(", ") end |
#replay_repair_edits(source, edits) ⇒ Array[RepairInput]
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 |
# File 'lib/ibex/runtime/parser.rb', line 2524 def replay_repair_edits(source, edits) by_position = edits.group_by(&:position) repaired = [] #: Array[RepairInput] source.each_with_index do |input, position| consumed = append_repair_edits(repaired, input, by_position.fetch(position, [])) repaired << input unless consumed end by_position.fetch(source.length, []).each do |edit| repaired << synthetic_repair_input(edit, value: nil, location: @lookahead_location) end repaired end |
#reset_cst_results(tables) ⇒ void
This method returns an undefined value.
1857 1858 1859 |
# File 'lib/ibex/runtime/parser.rb', line 1857 def reset_cst_results(tables) prepare_green_cst(tables) end |
#reset_parse_recovery_state ⇒ void
This method returns an undefined value.
1265 1266 1267 1268 1269 1270 1271 |
# File 'lib/ibex/runtime/parser.rb', line 1265 def reset_parse_recovery_state @recovery_shifts = 0 @semantic_error = false @repair_input_buffer = @repair_policy ? [] : nil @repair_selected = false clear_sync_recovery end |
#reset_push ⇒ nil
Discard a caller-driven session so this parser can accept a new one.
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/ibex/runtime/parser.rb', line 485 def reset_push ensure_runtime_initialized! @runtime_observation_mutex.synchronize do ensure_driver_available_without_lock! @push_status = :idle @source = nil @state_stack = [] install_value_stack([]) @location_stack = nil @lookahead = NO_LOOKAHEAD @lookahead_value = nil @lookahead_location = nil @runtime_lookahead_token_display = nil @repair_input_buffer = nil @repair_selected = false @runtime_parser_tables = nil @runtime_fast_path = false end nil end |
#resolve_initial_state(tables, requested) ⇒ Integer
1274 1275 1276 1277 1278 1279 1280 |
# File 'lib/ibex/runtime/parser.rb', line 1274 def resolve_initial_state(tables, requested) initial_state = requested || tables[:initial_state] || 0 return initial_state if initial_state.is_a?(Integer) && initial_state.between?(0, parser_state_count(tables) - 1) raise ParseError, "(tables):1:1: parser initial state #{initial_state.inspect} is invalid" end |
#resource_limits ⇒ ResourceLimits
366 367 368 369 |
# File 'lib/ibex/runtime/parser.rb', line 366 def resource_limits ensure_runtime_initialized! @resource_limits end |
#resource_limits=(limits) ⇒ ResourceLimits
Replace the limits used by future sessions.
392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/ibex/runtime/parser.rb', line 392 def resource_limits=(limits) ensure_runtime_initialized! validate_resource_limits!(limits) @runtime_observation_mutex.synchronize do ensure_driver_available_without_lock! if @push_status == :active raise ParseError, "(resource):1:1: resource_limits cannot change during an active push session" end @resource_limits = limits end end |
#result_loc ⇒ Object
Return the synthesized span of the reduction being evaluated.
669 670 671 672 673 674 675 |
# File 'lib/ibex/runtime/parser.rb', line 669 def result_loc unless @semantic_locations raise ParseError, "(runtime):1:1: result_loc is only available inside a semantic action" end @semantic_result_location end |
#run_push_driver { ... } ⇒ Object
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 |
# File 'lib/ibex/runtime/parser.rb', line 1183 def run_push_driver ensure_runtime_initialized! @runtime_observation_mutex.synchronize do ensure_driver_available_without_lock! @driver_status = :push @runtime_driver_thread = Thread.current end begin yield rescue StandardError finish_push_session raise ensure release_driver end end |
#run_push_lookahead ⇒ :need_more, ...
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 |
# File 'lib/ibex/runtime/parser.rb', line 1160 def run_push_lookahead loop do outcome = perform(action_for_current_state) return :need_more if outcome.first == :repair_pending case outcome.first when :accepted finish_push_session return [:accepted, outcome.fetch(1)] when :done finish_push_session return [:rejected, outcome.fetch(1)] end next unless @lookahead.equal?(NO_LOOKAHEAD) repair_buffer = @repair_input_buffer next if repair_buffer && !repair_buffer.empty? return :need_more end end |
#runtime_core_method(lookup, name) ⇒ Method?
Bypass an application-defined method helper while retaining Ruby's
complete singleton/prepend/subclass lookup semantics.
2948 2949 2950 2951 2952 |
# File 'lib/ibex/runtime/parser.rb', line 2948 def runtime_core_method(lookup, name) lookup.bind_call(self, name) rescue NameError nil end |
#runtime_current_token_data ⇒ Hash[String, untyped]
2398 2399 2400 2401 2402 2403 2404 2405 2406 |
# File 'lib/ibex/runtime/parser.rb', line 2398 def runtime_current_token_data runtime_token_data( token_id: @lookahead, token_display: @runtime_lookahead_token_display, value: @lookahead_value, location: @lookahead_location, state: @state_stack.last ) end |
#runtime_discard_data(token_display) ⇒ Hash[String, untyped]
2218 2219 2220 2221 2222 2223 2224 2225 2226 |
# File 'lib/ibex/runtime/parser.rb', line 2218 def runtime_discard_data(token_display) runtime_token_data( token_id: @lookahead, token_display: token_display, value: @lookahead_value, location: @lookahead_location, state: @state_stack.last ).merge("reason" => "recovery").freeze end |
#runtime_fast_path_class_hooks_eligible? ⇒ Boolean
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 |
# File 'lib/ibex/runtime/parser.rb', line 2904 def runtime_fast_path_class_hooks_eligible? install_runtime_fast_path_class_tracker! parser_class = self.class version = parser_class.instance_variable_get(:@__ibex_fast_path_hook_version) || 0 cached = parser_class.instance_variable_get(:@__ibex_fast_path_hook_cache) return cached.fetch(1) if cached.is_a?(Array) && cached[0] == version eligible = FAST_PATH_HOOK_REFERENCES.all? do |name, reference| runtime_methods_equivalent?( parser_class.instance_method(name), Parser.instance_method(reference) ) rescue NameError false end parser_class.instance_variable_set(:@__ibex_fast_path_hook_cache, [version, eligible].freeze) eligible rescue FrozenError, TypeError false end |
#runtime_fast_path_eligible?(tables) ⇒ Boolean
The generic driver remains authoritative whenever a public runtime extension can observe a committed shift or reduction.
2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 |
# File 'lib/ibex/runtime/parser.rb', line 2857 def runtime_fast_path_eligible?(tables) !@syntax_only && !@yydebug && @runtime_observers.nil? && @repair_policy.nil? && !tables[:cst] && !tables.fetch(:uses_locations, false) && @location_stack.nil? && runtime_fast_path_hooks_eligible? end |
#runtime_fast_path_hooks_eligible? ⇒ Boolean
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 |
# File 'lib/ibex/runtime/parser.rb', line 2869 def runtime_fast_path_hooks_eligible? cached = cached_runtime_fast_path_hooks_eligibility return cached unless cached.nil? lookup = Object.instance_method(:method) hooks_unchanged = runtime_method_unchanged?(lookup, :on_shift, :__ibex_fast_path_on_shift) && runtime_method_unchanged?(lookup, :on_shift_location, :__ibex_fast_path_on_shift_location) && runtime_method_unchanged?(lookup, :on_reduce, :__ibex_fast_path_on_reduce) && runtime_method_unchanged?(lookup, :on_reduce_location, :__ibex_fast_path_on_reduce_location) && runtime_method_unchanged?(lookup, :token_to_str, :__ibex_fast_path_token_to_str) return false unless hooks_unchanged return true unless @runtime_fast_path_tracker_installed tracker_effective = FastPathMutationTracker.effective_for?(self, lookup) if tracker_effective @runtime_fast_path_hooks_mutated = false singleton = Object.instance_method(:singleton_class).bind_call(self) @runtime_fast_path_singleton_ancestors = singleton.ancestors.freeze end tracker_effective end |
#runtime_method_unchanged?(lookup, name, reference) ⇒ Boolean
2926 2927 2928 2929 2930 2931 2932 |
# File 'lib/ibex/runtime/parser.rb', line 2926 def runtime_method_unchanged?(lookup, name, reference) implementation = runtime_core_method(lookup, name) expected = runtime_core_method(lookup, reference) return false if implementation.nil? || expected.nil? runtime_methods_equivalent?(implementation, expected) end |
#runtime_methods_equivalent?(implementation, expected) ⇒ Boolean
Ruby 3.0 and 3.1 do not preserve UnboundMethod#== for an inherited method and its alias even though both still identify the same body.
2937 2938 2939 2940 2941 2942 2943 |
# File 'lib/ibex/runtime/parser.rb', line 2937 def runtime_methods_equivalent?(implementation, expected) return true if implementation == expected implementation.owner.equal?(expected.owner) && implementation.original_name == expected.original_name && implementation.source_location == expected.source_location end |
#runtime_original_token_data(token_id, token_display, value, location, state) ⇒ Hash[String, untyped]
2321 2322 2323 2324 2325 2326 2327 2328 2329 |
# File 'lib/ibex/runtime/parser.rb', line 2321 def runtime_original_token_data(token_id, token_display, value, location, state) runtime_token_data( token_id: token_id, token_display: token_display, value: value, location: location, state: state ) end |
#runtime_reduce_data(production_id, production, length, pre_state, post_state, next_state, result, location) ⇒ Hash[String, untyped]
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 |
# File 'lib/ibex/runtime/parser.rb', line 1655 def runtime_reduce_data(production_id, production, length, pre_state, post_state, next_state, result, location) { "production_id" => production_id, "lhs" => production.fetch(:lhs), "rhs_length" => length, "pre_state" => pre_state, "post_state" => post_state, "goto_state" => next_state, "result" => EventSanitizer.value(result), "location" => EventSanitizer.location(location) }.freeze end |
#runtime_token_data(token_id:, token_display:, value:, location:, state:) ⇒ Hash[String, untyped]
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 |
# File 'lib/ibex/runtime/parser.rb', line 2377 def runtime_token_data(token_id:, token_display:, value:, location:, state:) if token_id.equal?(NO_LOOKAHEAD) return { "state" => state, "token_id" => nil, "token" => nil, "value" => nil, "location" => nil }.freeze end { "state" => state, "token_id" => token_id, "token" => EventSanitizer.value(token_display), "value" => EventSanitizer.value(value), "location" => EventSanitizer.location(location) }.freeze end |
#selected_repair ⇒ RepairPlan, ...
2478 2479 2480 2481 2482 2483 2484 |
# File 'lib/ibex/runtime/parser.rb', line 2478 def selected_repair policy = @repair_policy return unless policy tokens, complete = repair_search_tokens(policy) RepairSearch.new(parser_tables, policy, tokens, complete: complete).search(@state_stack) end |
#shift(next_state) ⇒ Object
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 |
# File 'lib/ibex/runtime/parser.rb', line 1548 def shift(next_state) token_id = @lookahead value = @lookahead_value location = @lookahead_location from_state = @state_stack.last token_display = token_to_str(token_id) trace("shift #{token_display}#{trace_value_suffix(value, token_id)} -> state #{next_state}") if @yydebug event_observers = runtime_observer_snapshot if @runtime_observers event_data = if event_observers runtime_token_data( token_id: token_id, token_display: token_display, value: value, location: location, state: next_state ).merge("from_state" => @state_stack.last).freeze end ensure_stack_capacity! @state_stack << next_state push_location(location) @value_stack << value shift_green_token(token_id, value, location, from_state) @lookahead = NO_LOOKAHEAD @lookahead_location = nil @runtime_lookahead_token_display = nil @recovery_shifts -= 1 if @recovery_shifts.positive? on_shift(token_id, value, next_state) on_shift_location(token_id, value, next_state, location) emit_runtime_event(:shift, event_data, observers: event_observers) if event_data CONTINUE_OUTCOME end |
#shift_error_token ⇒ Boolean
2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 |
# File 'lib/ibex/runtime/parser.rb', line 2426 def shift_error_token popped = 0 @green_parse_memo_valid = false loop do action = table_lookup(parser_tables.fetch(:actions), @state_stack.last, ERROR_TOKEN) if action&.first == :shift trace("recover: shift error -> state #{action.fetch(1)}") if @yydebug ensure_stack_capacity! @state_stack << action.fetch(1) push_location(@lookahead_location) @value_stack << nil @green_builder&.absorb_into_error(popped) empty = [] #: Array[CST::ParseMemo::Entry] children = popped.zero? ? empty : @green_memo_stack.pop(popped) @green_memo_stack << CST::ParseMemo::Entry.new(@state_stack.last, children: children) return true end return false if @state_stack.length == 1 trace("recover: pop state #{@state_stack.last}") if @yydebug @state_stack.pop @value_stack.pop @location_stack&.pop popped += 1 end end |
#shift_green_token(token_id, value, location, from_state) ⇒ void
This method returns an undefined value.
rubocop:disable Metrics/PerceivedComplexity -- generated locations have a deliberate allocation-free path.
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 |
# File 'lib/ibex/runtime/parser.rb', line 1863 def shift_green_token(token_id, value, location, from_state) builder = @green_builder kinds = @green_kinds return unless builder && kinds if location.is_a?(Hash) && location.key?(:ibex_lexer_start_state) trailing = location[:cst_previous_trailing] || EMPTY_GREEN_TRIVIA location_leading = location[:leading_trivia] || EMPTY_GREEN_TRIVIA repair = location[:ibex_repair] token_text = location[:ibex_cst_text] token_text = value unless token_text.is_a?(String) token_text = "" unless token_text.is_a?(String) lexer_state = location[:ibex_lexer_start_state] lexer_state = lexer_state ? lexer_state.to_sym : :INITIAL else trailing = green_location_trivia(location, :cst_previous_trailing) location_leading = green_location_trivia(location, :leading_trivia) repair = cst_location_value(location, :ibex_repair) token_text = green_token_text(value, location) lexer_state = green_lexer_state(location) end commit_green_token_shift( builder, kinds, token_id, from_state, trailing, location_leading, repair, token_text, lexer_state ) end |
#start_push_session ⇒ void
This method returns an undefined value.
1149 1150 1151 1152 1153 1154 1155 1156 1157 |
# File 'lib/ibex/runtime/parser.rb', line 1149 def start_push_session if @push_status == :finished raise ParseError, "(input):1:1: push session is finished; call reset_push before supplying more input" end return if @push_status == :active prepare_parse(-> { raise ParseError, "(input):1:1: push session needs another token" }) @push_status = :active end |
#syntax_parse_result(value) ⇒ CST::ParseResult
2069 2070 2071 2072 2073 2074 |
# File 'lib/ibex/runtime/parser.rb', line 2069 def syntax_parse_result(value) root = @syntax_root raise ParseError, "(cst):1:1: parse_with_syntax requires a format-v6 CST parser" unless root CST::ParseResult.new(value: value, syntax_root: root, diagnostics: @syntax_diagnostics) end |
#syntax_root ⇒ CST::SyntaxNode?
Return the Red source-file root built by the most recent CST parse.
420 421 422 423 |
# File 'lib/ibex/runtime/parser.rb', line 420 def syntax_root ensure_runtime_initialized! @syntax_root end |
#syntax_token_states ⇒ Array[Symbol]
2077 2078 2079 |
# File 'lib/ibex/runtime/parser.rb', line 2077 def syntax_token_states @green_token_states.dup.freeze end |
#synthetic_repair_input(edit, value:, location:) ⇒ RepairInput
2582 2583 2584 2585 |
# File 'lib/ibex/runtime/parser.rb', line 2582 def synthetic_repair_input(edit, value:, location:) location = cst_repair_location(location, edit.kind) if cst_enabled? RepairInput.new(token_id: edit.token_id, token_name: edit.token_name, value: value, location: location) end |
#table_lookup(table, row, column) ⇒ Object
2996 2997 2998 2999 3000 |
# File 'lib/ibex/runtime/parser.rb', line 2996 def table_lookup(table, row, column) return table.lookup(row, column) if table.respond_to?(:lookup) table.fetch(row, EMPTY_ROW)[column] end |
#token_suggestions(actual, expected) ⇒ Array[String]
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 |
# File 'lib/ibex/runtime/parser.rb', line 2776 def token_suggestions(actual, expected) word = normalized_token_word(actual) return [] unless word threshold = word.length < 5 ? 1 : 2 expected.filter_map do |candidate| candidate_word = normalized_token_word(candidate) candidate if candidate_word && edit_distance(word, candidate_word) <= threshold end end |
#token_to_str(token_id) ⇒ String Also known as: __ibex_fast_path_token_to_str
Return a human-readable name for an internal token id.
590 591 592 593 594 |
# File 'lib/ibex/runtime/parser.rb', line 590 def token_to_str(token_id) return @unknown_token_name || token_id.to_s if token_id == @unknown_token_id parser_tables.fetch(:token_names).fetch(token_id, token_id.to_s) end |
#trace(message) ⇒ void
This method returns an undefined value.
3003 3004 3005 |
# File 'lib/ibex/runtime/parser.rb', line 3003 def trace() @yydebug_output.puts("ibex: #{}") if @yydebug end |
#trace_reduction(production_id, length, lhs, result, next_state) ⇒ void
This method returns an undefined value.
1633 1634 1635 1636 1637 |
# File 'lib/ibex/runtime/parser.rb', line 1633 def trace_reduction(production_id, length, lhs, result, next_state) return unless @yydebug trace("reduce #{production_id} (#{length})#{trace_value_suffix(result, lhs)} -> state #{next_state}") end |
#trace_value_printer=(printer) ⇒ ^(untyped) -> untyped?
Install an opt-in value formatter for human-readable yydebug traces.
574 575 576 577 578 579 580 581 |
# File 'lib/ibex/runtime/parser.rb', line 574 def trace_value_printer=(printer) ensure_runtime_initialized! unless printer.nil? || printer.respond_to?(:call) raise ArgumentError, "trace value printer must respond to call or be nil" end @trace_value_printer = printer end |
#trace_value_suffix(value, symbol_id) ⇒ String
3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 |
# File 'lib/ibex/runtime/parser.rb', line 3008 def trace_value_suffix(value, symbol_id) return "" unless @yydebug printer = @trace_value_printer rendered = if printer printer.call(value) else method_name = parser_tables.fetch(:value_printers, EMPTY_ROW)[symbol_id] return "" unless method_name __send__(method_name, value) end " value=#{rendered}" rescue StandardError => e " value=<printer error: #{e.class}>" end |
#track_locations?(tables) ⇒ Boolean
2816 2817 2818 |
# File 'lib/ibex/runtime/parser.rb', line 2816 def track_locations?(tables) tables.fetch(:uses_locations, false) || !@runtime_observers.nil? end |
#validate_composition_action_contract!(production, index, version) ⇒ void
This method returns an undefined value.
1366 1367 1368 1369 1370 1371 1372 1373 |
# File 'lib/ibex/runtime/parser.rb', line 1366 def validate_composition_action_contract!(production, index, version) return unless production[:composition_action] == true return if production[:location_action] == true && generated_action_symbol?(production[:action]) raise ParseError, "(tables):1:1: parser table format version #{version} production #{index} has an inconsistent " \ ":composition_action marker; a generated action Symbol with :location_action is required" end |
#validate_current_cst_tables!(tables, actual) ⇒ void
This method returns an undefined value.
1308 1309 1310 1311 1312 1313 1314 1315 1316 |
# File 'lib/ibex/runtime/parser.rb', line 1308 def validate_current_cst_tables!(tables, actual) cst = tables[:cst] return if cst.nil? || cst == false return if actual == PARSER_TABLE_FORMAT_VERSION && cst.is_a?(Hash) raise ParseError, "(tables):1:1: legacy CST parser tables are unsupported; " \ "regenerate the parser with the installed Ibex version" end |
#validate_generated_action_contracts!(tables, version) ⇒ void
This method returns an undefined value.
1357 1358 1359 1360 1361 1362 1363 |
# File 'lib/ibex/runtime/parser.rb', line 1357 def validate_generated_action_contracts!(tables, version) tables.fetch(:productions).each_with_index do |production, index| validate_composition_action_contract!(production, index, version) validate_values_action_contract!(production, index, version) validate_positional_action_contract!(production, index, version) end end |
#validate_parser_table_format! ⇒ Hash[Symbol, untyped]
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 |
# File 'lib/ibex/runtime/parser.rb', line 1283 def validate_parser_table_format! tables = parser_tables unless tables.key?(:format_version) raise ParseError, "(tables):1:1: parser tables for #{self.class} are missing :format_version; " \ "regenerate the parser with the installed Ibex version" end actual = tables.fetch(:format_version) unless SUPPORTED_PARSER_TABLE_FORMAT_VERSIONS.include?(actual) raise ParseError, "(tables):1:1: unsupported parser table format version #{actual.inspect} for #{self.class}; " \ "runtime supports #{SUPPORTED_PARSER_TABLE_FORMAT_VERSIONS.join(', ')}; " \ "regenerate the parser with the installed Ibex version" end validate_current_cst_tables!(tables, actual) if actual >= 3 && !generated_action_contracts_validated?(tables) validate_generated_action_contracts!(tables, actual) cache_generated_action_contracts!(tables) end tables end |
#validate_positional_action_contract!(production, index, version) ⇒ void
This method returns an undefined value.
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 |
# File 'lib/ibex/runtime/parser.rb', line 1396 def validate_positional_action_contract!(production, index, version) return if version < 5 || production[:positional_action] != true return if positional_action_contract?(production) raise ParseError, "(tables):1:1: parser table format version #{version} production #{index} has an inconsistent " \ ":positional_action marker; a generated action Symbol with zero to four RHS values and no other " \ "action ABI markers is required" end |
#validate_resource_limits!(resource_limits) ⇒ void
This method returns an undefined value.
764 765 766 767 768 |
# File 'lib/ibex/runtime/parser.rb', line 764 def validate_resource_limits!(resource_limits) return if resource_limits.is_a?(ResourceLimits) raise ArgumentError, "resource_limits must be an Ibex::Runtime::ResourceLimits" end |
#validate_values_action_contract!(production, index, version) ⇒ void
This method returns an undefined value.
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 |
# File 'lib/ibex/runtime/parser.rb', line 1376 def validate_values_action_contract!(production, index, version) borrowed = production[:borrowed_values_action] == true if borrowed && production[:values_action] != true raise ParseError, "(tables):1:1: parser table format version #{version} production #{index} has an inconsistent " \ ":borrowed_values_action marker; :values_action is required" end return if version < 4 || production[:values_action] != true return if generated_action_symbol?(production[:action]) && production[:location_action] != true && production[:composition_action] != true && production.fetch(:location_context_length, 0).zero? raise ParseError, "(tables):1:1: parser table format version #{version} production #{index} has an inconsistent " \ ":values_action marker; a generated action Symbol without location, composition, or context " \ "markers is required" end |
#values_reduction_value(production, action, values, locations, location) ⇒ Object
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 |
# File 'lib/ibex/runtime/parser.rb', line 1726 def values_reduction_value(production, action, values, locations, location) previous_locations = @semantic_locations previous_names = @semantic_location_names previous_result_location = @semantic_result_location @semantic_locations = locations @semantic_location_names = production.fetch(:location_names, EMPTY_LOCATION_NAMES) @semantic_result_location = location __send__(action, values) ensure @semantic_locations = previous_locations @semantic_location_names = previous_names @semantic_result_location = previous_result_location end |
#with_syntax_only(cache) { ... } ⇒ Object
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 |
# File 'lib/ibex/runtime/parser.rb', line 2109 def with_syntax_only(cache) previous_mode = @syntax_only previous_cache = @green_cache_override @syntax_only = true @green_cache_override = cache @runtime_fast_path = false yield ensure @syntax_only = previous_mode @green_cache_override = previous_cache end |
#yyaccept ⇒ nil
Accept immediately after the current semantic action completes.
613 614 615 616 617 |
# File 'lib/ibex/runtime/parser.rb', line 613 def yyaccept @runtime_fast_path = false @accept_requested = true nil end |
#yydebug ⇒ Boolean
341 342 343 344 |
# File 'lib/ibex/runtime/parser.rb', line 341 def yydebug ensure_runtime_initialized! @yydebug end |
#yydebug=(enabled) ⇒ Boolean
347 348 349 350 351 |
# File 'lib/ibex/runtime/parser.rb', line 347 def yydebug=(enabled) ensure_runtime_initialized! @runtime_fast_path = false @yydebug = enabled end |
#yydebug_output=(output) ⇒ IO
354 355 356 357 |
# File 'lib/ibex/runtime/parser.rb', line 354 def yydebug_output=(output) ensure_runtime_initialized! @yydebug_output = output end |
#yyerrok ⇒ nil
Leave error recovery immediately.
606 607 608 609 |
# File 'lib/ibex/runtime/parser.rb', line 606 def yyerrok @recovery_shifts = 0 nil end |
#yyerror ⇒ nil
Enter error recovery from a semantic action without calling on_error.
598 599 600 601 602 |
# File 'lib/ibex/runtime/parser.rb', line 598 def yyerror @runtime_fast_path = false @semantic_error = true nil end |
#yyparse(receiver, method_id) ⇒ Object
Parse tokens yielded by receiver.method_id.
427 428 429 430 431 432 |
# File 'lib/ibex/runtime/parser.rb', line 427 def yyparse(receiver, method_id) stream = Enumerator.new do |tokens| receiver.__send__(method_id) { |token| tokens << token } end drive_parser(-> { stream.next }) end |