internal DeclareNode(ValueProviderBase valueProvider, Action<bool, string> addError) { if (!valueProvider.Variable.HasText()) addError(true, "declare[{0}] should end with 'as $someVariable'".FormatWith(valueProvider.ToString())); this.ValueProvider = valueProvider; }
public void CreateNodes() { foreach (var root in document.AllRootElements()) { var lists = root.Descendants <MatchNode>().ToList(); foreach (var matchNode in lists) { var m = matchNode.Match; var expr = m.Groups["expr"].Value; var keyword = m.Groups["keyword"].Value; var variable = m.Groups["dec"].Value; switch (keyword) { case "": var s = TemplateUtils.SplitToken(expr); if (s == null) { AddError(true, "{0} has invalid format".FormatWith(expr)); } else { var vp = ValueProviderBase.TryParse(s.Value.Token, variable, this); matchNode.Parent.ReplaceChild(new TokenNode(matchNode.NodeProvider, vp !, s.Value.Format !) { RunProperties = (OpenXmlCompositeElement?)matchNode.RunProperties?.CloneNode(true) }, matchNode); DeclareVariable(vp); } break;
internal AnyNode(INodeProvider nodeProvider, ValueProviderBase valueProvider, string operation, string value, Action <bool, string> addError) : base(nodeProvider) { this.ValueProvider = valueProvider; this.Operation = FilterValueConverter.ParseOperation(operation); this.Value = value; ValueProvider?.ValidateConditionValue(value, Operation, addError); }
public ForeachNode(INodeProvider nodeProvider, ValueProviderBase valueProvider) : base(nodeProvider) { this.ValueProvider = valueProvider; if (valueProvider != null) { valueProvider.IsForeach = true; } }
public ForeachNode(ForeachNode original) : base(original) { this.ValueProvider = original.ValueProvider; this.ForeachToken = original.ForeachToken.CloneNode(); this.EndForeachToken = original.EndForeachToken.CloneNode(); this.ForeachBlock = (BlockNode?)original.ForeachBlock?.Let(a => a.CloneNode(true)); }
internal DeclareNode(INodeProvider nodeProvider, ValueProviderBase valueProvider, Action <bool, string> addError) : base(nodeProvider) { if (!valueProvider.Variable.HasText()) { addError(true, "declare{0} should end with 'as $someVariable'".FormatWith(valueProvider.ToString())); } this.ValueProvider = valueProvider; }
internal IfNode(ValueProviderBase valueProvider, string operation, string value, Action <bool, string> addError) { this.ValueProvider = valueProvider; this.Operation = FilterValueConverter.ParseOperation(operation); this.Value = value; ValueProvider.ValidateConditionValue(value, Operation, addError); this.IfBlock = new BlockNode(this); }
void DeclareVariable(ValueProviderBase token) { if (token?.Variable.HasText() == true) { if (variables.ContainsKey(token.Variable)) { AddError(true, "There's already a variable '{0}' defined in this scope".FormatWith(token.Variable)); } variables.Add(token.Variable, token); } }
public static ConditionBase ParseCondition(string expr, string variable, ITemplateParser parser) { expr = expr.Trim(); var left = expr.TryBefore("||"); if (left != null) { return(new ConditionOr( ParseCondition(left, variable, parser), ParseCondition(expr.After("||"), variable, parser))); } left = expr.TryBefore(" OR "); if (left != null) { return(new ConditionOr( ParseCondition(left, variable, parser), ParseCondition(expr.After(" OR "), variable, parser))); } left = expr.TryBefore("&&"); if (left != null) { return(new ConditionAnd( ParseCondition(left, variable, parser), ParseCondition(expr.After("&&"), variable, parser))); } left = expr.TryBefore(" AND "); if (left != null) { return(new ConditionAnd( ParseCondition(left, variable, parser), ParseCondition(expr.After(" AND "), variable, parser))); } var filter = TemplateUtils.TokenOperationValueRegex.Match(expr); if (!filter.Success) { return(new ConditionCompare(ValueProviderBase.TryParse(expr, variable, parser) !)); } else { var vpb = ValueProviderBase.TryParse(filter.Groups["token"].Value, variable, parser); var operation = filter.Groups["operation"].Value; var value = filter.Groups["value"].Value; return(new ConditionCompare(vpb, operation, value, parser.AddError)); } }
public AnyNode(AnyNode original) : base(original) { this.ValueProvider = original.ValueProvider; this.Operation = original.Operation; this.Value = original.Value; this.AnyToken = original.AnyToken.CloneNode(); this.NotAnyToken = original.NotAnyToken.CloneNode(); this.EndAnyToken = original.EndAnyToken.CloneNode(); this.AnyBlock = (BlockNode)original.AnyBlock?.Let(a => a.CloneNode(true)); this.NotAnyBlock = (BlockNode)original.NotAnyBlock?.Let(a => a.CloneNode(true)); }
public IfNode(IfNode original) : base(original) { this.ValueProvider = original.ValueProvider; this.Operation = original.Operation; this.Value = original.Value; this.IfToken = original.IfToken.CloneNode(); this.ElseToken = original.ElseToken.CloneNode(); this.EndIfToken = original.EndIfToken.CloneNode(); this.IfBlock = (BlockNode)original.IfBlock?.Let(a => a.CloneNode(true)); this.ElseBlock = (BlockNode)original.ElseBlock?.Let(a => a.CloneNode(true)); }
void DeclareVariable(ValueProviderBase token) { if (token?.Variable.HasText() == true) { if (variables.TryGetValue(token.Variable, out ValueProviderBase t)) { if (!t.Equals(token)) { AddError(true, "There's already a variable '{0}' defined in this scope".FormatWith(token.Variable)); } } else { variables.Add(token.Variable, token); } } }
internal TokenNode(TokenNode original) : base(original) { this.ValueProvider = original.ValueProvider; this.Format = original.Format; }
internal ValueNode(ValueProviderBase valueProvider, string format, bool isRaw) { this.ValueProvider = valueProvider; this.Format = format; this.IsRaw = isRaw; }
internal IfNode(INodeProvider nodeProvider, ValueProviderBase valueProvider) : base(nodeProvider) { this.ValueProvider = valueProvider; }
internal TokenNode(ValueProviderBase valueProvider, string format) { this.ValueProvider = valueProvider; this.Format = format; }
internal IfNode(ValueProviderBase valueProvider, string operation, string value, Action<bool, string> addError) { this.ValueProvider = valueProvider; this.Operation = FilterValueConverter.ParseOperation(operation); this.Value = value; ValueProvider.ValidateConditionValue(value, Operation, addError); }
public AnyNode(AnyNode original) : base(original) { this.ValueProvider = original.ValueProvider; this.Operation = original.Operation; this.Value = original.Value; this.AnyToken = original.AnyToken.CloneNode(); this.NotAnyToken = original.NotAnyToken.CloneNode(); this.EndAnyToken = original.EndAnyToken.CloneNode(); this.AnyBlock = (BlockNode)original.AnyBlock.Try(a => a.CloneNode(true)); this.NotAnyBlock = (BlockNode)original.NotAnyBlock.Try(a => a.CloneNode(true)); }
public ForeachNode(ForeachNode original) : base(original) { this.ValueProvider = original.ValueProvider; this.ForeachToken = original.ForeachToken.CloneNode(); this.EndForeachToken = original.EndForeachToken.CloneNode(); this.ForeachBlock = (BlockNode)original.ForeachBlock.Try(a => a.CloneNode(true)); }
internal TokenNode(INodeProvider nodeProvider, ValueProviderBase valueProvider, string format) : base(nodeProvider) { this.ValueProvider = valueProvider; this.Format = format; }
void DeclareVariable(ValueProviderBase token) { if (token.Variable.HasText()) { if (variables.ContainsKey(token.Variable)) AddError(true, "There's already a variable '{0}' defined in this scope".FormatWith(token.Variable)); variables.Add(token.Variable, token); } }
public ForeachNode(ValueProviderBase valueProvider) { this.ValueProvider = valueProvider; valueProvider.IsForeach = true; }
void ParseInternal() { try { this.mainBlock = new BlockNode(null); this.stack = new Stack <BlockNode>(); this.errors = new List <TemplateError>(); PushBlock(mainBlock); var matches = TemplateUtils.KeywordsRegex.Matches(text); if (matches.Count == 0) { stack.Peek().Nodes.Add(new LiteralNode { Text = text }); stack.Pop(); return; } int index = 0; foreach (Match match in matches) { if (index < match.Index) { stack.Peek().Nodes.Add(new LiteralNode { Text = text.Substring(index, match.Index - index) }); } var type = match.Groups["type"].Value; var token = match.Groups["token"].Value; var keyword = match.Groups["keyword"].Value; var dec = match.Groups["dec"].Value; switch (keyword) { case "": case "raw": var s = TemplateUtils.SplitToken(token); if (s == null) { AddError(true, "{0} has invalid format".FormatWith(token)); } else { var t = TryParseValueProvider(type, s.Value.Token, dec); stack.Peek().Nodes.Add(new ValueNode(t, s.Value.Format, isRaw: keyword.Contains("raw"))); DeclareVariable(t); } break; case "declare": { var t = TryParseValueProvider(type, token, dec); stack.Peek().Nodes.Add(new DeclareNode(t, this.AddError)); DeclareVariable(t); } break; case "any": { AnyNode any; ValueProviderBase vp; var filter = TemplateUtils.TokenOperationValueRegex.Match(token); if (!filter.Success) { vp = TryParseValueProvider(type, token, dec); any = new AnyNode(vp); } else { vp = TryParseValueProvider(type, filter.Groups["token"].Value, dec); var comparer = filter.Groups["comparer"].Value; var value = filter.Groups["value"].Value; any = new AnyNode(vp, comparer, value, this.AddError); } stack.Peek().Nodes.Add(any); PushBlock(any.AnyBlock); DeclareVariable(vp); break; } case "notany": { var an = (AnyNode)PopBlock(typeof(AnyNode)).owner; if (an != null) { PushBlock(an.CreateNotAny()); } break; } case "endany": { PopBlock(typeof(AnyNode)); break; } case "foreach": { ValueProviderBase vp = TryParseValueProvider(type, token, dec); var fn = new ForeachNode(vp); stack.Peek().Nodes.Add(fn); PushBlock(fn.Block); vp.IsForeach = true; DeclareVariable(vp); break; } case "endforeach": { PopBlock(typeof(ForeachNode)); } break; case "if": { IfNode ifn; ValueProviderBase vp; var filter = TemplateUtils.TokenOperationValueRegex.Match(token); if (!filter.Success) { vp = TryParseValueProvider(type, token, dec); ifn = new IfNode(vp, this); } else { vp = TryParseValueProvider(type, filter.Groups["token"].Value, dec); var comparer = filter.Groups["comparer"].Value; var value = filter.Groups["value"].Value; ifn = new IfNode(vp, comparer, value, this.AddError); } stack.Peek().Nodes.Add(ifn); PushBlock(ifn.IfBlock); DeclareVariable(vp); break; } case "else": { var ifn = (IfNode)PopBlock(typeof(IfNode)).owner; if (ifn != null) { PushBlock(ifn.CreateElse()); } break; } case "endif": { PopBlock(typeof(IfNode)); break; } default: AddError(true, "'{0}' is deprecated".FormatWith(keyword)); break; } index = match.Index + match.Length; } if (stack.Count != 1) { AddError(true, "Last block is not closed: {0}".FormatWith(stack.Peek())); } var lastM = matches.Cast <Match>().LastOrDefault(); if (lastM != null && lastM.Index + lastM.Length < text.Length) { stack.Peek().Nodes.Add(new LiteralNode { Text = text.Substring(lastM.Index + lastM.Length) }); } stack.Pop(); } catch (Exception e) { AddError(true, e.Message); } }
public AnyNode(INodeProvider nodeProvider, ValueProviderBase valueProvider) : base(nodeProvider) { this.ValueProvider = valueProvider; }
public DeclareNode(DeclareNode original) : base(original) { this.ValueProvider = original.ValueProvider; }
public ForeachNode(ValueProviderBase valueProvider) { this.ValueProvider = valueProvider; }
internal IfNode(ValueProviderBase valueProvider, TemplateWalker walker) { this.ValueProvider = valueProvider; this.IfBlock = new BlockNode(this); }
public AnyNode(ValueProviderBase valueProvider) { this.ValueProvider = valueProvider; }
internal AnyNode(ValueProviderBase valueProvider) { this.ValueProvider = valueProvider; AnyBlock = new BlockNode(this); }
internal IfNode(ValueProviderBase valueProvider) { this.ValueProvider = valueProvider; }
public ForeachNode(ValueProviderBase valueProvider) { this.ValueProvider = valueProvider; this.Block = new BlockNode(this); }
public IfNode(IfNode original) : base(original) { this.ValueProvider = original.ValueProvider; this.Operation = original.Operation; this.Value = original.Value; this.IfToken = original.IfToken.CloneNode(); this.ElseToken = original.ElseToken.CloneNode(); this.EndIfToken = original.EndIfToken.CloneNode(); this.IfBlock = (BlockNode)original.IfBlock.Try(a => a.CloneNode(true)); this.ElseBlock = (BlockNode)original.ElseBlock.Try(a => a.CloneNode(true)); }
void ParseInternal() { try { this.mainBlock = new BlockNode(null); this.stack = new Stack <BlockNode>(); this.errors = new List <TemplateError>(); PushBlock(mainBlock); var matches = TemplateUtils.KeywordsRegex.Matches(text); if (matches.Count == 0) { stack.Peek().Nodes.Add(new LiteralNode(text)); stack.Pop(); return; } int index = 0; foreach (Match match in matches.Cast <Match>()) { if (index < match.Index) { stack.Peek().Nodes.Add(new LiteralNode(text.Substring(index, match.Index - index))); } var expr = match.Groups["expr"].Value; var keyword = match.Groups["keyword"].Value; var variable = match.Groups["dec"].Value; switch (keyword) { case "": case "raw": var s = TemplateUtils.SplitToken(expr); if (s == null) { AddError(true, "{0} has invalid format".FormatWith(expr)); } else { var t = ValueProviderBase.TryParse(s.Value.Token, variable, this); stack.Peek().Nodes.Add(new ValueNode(t, s.Value.Format, isRaw: keyword.Contains("raw"))); DeclareVariable(t); } break; case "declare": { var t = ValueProviderBase.TryParse(expr, variable, this); stack.Peek().Nodes.Add(new DeclareNode(t, this.AddError)); DeclareVariable(t); } break; case "any": { ConditionBase cond = TemplateUtils.ParseCondition(expr, variable, this); AnyNode any = new AnyNode(cond); stack.Peek().Nodes.Add(any); PushBlock(any.AnyBlock); if (cond is ConditionCompare cc) { DeclareVariable(cc.ValueProvider); } break; } case "notany": { var an = (AnyNode?)PopBlock(typeof(AnyNode))?.owner; if (an != null) { PushBlock(an.CreateNotAny()); } break; } case "endany": { PopBlock(typeof(AnyNode)); break; } case "foreach": { ValueProviderBase?vp = ValueProviderBase.TryParse(expr, variable, this); if (vp is TokenValueProvider tvp && tvp.ParsedToken.QueryToken != null && QueryToken.IsCollection(tvp.ParsedToken.QueryToken.Type)) { AddError(false, $"@foreach[{expr}] is a collection, missing 'Element' token at the end"); } var fn = new ForeachNode(vp); stack.Peek().Nodes.Add(fn); PushBlock(fn.Block); if (vp != null) { vp.IsForeach = true; } DeclareVariable(vp); break; } case "endforeach": { PopBlock(typeof(ForeachNode)); } break; case "if": { ConditionBase cond = TemplateUtils.ParseCondition(expr, variable, this); IfNode ifn = new IfNode(cond, this); stack.Peek().Nodes.Add(ifn); PushBlock(ifn.IfBlock); if (cond is ConditionCompare cc) { DeclareVariable(cc.ValueProvider); } break; } case "else": { var ifn = (IfNode?)PopBlock(typeof(IfNode))?.owner; if (ifn != null) { PushBlock(ifn.CreateElse()); } break; } case "endif": { PopBlock(typeof(IfNode)); break; } default: AddError(true, "'{0}' is deprecated".FormatWith(keyword)); break; } index = match.Index + match.Length; } if (stack.Count != 1) { AddError(true, "Last block is not closed: {0}".FormatWith(stack.Peek())); } var lastM = matches.Cast <Match>().LastOrDefault(); if (lastM != null && lastM.Index + lastM.Length < text.Length) { stack.Peek().Nodes.Add(new LiteralNode(text.Substring(lastM.Index + lastM.Length))); } stack.Pop(); } catch (Exception e) { AddError(true, e.Message); } }
public ValueProviderBase TryParseValueProvider(string type, string token, string variable) { return(ValueProviderBase.TryParse(type, token, variable, this.modelType, piSystemEmail, this.qd, this.variables, this.AddError)); }
public ValueProviderBase TryParseValueProvider(string type, string token, string variable) { return(ValueProviderBase.TryParse(type, token, variable, this.SystemWordTemplateType, piSystemWordTemplate, this.queryDescription, this.variables, this.AddError)); }