Created
June 13, 2018 20:36
-
-
Save jmlane/2a584758f8d178fd3b6246ff50278a42 to your computer and use it in GitHub Desktop.
Pidgin Parsers that parse keywords from surrounding text, but also keep the consumed text input.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var output = new System.Text.StringBuilder(); | |
var parser = Map((text, keyword) => | |
{ | |
if (text.HasValue) | |
output.Append(text.GetValueOrDefault()); | |
if (keyword.HasValue) | |
output.Append(GetStringFromKeyword(keyword.GetValueOrDefault())); | |
return Unit.Value; | |
}, | |
Any.AtLeastOnceString().Optional(), | |
OneOf(String("BeginA"), String("BeginB")) // etc... | |
.Optional() | |
).SkipUntil(End()); | |
// Do something with output. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var output = new System.Text.StringBuilder(); | |
var parser = Map((other, declaration) => | |
{ | |
if (other.HasValue) | |
output.Append(other.GetValueOrDefault()); | |
if (declaration.HasValue) | |
{ | |
var code = declaration.GetValueOrDefault(); | |
if (code.IsValid()) | |
// Runtime processing of declaration... | |
else | |
output.Append('#').Append(code); | |
} | |
return Unit.Value; | |
}, | |
AnyCharExcept('#').AtLeastOnceString().Optional(), | |
Token('#') | |
.Then(Token(c => !char.IsWhiteSpace(c)).AtLeastOnceString()) | |
.Optional() | |
).SkipUntil(End()); | |
// Do something with output. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment