Module predicates

Module predicates 

Source
Expand description

Parse predicates for compile-time parser control.

This module provides zero-cost abstractions for controlling parser behavior at compile time. All predicate types are zero-sized and have no runtime overhead.

§Base Values

  • Enable - Always succeeds without consuming tokens
  • Disable - Always fails without consuming tokens
  • TokensRemain - Succeeds only when tokens remain in the stream

§Logical Operators

  • AllOf - Logical AND (all must succeed, 2-4 operands)
  • AnyOf - Logical OR (at least one must succeed, 2-4 operands)
  • OneOf - Logical XOR (exactly one must succeed, 2-4 operands)
  • Not - Logical NOT (succeeds if inner fails)

§Example

The predicateflag macro creates newtype wrappers that can implement custom traits for compile-time context validation:

use unsynn::*;
use unsynn::predicates::*;


unsynn! {
   // Define a custom context predicate marker-trait
    trait ExpressionContext: PredicateOp;

    // Creates newtype implementing ExpressionContext
    predicateflag InExpression = Enable for ExpressionContext;

    // Only accepts ExpressionContext predicates
    struct StructLiteral<P: ExpressionContext> {
        _guard: P,
        name: Ident,
    }
}

See the Parse Predicates chapter in the Cookbook for detailed usage examples.

Structs§

AllOf
Logical AND: 2-4 predicates must all succeed.
AnyOf
Logical OR: 2-4 predicates, at least one must succeed.
Disable
Always fails without consuming tokens.
Enable
Always succeeds without consuming tokens.
Not
Logical NOT: succeeds if inner predicate fails.
OneOf
Logical XOR: exactly one of 2-4 predicates must succeed.
PredicateCmp
Predicate that compares type A with type B at runtime.
TokensRemain
Succeeds only when tokens remain in the stream.

Traits§

PredicateOp
Marker trait for compile-time parser predicates.