You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First and foremost, thanks for the great library (and for making it Core compatible!). Everything has worked fantastically out of the box as advertised.
A potential feature that could be nice (it would certainly be useful for me) would be the ability to hook error messages into different parsers to make the reasons behind parse failures more human-readable.
For example, if I have a parser that looks something like this:
publicstaticreadonlySprache.Parser<Property>Property=from_in Parse.String(" ")from variable inVariablefromdeclinParse.String(SyntaxElement.TypeDeclaration)from typeSpec inTypeSpecfrom end inParse.String(SyntaxElement.EndLine)selectnew Property(variable,typeSpec);
public staticreadonlySprache.Parser<Data>Data=fromkwydinParse.String(ReservedKeywords.Data)from_inParse.Char(' ')fromtypeSpecinTypeSpecfrombeginBlockinParse.String(SyntaxElement.BeginBlock)frompropertiesinParse.AtLeastOnce(Property)fromendBlockinParse.String(SyntaxElement.EndBlock)selectnewData(typeSpec,properties);
And I attempt to parse a string that looks like this (missing the expected Property from the AtLeastOnce):
data Example{
}
The error message I currently get is:
Unhandled Exception: Sprache.ParseException: Parsing failure: unexpected '}'; expected (Line 2, Column 1); recently consumed: a Example {
at Sprache.ParserExtensions.Parse[T](Parser`1 parser, String input)
at JKL.Parser.JKLParser.ParseFile(String text) in /Users/jkaye/src/JKL/JKL/Parser/Parser.cs:line 41
at JKL.Program.Main(String[] args) in /Users/jkaye/src/JKL/JKL/Program.cs:line 16
I think this is a pretty good error message for the developer. I've consumed some token and then hit another one before hitting the expected two spaces from the Property parser; however, it would be awesome if I were able to write the parser like this (or something equivalent):
publicstaticreadonlySprache.Parser<Property>Property=from_in Parse.String(" ")from variable inVariable.OnFailure("Property definition must begin with variable name")fromdeclinParse.String(SyntaxElement.TypeDeclaration)fromtypeSpecinTypeSpec.OnFailure("Missing variable type specification")fromendinParse.String(SyntaxElement.EndLine)selectnewProperty(variable,typeSpec);publicstaticreadonlySprache.Parser<Data>Data=fromkwydinParse.String(ReservedKeywords.Data)from_inParse.Char(' ')fromtypeSpecinTypeSpec.OnFailure("Missing data type name")frombeginBlockinParse.String(SyntaxElement.BeginBlock).OnFailure("Expected block begin")frompropertiesinParse.AtLeastOnce(Property).OnFailure("Expected at least one Property definition")fromendBlockinParse.String(SyntaxElement.EndBlock).OnFailure("Missing block end")selectnewData(typeSpec,properties);
And then get an error message like this from the same input:
Parse failure at line 2, column 1: Expected at least one Property definition
I haven't looked at the internal code much to know if this is easily do-able without a lot of effort. I was thinking you could propagate the current exception until you hit one of these OnFailure handles at which point you would replace the error message with the one provided by the first OnFailure that you hit. Do you have an idea of how difficult this would be to accomplish within the Sprache framework as it currently exists? I'd be willing to give it a shot if you think agree that this is something that would be possible and useful.
Thanks again!
The text was updated successfully, but these errors were encountered:
internalstaticreadonlyParser<IReadOnlyList<object>>Cat=(fromcinParse.Char('c').ErrorMessage("GIVE ME A 'C'!")fromainParse.Char('a').ErrorMessage("GIVE ME A 'A'!")fromtinParse.Char('t').ErrorMessage("GIVE ME A 'T'!")selectnewList<object>(0)).End();
But the error message gets lost when used in combination with Optional() and End().
@Odepax thanks for the follow-up, and sorry about the very slow reply, here! We're light on maintainer time in this project and so there isn't a lot of bandwidth to for us to dig in further. A PR with a complete example would still be more than welcome.
First and foremost, thanks for the great library (and for making it Core compatible!). Everything has worked fantastically out of the box as advertised.
A potential feature that could be nice (it would certainly be useful for me) would be the ability to hook error messages into different parsers to make the reasons behind parse failures more human-readable.
For example, if I have a parser that looks something like this:
And I attempt to parse a string that looks like this (missing the expected
Property
from theAtLeastOnce
):The error message I currently get is:
I think this is a pretty good error message for the developer. I've consumed some token and then hit another one before hitting the expected two spaces from the
Property
parser; however, it would be awesome if I were able to write the parser like this (or something equivalent):And then get an error message like this from the same input:
I haven't looked at the internal code much to know if this is easily do-able without a lot of effort. I was thinking you could propagate the current exception until you hit one of these
OnFailure
handles at which point you would replace the error message with the one provided by the firstOnFailure
that you hit. Do you have an idea of how difficult this would be to accomplish within the Sprache framework as it currently exists? I'd be willing to give it a shot if you think agree that this is something that would be possible and useful.Thanks again!
The text was updated successfully, but these errors were encountered: