From a1eca339911a485e9cfb0b696eb0d43690b1d366 Mon Sep 17 00:00:00 2001 From: nishi Date: Thu, 23 May 2024 10:21:17 +0000 Subject: [PATCH] parser is working git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@87 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Client/main.c | 13 +++++++++-- Library/dw_parser.h | 4 ++-- Library/parser.c | 57 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 7 deletions(-) diff --git a/Client/main.c b/Client/main.c index abe1f3b..80a2c4e 100644 --- a/Client/main.c +++ b/Client/main.c @@ -44,7 +44,6 @@ HANDLE winstdout; #endif void padleft(int leftpad, const char* str) { - int i; char* spaces = malloc(leftpad - strlen(str) + 1); memset(spaces, ' ', leftpad - strlen(str)); spaces[leftpad - strlen(str)] = 0; @@ -52,6 +51,15 @@ void padleft(int leftpad, const char* str) { free(spaces); } +void print_recursive(struct __dw_token* token, int depth){ + int i; + for(i = 0; i < depth; i++) printf(" "); + printf("%d:%s\n", token->type, token->name == NULL ? "(null)" : token->name); + if(token->token != NULL){ + for(i = 0; token->token[i] != NULL; i++) print_recursive(token->token[i], depth + 1); + } +} + int main(int argc, char** argv) { int i; bool clear = true; @@ -224,11 +232,12 @@ int main(int argc, char** argv) { char* line = malloc(i + 1); line[i] = 0; memcpy(line, linebuf, i); - struct __dw_token* token = __dw_parser_parse(NULL, line); + struct __dw_token* token = __dw_parser_parse(line); if(token != NULL) { if(token->error) { printf("%s\n", dataworks_database_strerror(token->errnum)); } else { + print_recursive(token, 0); } } else { printf("Parser returned NULL. Help!\n"); diff --git a/Library/dw_parser.h b/Library/dw_parser.h index a7521ff..9f6b38c 100644 --- a/Library/dw_parser.h +++ b/Library/dw_parser.h @@ -42,7 +42,7 @@ extern "C" { #include -enum __token { METHOD = 0, VALUE }; +enum __token { __DW_METHOD = 0, __DW_VALUE }; struct __dw_token { char* name; @@ -52,7 +52,7 @@ struct __dw_token { struct __dw_token** token; }; -struct __dw_token* __dw_parser_parse(const char* name, const char* str); +struct __dw_token* __dw_parser_parse(const char* str); #ifdef __cplusplus } diff --git a/Library/parser.c b/Library/parser.c index 3d72a35..57ffac6 100644 --- a/Library/parser.c +++ b/Library/parser.c @@ -36,11 +36,13 @@ #include #include -struct __dw_token* __dw_parser_parse(const char* name, const char* str) { +struct __dw_token* __dw_parser_parse(const char* str) { struct __dw_token* token = malloc(sizeof(*token)); token->error = false; token->errnum = DW_ERR_SUCCESS; token->token = NULL; + token->name = NULL; + token->type = __DW_METHOD; char* buf = malloc(1); buf[0] = 0; int i; @@ -49,6 +51,7 @@ struct __dw_token* __dw_parser_parse(const char* name, const char* str) { cbuf[1] = 0; int brace = 0; char* br = malloc(1); + bool has_brace = false; br[0] = 0; for(i = 0; str[i] != 0; i++) { cbuf[0] = str[i]; @@ -99,8 +102,47 @@ struct __dw_token* __dw_parser_parse(const char* name, const char* str) { free(buf); buf = newbuf; } - printf("%s:%s\n", buf, br); - __dw_parser_parse(buf, br); + token->name = __dw_strdup(buf); + int k; + char* comma = malloc(1); + comma[0] = 0; + int intbrace = 0; + for(k = 0;; k++){ + char c = br[k]; + if(c == '(' || c == ')' || intbrace > 0){ + if(c == '(') intbrace++; + if(c == ')') intbrace--; + cbuf[0] = c; + char* tmp = comma; + comma = __dw_strcat(tmp, cbuf); + free(tmp); + }else if(c == 0 || c == ','){ + j = 0; + if(token->token != NULL){ + for(j = 0; token->token[j] != NULL; j++); + } + struct __dw_token** newtokens = malloc(sizeof(*newtokens) * (j + 2)); + if(token->token != NULL){ + for(j = 0; token->token[j] != NULL; j++){ + newtokens[j] = token->token[j]; + } + free(token->token); + } + token->token = newtokens; + token->token[j] = __dw_parser_parse(comma); + token->token[j + 1] = NULL; + free(comma); + comma = malloc(1); + comma[0] = 0; + if(c == 0) break; + }else{ + cbuf[0] = c; + char* tmp = comma; + comma = __dw_strcat(tmp, cbuf); + free(tmp); + } + } + free(comma); free(br); br = malloc(1); br[0] = 0; @@ -117,17 +159,26 @@ struct __dw_token* __dw_parser_parse(const char* name, const char* str) { free(tmp); } } else if(str[i] == '(') { + has_brace = true; brace++; } else if(str[i] == ')') { brace--; } else if(str[i] == '"') { dq = !dq; + } else if(str[i] == ',') { + free(buf); + buf = malloc(1); + buf[0] = 0; } else { char* tmp = buf; buf = __dw_strcat(tmp, cbuf); free(tmp); } } + if(!has_brace){ + token->type = __DW_VALUE; + token->name = __dw_strdup(buf); + } free(br); free(buf); return token; -- 2.43.0