39struct __dw_token* __dw_parser_parse(
const char* str,
bool top) {
40 struct __dw_token* token = malloc(
sizeof(*token));
45 token->type = __DW_METHOD;
46 char* buf = malloc(1);
54 bool has_brace =
false;
57 for(i = 0; str[i] != 0; i++) {
60 if(str[i] ==
'(') brace++;
61 if(str[i] ==
')') brace--;
65 for(; str[i] != 0 && (str[i] ==
'\t' || str[i] ==
' '); i++)
70 br = __dw_strcat(tmp, cbuf);
74 for(j = 0; buf[j] != 0; j++) {
75 if(buf[j] !=
' ' && buf[j] !=
'\t') {
76 char* newbuf = __dw_strdup(buf + j);
82 for(j = 0; br[j] != 0; j++) {
83 if(br[j] !=
' ' && br[j] !=
'\t') {
84 char* newbr = __dw_strdup(br + j);
90 for(j = strlen(buf) - 1; j >= 0; j--) {
91 if(buf[j] !=
' ' && buf[j] !=
'\t') {
96 for(j = strlen(br) - 1; j >= 0; j--) {
97 if(br[j] !=
' ' && br[j] !=
'\t') {
103 char* newbuf = __dw_strdup(buf + 1);
107 token->name = __dw_strdup(buf);
109 char* comma = malloc(1);
114 if(c ==
'(' || c ==
')' || intbrace > 0) {
115 if(c ==
'(') intbrace++;
116 if(c ==
')') intbrace--;
119 comma = __dw_strcat(tmp, cbuf);
121 }
else if(c == 0 || c ==
',') {
122 if(strlen(comma) > 0) {
124 if(token->token != NULL) {
125 for(j = 0; token->token[j] != NULL; j++)
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];
135 token->token = newtokens;
136 token->token[j] = __dw_parser_parse(comma,
false);
137 token->token[j + 1] = NULL;
146 comma = __dw_strcat(tmp, cbuf);
150 if(top) printf(
"%s(%s)\n", buf, br);
164 buf = __dw_strcat(tmp, cbuf);
167 }
else if(str[i] ==
'(') {
170 }
else if(str[i] ==
')') {
172 }
else if(str[i] ==
'"') {
174 }
else if(str[i] ==
',') {
180 buf = __dw_strcat(tmp, cbuf);
185 token->type = __DW_VALUE;
186 token->name = __dw_strdup(buf);
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) {
198 for(i = 0; token->token[i] != NULL; i++) __dw_parser_free(token->token[i]);
205void __dw_parser_print(
struct __dw_token* token,
int depth) {
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);