Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need more examples with anonymous sequences #20

Open
singalen opened this issue May 9, 2021 · 0 comments
Open

Need more examples with anonymous sequences #20

singalen opened this issue May 9, 2021 · 0 comments

Comments

@singalen
Copy link

singalen commented May 9, 2021

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!

@singalen singalen changed the title A repetition of an anonymous sequence? Need more examples with anonymous sequences May 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant