otac0n / Pegasus

A PEG parser generator for .NET that integrates with MSBuild and Visual Studio.

Home Page:http://otac0n.com/Pegasus/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dynamic literal

david-pfx opened this issue · comments

So we have code assertions to control parsing, and code expressions to define a return value.

I propose code Literals, which represent C# code that returns a literal value, as a string, to be used by the parser.

EndLine = ";"
CommentMarker = "#"

becomes

EndLine = %{ GetEndLine() }
CommentMarker = %{ GetCommentMarker() }

The point is to make dynamic literals, set externally by a configuration file or internally during the parse. The examples given are real, taken from a current project.

You can kind-of do this via three approaches,

  • resource strings
    • If you want to swap out predefined strings, consider using resource strings. This was designed to support language translation for Gherkin specifically.
  • wildcard + assertion (. &{ ... })*
    • Very powerful approach for regular strings or PEG-parseable strings.
  • #PARSE expressions
    • Full control over the cursor and parse result. Shell out to some other parser and use the length of what is returned.

I am still not closing this issue because I think your suggestion is worth considering as an extension.