public override void Parse(ParsingContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            Children.Clear();

            int from = context.GetIntValue(From);
            int to   = context.GetIntValue(To);
            int step = context.GetIntValue(Step);

            if (step == 0)
            {
                throw  new ParserException("Invalid Loop: step cannot be 0", this);
            }
            if (step > 0 && to < from)
            {
                throw new ParserException("Invalid loop", this);
            }
            if (step < 0 && to > from)
            {
                throw new ParserException("Invalid loop", this);
            }

            int index = from;

            while (index * step <= to * step)
            {
                var gp = new GroupResult(index.ToString(CultureInfo.InvariantCulture));
                Children.Add(gp);
                gp.Children.AddRange(Template.Clone());

                index = index + step;
            }
        }
示例#2
0
        private void BuildResultTreeNodes(ParsingResultNodeList prnl, FieldCollectionBase confItems)
        {
            foreach (FieldElementBase item in confItems)
            {
                if (item is ConstantElement)
                {
                    var element = (ConstantElement)item;
                    prnl.Add(new ConstantResult(element));
                }
                else if (item is SwitchElement) // as switch is of field type, it must be checked before
                {
                    var element = (SwitchElement)item;
                    var sw      = new SwitchResult(element);
                    prnl.Add(sw);

                    foreach (GroupElement c in element.Cases)
                    {
                        if (c is CaseElement)
                        {
                            var caseE = (CaseElement)c;
                            var gp    = new GroupResult(caseE.Label);
                            sw.Cases.Add(caseE.When, gp);
                            BuildResultTreeNodes(gp.Children, caseE.Fields);
                        }
                        else if (c is ElseElement)
                        {
                            if (sw.Else != null)
                            {
                                throw new ParserException("Only 1 else case can be defined in a switch!");
                            }
                            var elseE = (ElseElement)c;
                            var gp    = new GroupResult(elseE.Label);
                            sw.Else = gp;
                            BuildResultTreeNodes(gp.Children, elseE.Fields);
                        }
                    }
                }
                else if (item is FieldElement)
                {
                    var element = (FieldElement)item;
                    prnl.Add(new FieldResult(element));
                }
                else if (item is LoopElement) // as loop is of type group, it must be checked before
                {
                    var element = (LoopElement)item;
                    var node    = new LoopResult(element);
                    prnl.Add(node);
                    BuildResultTreeNodes(node.Template, element.Fields);
                }
                else if (item is GroupElement)
                {
                    var element = (GroupElement)item;
                    var node    = new GroupResult(element);
                    prnl.Add(node);
                    BuildResultTreeNodes(node.Children, element.Fields);
                }
                else if (item is PartRefElement)
                {
                    var         element = (PartRefElement)item;
                    PartElement pe;
                    if (!Configuration.Parts.TryFindById(element.PartId, out pe))
                    {
                        throw new ParserException(Invariant($"Invalid partRef: unknow part id '{element.PartId}'"));
                    }
                    var node = new GroupResult(element.Label);
                    prnl.Add(node);
                    BuildResultTreeNodes(node.Children, pe.Fields);
                }
                else
                {
                    throw new ParserException(Invariant($"Unsupported field with name '{item.Label}'"));
                }
            }
        }