Ejemplo n.º 1
0
        public void TokenTest()
        {
            string tf = "test.input";
            Tokeniser tk = new Tokeniser (tf, ParseSample1);

            //line 1
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 1, 1), tk.Location);
            Assert.AreEqual (State.Content, tk.State);
            Assert.AreEqual ("", tk.Value);
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (State.Directive, tk.State);
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 1, 5), tk.Location);
            Assert.AreEqual (State.DirectiveName, tk.State);
            Assert.AreEqual ("template", tk.Value);
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (State.Directive, tk.State);
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 1, 14), tk.Location);
            Assert.AreEqual (State.DirectiveName, tk.State);
            Assert.AreEqual ("language", tk.Value);
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (State.Directive, tk.State);
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (State.DirectiveValue, tk.State);
            Assert.AreEqual (new Location (tf, 1, 23), tk.Location);
            Assert.AreEqual ("C#v3.5", tk.Value);
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (State.Directive, tk.State);

            //line 2, 3
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 2, 1), tk.Location);
            Assert.AreEqual (State.Content, tk.State);
            Assert.AreEqual ("Line One\r\nLine Two\r\n", tk.Value);

            //line 4, 5, 6
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 4, 1), tk.TagStartLocation);
            Assert.AreEqual (new Location (tf, 4, 3), tk.Location);
            Assert.AreEqual (new Location (tf, 6, 3), tk.TagEndLocation);
            Assert.AreEqual (State.Block, tk.State);
            Assert.AreEqual ("\r\nfoo\r\n", tk.Value);

            //line 7
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 7, 1), tk.Location);
            Assert.AreEqual (State.Content, tk.State);
            Assert.AreEqual ("Line Three ", tk.Value);
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 7, 12), tk.TagStartLocation);
            Assert.AreEqual (new Location (tf, 7, 15), tk.Location);
            Assert.AreEqual (new Location (tf, 7, 22), tk.TagEndLocation);
            Assert.AreEqual (State.Expression, tk.State);
            Assert.AreEqual (" bar ", tk.Value);

            //line 8
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 7, 22), tk.Location);
            Assert.AreEqual (State.Content, tk.State);
            Assert.AreEqual ("\r\nLine Four\r\n", tk.Value);

            //line 9, 10, 11
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 9, 1), tk.TagStartLocation);
            Assert.AreEqual (new Location (tf, 9, 4), tk.Location);
            Assert.AreEqual (new Location (tf, 11, 3), tk.TagEndLocation);
            Assert.AreEqual (State.Helper, tk.State);
            Assert.AreEqual (" \r\nbaz \\#>\r\n", tk.Value);

            //line 12
            Assert.IsTrue (tk.Advance ());
            Assert.AreEqual (new Location (tf, 12, 1), tk.Location);
            Assert.AreEqual (State.Content, tk.State);
            Assert.AreEqual ("", tk.Value);

            //EOF
            Assert.IsFalse (tk.Advance ());
            Assert.AreEqual (new Location (tf, 12, 1), tk.Location);
            Assert.AreEqual (State.EOF, tk.State);
        }
Ejemplo n.º 2
0
        public void TokenTest()
        {
            string tf = "test.input";
            var    tk = new Tokeniser(tf, ParseSample1.NormalizeNewlines());

            //line 1
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 1, 1), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual("",  tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.Directive, tk.State);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 1, 5), tk.Location);
            Assert.AreEqual(State.DirectiveName, tk.State);
            Assert.AreEqual("template",  tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.Directive, tk.State);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 1, 14), tk.Location);
            Assert.AreEqual(State.DirectiveName, tk.State);
            Assert.AreEqual("language",  tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.Directive, tk.State);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.DirectiveValue, tk.State);
            Assert.AreEqual(new Location(tf, 1, 23), tk.Location);
            Assert.AreEqual("C#v3.5",  tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.Directive, tk.State);

            //line 2, 3
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 2, 1), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual("Line One\nLine Two\n",  tk.Value);

            //line 4, 5, 6
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 4, 1), tk.TagStartLocation);
            Assert.AreEqual(new Location(tf, 4, 3), tk.Location);
            Assert.AreEqual(new Location(tf, 6, 3), tk.TagEndLocation);
            Assert.AreEqual(State.Block, tk.State);
            Assert.AreEqual("\nvar foo = 5;\n",  tk.Value);

            //line 7
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 7, 1), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual("Line Three ",  tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 7, 12), tk.TagStartLocation);
            Assert.AreEqual(new Location(tf, 7, 15), tk.Location);
            Assert.AreEqual(new Location(tf, 7, 22), tk.TagEndLocation);
            Assert.AreEqual(State.Expression, tk.State);
            Assert.AreEqual(" bar ",  tk.Value);

            //line 8
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 7, 22), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual("\nLine Four\n",  tk.Value);

            //line 9, 10, 11
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 9, 1), tk.TagStartLocation);
            Assert.AreEqual(new Location(tf, 9, 4), tk.Location);
            Assert.AreEqual(new Location(tf, 11, 3), tk.TagEndLocation);
            Assert.AreEqual(State.Helper, tk.State);
            Assert.AreEqual(" \nvar s = \"baz \\\\#>\";\n",  tk.Value);

            //line 12
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 12, 1), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual("",  tk.Value);

            //EOF
            Assert.IsFalse(tk.Advance());
            Assert.AreEqual(new Location(tf, 12, 1), tk.Location);
            Assert.AreEqual(State.EOF, tk.State);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses the specified template text.
        /// </summary>
        /// <param name="templateText">The template text.</param>
        private void Parse(string templateText)
        {
            //var filePath = Path.GetFullPath(Path.Combine(templateCodePath, TemplateFileName));
            var tokeniser = new Tokeniser(TemplateFileName, templateText);

            AddCode(tokeniser.Location, "");

            bool skip = false;
            while ((skip || tokeniser.Advance()) && tokeniser.State != State.EOF)
            {
                skip = false;
                switch (tokeniser.State)
                {
                    case State.Block:
                        if (!String.IsNullOrEmpty(tokeniser.Value))
                            AddDoTemplateCode(tokeniser.Location, tokeniser.Value);
                        break;
                    case State.Content:
                        if (!String.IsNullOrEmpty(tokeniser.Value))
                            AddContent(tokeniser.Location, tokeniser.Value);
                        break;
                    case State.Expression:
                        if (!String.IsNullOrEmpty(tokeniser.Value))
                            AddExpression(tokeniser.Location, tokeniser.Value);
                        break;
                    case State.Helper:
                        _isTemplateClassCode = true;
                        if (!String.IsNullOrEmpty(tokeniser.Value))
                            AddDoTemplateClassCode(tokeniser.Location, tokeniser.Value);
                        break;
                    case State.Directive:
                        Directive directive = null;
                        string attName = null;
                        while (!skip && tokeniser.Advance())
                        {
                            switch (tokeniser.State)
                            {
                                case State.DirectiveName:
                                    if (directive == null)
                                        directive = new Directive {Name = tokeniser.Value.ToLower()};
                                    else
                                        attName = tokeniser.Value;
                                    break;
                                case State.DirectiveValue:
                                    if (attName != null && directive != null)
                                        directive.Attributes.Add(attName.ToLower(), tokeniser.Value);
                                    attName = null;
                                    break;
                                case State.Directive:
                                    //if (directive != null)
                                    //    directive.EndLocation = tokeniser.TagEndLocation;
                                    break;
                                default:
                                    skip = true;
                                    break;
                            }
                        }
                        if (directive != null)
                        {
                            if (directive.Name == "include")
                            {
                                string includeFile = directive.Attributes["file"];
                                if (OnInclude == null)
                                    throw new InvalidOperationException("Include file found. OnInclude event must be implemented");
                                var includeArgs = new TemplateIncludeArgs() {IncludeName = includeFile};
                                OnInclude(this, includeArgs);
                                Parse(includeArgs.Text ?? "");
                            }
                            _directives.Add(directive);
                        }
                        break;
                    default:
                        throw new InvalidOperationException();
                }
            }
        }
Ejemplo n.º 4
0
        public void TokenTest()
        {
            string    tf = "test.input";
            Tokeniser tk = new Tokeniser(tf, ParseSample1);

            //line 1
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 1, 1), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual("", tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.Directive, tk.State);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 1, 5), tk.Location);
            Assert.AreEqual(State.DirectiveName, tk.State);
            Assert.AreEqual("template", tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.Directive, tk.State);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 1, 14), tk.Location);
            Assert.AreEqual(State.DirectiveName, tk.State);
            Assert.AreEqual("language", tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.Directive, tk.State);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.DirectiveValue, tk.State);
            Assert.AreEqual(new Location(tf, 1, 23), tk.Location);
            Assert.AreEqual("C#v3.5", tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(State.Directive, tk.State);

            //line 2, 3
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 2, 1), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual("Line One" + Environment.NewLine + "Line Two" + Environment.NewLine, tk.Value);

            //line 4, 5, 6
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 4, 1), tk.TagStartLocation);
            Assert.AreEqual(new Location(tf, 4, 3), tk.Location);
            Assert.AreEqual(new Location(tf, 6, 3), tk.TagEndLocation);
            Assert.AreEqual(State.Block, tk.State);
            Assert.AreEqual(Environment.NewLine + "foo" + Environment.NewLine, tk.Value);

            //line 7
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 7, 1), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual("Line Three ", tk.Value);
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 7, 12), tk.TagStartLocation);
            Assert.AreEqual(new Location(tf, 7, 15), tk.Location);
            Assert.AreEqual(new Location(tf, 7, 22), tk.TagEndLocation);
            Assert.AreEqual(State.Expression, tk.State);
            Assert.AreEqual(" bar ", tk.Value);

            //line 8
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 7, 22), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual(Environment.NewLine + "Line Four" + Environment.NewLine, tk.Value);

            //line 9, 10, 11
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 9, 1), tk.TagStartLocation);
            Assert.AreEqual(new Location(tf, 9, 4), tk.Location);
            Assert.AreEqual(new Location(tf, 11, 3), tk.TagEndLocation);
            Assert.AreEqual(State.Helper, tk.State);
            Assert.AreEqual(" " + Environment.NewLine + "baz \\#>" + Environment.NewLine, tk.Value);

            //line 12
            Assert.IsTrue(tk.Advance());
            Assert.AreEqual(new Location(tf, 12, 1), tk.Location);
            Assert.AreEqual(State.Content, tk.State);
            Assert.AreEqual("", tk.Value);

            //EOF
            Assert.IsFalse(tk.Advance());
            Assert.AreEqual(new Location(tf, 12, 1), tk.Location);
            Assert.AreEqual(State.EOF, tk.State);
        }
Ejemplo n.º 5
0
        private async Task <string> Transform(string input, IScriptContext scriptContext)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return(input);
            }

            var tokeniser = new Tokeniser("input", input);

            var output = new StringBuilder();

            while (tokeniser.Advance() && tokeniser.State != State.EOF)
            {
                switch (tokeniser.State)
                {
                case State.Content:
                    output.Append(tokeniser.Value);
                    break;

                case State.Directive:
                    break;

                case State.Expression:

                    object expressionResult;

                    try
                    {
                        expressionResult = await Run(tokeniser.Value, scriptContext);
                    }
                    catch (Exception)
                    {
                        expressionResult = string.Empty;
                    }

                    output.Append(expressionResult);

                    break;

                case State.Block:
                    await Run(tokeniser.Value, scriptContext);

                    break;

                case State.Helper:
                    break;

                case State.DirectiveName:
                    break;

                case State.DirectiveValue:
                    break;

                case State.Name:
                    break;

                case State.EOF:
                    break;

                default:
                    throw new InvalidOperationException();
                }
            }

            return(output.ToString());
        }