libidav/davqlparser.c

Tue, 19 May 2015 10:46:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 19 May 2015 10:46:32 +0200
changeset 110
53895e9a4bbb
parent 109
020a5b5aa510
child 111
39f4c5fcaa60
permissions
-rw-r--r--

update ucx

76
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2015 Olaf Wintermann. All rights reserved.
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "davqlparser.h"
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
30 #include <ucx/utils.h>
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
31 #include <string.h>
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
32 #include <stdio.h>
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
33 #include <ctype.h>
76
4c48ce3b9045 structure draft for DavQL statements
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
35 #define sfmtarg(s) ((int)(s).length), (s).ptr
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
36
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
37 // ------------------------------------------------------------------------
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
38 // D E B U G E R
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
39 // ------------------------------------------------------------------------
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
40
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
41 static const char* _map_querytype(davqltype_t type) {
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
42 switch(type) {
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
43 case DAVQL_ERROR: return "ERROR";
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
44 case DAVQL_GET: return "GET";
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
45 case DAVQL_SET: return "SET";
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
46 default: return "unknown";
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
47 }
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
48 }
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
49
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
50 static const char* _map_exprtype(davqlexprtype_t type) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
51 switch(type) {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
52 case DAVQL_UNDEFINED_TYPE: return "undefined";
96
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
53 case DAVQL_NUMBER: return "NUMBER";
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
54 case DAVQL_STRING: return "STRING";
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
55 case DAVQL_TIMESTAMP: return "TIMESTAMP";
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
56 case DAVQL_IDENTIFIER: return "IDENTIFIER";
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
57 case DAVQL_UNARY: return "UNARY";
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
58 case DAVQL_BINARY: return "BINARY";
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
59 case DAVQL_LOGICAL: return "LOGICAL";
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
60 case DAVQL_FUNCCALL: return "FUNCCALL";
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
61 default: return "unknown";
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
62 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
63 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
64
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
65 static const char* _map_specialfield(int info) {
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
66 switch(info) {
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
67 case 0: return "";
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
68 case 1: return "with wildcard";
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
69 case 2: return "(resource data only)";
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
70 default: return "with mysterious identifier";
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
71 }
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
72 }
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
73
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
74 static const char* _map_operator(davqloperator_t op) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
75 // don't use string array, because enum values may change
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
76 switch(op) {
83
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
77 case DAVQL_NOOP: return "no operator";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
78 case DAVQL_ADD: return "+"; case DAVQL_SUB: return "-";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
79 case DAVQL_MUL: return "*"; case DAVQL_DIV: return "/";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
80 case DAVQL_AND: return "&"; case DAVQL_OR: return "|";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
81 case DAVQL_XOR: return "^"; case DAVQL_NEG: return "~";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
82 case DAVQL_NOT: return "NOT"; case DAVQL_LAND: return "AND";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
83 case DAVQL_LOR: return "OR"; case DAVQL_LXOR: return "XOR";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
84 case DAVQL_EQ: return "="; case DAVQL_NEQ: return "!=";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
85 case DAVQL_LT: return "<"; case DAVQL_GT: return ">";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
86 case DAVQL_LE: return "<="; case DAVQL_GE: return ">=";
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
87 case DAVQL_LIKE: return "LIKE"; case DAVQL_UNLIKE: return "UNLIKE";
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
88 default: return "unknown";
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
89 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
90 }
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
91
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
92 static void dav_debug_ql_fnames_print(DavQLStatement *stmt) {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
93 if (stmt->fields) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
94 printf("Field names: ");
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
95 UCX_FOREACH(field, stmt->fields) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
96 DavQLField *f = field->data;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
97 printf("%.*s, ", sfmtarg(f->name));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
98 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
99 printf("\b\b \b\b\n");
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
100 }
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
101 }
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
102
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
103 static void dav_debug_ql_stmt_print(DavQLStatement *stmt) {
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
104 // Basic information
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
105 size_t fieldcount = ucx_list_size(stmt->fields);
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
106 int specialfield = 0;
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
107 if (stmt->fields) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
108 DavQLField* firstfield = (DavQLField*)stmt->fields->data;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
109 if (firstfield->expr->type == DAVQL_IDENTIFIER) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
110 switch (firstfield->expr->srctext.ptr[0]) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
111 case '*': specialfield = 1; break;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
112 case '-': specialfield = 2; break;
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
113 }
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
114 }
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
115 }
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
116 if (specialfield) {
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
117 fieldcount--;
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
118 }
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
119 printf("Statement: %.*s\nType: %s\nField count: %zu %s\n",
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
120 sfmtarg(stmt->srctext),
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
121 _map_querytype(stmt->type),
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
122 fieldcount,
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
123 _map_specialfield(specialfield));
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
124
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
125 dav_debug_ql_fnames_print(stmt);
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
126 printf("Path: %.*s\nHas where clause: %s\n",
87
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
127 sfmtarg(stmt->path),
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
128 stmt->where ? "yes" : "no");
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
129
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
130 // WITH attributes
96
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
131 if (stmt->depth == DAV_DEPTH_INFINITY) {
85
0ab1cf261a44 improved grammar + changed depth default, type and added infinity
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
132 printf("Depth: infinity\n");
96
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
133 } else if (stmt->depth == DAV_DEPTH_PLACEHOLDER) {
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
134 printf("Depth: placeholder\n");
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
135 } else {
85
0ab1cf261a44 improved grammar + changed depth default, type and added infinity
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
136 printf("Depth: %d\n", stmt->depth);
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
137 }
95
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
138
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
139 // order by clause
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
140 printf("Order by: ");
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
141 if (stmt->orderby) {
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
142 UCX_FOREACH(crit, stmt->orderby) {
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
143 DavQLOrderCriterion *critdata = crit->data;
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
144 printf("%.*s %s%s", sfmtarg(critdata->column->srctext),
96
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
145 critdata->descending ? "desc" : "asc",
95
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
146 crit->next ? ", " : "\n");
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
147 }
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
148 } else {
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
149 printf("nothing\n");
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
150 }
96
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
151
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
152 // error messages
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
153 if (stmt->errorcode) {
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
154 printf("\nError code: %d\nError: %s\n",
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
155 stmt->errorcode, stmt->errormessage);
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
156 }
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
157 }
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
158
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
159 static int dav_debug_ql_expr_selected(DavQLExpression *expr) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
160 if (!expr) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
161 printf("Currently no expression selected.\n");
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
162 return 0;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
163 } else {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
164 return 1;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
165 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
166 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
167
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
168 static void dav_debug_ql_expr_print(DavQLExpression *expr) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
169 if (dav_debug_ql_expr_selected(expr)) {
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
170 sstr_t empty = ST("(empty)");
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
171 printf(
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
172 "Text: %.*s\nType: %s\nOperator: %s\n",
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
173 sfmtarg(expr->srctext),
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
174 _map_exprtype(expr->type),
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
175 _map_operator(expr->op));
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
176 if (expr->left || expr->right) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
177 printf("Left hand: %.*s\nRight hand: %.*s\n",
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
178 sfmtarg(expr->left?expr->left->srctext:empty),
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
179 sfmtarg(expr->right?expr->right->srctext:empty));
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
180 }
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
181 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
182 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
183
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
184 static void dav_debug_ql_tree_print(DavQLExpression *expr, int depth) {
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
185 if (expr) {
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
186 if (expr->left) {
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
187 printf("%*c%s\n", depth, ' ', _map_operator(expr->op));
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
188 dav_debug_ql_tree_print(expr->left, depth+1);
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
189 dav_debug_ql_tree_print(expr->right, depth+1);
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
190 } else if (expr->type == DAVQL_UNARY) {
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
191 printf("%*c%s %.*s\n", depth, ' ', _map_operator(expr->op),
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
192 sfmtarg(expr->srctext));
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
193 } else {
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
194 printf("%*c%.*s\n", depth, ' ', sfmtarg(expr->srctext));
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
195 }
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
196 }
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
197 }
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
198
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
199 #define DQLD_CMD_Q 0
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
200 #define DQLD_CMD_PS 1
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
201 #define DQLD_CMD_PE 2
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
202 #define DQLD_CMD_PF 3
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
203 #define DQLD_CMD_PT 4
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
204 #define DQLD_CMD_F 10
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
205 #define DQLD_CMD_W 11
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
206 #define DQLD_CMD_O 12
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
207 #define DQLD_CMD_L 21
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
208 #define DQLD_CMD_R 22
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
209 #define DQLD_CMD_N 23
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
210 #define DQLD_CMD_P 24
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
211 #define DQLD_CMD_H 100
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
212
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
213 static int dav_debug_ql_command() {
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
214 printf("> ");
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
215
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
216 char buffer[8];
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
217 fgets(buffer, 8, stdin);
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
218 // discard remaining chars
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
219 if (!strchr(buffer, '\n')) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
220 int chr;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
221 while ((chr = fgetc(stdin) != '\n') && chr != EOF);
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
222 }
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
223
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
224 if (!strcmp(buffer, "q\n")) {
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
225 return DQLD_CMD_Q;
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
226 } else if (!strcmp(buffer, "ps\n")) {
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
227 return DQLD_CMD_PS;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
228 } else if (!strcmp(buffer, "pe\n")) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
229 return DQLD_CMD_PE;
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
230 } else if (!strcmp(buffer, "pf\n")) {
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
231 return DQLD_CMD_PF;
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
232 } else if (!strcmp(buffer, "pt\n")) {
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
233 return DQLD_CMD_PT;
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
234 } else if (!strcmp(buffer, "l\n")) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
235 return DQLD_CMD_L;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
236 } else if (!strcmp(buffer, "r\n")) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
237 return DQLD_CMD_R;
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
238 } else if (!strcmp(buffer, "h\n")) {
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
239 return DQLD_CMD_H;
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
240 } else if (!strcmp(buffer, "f\n")) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
241 return DQLD_CMD_F;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
242 } else if (!strcmp(buffer, "w\n")) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
243 return DQLD_CMD_W;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
244 } else if (!strcmp(buffer, "o\n")) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
245 return DQLD_CMD_O;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
246 } else if (!strcmp(buffer, "n\n")) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
247 return DQLD_CMD_N;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
248 } else if (!strcmp(buffer, "p\n")) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
249 return DQLD_CMD_P;
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
250 } else {
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
251 return -1;
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
252 }
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
253 }
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
254
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
255 void dav_debug_statement(DavQLStatement *stmt) {
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
256 if (!stmt) {
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
257 fprintf(stderr, "Debug DavQLStatement failed: null pointer");
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
258 return;
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
259 }
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
260
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
261 printf("Starting DavQL debugger (type 'h' for help)...\n\n");
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
262 dav_debug_ql_stmt_print(stmt);
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
263
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
264 if (stmt->errorcode) {
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
265 return;
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
266 }
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
267
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
268 DavQLExpression *examineexpr = NULL;
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
269 UcxList *examineelem = NULL;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
270 int examineclause = 0;
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
271
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
272 while(1) {
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
273 int cmd = dav_debug_ql_command();
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
274 switch (cmd) {
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
275 case DQLD_CMD_Q: return;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
276 case DQLD_CMD_PS: dav_debug_ql_stmt_print(stmt); break;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
277 case DQLD_CMD_PE: dav_debug_ql_expr_print(examineexpr); break;
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
278 case DQLD_CMD_PT: dav_debug_ql_tree_print(examineexpr, 1); break;
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
279 case DQLD_CMD_PF: dav_debug_ql_fnames_print(stmt); break;
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
280 case DQLD_CMD_F:
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
281 if (examineclause != DQLD_CMD_F) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
282 examineclause = DQLD_CMD_F;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
283 examineelem = stmt->fields;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
284 examineexpr = stmt->fields ?
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
285 ((DavQLField*)stmt->fields->data)->expr : NULL;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
286 dav_debug_ql_expr_print(examineexpr);
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
287 }
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
288 break;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
289 case DQLD_CMD_W:
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
290 examineclause = 0; examineelem = NULL;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
291 examineexpr = stmt->where;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
292 dav_debug_ql_expr_print(examineexpr);
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
293 break;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
294 case DQLD_CMD_O:
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
295 if (examineclause != DQLD_CMD_O) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
296 examineclause = DQLD_CMD_O;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
297 examineelem = stmt->orderby;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
298 examineexpr = stmt->orderby ?
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
299 ((DavQLOrderCriterion*)stmt->orderby->data)->column : NULL;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
300 dav_debug_ql_expr_print(examineexpr);
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
301 }
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
302 break;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
303 case DQLD_CMD_N:
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
304 case DQLD_CMD_P:
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
305 if (examineelem) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
306 UcxList *newelem = (cmd == DQLD_CMD_N ?
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
307 examineelem->next : examineelem->prev);
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
308 if (newelem) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
309 examineelem = newelem;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
310 if (examineclause == DQLD_CMD_O) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
311 examineexpr = ((DavQLOrderCriterion*)
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
312 examineelem->data)->column;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
313 } else if (examineclause == DQLD_CMD_F) {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
314 examineexpr = ((DavQLField*)examineelem->data)->expr;
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
315 } else {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
316 printf("Examining unknown clause type.");
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
317 }
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
318 dav_debug_ql_expr_print(examineexpr);
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
319 } else {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
320 printf("Reached end of list.\n");
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
321 }
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
322 } else {
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
323 printf("Currently not examining an expression list.\n");
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
324 }
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
325 break;
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
326 case DQLD_CMD_L:
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
327 if (dav_debug_ql_expr_selected(examineexpr)) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
328 if (examineexpr->left) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
329 examineexpr = examineexpr->left;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
330 dav_debug_ql_expr_print(examineexpr);
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
331 } else {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
332 printf("There is no left subtree.\n");
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
333 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
334 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
335 break;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
336 case DQLD_CMD_R:
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
337 if (dav_debug_ql_expr_selected(examineexpr)) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
338 if (examineexpr->right) {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
339 examineexpr = examineexpr->right;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
340 dav_debug_ql_expr_print(examineexpr);
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
341 } else {
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
342 printf("There is no right subtree.\n");
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
343 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
344 }
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
345 break;
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
346 case DQLD_CMD_H:
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
347 printf(
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
348 "\nCommands:\n"
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
349 "ps: print statement information\n"
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
350 "o: examine order by clause\n"
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
351 "f: examine field list\n"
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
352 "pf: print field names\n"
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
353 "w: examine where clause\n"
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
354 "n: examine next expression "
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
355 "(in order by clause or field list)\n"
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
356 "p: examine previous expression "
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
357 "(in order by clause or field list)\n"
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
358 "q: quit\n\n"
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
359 "\nExpression examination:\n"
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
360 "pe: print expression information\n"
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
361 "pt: print full syntax tree of current (sub-)expression\n"
80
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
362 "l: enter left subtree\n"
a2832c054c98 added path examination to debugger + changed field names of expression subtrees
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
363 "r: enter right subtree\n");
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
364 break;
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
365 default: printf("unknown command\n");
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
366 }
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
367 }
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
368 }
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
369
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
370 // ------------------------------------------------------------------------
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
371 // P A R S E R
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
372 // ------------------------------------------------------------------------
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
373
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
374 #define _error_context "(%.*s [->]%.*s %.*s)"
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
375 #define _error_invalid "invalid statement"
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
376 #define _error_unhandled "unhandled error " _error_context
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
377 #define _error_unexpected_token "unexpected token " _error_context
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
378 #define _error_invalid_token "invalid token " _error_context
107
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
379 #define _error_missing_path "expected path " _error_context
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
380 #define _error_missing_from "expecting FROM keyword " _error_context
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
381 #define _error_missing_by "expecting BY keyword " _error_context
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
382 #define _error_invalid_depth "invalid depth " _error_context
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
383 #define _error_missing_expr "missing expression " _error_context
108
0199f13c7fe2 fixed parsing of binary expressions did not yield the correct type
Mike Becker <universe@uap-core.de>
parents: 107
diff changeset
384 #define _error_invalid_expr "invalid expression " _error_context
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
385 #define _error_invalid_unary_op "invalid unary operator " _error_context
107
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
386 #define _error_invalid_fmtspec "invalid format specifier " _error_context
96
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
387
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
388 #define token_sstr(token) (((DavQLToken*)(token)->data)->value)
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
389
96
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
390 static void dav_error_in_context(int errorcode, const char *errormsg,
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
391 DavQLStatement *stmt, UcxList *token) {
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
392 sstr_t emptystring = ST("");
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
393 stmt->errorcode = errorcode;
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
394 stmt->errormessage = ucx_sprintf(errormsg,
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
395 sfmtarg(token->prev?token_sstr(token->prev):emptystring),
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
396 sfmtarg(token_sstr(token)),
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
397 sfmtarg(token->next?token_sstr(token->next):emptystring)).ptr;
96
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
398 }
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
399
896022673e0e special path parser + typed literals + order by parser + grammar changes
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
400 // special symbols are single tokens - the % sign MUST NOT be a special symbol
93
2176ffbf1346 added special symbol list to tokenizer
Mike Becker <universe@uap-core.de>
parents: 92
diff changeset
401 static const char *special_token_symbols = ",()+-*/&|^~=!<>";
2176ffbf1346 added special symbol list to tokenizer
Mike Becker <universe@uap-core.de>
parents: 92
diff changeset
402
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
403 static _Bool iskeyword(DavQLToken *token) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
404 sstr_t keywords[] = {ST("get"), ST("set"), ST("from"), ST("at"), ST("as"),
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
405 ST("where"), ST("with"), ST("order"), ST("by"), ST("asc"), ST("desc")
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
406 };
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
407 for (int i = 0 ; i < sizeof(keywords)/sizeof(char*) ; i++) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
408 if (!sstrcasecmp(token->value, keywords[i])) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
409 return 1;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
410 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
411 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
412 return 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
413 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
414
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
415 static UcxList* dav_parse_add_token(UcxList *tokenlist, DavQLToken *token) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
416
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
417 // determine token class (order of if-statements is very important!)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
418 char firstchar = token->value.ptr[0];
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
419
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
420 if (isdigit(firstchar)) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
421 token->tokenclass = DAVQL_TOKEN_NUMBER;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
422 } else if (firstchar == '%') {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
423 token->tokenclass = DAVQL_TOKEN_FMTSPEC;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
424 } else if (token->value.length == 1) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
425 switch (firstchar) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
426 case '(': token->tokenclass = DAVQL_TOKEN_OPENP; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
427 case ')': token->tokenclass = DAVQL_TOKEN_CLOSEP; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
428 case ',': token->tokenclass = DAVQL_TOKEN_COMMA; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
429 case '=': token->tokenclass = DAVQL_TOKEN_EQ; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
430 case '<': token->tokenclass = DAVQL_TOKEN_LT; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
431 case '>': token->tokenclass = DAVQL_TOKEN_GT; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
432 case '!': token->tokenclass = DAVQL_TOKEN_EXCLAIM; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
433 default:
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
434 token->tokenclass = strchr(special_token_symbols, firstchar) ?
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
435 DAVQL_TOKEN_OPERATOR : DAVQL_TOKEN_IDENTIFIER;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
436 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
437 } else if (firstchar == '\'') {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
438 token->tokenclass = DAVQL_TOKEN_STRING;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
439 } else if (firstchar == '`') {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
440 token->tokenclass = DAVQL_TOKEN_IDENTIFIER;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
441 } else if (iskeyword(token)) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
442 token->tokenclass = DAVQL_TOKEN_KEYWORD;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
443 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
444 token->tokenclass = DAVQL_TOKEN_IDENTIFIER;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
445 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
446
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
447 // remove quotes (extreme cool feature)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
448 if (token->tokenclass == DAVQL_TOKEN_STRING ||
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
449 (token->tokenclass == DAVQL_TOKEN_IDENTIFIER && firstchar == '`')) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
450
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
451 char lastchar = token->value.ptr[token->value.length-1];
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
452 if (firstchar == lastchar) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
453 token->value.ptr++;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
454 token->value.length -= 2;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
455 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
456 token->tokenclass = DAVQL_TOKEN_INVALID;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
457 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
458 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
459
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
460
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
461 return ucx_list_append(tokenlist, token);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
462 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
463
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
464 static UcxList* dav_parse_tokenize(sstr_t src) {
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
465 UcxList *tokens = NULL;
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
466
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
467 DavQLToken *token = NULL;
88
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
468 char insequence = '\0';
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
469 for (size_t i = 0 ; i < src.length ; i++) {
88
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
470 // quoted strings / identifiers are a single token
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
471 if (src.ptr[i] == '\'' || src.ptr[i] == '`') {
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
472 if (src.ptr[i] == insequence) {
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
473 // add quoted token to list
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
474 token->value.length++;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
475 tokens = dav_parse_add_token(tokens, token);
88
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
476 token = NULL;
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
477 insequence = '\0';
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
478 } else if (insequence == '\0') {
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
479 insequence = src.ptr[i];
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
480 // always create new token for quoted strings
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
481 if (token) {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
482 tokens = dav_parse_add_token(tokens, token);
88
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
483 }
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
484 token = malloc(sizeof(DavQLToken));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
485 token->value.ptr = src.ptr + i;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
486 token->value.length = 1;
88
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
487 } else {
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
488 // add other kind of quotes to token
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
489 token->value.length++;
88
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
490 }
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
491 } else if (insequence) {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
492 token->value.length++;
88
4d6b03bd7034 tokenizer now correctly handles quoted tokens
Mike Becker <universe@uap-core.de>
parents: 87
diff changeset
493 } else if (isspace(src.ptr[i])) {
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
494 // add token before spaces to list (if any)
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
495 if (token) {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
496 tokens = dav_parse_add_token(tokens, token);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
497 token = NULL;
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
498 }
93
2176ffbf1346 added special symbol list to tokenizer
Mike Becker <universe@uap-core.de>
parents: 92
diff changeset
499 } else if (strchr(special_token_symbols, src.ptr[i])) {
2176ffbf1346 added special symbol list to tokenizer
Mike Becker <universe@uap-core.de>
parents: 92
diff changeset
500 // add token before special symbol to list (if any)
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
501 if (token) {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
502 tokens = dav_parse_add_token(tokens, token);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
503 token = NULL;
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
504 }
93
2176ffbf1346 added special symbol list to tokenizer
Mike Becker <universe@uap-core.de>
parents: 92
diff changeset
505 // add special symbol as single token to list
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
506 token = malloc(sizeof(DavQLToken));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
507 token->value.ptr = src.ptr + i;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
508 token->value.length = 1;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
509 tokens = dav_parse_add_token(tokens, token);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
510 // set tokenizer ready to read more tokens
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
511 token = NULL;
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
512 } else {
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
513 // if this is a new token, create memory for it
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
514 if (!token) {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
515 token = malloc(sizeof(DavQLToken));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
516 token->value.ptr = src.ptr + i;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
517 token->value.length = 0;
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
518 }
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
519 // extend token length when reading more bytes
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
520 token->value.length++;
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
521 }
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
522 }
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
523
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
524 if (token) {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
525 tokens = dav_parse_add_token(tokens, token);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
526 }
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
527
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
528 return tokens;
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
529 }
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
530
87
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
531 static void dav_free_expression(DavQLExpression *expr) {
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
532 if (expr->left) {
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
533 dav_free_expression(expr->left);
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
534 }
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
535 if (expr->right) {
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
536 dav_free_expression(expr->right);
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
537 }
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
538 free(expr);
ed21d95984bb removed DavQLExpression type for path (since it always is an identifier)
Mike Becker <universe@uap-core.de>
parents: 86
diff changeset
539 }
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
540
99
579238097973 fieldlist parser can now parse fields with alias
Mike Becker <universe@uap-core.de>
parents: 98
diff changeset
541 static void dav_free_field(DavQLField *field) {
579238097973 fieldlist parser can now parse fields with alias
Mike Becker <universe@uap-core.de>
parents: 98
diff changeset
542 dav_free_expression(field->expr);
579238097973 fieldlist parser can now parse fields with alias
Mike Becker <universe@uap-core.de>
parents: 98
diff changeset
543 free(field);
579238097973 fieldlist parser can now parse fields with alias
Mike Becker <universe@uap-core.de>
parents: 98
diff changeset
544 }
579238097973 fieldlist parser can now parse fields with alias
Mike Becker <universe@uap-core.de>
parents: 98
diff changeset
545
95
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
546 static void dav_free_order_criterion(DavQLOrderCriterion *crit) {
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
547 if (crit->column) { // do it null-safe though column is expected to be set
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
548 dav_free_expression(crit->column);
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
549 }
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
550 free(crit);
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
551 }
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
552
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
553 #define token_is(token, expectedclass) (token && \
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
554 (((DavQLToken*)(token)->data)->tokenclass == expectedclass))
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
555
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
556 #define tokenvalue_is(token, expectedvalue) (token && \
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
557 !sstrcasecmp(((DavQLToken*)(token)->data)->value, S(expectedvalue)))
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
558
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
559 typedef int(*exprparser_f)(DavQLStatement*,UcxList*,DavQLExpression*);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
560
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
561 static int dav_parse_binary_expr(DavQLStatement* stmt, UcxList* token,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
562 DavQLExpression* expr, exprparser_f parseL, char* opc, int* opv,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
563 exprparser_f parseR) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
564
107
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
565 if (!token) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
566 return 0;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
567 }
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
568
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
569 int total_consumed = 0, consumed;
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
570
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
571 // save temporarily on stack (copy to heap later on)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
572 DavQLExpression left, right;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
573
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
574 // RULE: LEFT, [Operator, RIGHT]
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
575 memset(&left, 0, sizeof(DavQLExpression));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
576 consumed = parseL(stmt, token, &left);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
577 if (!consumed) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
578 return 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
579 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
580 total_consumed += consumed;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
581 token = ucx_list_get(token, consumed);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
582
107
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
583 char *op;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
584 if (token_is(token, DAVQL_TOKEN_OPERATOR) &&
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
585 (op = strchr(opc, token_sstr(token).ptr[0]))) {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
586 expr->op = opv[op-opc];
108
0199f13c7fe2 fixed parsing of binary expressions did not yield the correct type
Mike Becker <universe@uap-core.de>
parents: 107
diff changeset
587 expr->type = DAVQL_BINARY;
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
588 total_consumed++;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
589 token = token->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
590 memset(&right, 0, sizeof(DavQLExpression));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
591 consumed = parseR(stmt, token, &right);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
592 if (!consumed) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
593 dav_error_in_context(DAVQL_ERROR_MISSING_EXPR,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
594 _error_missing_expr, stmt, token);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
595 return 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
596 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
597 total_consumed += consumed;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
598 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
599
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
600 if (expr->op == DAVQL_NOOP) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
601 memcpy(expr, &left, sizeof(DavQLExpression));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
602 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
603 expr->left = malloc(sizeof(DavQLExpression));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
604 memcpy(expr->left, &left, sizeof(DavQLExpression));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
605 expr->right = malloc(sizeof(DavQLExpression));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
606 memcpy(expr->right, &right, sizeof(DavQLExpression));
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
607
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
608 expr->srctext.ptr = expr->left->srctext.ptr;
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
609 expr->srctext.length =
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
610 expr->right->srctext.ptr -
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
611 expr->left->srctext.ptr + expr->right->srctext.length;
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
612 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
613
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
614 return total_consumed;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
615 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
616
107
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
617 static int dav_parse_literal(DavQLStatement* stmt, UcxList* token,
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
618 DavQLExpression* expr) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
619
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
620 expr->srctext = token_sstr(token);
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
621 if (token_is(token, DAVQL_TOKEN_NUMBER)) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
622 expr->type = DAVQL_NUMBER;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
623 } else if (token_is(token, DAVQL_TOKEN_STRING)) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
624 expr->type = DAVQL_STRING;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
625 } else if (token_is(token, DAVQL_TOKEN_TIMESTAMP)) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
626 expr->type = DAVQL_TIMESTAMP;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
627 } else if (token_is(token, DAVQL_TOKEN_FMTSPEC)
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
628 && expr->srctext.length == 2) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
629 switch (expr->srctext.ptr[1]) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
630 case 'd': expr->type = DAVQL_NUMBER; break;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
631 case 's': expr->type = DAVQL_STRING; break;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
632 case 't': expr->type = DAVQL_TIMESTAMP; break;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
633 default:
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
634 dav_error_in_context(DAVQL_ERROR_INVALID_FMTSPEC,
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
635 _error_invalid_fmtspec, stmt, token);
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
636 return 0;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
637 }
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
638 } else {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
639 return 0;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
640 }
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
641
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
642 return 1;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
643 }
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
644
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
645 static int dav_parse_funccall(DavQLStatement* stmt, UcxList* token,
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
646 DavQLExpression* expr) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
647
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
648 // TODO: make it so
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
649 return 0;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
650 }
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
651
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
652 static int dav_parse_unary_expr(DavQLStatement* stmt, UcxList* token,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
653 DavQLExpression* expr) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
654
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
655 DavQLExpression* atom = expr;
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
656 expr->srctext.ptr = token_sstr(token).ptr;
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
657
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
658 int total_consumed = 0;
107
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
659
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
660 // optional unary operator
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
661 if (token_is(token, DAVQL_TOKEN_OPERATOR)) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
662 char *op = strchr("+-~", token_sstr(token).ptr[0]);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
663 if (op) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
664 expr->type = DAVQL_UNARY;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
665 switch (*op) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
666 case '+': expr->op = DAVQL_ADD; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
667 case '-': expr->op = DAVQL_SUB; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
668 case '~': expr->op = DAVQL_NEG; break;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
669 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
670 expr->left = calloc(sizeof(DavQLExpression), 1);
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
671 atom = expr->left;
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
672 total_consumed++;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
673 token = token->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
674 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
675 dav_error_in_context(DAVQL_ERROR_INVALID_UNARY_OP,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
676 _error_invalid_unary_op, stmt, token);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
677 return 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
678 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
679 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
680
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
681 // RULE: (ParExpression | AtomicExpression)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
682 if (token_is(token, DAVQL_TOKEN_OPENP)) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
683 // TODO: make it so (and don't forget CLOSEP)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
684 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
685 // RULE: FunctionCall
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
686 int consumed = dav_parse_funccall(stmt, token, atom);
107
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
687 if (consumed) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
688 total_consumed += consumed;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
689 } else if (token_is(token, DAVQL_TOKEN_IDENTIFIER)) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
690 // RULE: Identifier
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
691 total_consumed++;
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
692 atom->type = DAVQL_IDENTIFIER;
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
693 atom->srctext = token_sstr(token);
107
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
694 } else {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
695 // RULE: Literal
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
696 total_consumed += dav_parse_literal(stmt, token, atom);
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
697 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
698 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
699
109
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
700 // recover source text
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
701 if (atom != expr) {
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
702 expr->srctext.length =
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
703 atom->srctext.ptr - expr->srctext.ptr + atom->srctext.length;
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
704 }
020a5b5aa510 added syntax tree display to debugger + expression parser saves source text in every node
Mike Becker <universe@uap-core.de>
parents: 108
diff changeset
705
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
706
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
707 return total_consumed;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
708 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
709
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
710 static int dav_parse_bitexpr(DavQLStatement* stmt, UcxList* token,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
711 DavQLExpression* expr) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
712
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
713 return dav_parse_binary_expr(stmt, token, expr,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
714 dav_parse_unary_expr,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
715 "&|^", (int[]){DAVQL_AND, DAVQL_OR, DAVQL_XOR},
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
716 dav_parse_bitexpr);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
717 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
718
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
719 static int dav_parse_multexpr(DavQLStatement* stmt, UcxList* token,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
720 DavQLExpression* expr) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
721
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
722 return dav_parse_binary_expr(stmt, token, expr,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
723 dav_parse_bitexpr,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
724 "*/", (int[]){DAVQL_MUL, DAVQL_DIV},
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
725 dav_parse_multexpr);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
726 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
727
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
728 static int dav_parse_expression(DavQLStatement* stmt, UcxList* token,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
729 DavQLExpression* expr) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
730
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
731 return dav_parse_binary_expr(stmt, token, expr,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
732 dav_parse_multexpr,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
733 "+-", (int[]){DAVQL_ADD, DAVQL_SUB},
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
734 dav_parse_expression);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
735 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
736
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
737 static int dav_parse_fieldlist(DavQLStatement *stmt, UcxList *token) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
738
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
739 // RULE: "-"
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
740 if (token_is(token, DAVQL_TOKEN_OPERATOR) && tokenvalue_is(token, "-")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
741 DavQLField *field = malloc(sizeof(DavQLField));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
742 field->expr = calloc(sizeof(DavQLExpression), 1);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
743 field->expr->type = DAVQL_IDENTIFIER;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
744 field->expr->srctext = field->name = token_sstr(token);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
745 stmt->fields = ucx_list_append(stmt->fields, field);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
746 return 1;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
747 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
748
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
749 // RULE: "*", {",", Expression, " as ", Identifier}
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
750 if (token_is(token, DAVQL_TOKEN_OPERATOR) && tokenvalue_is(token, "*")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
751 DavQLField *field = malloc(sizeof(DavQLField));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
752 field->expr = calloc(sizeof(DavQLExpression), 1);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
753 field->expr->type = DAVQL_IDENTIFIER;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
754 field->expr->srctext = field->name = token_sstr(token);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
755 stmt->fields = ucx_list_append(stmt->fields, field);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
756
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
757 int total_consumed = 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
758 int consumed = 1;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
759
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
760 do {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
761 token = ucx_list_get(token, consumed);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
762 total_consumed += consumed;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
763
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
764 if (token_is(token, DAVQL_TOKEN_COMMA)) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
765 total_consumed++; token = token->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
766 DavQLExpression * expr = calloc(sizeof(DavQLExpression), 1);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
767 consumed = dav_parse_expression(stmt, token, expr);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
768 if (expr->type == DAVQL_UNDEFINED_TYPE) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
769 dav_free_expression(expr);
108
0199f13c7fe2 fixed parsing of binary expressions did not yield the correct type
Mike Becker <universe@uap-core.de>
parents: 107
diff changeset
770 consumed = 0;
0199f13c7fe2 fixed parsing of binary expressions did not yield the correct type
Mike Becker <universe@uap-core.de>
parents: 107
diff changeset
771 dav_error_in_context(DAVQL_ERROR_INVALID_EXPR,
0199f13c7fe2 fixed parsing of binary expressions did not yield the correct type
Mike Becker <universe@uap-core.de>
parents: 107
diff changeset
772 _error_invalid_expr, stmt, token);
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
773 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
774 DavQLField *field = malloc(sizeof(DavQLField));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
775 field->expr = expr;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
776 field->name = expr->srctext;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
777 stmt->fields = ucx_list_append(stmt->fields, field);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
778 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
779
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
780 // TODO: parse "as"
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
781 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
782 consumed = 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
783 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
784 } while (consumed > 0);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
785
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
786 return total_consumed;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
787 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
788
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
789 // RULE: FieldExpression, {",", FieldExpression}
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
790 // TODO: make it so
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
791
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
792 return 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
793 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
794
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
795 static int dav_parse_where_clause(DavQLStatement *stmt, UcxList *token) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
796 return 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
797 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
798
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
799 static int dav_parse_with_clause(DavQLStatement *stmt, UcxList *token) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
800
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
801 int total_consumed = 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
802
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
803 // RULE: "depth", "=", (Number | "infinity")
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
804 if (tokenvalue_is(token, "depth")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
805 token = token->next; total_consumed++;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
806 if (token_is(token, DAVQL_TOKEN_EQ)) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
807 token = token->next; total_consumed++;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
808 if (tokenvalue_is(token, "infinity")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
809 stmt->depth = DAV_DEPTH_INFINITY;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
810 token = token->next; total_consumed++;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
811 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
812 DavQLExpression *depthexpr = calloc(sizeof(DavQLExpression), 1);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
813
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
814 int consumed = dav_parse_expression(stmt, token, depthexpr);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
815
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
816 if (consumed) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
817 if (depthexpr->type == DAVQL_NUMBER) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
818 if (depthexpr->srctext.ptr[0] == '%') {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
819 stmt->depth = DAV_DEPTH_PLACEHOLDER;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
820 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
821 sstr_t depthstr = depthexpr->srctext;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
822 char *conv = malloc(depthstr.length+1);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
823 char *chk;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
824 memcpy(conv, depthstr.ptr, depthstr.length);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
825 conv[depthstr.length] = '\0';
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
826 stmt->depth = strtol(conv, &chk, 10);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
827 if (*chk || stmt->depth < -1) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
828 dav_error_in_context(DAVQL_ERROR_INVALID_DEPTH,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
829 _error_invalid_depth, stmt, token);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
830 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
831 free(conv);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
832 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
833 total_consumed += consumed;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
834 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
835 dav_error_in_context(DAVQL_ERROR_INVALID_DEPTH,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
836 _error_invalid_depth, stmt, token);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
837 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
838 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
839
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
840 dav_free_expression(depthexpr);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
841 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
842 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
843 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
844
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
845 return total_consumed;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
846 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
847
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
848 static int dav_parse_orderby_clause(DavQLStatement *stmt, UcxList *token) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
849 return 0;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
850 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
851
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
852 /**
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
853 * Semantic analysis of a get statement.
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
854 * @param stmt the statement to analyze.
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
855 */
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
856 static void dav_analyze_get_statement(DavQLStatement *stmt) {
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
857 // TODO: make it so
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
858 }
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
859
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
860
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
861 /**
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
862 * Parser of a get statement.
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
863 * @param stmt the statement object that shall contain the syntax tree
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
864 * @param tokens the token list
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
865 */
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
866 static void dav_parse_get_statement(DavQLStatement *stmt, UcxList *tokens) {
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
867 stmt->type = DAVQL_GET;
92
e073cf4afc6a added order by clause to grammar + moved with clause right after the from clause
Mike Becker <universe@uap-core.de>
parents: 91
diff changeset
868
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
869 // Consume field list
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
870 tokens = ucx_list_get(tokens, dav_parse_fieldlist(stmt, tokens));
108
0199f13c7fe2 fixed parsing of binary expressions did not yield the correct type
Mike Becker <universe@uap-core.de>
parents: 107
diff changeset
871 if (stmt->errorcode) {
0199f13c7fe2 fixed parsing of binary expressions did not yield the correct type
Mike Becker <universe@uap-core.de>
parents: 107
diff changeset
872 return;
0199f13c7fe2 fixed parsing of binary expressions did not yield the correct type
Mike Becker <universe@uap-core.de>
parents: 107
diff changeset
873 }
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
874
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
875 // Consume from keyword
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
876 if (token_is(tokens, DAVQL_TOKEN_KEYWORD)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
877 && tokenvalue_is(tokens, "from")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
878 tokens = tokens->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
879 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
880 dav_error_in_context(DAVQL_ERROR_MISSING_TOKEN,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
881 _error_missing_from, stmt, tokens);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
882 return;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
883 }
83
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
884
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
885 // Consume path
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
886 if (token_is(tokens, DAVQL_TOKEN_STRING)) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
887 stmt->path = token_sstr(tokens);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
888 tokens = tokens->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
889 } else if (token_is(tokens, DAVQL_TOKEN_OPERATOR)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
890 && tokenvalue_is(tokens, "/")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
891 stmt->path.ptr = token_sstr(tokens).ptr;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
892 tokens = tokens->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
893 while (!token_is(tokens, DAVQL_TOKEN_KEYWORD)) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
894 sstr_t toksstr = token_sstr(tokens);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
895 stmt->path.length = toksstr.ptr-stmt->path.ptr+toksstr.length;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
896 tokens = tokens->next;
83
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
897 }
107
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
898 } else if (token_is(tokens, DAVQL_TOKEN_FMTSPEC) &&
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
899 tokenvalue_is(tokens, "%s")) {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
900 stmt->path = token_sstr(tokens);
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
901 tokens = tokens->next;
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
902 } else {
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
903 dav_error_in_context(DAVQL_ERROR_MISSING_TOKEN,
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
904 _error_missing_path, stmt, tokens);
a0903d2d8e3e added format specifier parser
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
905 return;
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
906 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
907
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
908 // Consume with clause (if any)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
909 if (token_is(tokens, DAVQL_TOKEN_KEYWORD)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
910 && tokenvalue_is(tokens, "with")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
911 tokens = tokens->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
912 tokens = ucx_list_get(tokens,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
913 dav_parse_with_clause(stmt, tokens));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
914 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
915 if (stmt->errorcode) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
916 return;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
917 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
918
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
919 // Consume where clause (if any)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
920 if (token_is(tokens, DAVQL_TOKEN_KEYWORD)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
921 && tokenvalue_is(tokens, "where")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
922 tokens = tokens->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
923 tokens = ucx_list_get(tokens,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
924 dav_parse_where_clause(stmt, tokens));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
925 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
926 if (stmt->errorcode) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
927 return;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
928 }
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
929
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
930 // Consume order by clause (if any)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
931 if (token_is(tokens, DAVQL_TOKEN_KEYWORD)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
932 && tokenvalue_is(tokens, "order")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
933 tokens = tokens->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
934 if (token_is(tokens, DAVQL_TOKEN_KEYWORD)
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
935 && tokenvalue_is(tokens, "by")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
936 tokens = tokens->next;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
937 tokens = ucx_list_get(tokens,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
938 dav_parse_orderby_clause(stmt, tokens));
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
939 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
940 dav_error_in_context(DAVQL_ERROR_MISSING_TOKEN,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
941 _error_missing_by, stmt, tokens);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
942 return;
94
8822f7c08843 removed ultrabreak gotos
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
943 }
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
944 }
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
945 if (stmt->errorcode) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
946 return;
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
947 }
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
948
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
949
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
950 if (tokens) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
951 if (token_is(tokens, DAVQL_TOKEN_INVALID)) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
952 dav_error_in_context(DAVQL_ERROR_INVALID_TOKEN,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
953 _error_invalid_token, stmt, tokens);
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
954 } else {
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
955 dav_error_in_context(DAVQL_ERROR_UNEXPECTED_TOKEN,
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
956 _error_unexpected_token, stmt, tokens);
102
e9ae1318a559 reworked expression grammar (removed left recursion) + added field names to debugger + fixed bug when WITH-clause was the last clause in a statement
Mike Becker <universe@uap-core.de>
parents: 99
diff changeset
957 }
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
958 } else {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
959 dav_analyze_get_statement(stmt);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
960 }
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
961 }
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
962
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
963 static void dav_parse_set_statement(DavQLStatement *stmt, UcxList *tokens) {
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
964 stmt->type = DAVQL_SET;
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
965
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
966 // TODO: make it so
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
967 }
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
968
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
969 DavQLStatement* dav_parse_statement(sstr_t srctext) {
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
970 DavQLStatement *stmt = calloc(1, sizeof(DavQLStatement));
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
971
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
972 // default values
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
973 stmt->type = -1;
85
0ab1cf261a44 improved grammar + changed depth default, type and added infinity
Mike Becker <universe@uap-core.de>
parents: 83
diff changeset
974 stmt->depth = 1;
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
975
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
976 // save trimmed source text
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
977 stmt->srctext = sstrtrim(srctext);
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
978
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
979 // tokenization
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
980 UcxList* tokens = dav_parse_tokenize(stmt->srctext);
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
981
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
982 if (tokens) {
83
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
983 // use first token to determine query type
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
984
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
985 if (tokenvalue_is(tokens, "get")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
986 dav_parse_get_statement(stmt, tokens->next);
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
987 } else if (tokenvalue_is(tokens, "set")) {
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
988 dav_parse_set_statement(stmt, tokens->next);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
989 } else {
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
990 stmt->type = DAVQL_ERROR;
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
991 stmt->errorcode = DAVQL_ERROR_INVALID;
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
992 stmt->errormessage = strdup(_error_invalid);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
993 }
83
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
994
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
995 // free token data
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
996 UCX_FOREACH(token, tokens) {
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
997 free(token->data);
7d20ce5d235b improved expression parser (can now detect literals and identifiers) - TODO: tokenizer must not split strings and "backstick-identifiers"
Mike Becker <universe@uap-core.de>
parents: 82
diff changeset
998 }
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
999 ucx_list_free(tokens);
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1000 } else {
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1001 stmt->type = DAVQL_ERROR;
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
1002 stmt->errorcode = DAVQL_ERROR_INVALID;
106
9cec78f23cbf started refactoring davqlparser
Mike Becker <universe@uap-core.de>
parents: 103
diff changeset
1003 stmt->errormessage = strdup(_error_invalid);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1004 }
79
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
1005
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
1006 return stmt;
59c518ae0641 added parse function prototype + started writing a debugger
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
1007 }
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1008
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1009 void dav_free_statement(DavQLStatement *stmt) {
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1010 UCX_FOREACH(expr, stmt->fields) {
98
237844f263b4 minor grammar fix (alias name is now identifier and not string) + completed debugger (so far) + separated field list parser (currently unable to parse alias names) + new structure for fields
Mike Becker <universe@uap-core.de>
parents: 97
diff changeset
1011 dav_free_field(expr->data);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1012 }
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1013 ucx_list_free(stmt->fields);
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1014
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1015 if (stmt->where) {
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1016 dav_free_expression(stmt->where);
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1017 }
86
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
1018 if (stmt->errormessage) {
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
1019 free(stmt->errormessage);
ecba8bdf9741 davql: error handling + minor grammar changes
Mike Becker <universe@uap-core.de>
parents: 85
diff changeset
1020 }
95
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
1021 UCX_FOREACH(crit, stmt->orderby) {
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
1022 dav_free_order_criterion(crit->data);
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
1023 }
8ed7d8df6427 simplified with clause parsing + added order by to data structure and debugger (TODO: implement)
Mike Becker <universe@uap-core.de>
parents: 94
diff changeset
1024 ucx_list_free(stmt->orderby);
82
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1025 free(stmt);
0567444f2d76 renamed some enums to avoid collisions with macros + minor grammar change (backsticks for identifiers) + tokenizer + parser skeleton
Mike Becker <universe@uap-core.de>
parents: 80
diff changeset
1026 }

mercurial