Giter Club home page Giter Club logo

Comments (6)

wavebeem avatar wavebeem commented on September 13, 2024

Can you explain how these differ from method chaining?

How does wrapTimes compare to the following?

parser.times(n).wrap(before, after)
parser.times(min, max).wrap(before, after)
// --- or ---
parser.wrap(before, after).times(n)
parser.wrap(before, after).times(min, max)

Same sort of question for wrapAtLeast, wrapAtMost, wrapMany.

from parsimmon.

joseDaKing avatar joseDaKing commented on September 13, 2024

This parser will only succeed when the input is "(abra-abra-abra-)"

string("abra-").times(3).wrap(string("("), string(")"))
```

This parser will only succeed when the input is "(abra)(abra)(abra)"
```js 
string("abra").wrap(string("("), string(")")).times(3)
```

```
```

from parsimmon.

joseDaKing avatar joseDaKing commented on September 13, 2024

Times followed with wrap will only succeed when the input is "(abra-abra-abra-)"

string("abra-").times(3).wrap(string("("), string(")"))

Wrap followed with times will only succeed when the input is "(abra)(abra)(abra)"

string("abra").wrap(string("("), string(")")).times(3)

WrapTimes will only succeed when the input is "(((abra)))"

string("abra").wrapTimes(string("("), string(")"), 3);

// wrapTimes is same as this below
string("abra")
.wrap(string("("), string(")"))
.wrap(string("("), string(")"))
.wrap(string("("), string(")"))

WrapAtLeast will succeed if you have wrapped it at least n times. In this case "abra" must be wrapped at least in 2 parentheses or more, for example it would succeed if the input is "((abra))" or "(((((abra)))))"

string("abra").wrapAtLeast(string("("), string(")"), 2)

WrapAtMost will succeed if you have wrapped it at most n times. In this case "abra" must be wrapped at most 2 times or less, for example it would succeed if the input is "abra" or "(abra)" or "((abra))".

string("abra").wrapAtMost(string("("), string(")"), 2)

WrapMany will succeed if you have wrapped n times or not at all. In this case "abra" can be wrapped n times or not at all, for example it would succeed if the input is "abra" or "(((((((((((((abra)))))))))))))" or "(((((abra)))))"

from parsimmon.

wavebeem avatar wavebeem commented on September 13, 2024

Parsimmon already has a lot of methods, and I've never wanted this one personally. You can write this in terms of other combinators without a ton of fuss, so I don't want to add 4 new methods.

const P = require('parsimmon');

const x = P.string('x');
const xp = P.string('(').atLeast(1).chain((lp) => {
    return x.skip(P.string(')').times(lp.length));
});

xp.parse('(((x)))');

from parsimmon.

joseDaKing avatar joseDaKing commented on September 13, 2024

To be honest with you I thought I would need it, but I realized now I don't actually need and it is nisch feature. By the way thanks for the answer.

from parsimmon.

wavebeem avatar wavebeem commented on September 13, 2024

Ah, good to know. I like hearing real-world use-cases for methods before adding them anyway.

from parsimmon.

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.