Skip to content

Need more examples with anonymous sequences #20

Open

Description

@singalen

Hi, I'm trying pest-ast for my project. So far, it's been super-helpful.

Now, I have a few cases for which I cannot find a type mapping. i have looked in issues, in the examples of pest-ast and the examples of derive.

A repetition of an anonymous sequence

comparison = { arith_expr ~ (comp_op ~ arith_expr)* }

In #8, I see an example:

struct assigns<'pest>(
  #[pest_ast(outer)] Span<'pest>,
  Vec<struct _1(assign<'pest>)>,
  assign<'pest>,
);

But Vec<struct _1(assign<'pest>)> is not a valid Rust, there are no anonymous structs.

I can try to extract a named struct, but there is no grammar rule to put into pest_ast, and pest_ast is required:

    #[derive(Debug, FromPest)]
    // FIXME: This probably shouldn't work!
    #[pest_ast(rule(Rule::comparison))]
    pub struct ComparisonPart2 {
        pub op: CompOr,
        pub expr: ArithExpr
    }

    #[derive(Debug, FromPest)]
    #[pest_ast(rule(Rule::comparison))]
    // comparison = { arith_expr ~ (comp_op ~ arith_expr)* }
    pub struct Comparison {
        pub expr: ArithExpr,
        pub continuation: Vec<ComparisonPart2>,
        // pub continuation: Vec<(CompOr, ArithExpr)>,
    }

I'm not sure it will work (I haven't finished a mapping for my grammar yet).

A nested choice

arith_expr = { term ~ ((plus|minus) ~ term)* }
How do I map the (plus|minus) part? To an enum like this?

    #[derive(Debug, FromPest)]
    #[pest_ast(rule(Rule::plus))]
    pub struct Plus {
    }

    #[derive(Debug, FromPest)]
    #[pest_ast(rule(Rule::minus))]
    pub struct Minus {
    }

    #[derive(Debug, FromPest)]
    #[pest_ast(rule(Rule::arith_expr))]
    pub enum PlusMinus {
        Plus{ plus: Plus },
        Minus{ minus: Minus },
    }

    #[derive(Debug, FromPest)]
    #[pest_ast(rule(Rule::arith_expr))]
    pub struct ArithExprPart2 {
        pub op: PlusMinus,
        pub term: Term,
    }

    #[derive(Debug, FromPest)]
    #[pest_ast(rule(Rule::arith_expr))]
    // arith_expr = { term ~ ((plus|minus) ~ term)* }
    pub struct ArithExpr {
        pub term: Term,
        pub tail: Vec<ArithExprPart2>
    }

It would be great to have a such example. It would be a bit less great to know it's impossible as of now.

Thank you!

Activity

  1. changed the title [-]A repetition of an anonymous sequence?[/-] [+]Need more examples with anonymous sequences[/+] on May 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions