question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I would like an overload for Message for both TextParser and TokenListParser that looks like this:

        /// <summary>
        /// Construct a parser that fails with error message <paramref name="messageGenerator"/> when <paramref name="parser"/> fails.
        /// </summary>
        /// <typeparam name="TKind">The kind of the tokens being parsed.</typeparam>
        /// <typeparam name="T">The type of value being parsed.</typeparam>
        /// <param name="parser">The parser.</param>
        /// <param name="messageGenerator">A function that received the message result of the failed parser and returns the error message.</param>
        /// <returns>The resulting parser.</returns>
        public static TokenListParser<TKind, T> Message<TKind, T>(this TokenListParser<TKind, T> parser, Func<TokenListParserResult<TKind,T>, string> messageGenerator)
        {
            if (parser == null) throw new ArgumentNullException(nameof(parser));
            if (messageGenerator == null) throw new ArgumentNullException(nameof(messageGenerator));

            return input =>
            {
                var result = parser(input);
                if (result.HasValue)
                    return result;

                return TokenListParserResult.Empty<TKind, T>(result.Remainder, result.SubTokenErrorPosition, messageGenerator(result));
            };
        }

That would allow me to use information in the result object for constructing the error message. This will be especially useful since the error message presence overrides expectations, and sometimes they are something that I would like to include into the resulting error message.

Will you consider accepting such a PR?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewSavcommented, Jun 4, 2022

@nblumhardt happy, ehm…, Queen’s Birthday? 😉

0reactions
AndrewSavcommented, Jun 4, 2022

I think this one did not work out, but a different solution might be required depending on how #142 pans out.

I was considering here a scenario with using Parse.Not(something) as explained in the other issue, and it seems that my use case is specific to this particular scenario, and is not generic as I imagined. In particular, the example that I gave above:

TokenListParser<Argument, Unit> value = Superpower.Parse.Not(Token.Matching<Argument>(x => x.ArgumentType == ArgumentType.Value, "value"))
    .Message(x => $"We expected a switch. We got value: {x.Value}. Expected: {FormatExpectations(x.Expectations)}");

does not work with the proposed change, it cannot work: x.Value is undefined, because from the original code snippet:

if (result.HasValue)
  return result;

return TokenListParserResult.Empty<TKind, T>(result.Remainder, result.SubTokenErrorPosition, messageGenerator(result));

it will not get to our error message if it does have a value and returns before that. The intention was to display the value the previous Parse.Not rejected, but it is not available here, and because of this the entire idea is suddenly much less appealing.

As a workaround, the existent functionality probably suffice like this:

TokenListParser<Argument, Unit> value = Superpower.Parse.Not(Token.Matching<Argument>(x => x.ArgumentType == ArgumentType.Value, "value"))
               .Named("switch");

That would produce unexpected argument 'not_a_switch', expected switch.

Now if we only could also solve #142, the entire thing would be sorted out. I will close this for now.

To recap the scenario I’m trying to work out: a token only has 4 ways to be parsed: Binary Switch, Value Switch, List Switch and Value (source that might explain that better), we do not expect Value at certain stages during the parsing, and we want to detect this situation early to give a good error message. Current we are getting the error message “AtEnd” when parser has failed to progress for any of the switches.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Better Messages – Live Chat for WordPress, BuddyPress ...
Better Messages – is realtime private messaging system for WordPress, BuddyPress, BuddyBoss Platform, Ultimate Member, PeepSo and any other WordPress ...
Read more >
Better Messages: Realtime private messaging system for ...
Take engagement of your website users to the next level with realtime chat features, private video and audio call, group video calls and...
Read more >
Better Messages – Live Chat for WordPress, BuddyPress ...
Better Messages – is realtime private messaging system for WordPress, BuddyPress, BuddyBoss Platform, Ultimate Member, PeepSo and any other ...
Read more >
BP Better Messages
BP Better Messages – is a fully-featured private messaging system for WordPress and BuddyPress. Now its also compatible with WC Vendors, providing a...
Read more >
BP Better Messages Add On Plugin
BP Better Messages is a fully featured replacement for standard BuddyPress Messages. The plugin is fully backward compatible with BuddyPress Messages and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found