Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:44:23+00:00 2026-06-10T14:44:23+00:00

I have written a parser-generator that has expressions like this: a = f:foo bar

  • 0

I have written a parser-generator that has expressions like this:

a = f:"foo" "bar" { /* your code here */ }

Which, after a fashion, generates code that looks about like this:

r0 = this.ReturnHelper<string>(cursor, cursor, () => /* your code here */);

This is perfect, because the users can specify an expression, like f to just return a value, or they can add an extra set of curly braces and write a method. So far so good.

Now, I also allow assertions like this:

a = w:word !{ w == "disallowed" }

Which compiles into something like this, currently:

var w = this.ParseWord(...).Value;

...

if (!( w == "disallowed" ))
{
    r2 = new ParseResult<string>(cursor, cursor, string.Empty);
}

I would LIKE to have the assertions support the same method-body option that the ReturnHelper provides, but it seems inefficient to have a method bool AssertionHelper(Func<bool> assertion) that just calls the predicate.

I was imagining that new Func<bool>(() => /* your code here */)() would work, but I’m not sure that it is the most efficient.

Any ideas?

EDIT

How about ((Func<bool>)(() => /* code */))()? Does that avoid an allocation? Nope, that generates identical IL to the above version.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:44:24+00:00Added an answer on June 10, 2026 at 2:44 pm

    It’s not clear why you need to mention Func<bool> explicitly at all. Why not just:

    this.AssertionHelper<string>(cursor, cursor, () => !( w == "disallowed" ))
    

    ? Assuming it’s going to be a single expression with no parameters and it’s of type bool, that should be fine.

    If it needs a parameter w, just change it to:

    this.AssertionHelper<string>(cursor, cursor, w => !( w == "disallowed" ))
    

    I believe the MS implementation of the C# compiler will avoid creating a new delegate instance on each invocation of this – but really, does it matter? Have you profiled your application to check whether this is actually a significant performance hit at all? If you have, that’s fine – and I’ve certainly seen some interesting data around some cases where micro-optimization for delegates can make a difference – but I wouldn’t worry about it until I’d found it’s an actual problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, i have written a simple python program that parses HTML with HTMLParser. Here
have written this little class, which generates a UUID every time an object of
I have written a JSON parser that takes any valid json string and generates
I have a fairly complex content management system I've written in PHP, which has
I have written some custom R code that wraps a third-party binary. One of
I have written a working token parser based on the code shown at spirit
I have written this code to try and stream a video to my Android
I have written an app and part of it is uses a URL parser
I am implementing parser for language similar to java but simpler. I have written
How do recursive ascent parsers work? I have written a recursive descent parser myself

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.