### ----------------------------------------------------
### -- Marpa
### -- Licenses: Artistic-2.0
### -- Authors: 
### -- File: lib/Marpa.rakumod
### ----------------------------------------------------
### -- Chunk 2 of 4
 separator, −1. If rule_id is not a sequence rule, does not exist or is not well-formed; or on other failure, −2. 
      Failure value: −2. 
    }
  my sub marpa_g_sequence_separator
         ( g $g, Marpa-Rule-ID $rule_id )
         returns Int
         is native( LIB ) { * }
  method g-sequence-separator
         ( g $g, Marpa-Rule-ID $rule-id )
         returns Int {
           marpa_g_sequence_separator( $g, $rule-id ) }

  #|{ A symbol is counted if it appears on the RHS of a sequence rule, or if it is used as the separator symbol of a sequence rule.

      Success return: Returns 1 if symbol sym_id is counted, 0 if not.

      Failure return: If sym_id is well-formed, but there is no such symbol, −1. If sym_id is not well-formed, and on other failure, −2. 
    }
  my sub marpa_g_symbol_is_counted
         ( g $g, Marpa-Symbol-ID $sym_id )
         returns Int
         is native( LIB ) { * }
  method g-symbol-is-counted
         ( g $g, Marpa-Symbol-ID $sym-id )
         returns Int {
           marpa_g_symbol_is_counted( $g, $sym-id ) }

  #|{ These methods, respectively, set and query the rank of a rule rule_id. When rule_id is created, its rank initialized to the default rank of the grammar.

      Initially, the default rank of the grammar is 0. Methods to reset the grammar's default rank are currently in the experimental stage.

      Success value: Returns the rank after the call, and sets the error code to MARPA-ERR-NONE. On failure, returns −2, and sets the error code to an appropriate value, which will never be MARPA-ERR-NONE. Note that when the rank is −2, the error code is the only way to distinguish success from failure. The error code can be determined by using the marpa_g_error() call. 
      Failure value: −2. 
    }
  my sub marpa_g_rule_rank_set
         ( g $g, Marpa-Rule-ID $rule_id, Marpa-Rank $rank )
         returns Marpa-Rank
         is native( LIB ) { * }
  method g-rule-rank-set
         ( g $g, Marpa-Rule-ID $rule-id, Marpa-Rank $rank )
         returns Marpa-Rank {
           marpa_g_rule_rank_set( $g, $rule-id, $rank ) }
  my sub marpa_g_rule_rank
         ( g $g, Marpa-Rule-ID $rule_id )
         returns Marpa-Rank
         is native( LIB ) { * }
  method g-rule-rank
         ( g $g, Marpa-Rule-ID $rule-id )
         returns Marpa-Rank {
           marpa_g_rule_rank( $g, $rule-id ) }

  #|{ These methods, respectively, set and query the “null ranks high” setting of the rule rule_id. The “null ranks high” setting is either 0 or 1. When rule_id is created, its “null ranks high” setting is initialized to 0.

      The “null ranks high” setting affects the ranking of rules with properly nullable symbols on their right hand side. If a rule has properly nullable symbols on its RHS, each instance in which it appears in a parse will have a pattern of nulled and non-nulled symbols. Such a pattern is called a “null variant”.

      If the “null ranks high” setting is 0 (the default), nulled symbols rank low. If the “null ranks high” setting is 1, nulled symbols rank high. Ranking of a null variants is done from left-to-right.

      The marpa_g_rule_null_high_set() method will return failure after the grammar has been precomputed. If there is no other cause of failure, the marpa_g_rule_null_high() method succeeds on both precomputed and unprecomputed grammars.

      Success value: The value of the “null ranks high” flag after the call.
      Failure value: If rule_id is well-formed, but there is no such rule, −1. On all other failures, −2. 
    }
  my sub marpa_g_rule_null_high_set
         ( g $g, Marpa-Rule-ID $rule_id, Int $flag )
         returns Int
         is native( LIB ) { * }
  method g-rule-null-high-set
         ( g $g, Marpa-Rule-ID $rule-id, Int $flag )
         returns Int {
           marpa_g_rule_null_high_set( $g, $rule-id, $flag ) }
  my sub marpa_g_rule_null_high
         ( g $g, Marpa-Rule-ID $rule_id )
         returns Int
         is native( LIB ) { * }
  method g-rule-null-high
         ( g $g, Marpa-Rule-ID $rule-id )
         returns Int {
           marpa_g_rule_null_high( $g, $rule-id ) }

  #|{ Allows the user to deactivate and reactivate symbol completion events in the grammar. When a recognizer is created, the activation status of each of its events is initialized to the activation status of that event in the base grammar. If reactivate is zero, the event is deactivated in the grammar. If reactivate is one, the event is activated in the grammar.

      Symbol completion events are active by default if the symbol was set up for completion events in the grammar. If a symbol was not set up for completion events in the grammar, symbol completion events are inactive by default and any attempt to change that is a fatal error.

      The activation status of a completion event in the grammar can only be changed if the symbol is marked as a completion event symbol in the grammar, and before the grammar is precomputed. However, if a symbol is marked as a completion event symbol in the recognizer, the completion event can be deactivated and reactivated in the recognizer.

      Success cases: On success, the method returns the value of reactivate. The method succeeds trivially if the symbol is already set as indicated by reactivate.

      Failure cases: If the active status of the completion event for sym_id cannot be set as indicated by reactivate, the method fails. On failure, −2 is returned. 
    }
  my sub marpa_g_completion_symbol_activate
         ( g $g, Marpa-Symbol-ID $sym_id, Int $reactivate )
         returns Int
         is native( LIB ) { * }
  method g-completion-symbol-activate
         ( g $g, Marpa-Symbol-ID $sym-id, Int $reactivate )
         returns Int {
           marpa_g_completion_symbol_activate( $g, $sym-id, $reactivate ) }

  #|{ Allows the user to deactivate and reactivate symbol nulled events in the grammar. When a recognizer is created, the activation status of each of its events is initialized to the activation status of that event in the base grammar. If reactivate is zero, the event is deactivated in the grammar. If reactivate is one, the event is activated in the grammar.

      Symbol nulled events are active by default if the symbol was set up for nulled events in the grammar. If a symbol was not set up for nulled events in the grammar, symbol nulled events are inactive by default and any attempt to change that is a fatal error.

      The activation status of a nulled event in the grammar can only be changed if the symbol is marked as a nulled event symbol in the grammar, and before the grammar is precomputed. However, if a symbol is marked as a nulled event symbol in the recognizer, the nulled event can be deactivated and reactivated in the recognizer.

      Success value: On success, the method returns the value of reactivate. The method succeeds trivially if the symbol is already set as indicated by reactivate.
      Failure value: If the active status of the nulled event for sym_id cannot be set as indicated by reactivate, the method fails. On failure, −2 is returned. 
    }
  my sub marpa_g_nulled_symbol_activate
         ( g $g, Marpa-Symbol-ID $sym_id, Int $reactivate )
         returns Int
         is native( LIB ) { * }
  method g-nulled-symbol-activate
         ( g $g, Marpa-Symbol-ID $sym-id, Int $reactivate )
         returns Int {
           marpa_g_nulled_symbol_activate( $g, $sym-id, $reactivate ) }

  #|{ Allows the user to deactivate and reactivate symbol prediction events in the grammar. When a recognizer is created, the activation status of each of its events is initialized to the activation status of that event in the base grammar. If reactivate is zero, the event is deactivated in the grammar. If reactivate is one, the event is activated in the grammar.

      Symbol prediction events are active by default if the symbol was set up for prediction events in the grammar. If a symbol was not set up for prediction events in the grammar, symbol prediction events are inactive by default and any attempt to change that is a fatal error.

      The activation status of a prediction event in the grammar can only be changed if the symbol is marked as a prediction event symbol in the grammar, and before the grammar is precomputed. However, if a symbol is marked as a prediction event symbol in the recognizer, the prediction event can be deactivated and reactivated in the recognizer.

      Success cases: On success, the method returns the value of reactivate. The method succeeds trivially if the symbol is already set as indicated by reactivate.

      Failure cases: If the active status of the prediction event for sym_id cannot be set as indicated by reactivate, the method fails. On failure, −2 is returned. 
    }
  my sub marpa_g_prediction_symbol_activate
         ( g $g, Marpa-Symbol-ID $sym_id, Int $reactivate )
         returns Int
         is native( LIB ) { * }
  method g-prediction-symbol-activate
         ( g $g, Marpa-Symbol-ID $sym-id, Int $reactivate )
         returns Int {
           marpa_g_prediction_symbol_activate( $g, $sym-id, $reactivate ) }

  #|{ Libmarpa can be set up to generate an MARPA-EVENT-SYMBOL-COMPLETED event whenever the symbol is completed. A symbol is said to be completed when a non-nulling rule with that symbol on its LHS is completed.

      For completion events to occur, the symbol must be marked as a completion event symbol. The marpa_g_symbol_is_completion_event_set() function marks symbol sym_id as a completion event symbol if value is 1, and unmarks it it as a completion event symbol if value is 0. The marpa_g_symbol_is_completion_event() method returns the current value of the completion event marking for symbol sym_id.

      Marking a completion event sets its activation status to on. Unmarking a completion event sets its activation status to off. The completion event marking cannot be changed once the grammar is precomputed.

      If a completion event is marked, its activation status can be changed using the marpa_g_completion_symbol_activate() method. Note that, if a symbol is marked as a completion event symbol in the recognizer, its completion event can be deactivated and reactivated in the recognizer.

      Nulled rules and symbols will never cause completion events. Nullable symbols may be marked as completion event symbols, but this will have an effect only when the symbol is not nulled. Nulling symbols may be marked as completion event symbols, but no completion events will ever be generated for a nulling symbol. Note that this implies at no completion event will ever be generated at earleme 0, the start of parsing.

      Success value: On success, 1 if symbol sym_id is a completion event symbol after the call, 0 otherwise.

      Failure value: If sym_id is well-formed, but there is no such symbol, −1. If the grammar g is precomputed; or on other failure, −2. 
    }
  my sub marpa_g_symbol_is_completion_event
         ( g $g, Marpa-Symbol-ID $sym_id )
         returns Int
         is native( LIB ) { * }
  method g-symbol-is-completion-event
         ( g $g, Marpa-Symbol-ID $sym-id )
         returns Int {
           marpa_g_symbol_is_completion_event( $g, $sym-id ) }
  my sub marpa_g_symbol_is_completion_event_set
         ( g $g, Marpa-Symbol-ID $sym_id, Int $value )
         returns Int
         is native( LIB ) { * }
  method g-symbol-is-completion-event-set
         ( g $g, Marpa-Symbol-ID $sym-id, Int $value )
         returns Int {
           marpa_g_symbol_is_completion_event_set( $g, $sym-id, $value ) }

  #|{ Libmarpa can set up to generate an MARPA-EVENT-SYMBOL-NULLED event whenever the symbol is nulled. A symbol is said to be nulled when a zero length instance of that symbol is recognized.

      For nulled events to occur, the symbol must be marked as a nulled event symbol. The marpa_g_symbol_is_nulled_event_set() function marks symbol sym_id as a nulled event symbol if value is 1, and unmarks it it as a nulled event symbol if value is 0. The marpa_g_symbol_is_nulled_event() method returns the current value of the nulled event marking for symbol sym_id.

      Marking a nulled event sets its activation status to on. Unmarking a nulled event sets its activation status to off. The nulled event marking cannot be changed once the grammar is precomputed.

      If a nulled event is marked, its activation status can be changed using the marpa_g_nulled_symbol_activate() method. Note that, if a symbol is marked as a nulled event symbol in the recognizer, its nulled event can be deactivated and reactivated in the recognizer.

      As a reminder, a symbol instance is a symbol at a specific location in the input, and with a specific length. Also, whenever a nulled symbol instance is recognized at a location, it is acceptable at that location, and vice versa.

      When a symbol instance is recognized at a location, it will generate a nulled event or a prediction event, but never both. A symbol instance of zero length, when recognized at a location, generates a nulled event at that location, and does not generate a completion event. A symbol instance of non-zero length, when acceptable at a location, generates a completion event at that location, and does not generate a nulled event.

      When a symbol instance is acceptable at a location, it will generate a nulled event or a prediction event, but never both. A symbol instance of zero length, when acceptable at a location, generates a nulled event at that location, and does not generate a prediction event. A symbol instance of non-zero length, when acceptable at a location, generates a prediction event at that location, and does not generate a nulled event.

      While it is not possible for a symbol instance to generate both a nulled event and a completion event at a location, it is quite possible that a symbol might generate both kinds of event at that location. This is because multiple instances of the same symbol may be recognized at a given location, and these instances will have different lengths. If one instance is recognized at a given location as zero length and a second, non-zero-length, instance is recognized at the same location, the first will generate only nulled events, while the second will generate only completion events. For similar reasons, while a symbol instance will never generate both a null event and a prediction event at a location, multiple instances of the same symbol may do so.

      Zero length derivations can be ambiguous. When a zero length symbol is recognized, all of its zero-length derivations are also considered to be recognized.

      Success: On success, 1 if symbol sym_id is a nulled event symbol after the call, 0 otherwise.

      Failures: If sym_id is well-formed, but there is no such symbol, −1. If the grammar g is precomputed; or on other failure, −2. 
    }
  my sub marpa_g_symbol_is_nulled_event
         ( g $g, Marpa-Symbol-ID $sym_id )
         returns Int
         is native( LIB ) { * }
  method g-symbol-is-nulled-event
         ( g $g, Marpa-Symbol-ID $sym-id )
         returns Int {
           marpa_g_symbol_is_nulled_event( $g, $sym-id ) }
  my sub marpa_g_symbol_is_nulled_event_set
         ( g $g, Marpa-Symbol-ID $sym_id, Int $value )
         returns Int
         is native( LIB ) { * }
  method g-symbol-is-nulled-event-set
         ( g $g, Marpa-Symbol-ID $sym-id, Int $value )
         returns Int {
           marpa_g_symbol_is_nulled_event_set( $g, $sym-id, $value ) }

  #|{ Libmarpa can be set up to generate a MARPA-EVENT-SYMBOL-PREDICTED event when a non-nulled symbol is predicted. A non-nulled symbol is said to be predicted when a instance of it is acceptable at the current earleme according to the grammar. Nulled symbols do not generate predictions.

      For predicted events to occur, the symbol must be marked as a predicted event symbol. The marpa_g_symbol_is_predicted_event_set() function marks symbol sym_id as a predicted event symbol if value is 1, and unmarks it it as a predicted event symbol if value is 0. The marpa_g_symbol_is_predicted_event() method returns the current value of the predicted event marking for symbol sym_id.

      Marking a prediction event sets its activation status to on. Unmarking a prediction event sets its activation status to off. The prediction event marking cannot be changed once the grammar is precomputed.

      If a prediction event is marked, its activation status can be changed using the marpa_g_prediction_symbol_activate() method. Note that, if a symbol is marked as a prediction event symbol in the recognizer, its prediction event can be deactivated and reactivated in the recognizer.

      Success: On success, 1 if symbol sym_id is a predicted event symbol after the call, 0 otherwise.

      Failures: If sym_id is well-formed, but there is no such symbol, −1. If the grammar g is precomputed; or on other failure, −2. 
    }
  my sub marpa_g_symbol_is_prediction_event
         ( g $g, Marpa-Symbol-ID $sym_id )
         returns Int
         is native( LIB ) { * }
  method g-symbol-is-prediction-event
         ( g $g, Marpa-Symbol-ID $sym-id )
         returns Int {
           marpa_g_symbol_is_prediction_event( $g, $sym-id ) }
  my sub marpa_g_symbol_is_prediction_event_set
         ( g $g, Marpa-Symbol-ID $sym_id, Int $value )
         returns Int
         is native( LIB ) { * }
  method g-symbol-is-prediction-event-set
         ( g $g, Marpa-Symbol-ID $sym-id, Int $value )
         returns Int {
           marpa_g_symbol_is_prediction_event_set( $g, $sym-id, $value ) }

  #|{ Precomputation is necessary for a recognizer to be generated from a grammar. On success, marpa_g_precompute returns a non-negative number to indicate that it precomputed the grammar without issues. On failure, marpa_g_precompute returns −2.

      Precomputation may return one or more events, which may be queried using the marpa_g_event() method. At this point events only occur when failure is reported, and events always report issues. But application writers should expect future versions to have events which are reported on success, as well as events which do not represent issues.

      A MARPA-EVENT-LOOP-RULES event occurs when there are infinite loop rules (cycles) in the grammar. The presence of one or more of these will cause failure to be reported, but will not prevent the grammar from being precomputed.

      Each MARPA-EVENT-COUNTED-NULLABLE event is a symbol which is a nullable on the right hand side of a sequence rule — a “counted” symbol. The presence of one or more of these will cause failure to be reported, and will prevent the grammar from being precomputed. So that the programmer can fix several at once, these failures are delayed until events are created for all of the counted nullables.

      Each MARPA-EVENT-NULLING-TERMINAL event is a nulling symbol which is also flagged as a terminal. Since terminals cannot be of zero length, this is a logical impossibility. The presence of one or more of these will cause failure to be reported, and will prevent the grammar from being precomputed. So that the programmer can fix several at once, the failure is delayed until events are created for all of the counted nullables.

      Precomputation involves freezing and then thoroughly checking the grammar. Among the reasons for precomputation to fail are the following:

        MARPA-ERR-NO-RULES: The grammar has no rules.
        MARPA-ERR-NO-START-SYMBOL: No start symbol was specified.
        MARPA-ERR-INVALID-START-SYMBOL: A start symbol ID was specified, but it is not the ID of a valid symbol.
        MARPA-ERR-START-NOT-LHS: The start symbol is not on the LHS of any rule.
        MARPA-ERR-UNPRODUCTIVE-START: The start symbol is not productive.
        MARPA-ERR-COUNTED-NULLABLE: A symbol on the RHS of a sequence rule is nullable. Libmarpa does not allow this.
        MARPA-ERR-NULLING-TERMINAL: A terminal is also a nulling symbol. Libmarpa does not allow this. 

      More details of these can be found under the description of the appropriate code. See External error codes.

      marpa_g_precompute() is unusual in that it is possible to treat one of its failures as “advisory”, and to proceed with parsing. If marpa_g_precompute() fails with an error code of MARPA-ERR-GRAMMAR-HAS-CYCLE, parsing can proceed, just as it typically would for success. The grammar will have been precomputed, as calling the marpa_g_is_precomputed() method will confirm.

      Most applications, however, will want to simply treat failure with MARPA-ERR-GRAMMAR-HAS-CYCLE, as simply another failure, and fix the cycles before parsing. Cycles make a grammar infinitely ambiguous, and are considered useless in current practice. Cycles make processing the grammar less efficient, sometimes considerably so. Detection of cycles is returned as failure because that is by far the convenient thing to do for the vast majority of applications.

      Success value: A non-negative number.
      Failure value: −2. 
    }
  my sub marpa_g_precompute
         ( g $g )
         returns Int
         is native( LIB ) { * }
  method g-precompute
         ( g $g )
         returns Int {
           marpa_g_precompute( $g ) }

  #|{ Success value: 1 if grammar g is already precomputed, 0 otherwise.
      Failure value: −2. 
    }
  my sub marpa_g_is_precomputed
         ( g $g )
         returns Int
         is native( LIB ) { * }
  method g-is-precomputed
         ( g $g )
         returns Int {
           marpa_g_is_precomputed( $g ) }

  #|{ This function allows the application to determine if grammar g has a cycle. As mentioned, most applications will want to treat these as fatal errors. To determine which rules are in the cycle, marpa_g_rule_is_loop() can be used.

      Success value: 1 if the grammar has a cycle, 0 otherwise.
      Failure value: −2. 
    }
  my sub marpa_g_has_cycle
         ( g $g )
         returns Int
         is native( LIB ) { * }
  method g-has-cycle
         ( g $g )
         returns Int {
           marpa_g_has_cycle( $g ) }

  #|{ Creates a new recognizer. The reference count of the recognizer will be 1. The reference count of g, the base grammar, will be incremented by one.

      Success value: The newly created recognizer. If g is not precomputed, or on other failure, NULL. 
      Failure value: NULL.
    }
  my sub marpa_r_new
         ( g $g )
         returns Marpa-Recognizer
         is native( LIB ) { * }
  method r-new
         ( g $g )
         returns Marpa-Recognizer {
           marpa_r_new( $g ) }

  #|{ Increases the reference count by 1. Not needed by most applications.

      Success value: The recognizer object, $r.
      Failure value: NULL.
    }
  my sub marpa_r_ref
         ( Marpa-Recognizer $r )
         returns Marpa-Recognizer
         is native( LIB ) { * }
  method r-ref
         ( Marpa-Recognizer $r )
         returns Marpa-Recognizer {
           marpa_r_ref( $r ) }

  #|{ Decreases the reference count by 1, destroying r once the reference count reaches zero. When r is destroyed, the reference count of its base grammar is decreased by one. If this takes the reference count of the base grammar to zero, it too is destroyed. 
    }
  my sub marpa_r_unref
         ( Marpa-Recognizer $r )
         is native( LIB ) { * }
  method r-unref
         ( Marpa-Recognizer $r ) {
           marpa_r_unref( $r ) }

  #|{ Makes r ready to accept input. The first Earley set, the one at earleme 0, will be completed during this call.

      Because the call to marpa_r_start_input() completes an Earley set, it may generate events. For details about the events that may be generated during Earley set completion, see the description of the marpa_r_earleme_complete() method.

      Success value: A non-negative value.
      Failure value: −2. 
    }
  my sub marpa_r_start_input
         ( Marpa-Recognizer $r )
         returns Int
         is native( LIB ) { * }
  method r-start-input
         ( Marpa-Recognizer $r )
         returns Int {
           marpa_r_start_input( $r ) }

  #|{ Reads a token into r. The token will start at the current earleme. Libmarpa allows tokens to be ambiguous, to be of variable length and to overlap. token_id is the symbol ID of the token, which must be a terminal. length is the length of the token.

      value is an integer that represents the value of the token. In applications where the token's actual value is not an integer, it is expected that the application will use this value as a “virtual” value, perhaps finding the actual value by using value to index an array. value is not used inside Libmarpa — it is simply stored to be returned by the valuator as a convenience for the application. Some applications may prefer to track token values on their own, perhaps based on the earleme location and token_id, instead of using Libmarpa's token values.

      A value of 0 is reserved for a now-deprecated feature. Do not use it. For more details on that feature, see the section Valued and unvalued symbols.

      When marpa_r_alternative() is successful, the value of the furthest earleme is set to the greater of its value before the call, and current+length, where current is the value of the current earleme. The values of the current and latest earlemes are unchanged by calls to marpa_r_alternative().

      Several error codes leave the recognizer in a fully recoverable state, allowing the application to retry the marpa_r_alternative() method. Retry is efficient, and quite useable as a parsing technique. The error code of primary interest from this point of view is MARPA-ERR-UNEXPECTED-TOKEN-ID, which indicates that the token was not accepted because of its token ID. Retry after this condition is used in several applications, and is called “the Ruby Slippers technique”.

      The error codes MARPA-ERR-DUPLICATE-TOKEN, MARPA-ERR-NO-TOKEN-EXPECTED-HERE and MARPA-ERR-INACCESSIBLE-TOKEN also leave the recognizer in a fully recoverable state, and may also be useable for the Ruby Slippers or similar techniques. At this writing, the author knows of no applications which attempt to recover from these errors.

      Success value: MARPA-ERR-NONE.
      Failure value: Some other error code.
    }
  my sub marpa_r_alternative
         ( Marpa-Recognizer $r, Marpa-Symbol-ID $token_id, Int $value, Int $length )
         returns Int
         is native( LIB ) { * }
  method r-alternative
         ( Marpa-Recognizer $r, Marpa-Symbol-ID $token-id, Int $value, Int $length )
         returns Int {
           marpa_r_alternative( $r, $token-id, $value, $length ) }

  #|{ This method does the final processing for the current earleme. It then advances the current earleme by one. Note that marpa_r_earleme_complete() may be called even when no tokens have been read at the current earleme — in the character-per-earleme input model, for example, tokens can span many characters and, if the input is unambiguous over that span, there will be no other tokens that start inside it.

      As mentioned, marpa_r_earleme_complete() always advances the current earleme, incrementing its value by 1. This means that value of the current earleme after the call will be the one plus the value of the earleme processed by the call to marpa_r_earleme_complete(). If any token was accepted at the earleme being processed, marpa_r_earleme_complete() creates a new Earley set which will be the latest Earley set and, after the call, the latest earleme will be equal to the new current earleme. If no token was accepted at the earleme being processed, no Earley set is created, and the value of the latest earleme remains unchanged. The value of the furthest earleme is never changed by a call to marpa_r_earleme_complete().

    During this method, one or more events may occur. On success, this function returns the number of events generated, but it is important to note that events may be created whether earleme completion fails or succeeds. When this method fails, the application must call marpa_g_event() if it wants to determine if any events occurred. Since the reason for failure to complete an earleme is often detailed in the events, applications that fail will often be at least as interested in the events as those that succeed.

      The MARPA-EVENT-EARLEY-ITEM-THRESHOLD event indicates that an application-settable threshold on the number of Earley items has been reached or exceeded. What this means depends on the application, but when the default threshold is exceeded, it means that it is very likely that the time and space resources consumed by the parse will prove excessive.

      A parse is “exhausted” when it can accept no more input. This can happen both on success and on failure. Note that the failure due to parse exhaustion only means failure at the current earleme. There may be successful parses at earlier earlemes.

      If a parse is exhausted, but successful, an event with the event code MARPA-EVENT-EXHAUSTED occurs. Because the parse is exhausted, no input will be accepted at later earlemes. It is quite common for a parse to become exhausted when it succeeds. Many practical grammars are designed so that a successful parse cannot be extended.

      An exhausted parse may cause a failure, in which case marpa_r_earleme_complete() returns an error whose error code is MARPA-ERR-PARSE-EXHAUSTED. For a parse to fail at an earleme due to exhaustion, it must be the case that no alternatives were accepted at that earleme. In fact, in the standard input model, a failure due to parse exhaustion occurs if and only if no alternatives were accepted at the current earleme.

      The circumstances under which failure due to parse exhaustion occurs are slightly more complicated when variable length tokens are in use. Informally, a parse will never fail due to exhaustion as long as it is possible that a token ending at some future earleme will continue the parse. More precisely, a call to marpa_r_earleme_complete() fails due to parse exhaustion if and only if, first, no alternatives were added at the current earleme and, second, that call left the current earleme equal to the furthest earleme.

      Success value: The number of events generated.
      Failure value: −2. 
    }
  my sub marpa_r_earleme_complete
         ( Marpa-Recognizer $r )
         returns Int
         is native( LIB ) { * }
  method r-earleme-complete
         ( Marpa-Recognizer $r )
         returns Int {
           marpa_r_earleme_complete( $r ) }

  #|{ Success value: If input has started, the current earleme. If input has not started, −1.
      Failure value: Always succeeds.
    }
  my sub marpa_r_current_earleme
         ( Marpa-Recognizer $r )
         returns Marpa-Earleme
         is native( LIB ) { * }
  method r-current-earleme
         ( Marpa-Recognizer $r )
         returns Marpa-Earleme {
           marpa_r_current_earleme( $r ) }

  #|{ In the default, token-stream model, Earley set ID and earleme are always equal, but this is not the case in other input models. (The ID of an Earley set ID is also called its ordinal.) If there is no Earley set whose ID is set_id, marpa_r_earleme() fails. If set_id was negative, the error code is set to MARPA-ERR-INVALID-LOCATION. If set_id is greater than the ordinal of the latest Earley set, the error code is set to MARPA-ERR-NO-EARLEY-SET-AT-LOCATION.

      At this writing, there is no method for the inverse operation (conversion of an earleme to an Earley set ID). One consideration in writing such a method is that not all earlemes correspond to Earley sets. Applications that want to map earlemes to Earley sets will have no trouble if they are using the standard input model — the Earley set ID is always exactly equal to the earleme in that model. For other applications that want an earleme-to-ID mapping, the most general method is create an ID-to-earleme array using the marpa_r_earleme() method and invert it.

      Success value: The earleme corresponding to Earley set set_id.
      Failure value: −2. 
    }
  my sub marpa_r_earleme
         ( Marpa-Recognizer $r, Marpa-Earley-Set-ID $set_id )
         returns Marpa-Earleme
         is native( LIB ) { * }
  method r-earleme
         ( Marpa-Recognizer $r, Marpa-Earley-Set-ID $set-id )
         returns Marpa-Earleme {
           marpa_r_earleme( $r, $set-id ) }

  #|{ Returns the integer value of earley_set. For more details, see the description of marpa_r_earley_set_values().

      Success value: The value of earley_set.
      Failure value: −2. 
    }
  my sub marpa_r_earley_set_value
         ( Marpa-Recognizer $r, Marpa-Earley-Set-ID $earley_set )
         returns Int
         is native( LIB ) { * }
  method r-earley-set-value
         ( Marpa-Recognizer $r, Marpa-Earley-Set-ID $earley-set )
         returns Int {
           marpa_r_earley_set_value( $r, $earley-set ) }

  #|{ If p_value is non-zero, sets the location pointed to by p_value to the integer value of the Earley set. Similarly, if p_pvalue is non-zero, sets the location pointed to by p_pvalue to the pointer value of the Earley set.

      The “value” and “pointer” of an Earley set are an arbitrary integer and an arbitrary pointer that the application can use for its own purposes. In character-per-earleme input models, for example, the integer can be the codepoint of the current character. In a traditional token-per-earleme input model, they could be used to indicate the string value of the token – the pointer could point to the start of the string, and the integer could indicate its length.

      The Earley set value and pointer can be set using the marpa_r_latest_earley_set_values_set() method. The Earley set integer value defaults to −1, and the pointer value defaults to NULL.

      Success value: returns a non-negative integer.
      Failure value: −2. 
    }
  my sub marpa_r_earley_set_values
         ( Marpa-Recognizer $r, Marpa-Earley-Set-ID $earley_set, Pointer[Int] $p_value, Pointer