posts - 18,  comments - 0,  trackbacks - 0

class P extends Parser;

options {
 k=5;
 buildAST = true;
 defaultErrorHandler = false;
}

tokens {
 SELECT_STATEMENT;
 GROUP_BY; ORDER_BY;
 ALIAS_EQU;
 FUNCTION;
 LOGIC_OP;
 ALL_FIELDS;
}

segment
 : (COLUMN column
 | WHERE search_condition)
 EOF!
 ;

statements
 : statement (SEMI^ statement)* EOF!;

statement
 : tableUnion
 | tableCompare
 | select_statement
 {#statement=#([SELECT_STATEMENT], #statement);}
 ;

tableUnion
 : ("t_union"^|"表合并"^) table_name COMMA! table_name
 ;

tableCompare
 : ("t_compare"^|"表比较"^) table_name COMMA! table_name ("where"!|"条件"!) comparemethod_name search_condition
 ;

select_statement
 : ("select"^|"查询"^) ("distinct"^|"唯一"^)? select_list
  (("from"^|"来自"^) table_list)?
  (("where"^|"条件"^) search_condition)?
  (("group"^ "by"!|"分组"^) aggregate_expression_list)?
  (("order"^ "by"!|"排序"^) order_expression_list)?
 ;

select_list
 : column (COMMA^ column)*
 ;

table_list
 : table_name (COMMA^ table_name)*
 ;
search_condition
 : equation
  (logic_op search_condition {#search_condition=#([LOGIC_OP, "logic_op"], #search_condition);})?
 ;

aggregate_expression_list
 : aggregate_expr (COMMA^ aggregate_expr)*
 ;

order_expression_list
 : order_expression (COMMA^ order_expression)*
 ;

column
 : expression_with_aggr_func (("as"^|"作为"^) alias)?
 | alias ("="|"等于")! expression_with_aggr_func {#column=#([ALIAS_EQU, "="], #column);}
 | all:"所有" {#column=#([ALL_FIELDS, all.getText()]);}
 | STAR {#column=#([ALL_FIELDS, "*"]);}
 ;
aggregate_expr
 : (field_name|function) (
  two_arg_op aggregate_expr {#aggregate_expr=#([TWO_ARG_OP, "two_arg_op"], #aggregate_expr);})?
 ;
order_expression
 : (alias|field_name|aggregate_func|function) ("升序"^|"降序"^|"asc"^|"desc"^)?
 ;
expression
 : (field_name|constant|function|param_equ)
  (two_arg_op expression {#expression=#([TWO_ARG_OP, "two_arg_op"], #expression);})?
 | LPAREN expression RPAREN
  (two_arg_op expression {#expression=#([TWO_ARG_OP, "two_arg_op"], #expression);})?
 | one_arg_op expression {#expression=#([ONE_ARG_OP, "one_arg_op"], #expression);}
 ;
expression_with_aggr_func
 : (field_name|constant|function|aggregate_func)
  (two_arg_op expression_with_aggr_func
  {#expression_with_aggr_func=#([TWO_ARG_OP, "two_arg_op"], #expression_with_aggr_func);})?
 | LPAREN expression_with_aggr_func RPAREN
  (two_arg_op expression_with_aggr_func
  {#expression_with_aggr_func=#([TWO_ARG_OP, "two_arg_op"], #expression_with_aggr_func);})?
 | one_arg_op expression_with_aggr_func
  {#expression_with_aggr_func=#([ONE_ARG_OP, "one_arg_op"], #expression_with_aggr_func);}
 ;

equation
 : expression (("="|compare_op) expression
  {#equation=#([COMPARE_OP, "comp_op"], #equation);}
 |  ("is"! "null"^|"is"! "not"^ "null"!|"为空"^|"非空"^)
 |  ("between"^|"范围"^) expression ("and"!)? expression)
 ;

//param_equ
// : PARAM_LPAREN ID^ PARAM_RPAREN
// ;

param_equ
 : PARAM_ID
 ;

alias
 : ID | QUOTED_STRING;
field_name
 : ID POINT^ ID;
//field_name
// : ID
// | ID POINT^ ID;
constant
 : REAL_NUM
 | QUOTED_STRING
 | "null"
 ;

function
 : function_name LPAREN! parameters RPAREN!
 ;
aggregate_func
 : aggregate_func_name LPAREN! ("all"^|"全部"^|"distinct"^|"唯一"^)? parameters RPAREN!
 ;

parameters
 : expression (COMMA^ expression)*
 ;
table_name
 : ID (("as"^|"作为"^) alias)?
 ;

function_name
 : "sqrt"   |  "求平方根"
 | "getdate"  |  "求当前日期时间"
 | "abs"   |  "求绝对值"
 | "substring" |  "字符串截取"
 | "round"  | "格式化数值"
 | "right"  |  "字符串右截"
 | "ltrim"  | "去掉左空格"
 | "rtrim"  | "去掉右空格"
 | "char_length" | "求字符串的长度"
 | "floor"  | "求四舍后的整数"
 | "ceiling" | "求五入后的整数"
 | "lower"  |  "将字符串转为小写"
 | "charindex" | "存在于"
 | "str"   |  "数值转字符串"
 ;


aggregate_func_name
 : "sum"  | "求和"
 | "avg"  | "求平均数"
 | "max"  | "求最大值"
 | "min"  | "求最小值"
 | "count" | "求记录数"
 ;

one_arg_op
 : ONE_ARG_OP | "非";
two_arg_op
 : TWO_ARG_OP | STAR
 | "与" | "或" | "异或" | "加" | "减" | "乘" | "除" | "求模";
compare_op
 : COMPARE_OP | "等于" | "like"
 | "大于等于" | "小于等于" | "大于" | "小于" | "不等于"
 | "包含" | "不包含";
logic_op
 : "and" | "or" | "并且" | "或者";

comparemethod_name
 : "not exist" | "不存在"
 | "exist" | "存在";

posted on 2007-04-03 18:00 LORD BLOG 阅读(375) 评论(0)  编辑  收藏 所属分类: 项目笔记

只有注册用户登录后才能发表评论。


网站导航: