DataWorks
Database Library/Client
Loading...
Searching...
No Matches
parser.c
1/* $Id: parser.c 98 2024-05-23 23:51:27Z nishi $ */
2/* --- START LICENSE --- */
3/* -------------------------------------------------------------------------- */
4/* Copyright (c) 2024 Crabware. */
5/* Redistribution and use in source and binary forms, with or without modific */
6/* ation, are permitted provided that the following conditions are met: */
7/* 1. Redistributions of source code must retain the above copyright noti */
8/* ce, this list of conditions and the following disclaimer. */
9/* 2. Redistributions in binary form must reproduce the above copyright n */
10/* otice, this list of conditions and the following disclaimer in the documen */
11/* tation and/or other materials provided with the distribution. */
12/* 3. Neither the name of the copyright holder nor the names of its contr */
13/* ibutors may be used to endorse or promote products derived from this softw */
14/* are without specific prior written permission. */
15/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */
16/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */
17/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */
18/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */
19/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */
20/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */
21/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */
22/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */
23/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */
24/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */
25/* OF SUCH DAMAGE. */
26/* -------------------------------------------------------------------------- */
27/* --- END LICENSE --- */
28
29#include "dw_parser.h"
30
31#include "dw_database.h"
32#include "dw_util.h"
33
34#include <stdbool.h>
35#include <stddef.h>
36#include <stdlib.h>
37#include <string.h>
38
39struct __dw_token* __dw_parser_parse(const char* str, bool top) {
40 struct __dw_token* token = malloc(sizeof(*token));
41 token->error = false;
42 token->errnum = DW_ERR_SUCCESS;
43 token->token = NULL;
44 token->name = NULL;
45 token->type = __DW_METHOD;
46 char* buf = malloc(1);
47 buf[0] = 0;
48 int i;
49 bool dq = false;
50 char cbuf[2];
51 cbuf[1] = 0;
52 int brace = 0;
53 char* br = malloc(1);
54 bool has_brace = false;
55 br[0] = 0;
56 int start = 0;
57 for(i = 0; str[i] != 0; i++) {
58 cbuf[0] = str[i];
59 if(brace > 0) {
60 if(str[i] == '(') brace++;
61 if(str[i] == ')') brace--;
62 if(brace > 0) {
63 if(str[i] == ',') {
64 i++;
65 for(; str[i] != 0 && (str[i] == '\t' || str[i] == ' '); i++)
66 ;
67 i--;
68 }
69 char* tmp = br;
70 br = __dw_strcat(tmp, cbuf);
71 free(tmp);
72 } else {
73 int j;
74 for(j = 0; buf[j] != 0; j++) {
75 if(buf[j] != ' ' && buf[j] != '\t') {
76 char* newbuf = __dw_strdup(buf + j);
77 free(buf);
78 buf = newbuf;
79 break;
80 }
81 }
82 for(j = 0; br[j] != 0; j++) {
83 if(br[j] != ' ' && br[j] != '\t') {
84 char* newbr = __dw_strdup(br + j);
85 free(br);
86 br = newbr;
87 break;
88 }
89 }
90 for(j = strlen(buf) - 1; j >= 0; j--) {
91 if(buf[j] != ' ' && buf[j] != '\t') {
92 buf[j + 1] = 0;
93 break;
94 }
95 }
96 for(j = strlen(br) - 1; j >= 0; j--) {
97 if(br[j] != ' ' && br[j] != '\t') {
98 br[j + 1] = 0;
99 break;
100 }
101 }
102 if(buf[0] == ',') {
103 char* newbuf = __dw_strdup(buf + 1);
104 free(buf);
105 buf = newbuf;
106 }
107 token->name = __dw_strdup(buf);
108 int k;
109 char* comma = malloc(1);
110 comma[0] = 0;
111 int intbrace = 0;
112 for(k = 0;; k++) {
113 char c = br[k];
114 if(c == '(' || c == ')' || intbrace > 0) {
115 if(c == '(') intbrace++;
116 if(c == ')') intbrace--;
117 cbuf[0] = c;
118 char* tmp = comma;
119 comma = __dw_strcat(tmp, cbuf);
120 free(tmp);
121 } else if(c == 0 || c == ',') {
122 if(strlen(comma) > 0) {
123 j = 0;
124 if(token->token != NULL) {
125 for(j = 0; token->token[j] != NULL; j++)
126 ;
127 }
128 struct __dw_token** newtokens = malloc(sizeof(*newtokens) * (j + 2));
129 if(token->token != NULL) {
130 for(j = 0; token->token[j] != NULL; j++) {
131 newtokens[j] = token->token[j];
132 }
133 free(token->token);
134 }
135 token->token = newtokens;
136 token->token[j] = __dw_parser_parse(comma, false);
137 token->token[j + 1] = NULL;
138 free(comma);
139 comma = malloc(1);
140 comma[0] = 0;
141 }
142 if(c == 0) break;
143 } else {
144 cbuf[0] = c;
145 char* tmp = comma;
146 comma = __dw_strcat(tmp, cbuf);
147 free(tmp);
148 }
149 }
150 if(top) printf("%s(%s)\n", buf, br);
151 free(comma);
152 free(br);
153 br = malloc(1);
154 br[0] = 0;
155 free(buf);
156 buf = malloc(1);
157 buf[0] = 0;
158 }
159 } else if(dq) {
160 if(str[i] == '"') {
161 dq = !dq;
162 } else {
163 char* tmp = buf;
164 buf = __dw_strcat(tmp, cbuf);
165 free(tmp);
166 }
167 } else if(str[i] == '(') {
168 has_brace = true;
169 brace++;
170 } else if(str[i] == ')') {
171 brace--;
172 } else if(str[i] == '"') {
173 dq = !dq;
174 } else if(str[i] == ',') {
175 free(buf);
176 buf = malloc(1);
177 buf[0] = 0;
178 } else {
179 char* tmp = buf;
180 buf = __dw_strcat(tmp, cbuf);
181 free(tmp);
182 }
183 }
184 if(!has_brace) {
185 token->type = __DW_VALUE;
186 token->name = __dw_strdup(buf);
187 }
188 free(br);
189 free(buf);
190 return token;
191}
192
193void __dw_parser_free(struct __dw_token* token) {
194 if(token->name != NULL) free(token->name);
195 if(token->type == __DW_METHOD) {
196 if(token->token != NULL) {
197 int i;
198 for(i = 0; token->token[i] != NULL; i++) __dw_parser_free(token->token[i]);
199 free(token->token);
200 }
201 }
202 free(token);
203}
204
205void __dw_parser_print(struct __dw_token* token, int depth) {
206 int i;
207 for(i = 0; i < depth; i++) printf(" ");
208 printf("%d:[%s]\n", token->type, token->name == NULL ? "(null)" : token->name);
209 if(token->token != NULL) {
210 for(i = 0; token->token[i] != NULL; i++) __dw_parser_print(token->token[i], depth + 1);
211 }
212}
DataWorks database.
@ DW_ERR_SUCCESS
Success.
Definition dw_database.h:67
DataWorks parser.
DataWorks utils.