Exemplo n.º 1
0
 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));
 }
 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));
 }
Exemplo n.º 3
0
        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 type    = m.Groups["type"].Value;
                    var token   = m.Groups["token"].Value;
                    var keyword = m.Groups["keyword"].Value;
                    var dec     = m.Groups["dec"].Value;

                    switch (keyword)
                    {
                    case "":
                        var s = TemplateUtils.SplitToken(token);
                        if (s == null)
                        {
                            AddError(true, "{0} has invalid format".FormatWith(token));
                        }
                        else
                        {
                            var vp = TryParseValueProvider(type, s.Value.Token, dec);

                            matchNode.Parent.ReplaceChild(new TokenNode(matchNode.NodeProvider, vp, s.Value.Format)
                            {
                                RunProperties = (OpenXmlCompositeElement)matchNode.RunProperties?.CloneNode(true)
                            }, matchNode);

                            DeclareVariable(vp);
                        }
                        break;

                    case "declare":
                    {
                        var vp = TryParseValueProvider(type, token, dec);

                        matchNode.Parent.ReplaceChild(new DeclareNode(matchNode.NodeProvider, vp, this.AddError)
                            {
                                RunProperties = (OpenXmlCompositeElement)matchNode.RunProperties?.CloneNode(true)
                            }, matchNode);

                        DeclareVariable(vp);
                    }
                    break;

                    case "any":
                    {
                        AnyNode           any;
                        ValueProviderBase vp;
                        var filter = TemplateUtils.TokenOperationValueRegex.Match(token);
                        if (!filter.Success)
                        {
                            vp  = TryParseValueProvider(type, token, dec);
                            any = new AnyNode(matchNode.NodeProvider, vp)
                            {
                                AnyToken = new MatchNodePair(matchNode)
                            };
                        }
                        else
                        {
                            vp = TryParseValueProvider(type, filter.Groups["token"].Value, dec);
                            var comparer = filter.Groups["comparer"].Value;
                            var value    = filter.Groups["value"].Value;
                            any = new AnyNode(matchNode.NodeProvider, vp, comparer, value, this.AddError)
                            {
                                AnyToken = new MatchNodePair(matchNode)
                            };
                        }

                        PushBlock(any);

                        DeclareVariable(vp);
                        break;
                    }

                    case "notany":
                    {
                        var an = PeekBlock <AnyNode>();
                        if (an != null)
                        {
                            an.NotAnyToken = new MatchNodePair(matchNode);
                        }
                        break;
                    }

                    case "endany":
                    {
                        var an = PopBlock <AnyNode>();
                        if (an != null)
                        {
                            an.EndAnyToken = new MatchNodePair(matchNode);

                            an.ReplaceBlock();
                        }
                        break;
                    }

                    case "if":
                    {
                        IfNode            ifn;
                        ValueProviderBase vpb;
                        var filter = TemplateUtils.TokenOperationValueRegex.Match(token);
                        if (!filter.Success)
                        {
                            vpb = TryParseValueProvider(type, token, dec);
                            ifn = new IfNode(matchNode.NodeProvider, vpb)
                            {
                                IfToken = new MatchNodePair(matchNode)
                            };
                        }
                        else
                        {
                            vpb = TryParseValueProvider(type, filter.Groups["token"].Value, dec);
                            var comparer = filter.Groups["comparer"].Value;
                            var value    = filter.Groups["value"].Value;
                            ifn = new IfNode(matchNode.NodeProvider, vpb, comparer, value, this.AddError)
                            {
                                IfToken = new MatchNodePair(matchNode)
                            };
                        }

                        PushBlock(ifn);

                        DeclareVariable(vpb);

                        break;
                    }

                    case "else":
                    {
                        var an = PeekBlock <IfNode>();
                        if (an != null)
                        {
                            an.ElseToken = new MatchNodePair(matchNode);
                        }
                        break;
                    }

                    case "endif":
                    {
                        var ifn = PopBlock <IfNode>();
                        if (ifn != null)
                        {
                            ifn.EndIfToken = new MatchNodePair(matchNode);

                            ifn.ReplaceBlock();
                        }
                        break;
                    }

                    case "foreach":
                    {
                        var vp = TryParseValueProvider(type, token, dec);
                        var fn = new ForeachNode(matchNode.NodeProvider, vp)
                        {
                            ForeachToken = new MatchNodePair(matchNode)
                        };
                        PushBlock(fn);

                        DeclareVariable(vp);
                        break;
                    }

                    case "endforeach":
                    {
                        var fn = PopBlock <ForeachNode>();
                        if (fn != null)
                        {
                            fn.EndForeachToken = new MatchNodePair(matchNode);

                            fn.ReplaceBlock();
                        }
                        break;
                    }

                    default:
                        AddError(true, "'{0}' is deprecated".FormatWith(keyword));
                        break;
                    }
                }
            }
        }