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,
sig/ibex/runtime/syntax_session.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.
-
.syntax_execution_profile ⇒ Symbol
Report the trust boundary of syntax-only operations for this loaded parser class.
-
.syntax_session(source, execution_profile: nil, resource_limits: nil, limits: nil, cancellation: nil, blender: true) ⇒ SyntaxSession
Open a generated-language syntax session backed by the existing incremental Red/Green CST engine.
Instance Method Summary collapse
- #accept_reduction(result) ⇒ [ :accepted, runtime_value ]
- #action_for_current_state ⇒ runtime_value
- #actionless_reduction_value(_production_id, _production, values, _locations, _location) ⇒ runtime_value
- #append_repair_edits(output, input, edits) ⇒ Boolean
- #apply_repair(plan) ⇒ void
- #assign_repair_input(input) ⇒ void
- #begin_recovery(report) ⇒ runtime_value
- #build_reduce_event_data(observers, production_id, production, length, pre_state, post_state, next_state, result, location) ⇒ Hash[String, runtime_value]?
- #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 ⇒ runtime_value
- #cst_enabled? ⇒ Boolean
- #cst_lexical_failure(error) ⇒ nil
- #cst_location_value(location, key) ⇒ runtime_value
- #cst_repair_location(location, kind) ⇒ Hash[Symbol, runtime_value]
- #current_repair_input ⇒ RepairInput
- #default_action(state) ⇒ runtime_value
- #discard_green_lookahead ⇒ void
-
#do_parse ⇒ runtime_value
Pull tokens from
next_tokenand execute parser production actions. -
#drive_compact_fast_parser(tables) ⇒ runtime_value
Generated compact tables can keep the unobserved, location-free pull loop on their frozen displacement arrays.
- #drive_parser(source, initial_state: nil) ⇒ runtime_value
- #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) ⇒ runtime_value
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) ⇒ runtime_value
- #finalize_cst(value) ⇒ runtime_value
- #finalize_failed_green_cst ⇒ void
- #finalize_lexical_green_cst(error) ⇒ void
- #finalize_red_green_cst(value) ⇒ runtime_value
-
#finish(location: nil) ⇒ runtime_value
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, runtime_value]
-
#loc(reference) ⇒ runtime_value
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 ⇒ [ runtime_value, runtime_value ], ...
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) ⇒ runtime_value
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, runtime_value]
- #pending_green_trivia ⇒ Array[CST::GreenTrivia]
- #perform(action) ⇒ runtime_value
- #pop_reduction_locations(length) ⇒ Array[runtime_value]
- #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, runtime_value]?, Array[Proc]? ]
-
#push(token, value = nil, location = nil) ⇒ runtime_value
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 ⇒ runtime_value
- #read_lookahead ⇒ void
- #read_repair_lookahead ⇒ void
- #recover(report: true) ⇒ runtime_value
- #recovery_context(report) ⇒ Hash[Symbol, runtime_value]
- #reduce(production_id, prefetched_production = nil) ⇒ runtime_value
- #reduce_green(production_id, production, length, left_state) ⇒ void
- #reduction_value(production_id, production, values, locations, location) ⇒ runtime_value
-
#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 ⇒ runtime_value
Return the synthesized span of the reduction being evaluated.
- #run_push_driver { ... } ⇒ runtime_value
- #run_push_lookahead ⇒ runtime_value
-
#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, runtime_value]
- #runtime_discard_data(token_display) ⇒ Hash[String, runtime_value]
- #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, runtime_value]
- #runtime_reduce_data(production_id, production, length, pre_state, post_state, next_state, result, location) ⇒ Hash[String, runtime_value]
- #runtime_token_data(token_id:, token_display:, value:, location:, state:) ⇒ Hash[String, runtime_value]
- #selected_repair ⇒ RepairPlan, ...
- #shift(next_state) ⇒ runtime_value
- #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) ⇒ runtime_value
- #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) ⇒ ^(runtime_value) -> runtime_value?
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) ⇒ void
- #validate_current_cst_tables!(tables) ⇒ void
- #validate_generated_action_contracts!(tables) ⇒ void
- #validate_parser_table_format! ⇒ Hash[Symbol, runtime_value]
- #validate_positional_action_contract!(production, index) ⇒ void
- #validate_resource_limits!(resource_limits) ⇒ void
- #validate_values_action_contract!(production, index) ⇒ void
- #values_reduction_value(production, action, values, locations, location) ⇒ runtime_value
- #with_syntax_only(cache) { ... } ⇒ runtime_value
-
#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) ⇒ runtime_value
Parse tokens yielded by
receiver.method_idand execute parser production actions.
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.
377 378 379 380 |
# File 'lib/ibex/runtime/parser.rb', line 377 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)
331 332 333 |
# File 'lib/ibex/runtime/parser.rb', line 331 def incremental_reused_descendants @incremental_reused_descendants end |
#syntax_parse_memo ⇒ CST::ParseMemo? (readonly)
330 331 332 |
# File 'lib/ibex/runtime/parser.rb', line 330 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. Parser production actions are suppressed, but generated lexer actions still execute. The generated class is trusted application code, not a sandbox.
370 371 372 373 374 |
# File 'lib/ibex/runtime/parser.rb', line 370 def self.incremental_session(source_text, resource_limits: nil, blender: true) CST::IncrementalParseSession.new( self, source_text, resource_limits: resource_limits, blender: blender ) end |
.syntax_execution_profile ⇒ Symbol
Report the trust boundary of syntax-only operations for this loaded parser class. Current generated artifacts can contain user sections and Ruby lexer actions, so they are always trusted application code.
337 338 339 |
# File 'lib/ibex/runtime/parser.rb', line 337 def self.syntax_execution_profile :trusted_application_code end |
.syntax_session(source, execution_profile: nil, resource_limits: nil, limits: nil, cancellation: nil, blender: true) ⇒ SyntaxSession
Open a generated-language syntax session backed by the existing incremental Red/Green CST engine. The execution profile must be passed explicitly because generated lexer actions execute in this path.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/ibex/runtime/parser.rb', line 345 def self.syntax_session( source, execution_profile: nil, resource_limits: nil, limits: nil, cancellation: nil, blender: true ) SyntaxSession.new( self, source, execution_profile: execution_profile, resource_limits: resource_limits, limits: limits, cancellation: cancellation, blender: blender ) end |
Instance Method Details
#accept_reduction(result) ⇒ [ :accepted, runtime_value ]
1728 1729 1730 1731 1732 |
# File 'lib/ibex/runtime/parser.rb', line 1728 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 ⇒ runtime_value
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 |
# File 'lib/ibex/runtime/parser.rb', line 1478 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) ⇒ runtime_value
1801 1802 1803 |
# File 'lib/ibex/runtime/parser.rb', line 1801 def actionless_reduction_value(_production_id, _production, values, _locations, _location) values.first end |
#append_repair_edits(output, input, edits) ⇒ Boolean
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 |
# File 'lib/ibex/runtime/parser.rb', line 2627 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.
2584 2585 2586 2587 2588 2589 2590 |
# File 'lib/ibex/runtime/parser.rb', line 2584 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.
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 |
# File 'lib/ibex/runtime/parser.rb', line 2803 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) ⇒ runtime_value
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 |
# File 'lib/ibex/runtime/parser.rb', line 2292 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, runtime_value]?
1701 1702 1703 1704 1705 1706 1707 1708 1709 |
# File 'lib/ibex/runtime/parser.rb', line 1701 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.
1385 1386 1387 1388 1389 1390 1391 1392 1393 |
# File 'lib/ibex/runtime/parser.rb', line 1385 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?
3011 3012 3013 3014 3015 3016 3017 3018 3019 |
# File 'lib/ibex/runtime/parser.rb', line 3011 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.
1840 1841 1842 1843 1844 1845 1846 1847 |
# File 'lib/ibex/runtime/parser.rb', line 1840 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.
2663 2664 2665 2666 2667 2668 |
# File 'lib/ibex/runtime/parser.rb', line 2663 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.
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 |
# File 'lib/ibex/runtime/parser.rb', line 1957 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 ]
2344 2345 2346 2347 2348 |
# File 'lib/ibex/runtime/parser.rb', line 2344 def commit_repair(repair) on_repair(repair) apply_repair(repair) CONTINUE_OUTCOME end |
#compact_fast_driver_eligible?(tables) ⇒ Boolean
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 |
# File 'lib/ibex/runtime/parser.rb', line 1189 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? ]
2856 2857 2858 2859 2860 2861 2862 |
# File 'lib/ibex/runtime/parser.rb', line 2856 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.
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 |
# File 'lib/ibex/runtime/parser.rb', line 2532 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 ⇒ runtime_value
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 |
# File 'lib/ibex/runtime/parser.rb', line 2254 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
1835 1836 1837 |
# File 'lib/ibex/runtime/parser.rb', line 1835 def cst_enabled? parser_tables[:cst].is_a?(Hash) end |
#cst_lexical_failure(error) ⇒ nil
1863 1864 1865 1866 1867 |
# File 'lib/ibex/runtime/parser.rb', line 1863 def cst_lexical_failure(error) @syntax_diagnostics << error finalize_lexical_green_cst(error) nil end |
#cst_location_value(location, key) ⇒ runtime_value
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 |
# File 'lib/ibex/runtime/parser.rb', line 1870 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, runtime_value]
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 |
# File 'lib/ibex/runtime/parser.rb', line 2677 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
2593 2594 2595 2596 2597 2598 2599 2600 |
# File 'lib/ibex/runtime/parser.rb', line 2593 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) ⇒ runtime_value
2900 2901 2902 |
# File 'lib/ibex/runtime/parser.rb', line 2900 def default_action(state) parser_tables.fetch(:default_actions, EMPTY_ROW)[state] end |
#discard_green_lookahead ⇒ void
This method returns an undefined value.
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 |
# File 'lib/ibex/runtime/parser.rb', line 2224 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 ⇒ runtime_value
Pull tokens from next_token and execute parser production actions.
A generated-lexer next_token executes lexer actions; a handwritten
implementation does not invoke the generated lexer.
452 453 454 |
# File 'lib/ibex/runtime/parser.rb', line 452 def do_parse drive_parser(nil) end |
#drive_compact_fast_parser(tables) ⇒ runtime_value
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
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 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 |
# File 'lib/ibex/runtime/parser.rb', line 949 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) ⇒ runtime_value
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 |
# File 'lib/ibex/runtime/parser.rb', line 905 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
2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 |
# File 'lib/ibex/runtime/parser.rb', line 2883 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.
2472 2473 2474 2475 2476 2477 |
# File 'lib/ibex/runtime/parser.rb', line 2472 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.
2219 2220 2221 |
# File 'lib/ibex/runtime/parser.rb', line 2219 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.
2480 2481 2482 2483 2484 2485 2486 |
# File 'lib/ibex/runtime/parser.rb', line 2480 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.
2793 2794 2795 2796 2797 2798 2799 2800 |
# File 'lib/ibex/runtime/parser.rb', line 2793 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.
1254 1255 1256 1257 1258 |
# File 'lib/ibex/runtime/parser.rb', line 1254 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.
739 740 741 742 743 744 745 746 747 748 |
# File 'lib/ibex/runtime/parser.rb', line 739 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.
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 |
# File 'lib/ibex/runtime/parser.rb', line 2520 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
2851 2852 2853 |
# File 'lib/ibex/runtime/parser.rb', line 2851 def error_action?(action) action.nil? || action.first == :error end |
#exact_lookahead_accepted?(token_id) ⇒ Boolean
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
# File 'lib/ibex/runtime/parser.rb', line 833 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
855 856 857 858 859 860 861 862 863 |
# File 'lib/ibex/runtime/parser.rb', line 855 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
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 |
# File 'lib/ibex/runtime/parser.rb', line 878 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.
671 672 673 674 675 676 677 678 679 680 681 |
# File 'lib/ibex/runtime/parser.rb', line 671 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.
686 687 688 689 690 691 692 693 694 695 |
# File 'lib/ibex/runtime/parser.rb', line 686 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
1857 1858 1859 1860 |
# File 'lib/ibex/runtime/parser.rb', line 1857 def failed_cst finalize_failed_green_cst nil end |
#fallback_recovery(context, token_data, recovery_observers) ⇒ runtime_value
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity Green restoration mirrors the existing state/value/location transactional fallback.
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 |
# File 'lib/ibex/runtime/parser.rb', line 2354 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 ]
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 |
# File 'lib/ibex/runtime/parser.rb', line 1552 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
1543 1544 1545 1546 1547 1548 1549 |
# File 'lib/ibex/runtime/parser.rb', line 1543 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 ]
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 |
# File 'lib/ibex/runtime/parser.rb', line 1530 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) ⇒ runtime_value
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 |
# File 'lib/ibex/runtime/parser.rb', line 1574 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) ⇒ runtime_value
1850 1851 1852 1853 1854 |
# File 'lib/ibex/runtime/parser.rb', line 1850 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.
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 |
# File 'lib/ibex/runtime/parser.rb', line 2058 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.
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 |
# File 'lib/ibex/runtime/parser.rb', line 2081 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) ⇒ runtime_value
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 |
# File 'lib/ibex/runtime/parser.rb', line 2030 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) ⇒ runtime_value
Supply EOF to a caller-driven parser session and return its result. Committed reductions execute parser production actions; this token-fed path does not invoke generated lexer actions.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/ibex/runtime/parser.rb', line 511 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.
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 |
# File 'lib/ibex/runtime/parser.rb', line 1269 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 ]
2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 |
# File 'lib/ibex/runtime/parser.rb', line 2414 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.
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 |
# File 'lib/ibex/runtime/parser.rb', line 1399 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.
1377 1378 1379 1380 1381 1382 |
# File 'lib/ibex/runtime/parser.rb', line 1377 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
1830 1831 1832 |
# File 'lib/ibex/runtime/parser.rb', line 1830 def generated_action_symbol?(action) action.is_a?(Symbol) && action.name.match?(GENERATED_ACTION_NAME) end |
#generated_composition_action?(production, action) ⇒ Boolean
1824 1825 1826 1827 |
# File 'lib/ibex/runtime/parser.rb', line 1824 def generated_composition_action?(production, action) production[:composition_action] == true && generated_location_action?(production, action) end |
#generated_location_action?(production, action) ⇒ Boolean
1806 1807 1808 1809 |
# File 'lib/ibex/runtime/parser.rb', line 1806 def generated_location_action?(production, action) production[:location_action] == true && generated_action_symbol?(action) end |
#generated_positional_action?(production, action) ⇒ Boolean
1818 1819 1820 1821 |
# File 'lib/ibex/runtime/parser.rb', line 1818 def generated_positional_action?(production, action) production[:positional_action] == true && generated_action_symbol?(action) end |
#generated_values_action?(production, action) ⇒ Boolean
1812 1813 1814 1815 |
# File 'lib/ibex/runtime/parser.rb', line 1812 def generated_values_action?(production, action) production[:values_action] == true && generated_action_symbol?(action) end |
#green_lexer_state(location) ⇒ Symbol
2162 2163 2164 2165 2166 2167 2168 2169 |
# File 'lib/ibex/runtime/parser.rb', line 2162 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]
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 |
# File 'lib/ibex/runtime/parser.rb', line 2016 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
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 |
# File 'lib/ibex/runtime/parser.rb', line 2003 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.
2910 2911 2912 2913 2914 2915 2916 2917 |
# File 'lib/ibex/runtime/parser.rb', line 2910 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.
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
# File 'lib/ibex/runtime/parser.rb', line 753 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) unless preserve_existing && defined?(@repair_search_results) @repair_search_results = [] #: Array[RepairSearchResult] end @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 = EMPTY_LOCATIONS unless preserve_existing && defined?(@syntax_diagnostics) @green_pending_skipped = EMPTY_GREEN_TRIVIA 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 = EMPTY_LOCATIONS unless preserve_existing && defined?(@green_token_states) @green_memo_stack = EMPTY_LOCATIONS 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.
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 |
# File 'lib/ibex/runtime/parser.rb', line 2145 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.
2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 |
# File 'lib/ibex/runtime/parser.rb', line 2949 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 # Keep the same module object out of both a class and its superclass # callback chains; TruffleRuby can re-enter `super` in that topology. singleton.prepend(FastPathClassMutationTracker.dup) 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.
2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 |
# File 'lib/ibex/runtime/parser.rb', line 2920 def install_runtime_fast_path_tracker!(singleton = nil) if singleton prepend_lookup = Module.instance_method(:prepend) elsif RUBY_ENGINE == "ruby" thread = ::Thread.current singleton_lookup = thread.thread_variable_get(:__ibex_runtime_parser_singleton_class_lookup) unless singleton_lookup.is_a?(UnboundMethod) singleton_lookup = Object.instance_method(:singleton_class) thread.thread_variable_set(:__ibex_runtime_parser_singleton_class_lookup, singleton_lookup) end singleton = singleton_lookup.bind_call(self) prepend_lookup = thread.thread_variable_get(:__ibex_runtime_parser_module_prepend_lookup) unless prepend_lookup.is_a?(UnboundMethod) prepend_lookup = Module.instance_method(:prepend) thread.thread_variable_set(:__ibex_runtime_parser_module_prepend_lookup, prepend_lookup) end else singleton = Object.instance_method(:singleton_class).bind_call(self) prepend_lookup = Module.instance_method(:prepend) end prepend_lookup.bind_call(singleton, FastPathMutationTracker) @runtime_fast_path_tracker_installed = true @runtime_fast_path_hooks_mutated = false @runtime_fast_path_singleton_ancestors = nil rescue FrozenError, TypeError @runtime_fast_path = false end |
#install_syntax_root(green, kinds, file: nil) ⇒ void
This method returns an undefined value.
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 |
# File 'lib/ibex/runtime/parser.rb', line 2112 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.
826 827 828 829 830 |
# File 'lib/ibex/runtime/parser.rb', line 826 def install_value_stack(values) @value_stack = values @vstack = values @racc_vstack = values end |
#internal_token_id(external_token) ⇒ Integer
2827 2828 2829 2830 2831 2832 2833 |
# File 'lib/ibex/runtime/parser.rb', line 2827 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, runtime_value]
2844 2845 2846 2847 2848 |
# File 'lib/ibex/runtime/parser.rb', line 2844 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) ⇒ runtime_value
Return the location of a one-based RHS position or named reference while a semantic action is running.
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/ibex/runtime/parser.rb', line 700 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.
2740 2741 2742 2743 2744 2745 2746 2747 |
# File 'lib/ibex/runtime/parser.rb', line 2740 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
2750 2751 2752 2753 2754 2755 |
# File 'lib/ibex/runtime/parser.rb', line 2750 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 ⇒ [ runtime_value, runtime_value ], ...
Override in pull parsers. Return [token, value],
[token, value, location], false, or nil.
559 560 561 |
# File 'lib/ibex/runtime/parser.rb', line 559 def next_token raise NotImplementedError, "(input):1:1: next_token must be implemented" end |
#normalized_token_word(token_name) ⇒ String?
2877 2878 2879 2880 |
# File 'lib/ibex/runtime/parser.rb', line 2877 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.
2334 2335 2336 2337 2338 2339 2340 2341 |
# File 'lib/ibex/runtime/parser.rb', line 2334 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.
620 |
# File 'lib/ibex/runtime/parser.rb', line 620 def on_discard(_token_id, _value, _location, _reason); end |
#on_error(token_id, value, _value_stack) ⇒ runtime_value
Override to recover from syntax errors. The default raises unless a bounded automatic repair has already been selected.
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
# File 'lib/ibex/runtime/parser.rb', line 566 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.
611 |
# File 'lib/ibex/runtime/parser.rb', line 611 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.
616 |
# File 'lib/ibex/runtime/parser.rb', line 616 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.
601 |
# File 'lib/ibex/runtime/parser.rb', line 601 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.
606 |
# File 'lib/ibex/runtime/parser.rb', line 606 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.
636 |
# File 'lib/ibex/runtime/parser.rb', line 636 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.
591 |
# File 'lib/ibex/runtime/parser.rb', line 591 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.
596 |
# File 'lib/ibex/runtime/parser.rb', line 596 def on_shift_location(_token_id, _value, _state, _location); end |
#parse_syntax_token_source(source, cache) ⇒ CST::SyntaxResult
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 |
# File 'lib/ibex/runtime/parser.rb', line 2185 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.
Parser production actions execute. Generated lexer actions execute only
when next_token is supplied by the generated lexer.
460 461 462 |
# File 'lib/ibex/runtime/parser.rb', line 460 def parse_with_syntax syntax_parse_result(do_parse) end |
#parser_state_count(tables) ⇒ Integer
866 867 868 869 870 871 872 873 874 875 |
# File 'lib/ibex/runtime/parser.rb', line 866 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, runtime_value]
2836 2837 2838 2839 2840 2841 |
# File 'lib/ibex/runtime/parser.rb', line 2836 def parser_tables tables = @runtime_parser_tables return tables if tables load_parser_tables end |
#pending_green_trivia ⇒ Array[CST::GreenTrivia]
2105 2106 2107 2108 2109 |
# File 'lib/ibex/runtime/parser.rb', line 2105 def pending_green_trivia return [] unless respond_to?(:take_cst_pending_green_trivia, true) __send__(:take_cst_pending_green_trivia) end |
#perform(action) ⇒ runtime_value
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 |
# File 'lib/ibex/runtime/parser.rb', line 1501 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[runtime_value]
3104 3105 3106 3107 3108 3109 3110 3111 |
# File 'lib/ibex/runtime/parser.rb', line 3104 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
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 |
# File 'lib/ibex/runtime/parser.rb', line 1466 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.
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 |
# File 'lib/ibex/runtime/parser.rb', line 1884 def prepare_green_cst(tables) # rubocop:disable Metrics/MethodLength @syntax_root = nil @syntax_parse_memo = nil config = tables[:cst] unless config.is_a?(Hash) @green_builder = nil @green_kinds = nil @green_cache = nil @syntax_diagnostics = EMPTY_LOCATIONS @green_pending_skipped = EMPTY_GREEN_TRIVIA @green_token_states = EMPTY_LOCATIONS @green_memo_stack = EMPTY_LOCATIONS return end @syntax_diagnostics = [] if @syntax_diagnostics.frozen? @green_pending_skipped = [] if @green_pending_skipped.frozen? @green_token_states = [] if @green_token_states.frozen? @green_memo_stack = [] if @green_memo_stack.frozen? @syntax_diagnostics.clear @green_pending_skipped.clear 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.
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 |
# File 'lib/ibex/runtime/parser.rb', line 1282 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.
2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 |
# File 'lib/ibex/runtime/parser.rb', line 2642 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, runtime_value]?, Array[Proc]? ]
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 |
# File 'lib/ibex/runtime/parser.rb', line 2320 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) ⇒ runtime_value
Supply one token to a caller-driven parser session.
Committed reductions execute parser production actions; this token-fed
path does not invoke generated lexer actions.
Returns :need_more after consuming it, [:accepted, result] after
acceptance, or [:rejected, result] after recovery terminates.
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/ibex/runtime/parser.rb', line 488 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.
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 |
# File 'lib/ibex/runtime/parser.rb', line 3092 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.
1685 1686 1687 1688 1689 |
# File 'lib/ibex/runtime/parser.rb', line 1685 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.
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 |
# File 'lib/ibex/runtime/parser.rb', line 2198 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.
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 |
# File 'lib/ibex/runtime/parser.rb', line 2715 def read_compatible_lookahead token = read_external_token refresh_runtime_fast_path_after_user_code! if defined?(CST::ReusableSubtree) && 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 ⇒ runtime_value
2816 2817 2818 2819 2820 2821 2822 2823 2824 |
# File 'lib/ibex/runtime/parser.rb', line 2816 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.
2697 2698 2699 2700 2701 |
# File 'lib/ibex/runtime/parser.rb', line 2697 def read_lookahead return read_repair_lookahead if @repair_policy read_compatible_lookahead end |
#read_repair_lookahead ⇒ void
This method returns an undefined value.
2704 2705 2706 2707 2708 2709 2710 2711 2712 |
# File 'lib/ibex/runtime/parser.rb', line 2704 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) ⇒ runtime_value
2244 2245 2246 2247 2248 2249 2250 2251 |
# File 'lib/ibex/runtime/parser.rb', line 2244 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, runtime_value]
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 |
# File 'lib/ibex/runtime/parser.rb', line 2307 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) ⇒ runtime_value
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 |
# File 'lib/ibex/runtime/parser.rb', line 1640 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.
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 |
# File 'lib/ibex/runtime/parser.rb', line 1985 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) ⇒ runtime_value
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 |
# File 'lib/ibex/runtime/parser.rb', line 1736 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.
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 |
# File 'lib/ibex/runtime/parser.rb', line 3076 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 ]
2271 2272 2273 2274 2275 2276 2277 2278 |
# File 'lib/ibex/runtime/parser.rb', line 2271 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 ]
2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 |
# File 'lib/ibex/runtime/parser.rb', line 2397 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.
1261 1262 1263 1264 1265 1266 |
# File 'lib/ibex/runtime/parser.rb', line 1261 def release_driver @runtime_observation_mutex.synchronize do @driver_status = :idle @runtime_driver_thread = nil end end |
#repair_input(external_token, value, location) ⇒ RepairInput
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 |
# File 'lib/ibex/runtime/parser.rb', line 2758 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
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 |
# File 'lib/ibex/runtime/parser.rb', line 2775 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?
402 403 404 405 |
# File 'lib/ibex/runtime/parser.rb', line 402 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.
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/ibex/runtime/parser.rb', line 416 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 ]
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 |
# File 'lib/ibex/runtime/parser.rb', line 2566 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
2692 2693 2694 |
# File 'lib/ibex/runtime/parser.rb', line 2692 def repair_trace(plan) plan.edits.map { |edit| "#{edit.kind}(#{edit.token_name}@#{edit.position})" }.join(", ") end |
#replay_repair_edits(source, edits) ⇒ Array[RepairInput]
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 |
# File 'lib/ibex/runtime/parser.rb', line 2613 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.
1920 1921 1922 |
# File 'lib/ibex/runtime/parser.rb', line 1920 def reset_cst_results(tables) prepare_green_cst(tables) end |
#reset_parse_recovery_state ⇒ void
This method returns an undefined value.
1318 1319 1320 1321 1322 1323 1324 1325 |
# File 'lib/ibex/runtime/parser.rb', line 1318 def reset_parse_recovery_state @recovery_shifts = 0 @semantic_error = false @repair_input_buffer = @repair_policy ? [] : nil @repair_selected = false @repair_search_results = [] #: Array[RepairSearchResult] clear_sync_recovery end |
#reset_push ⇒ nil
Discard a caller-driven session so this parser can accept a new one.
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/ibex/runtime/parser.rb', line 535 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
1328 1329 1330 1331 1332 1333 1334 |
# File 'lib/ibex/runtime/parser.rb', line 1328 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
408 409 410 411 |
# File 'lib/ibex/runtime/parser.rb', line 408 def resource_limits ensure_runtime_initialized! @resource_limits end |
#resource_limits=(limits) ⇒ ResourceLimits
Replace the limits used by future sessions.
434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/ibex/runtime/parser.rb', line 434 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 ⇒ runtime_value
Return the synthesized span of the reduction being evaluated.
719 720 721 722 723 724 725 |
# File 'lib/ibex/runtime/parser.rb', line 719 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 { ... } ⇒ runtime_value
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 |
# File 'lib/ibex/runtime/parser.rb', line 1236 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 ⇒ runtime_value
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 |
# File 'lib/ibex/runtime/parser.rb', line 1213 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.
3066 3067 3068 3069 3070 |
# File 'lib/ibex/runtime/parser.rb', line 3066 def runtime_core_method(lookup, name) lookup.bind_call(self, name) rescue NameError nil end |
#runtime_current_token_data ⇒ Hash[String, runtime_value]
2461 2462 2463 2464 2465 2466 2467 2468 2469 |
# File 'lib/ibex/runtime/parser.rb', line 2461 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, runtime_value]
2281 2282 2283 2284 2285 2286 2287 2288 2289 |
# File 'lib/ibex/runtime/parser.rb', line 2281 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
3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 |
# File 'lib/ibex/runtime/parser.rb', line 3022 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.
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 |
# File 'lib/ibex/runtime/parser.rb', line 2966 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
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 |
# File 'lib/ibex/runtime/parser.rb', line 2978 def runtime_fast_path_hooks_eligible? # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity cached = cached_runtime_fast_path_hooks_eligibility return cached unless cached.nil? lookup = Object.instance_method(:method) if RUBY_ENGINE == "ruby" thread = ::Thread.current cached_lookup = thread.thread_variable_get(:__ibex_runtime_parser_object_method_lookup) if cached_lookup.is_a?(UnboundMethod) lookup = cached_lookup else thread.thread_variable_set(:__ibex_runtime_parser_object_method_lookup, lookup) end end 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
3044 3045 3046 3047 3048 3049 3050 |
# File 'lib/ibex/runtime/parser.rb', line 3044 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.
3055 3056 3057 3058 3059 3060 3061 |
# File 'lib/ibex/runtime/parser.rb', line 3055 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, runtime_value]
2384 2385 2386 2387 2388 2389 2390 2391 2392 |
# File 'lib/ibex/runtime/parser.rb', line 2384 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, runtime_value]
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 |
# File 'lib/ibex/runtime/parser.rb', line 1714 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, runtime_value]
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 |
# File 'lib/ibex/runtime/parser.rb', line 2440 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, ...
2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 |
# File 'lib/ibex/runtime/parser.rb', line 2544 def selected_repair policy = @repair_policy return unless policy tokens, complete = repair_search_tokens(policy) result = RepairSearch.new(parser_tables, policy, tokens, complete: complete).search_result(@state_stack) @repair_search_results << result return result.plan if result.selected? return RepairSearch::NEED_INPUT if result.status == :need_input nil end |
#shift(next_state) ⇒ runtime_value
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 |
# File 'lib/ibex/runtime/parser.rb', line 1607 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
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 |
# File 'lib/ibex/runtime/parser.rb', line 2489 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 builder = @green_builder if builder 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) end 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.
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 |
# File 'lib/ibex/runtime/parser.rb', line 1926 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.
1202 1203 1204 1205 1206 1207 1208 1209 1210 |
# File 'lib/ibex/runtime/parser.rb', line 1202 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
2132 2133 2134 2135 2136 2137 |
# File 'lib/ibex/runtime/parser.rb', line 2132 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.
466 467 468 469 |
# File 'lib/ibex/runtime/parser.rb', line 466 def syntax_root ensure_runtime_initialized! @syntax_root end |
#syntax_token_states ⇒ Array[Symbol]
2140 2141 2142 |
# File 'lib/ibex/runtime/parser.rb', line 2140 def syntax_token_states @green_token_states.dup.freeze end |
#synthetic_repair_input(edit, value:, location:) ⇒ RepairInput
2671 2672 2673 2674 |
# File 'lib/ibex/runtime/parser.rb', line 2671 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) ⇒ runtime_value
3114 3115 3116 3117 3118 |
# File 'lib/ibex/runtime/parser.rb', line 3114 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]
2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 |
# File 'lib/ibex/runtime/parser.rb', line 2865 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.
640 641 642 643 644 |
# File 'lib/ibex/runtime/parser.rb', line 640 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.
3121 3122 3123 |
# File 'lib/ibex/runtime/parser.rb', line 3121 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.
1692 1693 1694 1695 1696 |
# File 'lib/ibex/runtime/parser.rb', line 1692 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) ⇒ ^(runtime_value) -> runtime_value?
Install an opt-in value formatter for human-readable yydebug traces.
624 625 626 627 628 629 630 631 |
# File 'lib/ibex/runtime/parser.rb', line 624 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
3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 |
# File 'lib/ibex/runtime/parser.rb', line 3126 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
2905 2906 2907 |
# File 'lib/ibex/runtime/parser.rb', line 2905 def track_locations?(tables) tables.fetch(:uses_locations, false) || !@runtime_observers.nil? end |
#validate_composition_action_contract!(production, index) ⇒ void
This method returns an undefined value.
1420 1421 1422 1423 1424 1425 1426 1427 1428 |
# File 'lib/ibex/runtime/parser.rb', line 1420 def validate_composition_action_contract!(production, index) 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 #{PARSER_TABLE_FORMAT_VERSION} " \ "production #{index} has an inconsistent " \ ":composition_action marker; a generated action Symbol with :location_action is required" end |
#validate_current_cst_tables!(tables) ⇒ void
This method returns an undefined value.
1362 1363 1364 1365 1366 1367 1368 1369 1370 |
# File 'lib/ibex/runtime/parser.rb', line 1362 def validate_current_cst_tables!(tables) cst = tables[:cst] return if cst.nil? || cst == false return if 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) ⇒ void
This method returns an undefined value.
1411 1412 1413 1414 1415 1416 1417 |
# File 'lib/ibex/runtime/parser.rb', line 1411 def validate_generated_action_contracts!(tables) tables.fetch(:productions).each_with_index do |production, index| validate_composition_action_contract!(production, index) validate_values_action_contract!(production, index) validate_positional_action_contract!(production, index) end end |
#validate_parser_table_format! ⇒ Hash[Symbol, runtime_value]
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 |
# File 'lib/ibex/runtime/parser.rb', line 1337 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) unless generated_action_contracts_validated?(tables) validate_generated_action_contracts!(tables) cache_generated_action_contracts!(tables) end tables end |
#validate_positional_action_contract!(production, index) ⇒ void
This method returns an undefined value.
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 |
# File 'lib/ibex/runtime/parser.rb', line 1453 def validate_positional_action_contract!(production, index) return unless production[:positional_action] == true return if positional_action_contract?(production) raise ParseError, "(tables):1:1: parser table format version #{PARSER_TABLE_FORMAT_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.
817 818 819 820 821 |
# File 'lib/ibex/runtime/parser.rb', line 817 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) ⇒ void
This method returns an undefined value.
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 |
# File 'lib/ibex/runtime/parser.rb', line 1431 def validate_values_action_contract!(production, index) borrowed = production[:borrowed_values_action] == true if borrowed && production[:values_action] != true raise ParseError, "(tables):1:1: parser table format version #{PARSER_TABLE_FORMAT_VERSION} " \ "production #{index} has an inconsistent " \ ":borrowed_values_action marker; :values_action is required" end return if 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 #{PARSER_TABLE_FORMAT_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) ⇒ runtime_value
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 |
# File 'lib/ibex/runtime/parser.rb', line 1785 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) { ... } ⇒ runtime_value
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 |
# File 'lib/ibex/runtime/parser.rb', line 2172 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.
663 664 665 666 667 |
# File 'lib/ibex/runtime/parser.rb', line 663 def yyaccept @runtime_fast_path = false @accept_requested = true nil end |
#yydebug ⇒ Boolean
383 384 385 386 |
# File 'lib/ibex/runtime/parser.rb', line 383 def yydebug ensure_runtime_initialized! @yydebug end |
#yydebug=(enabled) ⇒ Boolean
389 390 391 392 393 |
# File 'lib/ibex/runtime/parser.rb', line 389 def yydebug=(enabled) ensure_runtime_initialized! @runtime_fast_path = false @yydebug = enabled end |
#yydebug_output=(output) ⇒ IO
396 397 398 399 |
# File 'lib/ibex/runtime/parser.rb', line 396 def yydebug_output=(output) ensure_runtime_initialized! @yydebug_output = output end |
#yyerrok ⇒ nil
Leave error recovery immediately.
656 657 658 659 |
# File 'lib/ibex/runtime/parser.rb', line 656 def yyerrok @recovery_shifts = 0 nil end |
#yyerror ⇒ nil
Enter error recovery from a semantic action without calling on_error.
648 649 650 651 652 |
# File 'lib/ibex/runtime/parser.rb', line 648 def yyerror @runtime_fast_path = false @semantic_error = true nil end |
#yyparse(receiver, method_id) ⇒ runtime_value
Parse tokens yielded by receiver.method_id and execute parser
production actions. This caller-fed path does not invoke generated
lexer actions.
475 476 477 478 479 480 |
# File 'lib/ibex/runtime/parser.rb', line 475 def yyparse(receiver, method_id) stream = Enumerator.new do |tokens| receiver.__send__(method_id) { |token| tokens << token } end drive_parser(-> { stream.next }) end |