new_sql.y 8.4 KB
Newer Older
1 2 3 4 5 6
//lemon parser file to generate sql parse by using finite-state-machine code used to parse sql
//usage: lemon sql.y

%name NewParse

%token_prefix NEW_TK_
7
%token_type { SToken }
8 9 10
%default_type { SNode* }
%default_destructor { nodesDestroyNode($$); }

11
%extra_argument { SAstCreateContext* pCxt }
12 13 14 15 16 17 18 19 20 21 22 23

%include {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>

#include "nodes.h"
#include "ttoken.h"
#include "ttokendef.h"
#include "astCreateFuncs.h"
24 25

#define PARSER_TRACE printf("rule = %s\n", yyRuleName[yyruleno])
26 27 28
}

%syntax_error {  
29
  if(TOKEN.z) {
30
    char msg[] = "syntax error near \"%s\"";
31
    int32_t sqlLen = strlen(&TOKEN.z[0]);
32 33 34

    if (sqlLen + sizeof(msg)/sizeof(msg[0]) + 1 > pCxt->pQueryCxt->msgLen) {
        char tmpstr[128] = {0};
35
        memcpy(tmpstr, &TOKEN.z[0], sizeof(tmpstr)/sizeof(tmpstr[0]) - 1);
36 37
        sprintf(pCxt->pQueryCxt->pMsg, msg, tmpstr);
    } else {
38
        sprintf(pCxt->pQueryCxt->pMsg, msg, &TOKEN.z[0]);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    }
  } else {
    sprintf(pCxt->pQueryCxt->pMsg, "Incomplete SQL statement");
  }
  pCxt->valid = false;
}

%parse_accept       { printf("parsing complete!\n" );}

//%left OR.
//%left AND.
//%right NOT.
%left UNION ALL MINUS EXCEPT INTERSECT.
//%left EQ NE ISNULL NOTNULL IS LIKE MATCH NMATCH GLOB BETWEEN IN.
//%left GT GE LT LE.
//%left BITAND BITOR LSHIFT RSHIFT.
%left NK_PLUS NK_MINUS.
//%left DIVIDE TIMES.
%left NK_STAR NK_SLASH. //REM.
//%left CONCAT.
//%right UMINUS UPLUS BITNOT.

61
cmd ::= SHOW DATABASES.  { PARSER_TRACE; createShowStmt(pCxt, SHOW_TYPE_DATABASE); }
62

63
cmd ::= query_expression(A). { PARSER_TRACE; pCxt->pRootNode = A; }
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

//////////////////////// value_function /////////////////////////////////
value_function ::= NK_ID NK_LP value_expression NK_RP.
value_function ::= NK_ID NK_LP value_expression NK_COMMA value_expression NK_RP.

//////////////////////// value_expression_primary /////////////////////////////////
value_expression_primary ::= NK_LP value_expression NK_RP.
value_expression_primary ::= nonparenthesized_value_expression_primary.

nonparenthesized_value_expression_primary ::= literal.
// ?
nonparenthesized_value_expression_primary ::= column_reference.

literal ::= NK_LITERAL.

79 80
column_reference(A) ::= NK_ID(B).                      { PARSER_TRACE; A = createColumnNode(pCxt, NULL, &B); }
column_reference(A) ::= table_name(B) NK_DOT NK_ID(C). { PARSER_TRACE; A = createColumnNode(pCxt, &B, &C); }
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

//////////////////////// value_expression /////////////////////////////////
value_expression ::= common_value_expression.

common_value_expression ::= numeric_value_expression.

numeric_value_expression ::= numeric_primary.
numeric_value_expression ::= NK_PLUS numeric_primary.
numeric_value_expression ::= NK_MINUS numeric_primary.
numeric_value_expression ::= numeric_value_expression NK_PLUS numeric_value_expression.
numeric_value_expression ::= numeric_value_expression NK_MINUS numeric_value_expression.
numeric_value_expression ::= numeric_value_expression NK_STAR numeric_value_expression.
numeric_value_expression ::= numeric_value_expression NK_SLASH numeric_value_expression.

numeric_primary ::= value_expression_primary.
numeric_primary ::= value_function.

//////////////////////// query_specification /////////////////////////////////
99
query_specification(A) ::= SELECT set_quantifier_opt(B) select_list(C) from_clause(D). { PARSER_TRACE; A = createSelectStmt(pCxt, B, C, D); }
100 101 102

%type set_quantifier_opt { bool }
%destructor set_quantifier_opt {}
103 104 105
set_quantifier_opt(A) ::= .         { PARSER_TRACE; A = false; }
set_quantifier_opt(A) ::= DISTINCT. { PARSER_TRACE; A = true; }
set_quantifier_opt(A) ::= ALL.      { PARSER_TRACE; A = false; }
106 107 108

%type select_list { SNodeList* }
%destructor select_list { nodesDestroyNodeList($$); }
109 110
select_list(A) ::= NK_STAR.           { PARSER_TRACE; A = NULL; }
select_list(A) ::= select_sublist(B). { PARSER_TRACE; A = B; }
111 112 113

%type select_sublist { SNodeList* }
%destructor select_sublist { nodesDestroyNodeList($$); }
114 115
select_sublist(A) ::= select_item(B).                            { PARSER_TRACE; A = createNodeList(pCxt, B); }
select_sublist(A) ::= select_sublist(B) NK_COMMA select_item(C). { PARSER_TRACE; A = addNodeToList(pCxt, B, C); }
116

117 118 119
select_item(A) ::= value_expression(B).                     { PARSER_TRACE; A = B; }
select_item(A) ::= value_expression(B) AS NK_ID(C).         { PARSER_TRACE; A = setProjectionAlias(pCxt, B, &C); }
select_item(A) ::= table_name(B) NK_DOT NK_STAR(C).                 { PARSER_TRACE; A = createColumnNode(pCxt, &B, &C); }
120

121
from_clause(A) ::= FROM table_reference_list(B). { PARSER_TRACE; A = B; }
122

123 124 125 126
//%type table_reference_list { SNodeList* }
//%destructor table_reference_list { nodesDestroyNodeList($$); }
table_reference_list(A) ::= table_reference(B).                               { PARSER_TRACE; A = B; }
//table_reference_list(A) ::= table_reference_list(B) NK_COMMA table_reference(C). { PARSER_TRACE; A = createJoinTableNode(pCxt, B, C); }
127

128 129
//table_reference(A) ::= NK_ID(B). { PARSER_TRACE; A = createRealTableNode(pCxt, ); }
table_reference(A) ::= table_factor(B). { PARSER_TRACE; A = B; }
130 131
//table_reference ::= joined_table.

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
table_factor(A) ::= table_primary(B). { PARSER_TRACE; A = B; }

table_primary(A) ::= table_name(B).                   { PARSER_TRACE; A = createRealTableNode(pCxt, NULL, &B); }
table_primary(A) ::= db_name(B) NK_DOT table_name(C). { PARSER_TRACE; A = createRealTableNode(pCxt, &B, &C); }
table_primary ::= derived_table.

derived_table ::= table_subquery.

%type db_name { SToken }
db_name(A) ::= NK_ID(B).    { PARSER_TRACE; A = B; }
%type table_name { SToken }
table_name(A) ::= NK_ID(B). { PARSER_TRACE; A = B; }

//////////////////////// subquery /////////////////////////////////
subquery ::= NK_LR query_expression NK_RP.

table_subquery ::= subquery.


151
//////////////////////// query_expression /////////////////////////////////
152
query_expression(A) ::= with_clause_opt query_expression_body(B) order_by_clause_opt limit_clause_opt slimit_clause_opt. { PARSER_TRACE; A = B; }
153 154

with_clause_opt ::= .                         {}
155 156
with_clause_opt ::= WITH with_list.           { PARSER_TRACE; pCxt->notSupport = true; pCxt->valid = false; }
with_clause_opt ::= WITH RECURSIVE with_list. { PARSER_TRACE; pCxt->notSupport = true; pCxt->valid = false; }
157 158 159 160 161 162 163 164

with_list ::= with_list_element.                    {}
with_list ::= with_list NK_COMMA with_list_element. {}

with_list_element ::= NK_ID AS table_subquery. {}

table_subquery ::= . {}

165 166
query_expression_body(A) ::= query_primary(B).                                            { PARSER_TRACE; A = B; }
query_expression_body(A) ::= query_expression_body(B) UNION ALL query_expression_body(C). { PARSER_TRACE; A = createSetOperator(pCxt, SET_OP_TYPE_UNION_ALL, B, C); }
167

168 169
query_primary(A) ::= query_specification(B).                                                                   { PARSER_TRACE; A = B; }
query_primary(A) ::= NK_LP query_expression_body(B) order_by_clause_opt limit_clause_opt slimit_clause_opt NK_RP. { PARSER_TRACE; A = B;}
170 171 172

%type order_by_clause_opt { SNodeList* }
%destructor order_by_clause_opt { nodesDestroyNodeList($$); }
173 174
order_by_clause_opt(A) ::= .                                    { PARSER_TRACE; A = NULL; }
order_by_clause_opt(A) ::= ORDER BY sort_specification_list(B). { PARSER_TRACE; A = B; }
175 176 177 178 179 180 181 182

limit_clause_opt ::= .

slimit_clause_opt ::= .

//////////////////////// sort_specification_list /////////////////////////////////
%type sort_specification_list { SNodeList* }
%destructor sort_specification_list { nodesDestroyNodeList($$); }
183 184
sort_specification_list(A) ::= sort_specification(B).                                     { PARSER_TRACE; A = createNodeList(pCxt, B); }
sort_specification_list(A) ::= sort_specification_list(B) NK_COMMA sort_specification(C). { PARSER_TRACE; A = addNodeToList(pCxt, B, C); }
185

186
sort_specification(A) ::= value_expression(B) ordering_specification_opt(C) null_ordering_opt(D). { PARSER_TRACE; A = createOrderByExprNode(pCxt, B, C, D); }
187 188 189

%type ordering_specification_opt EOrder
%destructor ordering_specification_opt {}
190 191 192
ordering_specification_opt(A) ::= .     { PARSER_TRACE; A = ORDER_ASC; }
ordering_specification_opt(A) ::= ASC.  { PARSER_TRACE; A = ORDER_ASC; }
ordering_specification_opt(A) ::= DESC. { PARSER_TRACE; A = ORDER_DESC; }
193 194 195

%type null_ordering_opt ENullOrder
%destructor null_ordering_opt {}
196 197 198
null_ordering_opt(A) ::= .            { PARSER_TRACE; A = NULL_ORDER_DEFAULT; }
null_ordering_opt(A) ::= NULLS FIRST. { PARSER_TRACE; A = NULL_ORDER_FIRST; }
null_ordering_opt(A) ::= NULLS LAST.  { PARSER_TRACE; A = NULL_ORDER_LAST; }