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

Add a repSplit(p) function to Parser #138

Closed
nicolas-zozol opened this issue May 12, 2019 · 3 comments
Closed

Add a repSplit(p) function to Parser #138

nicolas-zozol opened this issue May 12, 2019 · 3 comments
Assignees

Comments

@nicolas-zozol
Copy link
Collaborator

nicolas-zozol commented May 12, 2019

A simple exemple would be for bullet points

* bullet 1
* bullet 2
* bullet 3

If we use bullet().then(eol()).rep(), then the last eol() is eaten by the parser, so we can't separate easily bullet block by at least two eol().

So the idea would be bullet().repSplit(eol()) and would return a TupleParser<Bullet>.

Other option maybe more general but maybe less in the Parsec philosohy is to make

bullet().then(eol())
.rep() // eat last '\n'
.backdrop(1); // go back 1 character

It could be use in many more situations (including bad ones)

@nicolas-zozol nicolas-zozol changed the title Add a repJoin(p) function to Parser Add a split(p) function to Parser May 12, 2019
@nicolas-zozol nicolas-zozol changed the title Add a split(p) function to Parser Add a repSplit(p) function to Parser May 12, 2019
@d-plaindoux
Copy link
Member

This should be written:

bullet.then(eol().then(bullet()).rep()

Indeed the rep parser can be generalized with start, join and end parsers.

@nicolas-zozol
Copy link
Collaborator Author

The back() function would not help, because for example if the document finishes at the * bullet3 without last \n, applying the back() function would move the cursor before the 3.

@nicolas-zozol
Copy link
Collaborator Author

For the record, here is a way to go back if ever needed.

const parser = C.string('this is wanted').then(C.char(';'));

const backParser = new Parser(function (input, index) {
    return parser.parse(input, index)    // parsing with regular parser
        .fold(    // then modifing the response
            accept =>
                response.accept(
                    accept.value,
                    accept.input,
                    accept.offset - 1,   // moving back of one offset
                    accept.consumed
                ),
            reject =>
                response.reject(
                    reject.input, reject.offset, reject.consumed
                )
        );
});

const resp = backParser.parse(Streams.ofString(line));

nicolas-zozol added a commit that referenced this issue May 12, 2019
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

2 participants