parlot
parlot copied to clipboard
Question
void Main()
{
Parsers.MyText.Parse("'text'").Dump();
//Fails Parsers.ManyMyText.Parse("").Dump();
//Fails Parsers.ManyMyText.Parse("'text'").Dump();
//Fails Parsers.ManyMyText.Parse("'text''text'").Dump();
}
public static class Parsers
{
public static Parser<string> MyText = Terms.String(StringLiteralQuotes.SingleOrDouble).Then(s => s.ToString()).ElseError("Text");
public static Parser<List<string>> ManyMyText = ZeroOrMany(MyText);
}
The ZeroOrMany works if I remove ElseError() from the MyText parser. But if you want to provide specific error informaton on whether MyText is valid or not, how are you supposed to do it?
MyText is valid or not
I need more details. Not sure if you are trying to validate the content of the string. If so you could throw a ParseException inside the Then
lambda
Otherwise it's ambiguous what errors you could catch, because if you put .ElseError
on MyText
you will always have an error with ZeroOrMany
because this one will always end on a MyText
that is not found (or EOF).
If you want at least one MyText
, I would use OneOrMany(MyText).ElseError()
.
Maybe I should try and explain this in another way.
Say you have a HTML tag, which can have zero or many attributes. If the attribute was not specified correctly I would like to end up with a parse error on that specific attribute.
I made the example above just to simplify the problem, where each string literal is just substituted for an attribute parser.
So I'd like to accept any number of attributes if they exist, but in case one of them doesn't parse fully, I'd like to provide an error message on that specific attribute.
So I thought ElseError was the correct way of introducing the error handling.
Does it makes sense, or should I try to create a better example.