Richie

Sometimes at night when I look up at the stars, and see the whole sky just laid out there, don't you think I ain't remembering it all. I still got dreams like anybody else, and ever so often, I am thinking about how things might of been. And then, all of a sudden, I'm forty, fifty, sixty years old, you know?

Parse the StringTemplate expression

    If you need to get the attributes list that referenced in a StringTemplate, as well as which specific templates that invoked by it, you need to parse the StringTemplate expression. The following code snippet demonstrates how to do this using StringTemplate.
using Antlr.StringTemplate;
using Antlr.StringTemplate.Language;
using antlr.collections;

public partial class Default4 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
//define the template
        StringTemplate st = new StringTemplate(@"
<html>
<head>
    $StyleImport$
    $ScriptImport$
</head>
<body>
    $_b_1100()$
    $users:_t_13(userName=attr.Name):_b_37(age=12)$
    $users:{<li>$attr.Name$</li>}$
    $Copyright$
</body>
</html>
");
        
//loop to print all nodes in the expression tree
        for (int i = 0; i < st.Chunks.Count; i++)
        {
            
if (st.Chunks[i] is ASTExpr)
            {
                RecurTokens((st.Chunks[i] 
as ASTExpr).AST, 0);
            }
        }
    }

    
private void RecurTokens(AST ast, int level)
    {
        
if (ast != null)
        {
            
//print current node
            for (int i = 0; i < level; i++)
                
this.Response.Write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            
this.Response.Write(ast.Type.ToString("0#"+ "----" + ast.ToString().Replace("<""&lt;").Replace(">""&gt;"));
            
this.Response.Write("<br>");

            
//recursive to the child nodes
            RecurTokens(ast.getFirstChild(), level + 1);
            
//recursive to the sibling nodes
            AST sibling = ast.getNextSibling();
            
while (sibling != null)
            {
                RecurTokens(
sibling, level + 1);
                sibling
= sibling.getNextSibling();
            }
        }
    }
}
    The output is like:
18----StyleImport
18----ScriptImport
07----include
       18----_b_1100
              06----ARGS
04----:
       04----:
              18----users
                     10----
                            18----_t_13
                                   06----ARGS
                                          19----=
                                                 18----userName
                                                        24----.
                                                               18----attr
                                                                      18----Name
              10----
                     18----_b_37
                            06----ARGS
                                   19----=
                                          18----age
                                                 33----12
04----:
       18----users
              10----
                     31----<li>$attr.Name$</li>
18----Copyright
    Now you should be aware of how to make those things to be done.

posted on 2007-06-11 17:15 RicCC 阅读(281) 评论(2)  编辑 收藏 所属分类: StringTemplate

Feedback

#1楼  2007-06-13 09:15 大雾      

最后的 temp = sibling.getNextSibling();
应该是
sibling = sibling.getNextSibling();

我一开始拷过去,编译不通过,就在temp前加了个 AST定义 ,结果运行时进行了一个死循环,机子差点死掉了,呵呵
  回复  引用  查看    

#2楼 [楼主] 2007-06-13 17:10 RicCC      

哦是写错了,谢谢提醒   回复  引用  查看    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-11-14 21:58 编辑过