public void ParsePage(string name, TextReader reader) { Console.WriteLine ("parsing page: {0}", name); Tokenizer tk = new Tokenizer (environment, reader); codegen.BeginPage (name); Token tok = null; StringBuilder data = new StringBuilder (); while (true) { tok = tk.GetNextToken (); switch (tok.Type) { case TokenType.TOKEN_VARIABLE_BEGIN: FlushData (data); ParseVariable (tk); break; case TokenType.TOKEN_COMMENT_BEGIN: FlushData (data); ParseComment (tk); break; case TokenType.TOKEN_BLOCK_BEGIN: FlushData (data); ParseControlBlock (tk); break; case TokenType.TOKEN_EOF: FlushData (data); codegen.EndPage (); return; default: data.Append (tok.Value); break; } } }
public void ParseComment(Tokenizer tk) { Token tok; StringBuilder builder = new StringBuilder (); do { tok = tk.GetNextToken (); if (tok.Type == TokenType.TOKEN_COMMENT_END) { return; } builder.Append (tok.Value); } while (tok.Type != TokenType.TOKEN_EOF); // FAIL }
public void ParseControlBlock(Tokenizer tk) { Token tok; StringBuilder builder = new StringBuilder (); ParseStatement (tk); tok = tk.Current; do { if (tok.Type == TokenType.TOKEN_BLOCK_END) { return; } builder.Append (tok.Value); tok = tk.GetNextToken (); } while (tok.Type != TokenType.TOKEN_EOF); }
/* public TemplateFilter ParseFilter (TemplateTokenizer tk) { Token tok = NextNonWhiteSpaceToken (tk); if (tok.Type != TokenType.TOKEN_NAME) RaiseFailure (tk, String.Format ("Invalid filter expression, token '{0}' found where a name was expected.", tok.Value)); string name = tok.Value; List<Expression> args = new List<Expression> (); tok = NextNonWhiteSpaceToken (tk); if (tok.Type == TokenType.TOKEN_LPAREN) { args = ParseArguments (tk); if (tk.Current.Type != TokenType.TOKEN_RPAREN) RaiseFailure (tk, String.Format ("Invalid filter expression, token '{0}' where a ) was expected.", tk.Current.Value)); // Advance pass the RPAREN NextNonWhiteSpaceToken (tk); } return new TemplateFilter (name, args); } */ /* public List<Expression> ParseArguments (TemplateTokenizer tk) { List<Expression> expressions = new List<Expression> (); Token tok = null; do { Expression expression = ParseExpression (tk, TokenType.TOKEN_COMMA); expressions.Add (expression); tok = tk.Current; if (tok.Type == TokenType.TOKEN_RPAREN) break; if (tok.Type != TokenType.TOKEN_COMMA) RaiseFailure (tk, String.Format ("Invalid argument list, expected comma got a {0}", tk.Current.Value)); } while (tok.Type != TokenType.TOKEN_EOF); if (tk.Current.Type == TokenType.TOKEN_EOF) RaiseFailure (tk, String.Format ("Unexpected end of file.")); return expressions; } public List<ArgumentDefinition> ParseArgumentDefinitions (TemplateTokenizer tk) { Token tok = NextNonWhiteSpaceToken (tk); List<ArgumentDefinition> args = new List<ArgumentDefinition> (); do { if (tok.Type == TokenType.TOKEN_RPAREN) break; if (tok.Type != TokenType.TOKEN_NAME) RaiseFailure (tk, String.Format ("Invalid argument definition, expected a name got a '{0}'", tok.Value)); string name = tok.Value; ConstantValue default_value = null; tok = NextNonWhiteSpaceToken (tk); if (tok.Type == TokenType.TOKEN_ASSIGN) { default_value = ParseConstantValue (tk); tok = NextNonWhiteSpaceToken (tk); } args.Add (new ArgumentDefinition (name, default_value)); if (tok.Type == TokenType.TOKEN_RPAREN) break; if (tok.Type != TokenType.TOKEN_COMMA) RaiseFailure (tk, String.Format ("Invalid argument list, expected comma got a {0}", tk.Current.Type)); tok = NextNonWhiteSpaceToken (tk); } while (tok.Type != TokenType.TOKEN_EOF); if (tk.Current.Type == TokenType.TOKEN_EOF) RaiseFailure (tk, String.Format ("Unexpected end of file.")); return args; } */ public Token NextNonWhiteSpaceToken(Tokenizer tk) { Token tok; do { tok = tk.GetNextToken (); } while (tok.Type == TokenType.TOKEN_WHITESPACE); return tok; }