Skip to content

Commit d39cfb9

Browse files
author
phalcon
committed
Adding latest scanner.re/parser.lemon for PHQL [ci skip]
1 parent b290f4b commit d39cfb9

File tree

2 files changed

+156
-45
lines changed

2 files changed

+156
-45
lines changed

ext/mvc/model/query/parser.lemon

+96-35
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
+------------------------------------------------------------------------+
44
| Phalcon Framework |
55
+------------------------------------------------------------------------+
6-
| Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) |
6+
| Copyright (c) 2011-2013 Phalcon Team (http://www.phalconphp.com) |
77
+------------------------------------------------------------------------+
88
| This source file is subject to the New BSD License that is bundled |
99
| with this package in the file docs/LICENSE.txt. |
@@ -28,12 +28,12 @@
2828
%left EQUALS NOTEQUALS LESS GREATER GREATEREQUAL LESSEQUAL .
2929
%left AND OR .
3030
%left LIKE ILIKE .
31-
%left BITWISE_AND BITWISE_OR .
31+
%left BITWISE_AND BITWISE_OR BITWISE_XOR .
3232
%left DIVIDE TIMES MOD .
3333
%left PLUS MINUS .
3434
%left IS .
3535
%right IN DISTINCT .
36-
%right NOT .
36+
%right NOT BITWISE_NOT .
3737

3838
%include {
3939

@@ -82,13 +82,39 @@ static zval *phql_ret_placeholder_zval(int type, phql_parser_token *T)
8282
return ret;
8383
}
8484

85-
static zval *phql_ret_qualified_name(phql_parser_token *A, phql_parser_token *B)
85+
static zval *phql_ret_qualified_name(phql_parser_token *A, phql_parser_token *B, phql_parser_token *C)
8686
{
8787
zval *ret;
8888

8989
MAKE_STD_ZVAL(ret);
9090
array_init(ret);
91+
9192
add_assoc_long(ret, "type", PHQL_T_QUALIFIED);
93+
94+
if (A != NULL) {
95+
add_assoc_stringl(ret, "ns-alias", A->token, A->token_len, 0);
96+
efree(A);
97+
}
98+
99+
if (B != NULL) {
100+
add_assoc_stringl(ret, "domain", B->token, B->token_len, 0);
101+
efree(B);
102+
}
103+
104+
add_assoc_stringl(ret, "name", C->token, C->token_len, 0);
105+
efree(C);
106+
107+
return ret;
108+
}
109+
110+
static zval *phql_ret_raw_qualified_name(phql_parser_token *A, phql_parser_token *B)
111+
{
112+
zval *ret;
113+
114+
MAKE_STD_ZVAL(ret);
115+
array_init(ret);
116+
117+
add_assoc_long(ret, "type", PHQL_T_RAW_QUALIFIED);
92118
if (B != NULL) {
93119
add_assoc_stringl(ret, "domain", A->token, A->token_len, 0);
94120
add_assoc_stringl(ret, "name", B->token, B->token_len, 0);
@@ -448,26 +474,26 @@ static zval *phql_ret_func_call(phql_parser_token *name, zval *arguments)
448474
token_found = 0;
449475
}
450476

451-
status->syntax_error_len = 64 + status->token->len + token_length + near_length;
477+
status->syntax_error_len = 96 + status->token->len + token_length + near_length + status->phql_length;;
452478
status->syntax_error = emalloc(sizeof(char) * status->syntax_error_len);
453479

454480
if (near_length > 0) {
455481
if (status->token->value) {
456-
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s(%s), near to '%s'", token_name, status->token->value, status->scanner_state->start);
482+
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s(%s), near to '%s', when parsing: %s (%d)", token_name, status->token->value, status->scanner_state->start, status->phql, status->phql_length);
457483
} else {
458-
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s, near to '%s'", token_name, status->scanner_state->start);
484+
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s, near to '%s', when parsing: %s (%d)", token_name, status->scanner_state->start, status->phql, status->phql_length);
459485
}
460486
} else {
461487
if (active_token != PHQL_T_IGNORE) {
462488
if (status->token->value) {
463-
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s(%s), at the end of query", token_name, status->token->value);
489+
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s(%s), at the end of query, when parsing: %s (%d)", token_name, status->token->value, status->phql, status->phql_length);
464490
} else {
465-
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s, at the end of query", token_name);
491+
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s, at the end of query, when parsing: %s (%d)", token_name, status->phql, status->phql_length);
466492
}
467493
} else {
468494
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected EOF, at the end of query");
469495
}
470-
status->syntax_error[status->syntax_error_len-1] = '\0';
496+
status->syntax_error[status->syntax_error_len - 1] = '\0';
471497
}
472498

473499
if (!token_found) {
@@ -684,33 +710,33 @@ join_item(R) ::= join_clause(C) . {
684710
%destructor join_clause { zval_ptr_dtor(&$$); }
685711

686712
/** Join - conditions - alias */
687-
join_clause(R) ::= join_type(T) qualified_name(Q) . {
713+
join_clause(R) ::= join_type(T) aliased_or_qualified_name(Q) . {
688714
R = phql_ret_join_item(T, Q, NULL, NULL);
689715
}
690716

691717
/** Join - conditions + alias */
692-
join_clause(R) ::= join_type(T) qualified_name(Q) join_associated_name(A) . {
718+
join_clause(R) ::= join_type(T) aliased_or_qualified_name(Q) join_associated_name(A) . {
693719
R = phql_ret_join_item(T, Q, A, NULL);
694720
}
695721

696722
/** Join + conditions - alias */
697-
join_clause(R) ::= join_type(T) qualified_name(Q) join_conditions(C) . {
723+
join_clause(R) ::= join_type(T) aliased_or_qualified_name(Q) join_conditions(C) . {
698724
R = phql_ret_join_item(T, Q, NULL, C);
699725
}
700726

701727
/** Join + conditions + alias */
702-
join_clause(R) ::= join_type(T) qualified_name(Q) join_associated_name(A) join_conditions(C) . {
728+
join_clause(R) ::= join_type(T) aliased_or_qualified_name(Q) join_associated_name(A) join_conditions(C) . {
703729
R = phql_ret_join_item(T, Q, A, C);
704730
}
705731

706732
%destructor join_associated_name { zval_ptr_dtor(&$$); }
707733

708734
join_associated_name(R) ::= AS IDENTIFIER(I) . {
709-
R = phql_ret_qualified_name(I, NULL);
735+
R = phql_ret_qualified_name(NULL, NULL, I);
710736
}
711737

712738
join_associated_name(R) ::= IDENTIFIER(I) . {
713-
R = phql_ret_qualified_name(I, NULL);
739+
R = phql_ret_qualified_name(NULL, NULL, I);
714740
}
715741

716742
%destructor join_type { zval_ptr_dtor(&$$); }
@@ -760,11 +786,11 @@ join_conditions(R) ::= ON expr(E) . {
760786
%destructor insert_statement { zval_ptr_dtor(&$$); }
761787

762788
/* Insert */
763-
insert_statement(R) ::= INSERT INTO qualified_name(Q) VALUES BRACKET_OPEN values_list(V) BRACKET_CLOSE . {
789+
insert_statement(R) ::= INSERT INTO aliased_or_qualified_name(Q) VALUES PARENTHESES_OPEN values_list(V) PARENTHESES_CLOSE . {
764790
R = phql_ret_insert_statement(Q, NULL, V);
765791
}
766792

767-
insert_statement(R) ::= INSERT INTO qualified_name(Q) BRACKET_OPEN field_list(F) BRACKET_CLOSE VALUES BRACKET_OPEN values_list(V) BRACKET_CLOSE . {
793+
insert_statement(R) ::= INSERT INTO aliased_or_qualified_name(Q) PARENTHESES_OPEN field_list(F) PARENTHESES_CLOSE VALUES PARENTHESES_OPEN values_list(V) PARENTHESES_CLOSE . {
768794
R = phql_ret_insert_statement(Q, F, V);
769795
}
770796

@@ -795,7 +821,7 @@ field_list(R) ::= field_item(F) . {
795821
%destructor field_item { zval_ptr_dtor(&$$); }
796822

797823
field_item(R) ::= IDENTIFIER(I) . {
798-
R = phql_ret_qualified_name(I, NULL);
824+
R = phql_ret_qualified_name(NULL, NULL, I);
799825
}
800826

801827
/* Update */
@@ -872,18 +898,24 @@ delete_clause(R) ::= DELETE FROM associated_name(A) . {
872898

873899
%destructor associated_name { zval_ptr_dtor(&$$); }
874900

875-
associated_name(R) ::= qualified_name(Q) AS IDENTIFIER(I) . {
901+
associated_name(R) ::= aliased_or_qualified_name(Q) AS IDENTIFIER(I) . {
876902
R = phql_ret_assoc_name(Q, I);
877903
}
878904

879-
associated_name(R) ::= qualified_name(Q) IDENTIFIER(I) . {
905+
associated_name(R) ::= aliased_or_qualified_name(Q) IDENTIFIER(I) . {
880906
R = phql_ret_assoc_name(Q, I);
881907
}
882908

883-
associated_name(R) ::= qualified_name(Q) . {
909+
associated_name(R) ::= aliased_or_qualified_name(Q) . {
884910
R = phql_ret_assoc_name(Q, NULL);
885911
}
886912

913+
%destructor aliased_or_qualified_name { zval_ptr_dtor(&$$); }
914+
915+
aliased_or_qualified_name(R) ::= qualified_name(Q) . {
916+
R = Q;
917+
}
918+
887919
%destructor where_clause { zval_ptr_dtor(&$$); }
888920

889921
where_clause(R) ::= WHERE expr(E) . {
@@ -938,12 +970,8 @@ group_list(R) ::= group_item(I) . {
938970

939971
%destructor group_item { zval_ptr_dtor(&$$); }
940972

941-
group_item(R) ::= qualified_name(Q) . {
942-
R = Q;
943-
}
944-
945-
group_item(R) ::= INTEGER(I) . {
946-
R = phql_ret_literal_zval(PHQL_T_INTEGER, I);
973+
group_item(R) ::= expr(E) . {
974+
R = E;
947975
}
948976

949977
%destructor having_clause { zval_ptr_dtor(&$$); }
@@ -1014,6 +1042,10 @@ expr(R) ::= expr(O1) BITWISE_OR expr(O2) . {
10141042
R = phql_ret_expr(PHQL_T_BITWISE_OR, O1, O2);
10151043
}
10161044

1045+
expr(R) ::= expr(O1) BITWISE_XOR expr(O2) . {
1046+
R = phql_ret_expr(PHQL_T_BITWISE_XOR, O1, O2);
1047+
}
1048+
10171049
expr(R) ::= expr(O1) EQUALS expr(O2) . {
10181050
R = phql_ret_expr(PHQL_T_EQUALS, O1, O2);
10191051
}
@@ -1054,29 +1086,37 @@ expr(R) ::= expr(O1) NOT ILIKE expr(O2) . {
10541086
R = phql_ret_expr(PHQL_T_NILIKE, O1, O2);
10551087
}
10561088

1057-
expr(R) ::= expr(E) IN BRACKET_OPEN argument_list(L) BRACKET_CLOSE . {
1089+
expr(R) ::= expr(E) IN PARENTHESES_OPEN argument_list(L) PARENTHESES_CLOSE . {
10581090
R = phql_ret_expr(PHQL_T_IN, E, L);
10591091
}
10601092

1061-
expr(R) ::= expr(E) NOT IN BRACKET_OPEN argument_list(L) BRACKET_CLOSE . {
1093+
expr(R) ::= expr(E) NOT IN PARENTHESES_OPEN argument_list(L) PARENTHESES_CLOSE . {
10621094
R = phql_ret_expr(PHQL_T_NOTIN, E, L);
10631095
}
10641096

10651097
expr(R) ::= expr(O1) AGAINST expr(O2) . {
10661098
R = phql_ret_expr(PHQL_T_AGAINST, O1, O2);
10671099
}
10681100

1101+
expr(R) ::= CAST PARENTHESES_OPEN expr(E) AS IDENTIFIER(I) PARENTHESES_CLOSE . {
1102+
R = phql_ret_expr(PHQL_T_CAST, E, phql_ret_raw_qualified_name(I, NULL));
1103+
}
1104+
1105+
expr(R) ::= CONVERT PARENTHESES_OPEN expr(E) USING IDENTIFIER(I) PARENTHESES_CLOSE . {
1106+
R = phql_ret_expr(PHQL_T_CONVERT, E, phql_ret_raw_qualified_name(I, NULL));
1107+
}
1108+
10691109
%destructor function_call { zval_ptr_dtor(&$$); }
10701110

10711111
expr(R) ::= function_call(F) . {
10721112
R = F;
10731113
}
10741114

1075-
function_call(R) ::= IDENTIFIER(I) BRACKET_OPEN argument_list(L) BRACKET_CLOSE . {
1115+
function_call(R) ::= IDENTIFIER(I) PARENTHESES_OPEN argument_list(L) PARENTHESES_CLOSE . {
10761116
R = phql_ret_func_call(I, L);
10771117
}
10781118

1079-
function_call(R) ::= IDENTIFIER(I) BRACKET_OPEN BRACKET_CLOSE . {
1119+
function_call(R) ::= IDENTIFIER(I) PARENTHESES_OPEN PARENTHESES_CLOSE . {
10801120
R = phql_ret_func_call(I, NULL);
10811121
}
10821122

@@ -1120,7 +1160,11 @@ expr(R) ::= NOT expr(E) . {
11201160
R = phql_ret_expr(PHQL_T_NOT, NULL, E);
11211161
}
11221162

1123-
expr(R) ::= BRACKET_OPEN expr(E) BRACKET_CLOSE . {
1163+
expr(R) ::= BITWISE_NOT expr(E) . {
1164+
R = phql_ret_expr(PHQL_T_BITWISE_NOT, NULL, E);
1165+
}
1166+
1167+
expr(R) ::= PARENTHESES_OPEN expr(E) PARENTHESES_CLOSE . {
11241168
R = phql_ret_expr(PHQL_T_ENCLOSED, E, NULL);
11251169
}
11261170

@@ -1144,6 +1188,14 @@ expr(R) ::= NULL . {
11441188
R = phql_ret_literal_zval(PHQL_T_NULL, NULL);
11451189
}
11461190

1191+
expr(R) ::= TRUE . {
1192+
R = phql_ret_literal_zval(PHQL_T_TRUE, NULL);
1193+
}
1194+
1195+
expr(R) ::= FALSE . {
1196+
R = phql_ret_literal_zval(PHQL_T_FALSE, NULL);
1197+
}
1198+
11471199
expr(R) ::= NPLACEHOLDER(P) . {
11481200
R = phql_ret_placeholder_zval(PHQL_T_NPLACEHOLDER, P);
11491201
}
@@ -1154,10 +1206,19 @@ expr(R) ::= SPLACEHOLDER(P) . {
11541206

11551207
%destructor qualified_name { zval_ptr_dtor(&$$); }
11561208

1209+
qualified_name(R) ::= IDENTIFIER(A) COLON IDENTIFIER(B) DOT IDENTIFIER(C) . {
1210+
R = phql_ret_qualified_name(A, B, C);
1211+
}
1212+
1213+
qualified_name(R) ::= IDENTIFIER(A) COLON IDENTIFIER(B) . {
1214+
R = phql_ret_qualified_name(A, NULL, B);
1215+
}
1216+
11571217
qualified_name(R) ::= IDENTIFIER(A) DOT IDENTIFIER(B) . {
1158-
R = phql_ret_qualified_name(A, B);
1218+
R = phql_ret_qualified_name(NULL, A, B);
11591219
}
11601220

11611221
qualified_name(R) ::= IDENTIFIER(A) . {
1162-
R = phql_ret_qualified_name(A, NULL);
1222+
R = phql_ret_qualified_name(NULL, NULL, A);
11631223
}
1224+

0 commit comments

Comments
 (0)