Code Monkey home page Code Monkey logo

Comments (5)

kevinmehall avatar kevinmehall commented on May 22, 2024

In the general case, mutating a parent rule's variable isn't safe due to backtracking, so it's not supported.

The * operator collects its children into a Vec by default, so your case could be rewritten:

document -> Document
    = children: block*
    { Document { children: children } }

block -> Element
    = ...

where Document.children has type Vec<Element>. It might be possible to make it generic over some Container trait rather than requiring the use of Vec.

from rust-peg.

kevinmehall avatar kevinmehall commented on May 22, 2024

And if you want to mutate a variable within the same rule, the rule action has ownership, so it can move it into a mutable variable:

document -> Document
    = children: block* {
      let mut children = children;
      children.push(another_element());
      Document { children: children } 
    }

from rust-peg.

indiv0 avatar indiv0 commented on May 22, 2024

Alright, that might work for the basic use case presented in the initial post, but the structure of my program is such that the block rule from above also calls other rules, which, in turn, may return Elements themselves.

If the rules that block calls return a Vec<Element> as well, how would I then add that to the Vec<Element> children?

from rust-peg.

kevinmehall avatar kevinmehall commented on May 22, 2024

One way would be to have block return Vec<Element> and have the document rule concat_vec the Vec<Vec<Element>> it receives. Yes, it allocates more than a single vector, but matches need to be independent to be able to backtrack.

from rust-peg.

indiv0 avatar indiv0 commented on May 22, 2024

Alright I'll try that. Thanks!

from rust-peg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.