private SyntaxVisitorResult <IN, OUT> Visit(SyntaxNode <IN> node, object context = null) { var result = SyntaxVisitorResult <IN, OUT> .NoneResult(); if (node.Visitor != null || node.IsByPassNode) { var args = new List <object>(); var i = 0; foreach (var n in node.Children) { var v = Visit(n, context); if (v.IsToken) { if (!v.Discarded) { args.Add(v.TokenResult); } } else if (v.IsValue) { args.Add(v.ValueResult); } i++; } if (node.IsByPassNode) { result = SyntaxVisitorResult <IN, OUT> .NewValue((OUT)args[0]); } else { MethodInfo method = null; try { if (!(context is NoContext)) { args.Add(context); } method = node.Visitor; var t = method?.Invoke(ParserVsisitorInstance, args.ToArray()); var res = (OUT)t; result = SyntaxVisitorResult <IN, OUT> .NewValue(res); } catch (Exception e) { throw new ParserConfigurationException($"ERROR : calling visitor method {method?.Name} with {node.Name}"); } } } return(result); }
private SyntaxVisitorResult <IN, OUT> Visit(SyntaxNode <IN> node, object context = null) { var result = SyntaxVisitorResult <IN, OUT> .NoneResult(); if (node.Visitor != null || node.IsByPassNode) { var args = new List <object>(); foreach (var n in node.Children) { var v = Visit(n, context); if (v.IsToken) { if (!n.Discarded) { args.Add(v.TokenResult); } } else if (v.IsValue) { args.Add(v.ValueResult); } else if (v.IsOption) { args.Add(v.OptionResult); } else if (v.IsOptionGroup) { args.Add(v.OptionGroupResult); } else if (v.IsGroup) { args.Add(v.GroupResult); } else if (v.IsTokenList) { args.Add(v.TokenListResult); } else if (v.IsValueList) { args.Add(v.ValueListResult); } else if (v.IsGroupList) { args.Add(v.GroupListResult); } } if (node.IsByPassNode) { result = SyntaxVisitorResult <IN, OUT> .NewValue((OUT)args[0]); } else { MethodInfo method = null; try { if (!(context is NoContext)) { args.Add(context); } if (method == null) { method = node.Visitor; } var t = method.Invoke(ParserVsisitorInstance, args.ToArray()); var res = (OUT)t; result = SyntaxVisitorResult <IN, OUT> .NewValue(res); } catch (TargetInvocationException tie) { if (tie.InnerException != null) { throw tie.InnerException; } } catch (Exception e) { throw e; } } } return(result); }
private SyntaxVisitorResult <IN, OUT> Visit(SyntaxNode <IN> node, object context = null) { var result = SyntaxVisitorResult <IN, OUT> .NoneResult(); if (node.Visitor != null || node.IsByPassNode) { var args = new List <object>(); foreach (var n in node.Children) { var v = Visit(n, context); if (v.IsToken) { if (!n.Discarded) { args.Add(v.TokenResult); } } else if (v.IsValue) { args.Add(v.ValueResult); } else if (v.IsOption) { args.Add(v.OptionResult); } else if (v.IsOptionGroup) { args.Add(v.OptionGroupResult); } else if (v.IsGroup) { args.Add(v.GroupResult); } else if (v.IsTokenList) { args.Add(v.TokenListResult); } else if (v.IsValueList) { args.Add(v.ValueListResult); } else if (v.IsGroupList) { args.Add(v.GroupListResult); } } if (node.IsByPassNode) { result = SyntaxVisitorResult <IN, OUT> .NewValue((OUT)args[0]); } else { MethodInfo method = null; try { if (method == null) { method = node.Visitor; } var t = method.Invoke(ParserVsisitorInstance, args.ToArray()); var res = (OUT)t; result = SyntaxVisitorResult <IN, OUT> .NewValue(res); } catch (Exception e) { Console.WriteLine($"OUTCH {e.Message} calling {node.Name} => {method.Name}"); } } } return(result); }