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
I'm doing a lot of custom parsing logic. I am not sure if I'm going about it correctly, but what I have seems to work in the successful scenarios. Here's a small piece of it:
privatevoidParseAndAdd<TStart,TEnd>(IParserparser,ICollection<T>configs)whereTStart:ParsingEventwhereTEnd:ParsingEvent{
parser.Consume<TStart>();while(!parser.TryConsume<TEnd>(out _)){try{varlineNumber= parser.Current?.Start.Line;string?instanceName=null;if(parser.TryConsume<Scalar>(outvar key)){instanceName= key.Value;}varnewConfig= _deserializer.Deserialize<T>(parser);
newConfig.Name =instanceName??"";varresult= _validator.Validate(newConfig);if(result is{IsValid:false}){varprintableName=instanceName?? FlurlLogging.SanitizeUrl(newConfig.BaseUrl);
_log.Error("Validation failed for instance config {Instance} at line {Line} with errors {Errors}",
printableName, lineNumber, result.Errors);return;}
configs.Add(newConfig);}catch(YamlExceptione){
HandleException(e);
parser.SkipThisAndNestedEvents();}}}
The purpose of this method is to either parse a series of sibling mappings or sequence elements. I do not use List or Dictionary for this since I need more granular control of what happens.
The question is: On the line where I do Deserialize<T>(), if that throws, then the catch statement is not able to recover properly. I do a SkipThisAndNestedEvents() but what I really need to do is return to a particular parent node, and then skip it and its events. As of right now, it doesn't recover and position the parser at the correct "next" sibling element.
I also do not see a mechanism to do push/pop semantics on the parser. I could very easily do a "Push" of some kind at the start of the while loop and use that to "pop" on error (to return to that point) but I don't see a way to do that either (for example, Current is not assignable)
I thought about maybe using a INodeDeserializer, but I don't know if that would solve anything and it would also make what I'm doing even more complicated to reason about.
How can I exit back to a parent node in the parsing tree for error recovery purposes?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm doing a lot of custom parsing logic. I am not sure if I'm going about it correctly, but what I have seems to work in the successful scenarios. Here's a small piece of it:
The purpose of this method is to either parse a series of sibling mappings or sequence elements. I do not use
List
orDictionary
for this since I need more granular control of what happens.The question is: On the line where I do
Deserialize<T>()
, if that throws, then the catch statement is not able to recover properly. I do aSkipThisAndNestedEvents()
but what I really need to do is return to a particular parent node, and then skip it and its events. As of right now, it doesn't recover and position the parser at the correct "next" sibling element.I also do not see a mechanism to do push/pop semantics on the parser. I could very easily do a "Push" of some kind at the start of the while loop and use that to "pop" on error (to return to that point) but I don't see a way to do that either (for example,
Current
is not assignable)I thought about maybe using a
INodeDeserializer
, but I don't know if that would solve anything and it would also make what I'm doing even more complicated to reason about.How can I exit back to a parent node in the parsing tree for error recovery purposes?
Beta Was this translation helpful? Give feedback.
All reactions