parlot icon indicating copy to clipboard operation
parlot copied to clipboard

Question

Open Ellested opened this issue 3 years ago • 2 comments

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?

Ellested avatar Apr 19 '21 07:04 Ellested

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().

sebastienros avatar Apr 27 '21 17:04 sebastienros

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.

Ellested avatar May 04 '21 07:05 Ellested