Hejsil / mecha

A parser combinator library for Zig
MIT License
473 stars 21 forks source link

New parsers `bracket` and `consumeWith` #45

Closed hasundue closed 1 year ago

hasundue commented 1 year ago

Hello and appreciate your great work. It's super fun to make my parser mechs with mecha 👍

I implemented a new parser bracket for my mech (a HTML parser), as well as a supplemental parser consumeWith. Here I'm opening this pull request just in case you liked them.

If not, just publicizing parserTypes and Combine would be also helpful for me, so that I could implement bracket in my own package.

Hejsil commented 1 year ago

Can you post an example of an actual parser that uses bracket? I do see the (a b), but I would like to see some actual code before I make further decision on whether to merge this. I mostly wonder how you deal with nesting and such.

Also, pretty sure the consumeWith is the same as combine(.{parser, mecha.eos}) no?

If not, just publicizing parserTypes and Combine would be also helpful for me, so that I could implement bracket in my own package.

This seems like a good idea :)

hasundue commented 1 year ago

Thanks for the reply! The actual code is here: https://github.com/hasundue/browse.vim/blob/main/src/parser.zig#L79

The character '-' is included both in the end tag of a HTML comment and the text in it, using combine let the text parser consume "--" of the end tag. I wouldn't be surprised if there is a better solution though 😅

I mostly wonder how you deal with nesting and such.

TBH I have not considered such tricky cases seriously. Since the implementation is quite straightforward and inefficient, I assume it is only used for simple cases like above.

I was also thinking of making a parser to exclude some cases from combine to realize the same thing, which may be more generic and useful.

Also, pretty sure the consumeWith is the same as combine(.{parser, mecha.eos}) no?

Absolutely. I was dumb.

Hejsil commented 1 year ago

Thanks for the reply! The actual code is here: https://github.com/hasundue/browse.vim/blob/main/src/parser.zig#L79

The character '-' is included both in the end tag of a HTML comment and the text in it, using combine let the text parser consume "--" of the end tag. I wouldn't be surprised if there is a better solution though sweat_smile

Aah, i see. In that case, I think you might be able to do:

const comment = m.discard(m.combine(.{
    m.string("<!--"),
    m.many(m.utf8.not(m.string("-->"), .{ .collect = false }),
    m.string("-->"),
}));
hasundue commented 1 year ago

Oh wow, this does work 😅 I didn't notice not is actually defined there.

I'm gonna try this approach until I find any problem. You can safely close this PR.

Thank you for taking time 🙏

Hejsil commented 1 year ago

Happy to help :+1: