예제 #1
0
        private Statement ParseTemplateStatement()
        {
            // parse statement declaration
            TemplateStatement stm = new TemplateStatement(current.Line, current.Column);

            Consume(TokenType.Template);

            // parse tag attributes
            do
            {
                if (Current.TokenType != TokenType.Attribute)
                {
                    throw new ParserException("Expected tag attribute", stm.Line, stm.Column);
                }

                string attrName = Current.Data;
                Consume(TokenType.Attribute);
                Consume(TokenType.Assign);

                // look for "test" attribute
                if (attrName == "name")
                {
                    stm.Name = Current.Data;
                    Consume(TokenType.String);
                }
            }while (Current.TokenType == TokenType.Attribute);

            // check statement
            if (stm.Name == null)
            {
                throw new ParserException("\"name\" attribute was not specified", stm.Line, stm.Column);
            }

            // parse body
            while (true)
            {
                if (Current.TokenType == TokenType.EndTemplate)
                {
                    break;
                }
                else
                {
                    Statement blockStm = ParseStatement();
                    if (blockStm == null)
                    {
                        throw new ParserException("Expected {/template} tag", stm.Line, stm.Column);
                    }

                    // parse statement
                    stm.Statements.Add(blockStm);
                }
            }

            Consume(TokenType.EndTemplate);

            return(stm);
        }
예제 #2
0
        /// <summary>
        /// Evaluates template to the StringWriter.
        /// </summary>
        /// <param name="writer">StringWriter to write evaluation results.</param>
        public void Evaluate(StringWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (lexer == null)
            {
                // create lexer
                lexer = new Lexer(data);

                // create parser
                parser = new Parser(lexer);

                // parse template
                statements = parser.Parse();
            }

            // index custom templates
            int i = 0;

            while (i < statements.Count)
            {
                TemplateStatement tmpStatement = statements[i] as TemplateStatement;
                if (tmpStatement != null)
                {
                    context.Templates.Add(tmpStatement.Name, tmpStatement);
                    statements.RemoveAt(i);
                    continue;
                }
                i++;
            }

            // evaluate template statements
            foreach (AST.Statement stm in statements)
            {
                // eval next statement
                stm.Eval(context, writer);
            }
        }