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

Document what map passes to functions with various parsers (combine, oneOf, ...) #53

Open
ethindp opened this issue Sep 16, 2023 · 2 comments

Comments

@ethindp
Copy link

ethindp commented Sep 16, 2023

From reading the code, it's unclear how I'm supposed to define a function as when I want to use map. For instance, if I have this parser:

const if_statement = combine(.{keyword_if, condition, keyword_then, statements});

I can't easily tell how my function in map will be called, i.e., will I get a call like fn x(kw_if: []u8, condition: Condition, kw_then: []u8, statements: Statements), or something else?

@Hejsil
Copy link
Owner

Hejsil commented Sep 17, 2023

Looking at map, it is currently documented as this:

/// Constructs a parser that has its result converted with the
/// `conv` function. The ´conv` functions signature is
/// `*const fn (ParserResult(parser)) T`, so this function should only
/// be used for conversions that cannot fail. See `convert`.

So the conv function will be passed one argument, which is the result of the parser it maps over. For combine the result is a tuple, as documented:

/// Takes a tuple of `Parser(any)` and constructs a parser that
/// only succeeds if all parsers succeed to parse. The parsers
/// will be called in order and parser `N` will use the `rest`
/// from parser `N-1`. The parsers result will be a `Tuple` of
/// all parser not of type `Parser(void)`. If only one parser
/// is not of type `Parser(void)` then this parsers result is
/// returned instead of a tuple.

I'm unsure what is missing to make this more clear, but if you have any ideas I'm all ear.

@ethindp
Copy link
Author

ethindp commented Sep 18, 2023

Thanks, I missed those parts, I understand now. I am wondering how map actually works though, like say I want to build an AST out of it and I use .map to do it. Would I return the AST nodes in the conversion function, add them to an external list, or something else?

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