分享

hibernate HSQL语法文件分析(连载)[CowNew开源社区]

 快乐学习 2007-02-02
hibernate HSQL语法文件分析(连载)

deleteStatement
 : DELETE^
  (optionalFromTokenFromClause)
  (whereClause)?
 ;

 

其中“^”代表DELETE是根节点。

Root nodes

Any token suffixed with the "^" operator is considered a root token. A tree node is constructed for that token and is made the root of whatever portion of the tree has been built

a : A B^ C^ ;

results in tree #(C #(B A)).

 

 
setClause
 : (SET^ assignment (COMMA! assignment)*);

(COMMA! assignment)*表示可能有多个set语句,比如
set fid=1,fnumber=22,fage=333
COMMA在Antlr中代表“,”,“*”代表0到多个。
特别的COMMA后边的"!"代表COMMA不被翻译成AST的一部分,这个容易理解,因为在AST中象","完全没有必要为它建立一个节点。
Suffix a token reference with "!" to prevent incorporation of the node for that token into the resulting tree (the AST node for the token is still constructed and may be referenced in actions, it is just not added to the result tree automatically). Suffix a rule reference "!" to indicate that the tree constructed by the invoked rule should not be linked into the tree constructed for the current rule.

Suffix a rule definition with "!" to indicate that tree construction for the rule is to be turned off. Rules and tokens referenced within that rule still create ASTs, but they are not linked into a result tree. The following rule does no automatic tree construction. Actions must be used to set the return AST value, for example:

begin!
    :   INT PLUS i:INT
        { #begin = #(PLUS INT i); }
    ;

optionalFromTokenFromClause!
 : (FROM!)? f:path (a:asAlias)? {
  AST #range = #([RANGE, "RANGE"], #f, #a);
  #optionalFromTokenFromClause = #([FROM, "FROM"], #range);
 }
 ;
这个看起来比较麻烦,仔细分析也没什么。
1、f:path表示的就是表名,(a:asAlias)就是别名,(a:asAlias)?就是别名可有可无。path是另一个规则的定义:
path
 : identifier ( DOT^ { weakKeywords(); } identifier )*
 ;
f:path定义了一个变量,变量的类型就是path。定义一个变量的意思就是方便后边action中引用。
2、
{  AST #range = #([RANGE, "RANGE"], #f, #a);
  #optionalFromTokenFromClause = #([FROM, "FROM"], #range);
}
这个部分是action的定义,定义的action会将其转化成java代码插入到生成的java代码中。action主要是为了更灵活的控制代码生成而设立的。
AST #range = #([RANGE, "RANGE"], #f, #a);
代表声明一个AST类型的变量#range,新定义一个“RANGE”作为根,f、a为子树的树,并将其赋值给#range。
#optionalFromTokenFromClause = #([FROM, "FROM"], #range);
也是,建立一个“FROM”做为根, #range为子树的树,并将其做为返回值返回(因为#optionalFromTokenFromClause和当前rule名字一致,所

以做为返回值)
When rule is the name of the enclosing rule, ANTLR will translate this into the variable containing the result AST for the

rule. This allows you to set the return AST for a rule or examine it from within an action. This can be used when AST

generation is on or suppressed for the rule or alternate. For example:
r! : a:A { #r = #a; }
Setting the return tree is very useful in combination with normal tree construction because you can have ANTLR do all the

work of building a tree and then add an imaginary root node such as:
 
decl : ( TYPE ID )+
       { #decl = #([DECL,"decl"], #decl); }
     ;
ANTLR allows you to assign to #rule anywhere within an alternative of the rule. ANTLR ensures that references of and

assignments to #rule within an action force the parser‘s internal AST construction variables into a stable state. After you

assign to #rule, the state of the parser‘s automatic AST construction variables will be set as if ANTLR had generated the

tree rooted at #rule. For example, any children nodes added after the action will be added to the children of #rule.

alias
 : a:identifier { #a.setType(ALIAS); }
别名也是一个标识符的实例,而标识符又有setType方法,这个方法可以用来表示标识符到底是什么类型的。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约