pub enum RightAssoc<O, L> {
    Binary {
        lhs: L,
        op: O,
        rhs: Box<Self>,
    },
    Lhs(L),
}
Expand description

Helper type for parsing right-associative binary infix operators.

Reduces deep right-recursion by parsing a stream of binary infix operators iteratively, then transforming the sequence of nodes into a right-leaning tree.

  • L is the type of the left-hand side of the production, i.e., the sub-production at the next highest precedence level.
  • O is the type of the interleaved binary operator itself.

That is, a production in EBNF style would look like:

RightAssoc = L O RightAssoc
           | L

Examples

#[derive(PartialEq, Eq, Debug, Parse, ToTokens)]
enum Term {
    Lit(LitBool),
    Var(Ident),
    Not(
        Tilde,
        #[parsel(recursive)]
        Box<Term>,
    ),
    Group(
        #[parsel(recursive)]
        Box<Paren<Expr>>
    ),
}

type Expr = RightAssoc<
    Or,
    RightAssoc<
        And,
        Term
    >
>;

let actual: Expr = parse_quote!((~false | a) & ~(b & c & true));
let expected = RightAssoc::<Or, _>::Lhs(RightAssoc::<And, _>::Binary {
    lhs: Term::Group(Box::new(Paren::from(
        RightAssoc::<Or, _>::Binary {
           lhs: RightAssoc::<And, _>::Lhs(Term::Not(
               Tilde::default(),
               Box::new(Term::Lit(LitBool::from(false)))
           )),
           op: Or::default(),
           rhs: Box::new(RightAssoc::<Or, _>::Lhs(
               RightAssoc::<And, _>::Lhs(
                   Term::Var(ident("a"))
               )
           )),
       }
    ))),
    op: And::default(),
    rhs: Box::new(RightAssoc::<And, _>::Lhs(
        Term::Not(
            Tilde::default(),
            Box::new(Term::Group(Box::new(Paren::from(
                RightAssoc::<Or, _>::Lhs(RightAssoc::<And, _>::Binary {
                    lhs: Term::Var(ident("b")),
                    op: And::default(),
                    rhs: Box::new(RightAssoc::<And, _>::Binary {
                        lhs: Term::Var(ident("c")),
                        op: And::default(),
                        rhs: Box::new(RightAssoc::<And, _>::Lhs(
                            Term::Lit(LitBool::from(true)),
                        )),
                    }),
                })
            ))))
        )
    )),
});

assert_eq!(actual, expected);
assert_eq!(actual.to_string(), expected.to_string());

Variants

Binary

Fields

lhs: L

Left-hand side of the binary operation.

op: O

The infix binary operator.

rhs: Box<Self>

Right-hand side of the binary operation.

A right-associative binary infix operator was found.

Lhs(L)

No binary operator was found at this level of precedence, so simply forward to the next level.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Write self to the given TokenStream. Read more
Convert self directly into a TokenStream object. Read more
Convert self directly into a TokenStream object. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty. Read more

TODO(H2CO3): a faster, less naive implementation would be great. We should use the byte offset of start to compute that of end, sparing the double scan of the source up until the start location.

let source = r#"
   -3.667
  1248  "string ű literal"
      "wíőzs"
"#;
let tokens: Many<Lit> = source.parse()?;

assert_eq!(tokens.len(), 4);
assert_eq!(tokens[0].byte_range(source),  4..10);
assert_eq!(tokens[1].byte_range(source), 13..17);
assert_eq!(tokens[2].byte_range(source), 19..38);
assert_eq!(tokens[3].byte_range(source), 45..54);

TODO(H2CO3): a faster, less naive implementation would be great. We should use the char offset of start to compute that of end, sparing the double scan of the source up until the start location.

let source = r#"
   -3.667
  1248  "string ű literal"
      "wíőzs"
"#;
let tokens: Many<Lit> = source.parse()?;

assert_eq!(tokens.len(), 4);
assert_eq!(tokens[0].char_range(source),  4..10);
assert_eq!(tokens[1].char_range(source), 13..17);
assert_eq!(tokens[2].char_range(source), 19..37);
assert_eq!(tokens[3].char_range(source), 44..51);
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.