GithubHelp home page GithubHelp logo

Add permutation operator about parboiled HOT 7 OPEN

sirthias avatar sirthias commented on June 2, 2024
Add permutation operator

from parboiled.

Comments (7)

jason-s avatar jason-s commented on June 2, 2024

important (and probably obvious) nuance: actions should be disallowed: they're ok in sequences, but not permutations, because the order may be scrambled

from parboiled.

sirthias avatar sirthias commented on June 2, 2024

Actually, Jason, I have been thinking about this a bit more and I find it really hard to come with a good general construct for permutations. The reason is that there are really quite a few dimensions along which one permutation application can differ from another:

  • Do all the elements have to appear or are partial permutations, where only a subset of all elements is present, ok? Is the empty match (zero elements) ok?
  • How are permutation elements separated? Only empty matches or things like commas, colons, and the like?
  • Should a custom action be run after matching an element?

After having looked at a few of my own application I realized that sometimes I need a "partial, non-zero, comma separated permuation with a custom element action" and sometimes I need a "complete, unseparated permutation without element action".

So in total, this construct really seems very hard to generalize properly and I am actually inclined to not offer it but require applications to implement it themselves, to really fit there needs...

Cheers,
Mathias

from parboiled.

jason-s avatar jason-s commented on June 2, 2024

Do all the elements have to appear or are partial permutations, where only a subset of all elements is present, ok? Is the empty match (zero elements) ok?

If you want an element to be optional, you wrap it in an Optional() rule, the same way you would in a Sequence().

How are permutation elements separated? Only empty matches or things like commas, colons, and the like?

I can see this as being a feature, but right now you don't offer this feature with other rules.

Should a custom action be run after matching an element?

No, if you want that, you wrap it in a Sequence().

For example:

  Permutation(
      Year(),
      Sequence(FirstOf(WordMonth(),NumericMonth()), runSomeActionOnMonth())
      Optional(NumericDayOfMonth())
  )

I could see wanting a separator, but that's a feature, not a core property of a permutation. There's probably a way to implement it in other ways, but I can't think of it at the top of my head.

I still think this is a valuable primitive that is very difficult for users of Parboiled to implement (as compared to within the Parboiled library), and you should offer primitive constructs with a minimum of extra features.

from parboiled.

sirthias avatar sirthias commented on June 2, 2024

Ok, I see your logic.
If the production elements can be complex rules and not just primitives (e.g. Strings or Chars) than most problems seem solvable. The separator rule could be attached to a Permutation with a "withSeparator" modifier, like this:

Permutation(
    Year(),
    Sequence(FirstOf(WordMonth(),NumericMonth()), runSomeActionOnMonth())
    Optional(NumericDayOfMonth())
).withSeparator(Sequence(',', OptWhiteSpace))

Looks doable...

I'll give it a first implementation with the next release...

Cheers and thanks for your ideas,
Mathias

from parboiled.

jason-s avatar jason-s commented on June 2, 2024

Cool! I like the withSeparator idea, I had no idea you could do things like that with rule objects.

from parboiled.

sirthias avatar sirthias commented on June 2, 2024

Actually, you can't generally.
But the Permutation rule would support 'withSeparator' to apply one...


[email protected]
http://www.parboiled.org

On 21.04.2011, at 14:09, jason-s wrote:

Cool! I like the withSeparator idea, I had no idea you could do things like that with rule objects.

Reply to this email directly or view it on GitHub:
#18 (comment)

from parboiled.

fge avatar fge commented on June 2, 2024

I have implemented something like this:

https://github.com/fge/parboiled-css/blob/master/src/main/java/org/eel/kitchen/css/base/RuleSetRunner.java

But it is not really "generic". I suppose it could be made generic enough if you registered rules instead of parser classes as I do now, but the effect is:

  • I register n parsers/rules;
  • some of them are required to match;
  • I check that a rule only matches once.

It works beautifully well for me. But I don't know parboiled enough to make it general.

from parboiled.

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.