]> Nishi Git Mirror - dataworks.git/commitdiff
parser is working
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Thu, 23 May 2024 10:21:17 +0000 (10:21 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Thu, 23 May 2024 10:21:17 +0000 (10:21 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@87 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

Client/main.c
Library/dw_parser.h
Library/parser.c

index abe1f3b35210d79006fd7ae3eddc721b11d58515..80a2c4ea29581e164226edcc6be8e334a41505ce 100644 (file)
@@ -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");
index a7521ff7cbe0bf1993aedbb7756c9320261fad61..9f6b38ccd7e7109838bf7a679ed4f9b9bc27d9ff 100644 (file)
@@ -42,7 +42,7 @@ extern "C" {
 
 #include <stdbool.h>
 
-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
 }
index 3d72a359a63794193488df7598fc1d3e5e252722..57ffac6cfebb7d5235d6ccf97e2f61b67a2546bf 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-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;