Ejemplo n.º 1
0
 public void Should_not_parse_if_not_exists_variables()
 {
     var line = new TemplateLine("Dont exists variables here", this.parameters).Parse();
     line.Variables.ShouldBeEmpty();
     line.ForEach.ShouldBeNull();
     line.Content.ShouldEqual("Dont exists variables here");
 }
Ejemplo n.º 2
0
        internal bool TryGetNext(out LinePair linePair, TemplateLineEvaluationResult lastTemplateLineEvaluationResult)
        {
            bool         result             = false;
            Line         targetLine         = null;
            TemplateLine templateLine       = null;
            bool         targetLineReturned = targetIterator.TryGetNext(out targetLine);

            //If there are no more target lines then escape the current repeating line if there is one
            //and move through the rest of the template.
            if (targetLineReturned == false && lastTemplateLineEvaluationResult != null)
            {
                lastTemplateLineEvaluationResult.EscapeRepeat = true;
            }
            bool templateLineReturned = templateIterator.TryGetNext(out templateLine, lastTemplateLineEvaluationResult);

            if (targetLineReturned || templateLineReturned)
            {
                result   = true;
                linePair = factory.CreateLinePair(targetLine, templateLine);
            }
            else
            {
                linePair = null;
            }
            return(result);
        }
Ejemplo n.º 3
0
        public CachedTemplateLine(TemplateLine tl)
        {
            TemplateLine = tl;
            Sections     = tl.Sections.Select(x => {
                if (x is CaptureSection cs)
                {
                    // wrap in CachedCaptureSection in any case for the computed varname
                    if (cs.VarPath.Count == 0 && cs.Flags.HasFlag(CaptureFlags.SkipDataLineIfNotFound) || cs.Flags.HasFlag(CaptureFlags.SkipTemplateLineIfNotFound))
                    {
                        // if current CaptureSection doesn't actually capture
                        // we still need to keep track of its name to know if the current line match failed
                        // fake a capture name in this case
                        return(new CachedCaptureSection(new CaptureSection(cs.Pattern, new VariablePart[] { new ObjectVariablePart($"__ctl") }, cs.Flags)));
                    }
                    else
                    {
                        return(new CachedCaptureSection(cs));
                    }
                }
                return(x);
            }).ToList();
            var cachedCaptureSections = Sections.OfType <CachedCaptureSection>().ToList();

            Captures                        = cachedCaptureSections.Where(cs => cs.VarPath.Any(x => !x.Name.StartsWith("__"))).ToList();
            RegexString                     = ToRegex(Sections);
            RegexObject                     = new Regex(RegexString);
            AllCaptureGroups                = RegexObject.GetGroupNames().Where(g => !g.StartsWith("__") && !Regex.IsMatch(g, "^[0-9]*$")).ToList();
            SkipDataLineIfNotFound          = cachedCaptureSections.Any(x => x.Flags.HasFlag(CaptureFlags.SkipDataLineIfNotFound));
            SkipTemplateLineIfNotFound      = cachedCaptureSections.Any(x => x.Flags.HasFlag(CaptureFlags.SkipTemplateLineIfNotFound));
            RepeatTemplateLineUntilNotFound = cachedCaptureSections.Any(x => x.Flags.HasFlag(CaptureFlags.RepeatTemplateLineUntilNotFound));
        }
Ejemplo n.º 4
0
 public void Should_parse_template_line_with_one_variable()
 {
     var line = new TemplateLine("My name is ${name}", this.parameters).Parse();
     line.Variables.Contains("name").ShouldBeTrue();
     line.ForEach.ShouldBeNull();
     line.Content.ShouldEqual("My name is ${name}");
 }
Ejemplo n.º 5
0
 public void Should_parse_foreach_start_with_array_variable()
 {
     var line = new TemplateLine("${foreach childrens}", this.parameters).Parse();
     line.Variables.ShouldBeEmpty();
     line.ForEach.Variable.ShouldEqual("childrens");
     line.ForEach.IsStart.ShouldBeTrue();
     line.ForEach.IsEnd.ShouldBeFalse();
     line.Content.ShouldEqual("${foreach childrens}");
 }
Ejemplo n.º 6
0
        private string TransformLine(TemplateLine line, IDictionary<string, string> parameters)
        {
            var transformedLine = line.Content;

            foreach (var variable in line.Variables)
            {
                transformedLine = transformedLine.Replace(
                    string.Format("${{{0}}}", variable),
                    parameters[variable]);
            }

            return transformedLine;
        }
Ejemplo n.º 7
0
        private void ParseTemplateFile()
        {
            using (var reader = this.CreateStreamReader())
            {
                while (reader.EndOfStream == false)
                {
                    var line = reader.ReadLine();

                    var templateLine = new TemplateLine(line, this.template.Parameters).Parse();

                    this.lines.Add(templateLine);
                }
            }
        }
        internal TemplateLine(int lineNumber, string lineValue, string nextLineValue)
            : base(lineNumber, lineValue, nextLineValue)
        {
            if (lineValue.StartsWith("{{"))
                IsRegexPattern = true;

            if (lineValue.EndsWith("/rl"))
                HasRepeatFlag = true;

            int startIndex = (IsRegexPattern ? 2 : 0);
            int lengthRemove = (IsRegexPattern ? (HasRepeatFlag ? 7 : 4) : 0);
            LineValue = lineValue.Substring(startIndex, lineValue.Length - lengthRemove);

            if (nextLineValue != null)
                NextLine = new TemplateLine(lineNumber + 1, nextLineValue, null);
        }
        internal TemplateLine(int lineNumber, string lineValue, string nextLineValue)
            : base(lineNumber, lineValue, nextLineValue)
        {
            if (lineValue.StartsWith("{{"))
            {
                IsRegexPattern = true;
            }

            if (lineValue.EndsWith("/rl"))
            {
                HasRepeatFlag = true;
            }

            int startIndex   = (IsRegexPattern ? 2 : 0);
            int lengthRemove = (IsRegexPattern ? (HasRepeatFlag ? 7 : 4) : 0);

            LineValue = lineValue.Substring(startIndex, lineValue.Length - lengthRemove);

            if (nextLineValue != null)
            {
                NextLine = new TemplateLine(lineNumber + 1, nextLineValue, null);
            }
        }
        internal void AddItem(int selectedIndex)
        {
            TemplateLine line = new TemplateLine();

            switch (selectedIndex)
            {
            case 0:
                template.Items.Add(new TemplateHeader());
                break;

            case 1:
                line.Field = new TextBoxEditable();
                template.Items.Add(line);
                break;

            case 2:
                line.Field = new ComboboxEditable();
                template.Items.Add(line);
                break;

            default:
                break;
            }
        }
Ejemplo n.º 11
0
 public static CaptureSection AssertIsCapture(this TemplateLine line, int idx)
 {
     Assert.IsType <CaptureSection>(line.Sections[idx]);
     return((CaptureSection)line.Sections[idx]);
 }
Ejemplo n.º 12
0
 internal LinePair(Line targetLine, TemplateLine templateLine)
 {
     TargetLine = targetLine;
     TemplateLine = templateLine;
 }
Ejemplo n.º 13
0
 public static TextSection AssertIsText(this TemplateLine line, int idx)
 {
     Assert.IsType <TextSection>(line.Sections[idx]);
     return((TextSection)line.Sections[idx]);
 }
Ejemplo n.º 14
0
 internal LinePair(Line targetLine, TemplateLine templateLine)
 {
     TargetLine   = targetLine;
     TemplateLine = templateLine;
 }
Ejemplo n.º 15
0
 internal LinePair CreateLinePair(Line targetLine, TemplateLine templateLine)
 {
     return(new LinePair(targetLine, templateLine));
 }
Ejemplo n.º 16
0
 internal LinePair CreateLinePair(Line targetLine, TemplateLine templateLine)
 {
     return new LinePair(targetLine, templateLine);
 }
Ejemplo n.º 17
0
 public static TextSection AssertIsText(this TemplateLine line, int idx, string content)
 {
     Assert.IsType <TextSection>(line.Sections[idx]);
     Assert.Equal(content, line.Sections[idx].ToString());
     return((TextSection)line.Sections[idx]);
 }
Ejemplo n.º 18
0
        private void button2_Click(object sender, EventArgs e)
        {
            string outPath = System.IO.Path.GetDirectoryName(SachnrInputPath);

            System.IO.StreamReader headerTxt = new System.IO.StreamReader("SachnrHeader.txt");
            System.IO.StreamReader interTxt  = new System.IO.StreamReader("Sachnr_inter.txt");

            System.IO.StreamWriter outParaTxt = new System.IO.StreamWriter(outPath + "\\" + "Sachnr_output.txt", false, Encoding.Default);

            string headerLine;

            while ((headerLine = headerTxt.ReadLine()) != null)
            {
                outParaTxt.WriteLine(headerLine);
            }
            headerTxt.Close();

            string interLine;

            while ((interLine = interTxt.ReadLine()) != null)
            {
                Dictionary <string, string> varDic = new Dictionary <string, string>();
                InterPretLine pline = new InterPretLine(interLine);
                varDic = pline.LexemeToDic();

                //Template transform to output

                if (varDic.Count != 0)
                {
                    string templateLine;
                    System.IO.StreamReader templateTxt = new System.IO.StreamReader("SachnrTemplate.txt");
                    while ((templateLine = templateTxt.ReadLine()) != null)
                    {
                        TemplateLine tLine      = new TemplateLine(templateLine);
                        string       outputLine = "";
                        TokenType    _tokenType;
                        while (true)
                        {
                            _tokenType = tLine.GetNextToken();

                            if (_tokenType == TokenType.END)
                            {
                                break;
                            }

                            if (_tokenType == TokenType.VARIABLE)
                            {
                                if (varDic.ContainsKey(tLine.CurrentLexeme))
                                {
                                    outputLine = outputLine + varDic[tLine.CurrentLexeme];
                                }
                                else
                                {
                                    outputLine = outputLine + "0";
                                }
                            }
                            if (_tokenType == TokenType.STRING)
                            {
                                outputLine = outputLine + tLine.CurrentLexeme;
                            }
                        }
                        outParaTxt.WriteLine(outputLine);
                        //  Debug.WriteLine(outputLine);
                    }
                    templateTxt.Close();
                }
            }

            interTxt.Close();
            outParaTxt.Close();

            MessageBox.Show("inter transform generate complete");
        }