yylval.node.string = strdup(yytext + 1);
yylval.node.string[strlen(yylval.node.string) - 1] = 0;
yylval.node.nodes = NULL;
+ yylval.node.ident = NULL;
return (STRING);
}
['][^']*['] {
yylval.node.string = strdup(yytext + 1);
yylval.node.string[strlen(yylval.node.string) - 1] = 0;
yylval.node.nodes = NULL;
+ yylval.node.ident = NULL;
return (STRING);
}
[a-zA-Z0-9_\-]+ {
yylval.node.ident = strdup(yytext);
yylval.node.nodes = NULL;
+ yylval.node.string = NULL;
return (IDENTIFIER);
}
[\(\),] {
} node;
}
+%{
+void parser_process(struct Node* node);
+%}
+
%%
argument
$<node>$.nodes = $<node>1.nodes;
$<node>$.ident = $<node>1.ident;
}
+ | command {
+ $<node>$ = $<node>1;
+ }
;
single_argument
arguments
: single_argument {
$<node>$.nodes = malloc(sizeof(*$<node>$.nodes) * 2);
- $<node>$.nodes[0] = &$<node>1;
+ $<node>$.nodes[0] = malloc(sizeof($<node>$));
+ $<node>$.nodes[0]->nodes = NULL;
+ $<node>$.nodes[0]->ident = NULL;
+ $<node>$.nodes[0]->string = NULL;
+ if($<node>1.ident != NULL) $<node>$.nodes[0]->ident = strdup($<node>1.ident);
+ if($<node>1.string != NULL) $<node>$.nodes[0]->string = strdup($<node>1.string);
+ if($<node>1.nodes != NULL) $<node>$.nodes[0]->nodes = $<node>1.nodes;
$<node>$.nodes[1] = NULL;
}
| arguments ',' single_argument {
for(i = 0; old_nodes[i] != NULL; i++);
$<node>$.nodes = malloc(sizeof(*$<node>$.nodes) * (i + 2));
for(i = 0; old_nodes[i] != NULL; i++) $<node>$.nodes[i] = old_nodes[i];
- $<node>$.nodes[i] = &$<node>3;
+ $<node>$.nodes[i] = malloc(sizeof($<node>$));
+ $<node>$.nodes[i]->nodes = NULL;
+ $<node>$.nodes[i]->ident = NULL;
+ $<node>$.nodes[i]->string = NULL;
+ if($<node>3.ident != NULL) $<node>$.nodes[i]->ident = strdup($<node>3.ident);
+ if($<node>3.string != NULL) $<node>$.nodes[i]->string = strdup($<node>3.string);
+ if($<node>3.nodes != NULL) $<node>$.nodes[i]->nodes = $<node>3.nodes;
$<node>$.nodes[i + 1] = NULL;
free(old_nodes);
}
int yywrap() { return 1; }
-int yyerror(const char* err) { fprintf(stderr, "Parser error: %s\n", err); return 0; }
+int yyerror(const char* err) {
+ fprintf(stderr, "Parser error: %s\n", err);
+ return 0;
+}
bool dataworks_get_if_supported(void) {
#ifdef SUPPORTED
struct dataworks_db_result* dataworks_database_execute_code(struct dataworks_db* db, const char* code, bool dolog) {
struct dataworks_db_result* r = malloc(sizeof(*r));
r->error = false;
- r->value = NULL;
+ r->value = NULL;
int err;
- if((err = __dw_parser_parse(code, dolog)) != DW_ERR_SUCCESS){
+ if((err = __dw_parser_parse(code, dolog)) != DW_ERR_SUCCESS) {
r->error = true;
r->errnum = err;
}
#include "dw_database.h"
#include "dw_util.h"
+#include "../Grammar/dw.tab.h"
+
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
extern void* yy_scan_string(const char* str);
extern void yy_delete_buffer(void* buffer);
+void print_node(struct Node* node, bool top) {
+ printf("%s(", node->ident);
+ if(node->nodes != NULL) {
+ int i;
+ for(i = 0; node->nodes[i] != NULL; i++) {
+ if(i > 0) printf(", ");
+ if(node->nodes[i]->ident != NULL) {
+ print_node(node->nodes[i], false);
+ } else {
+ printf("\"%s\"", node->nodes[i]->string);
+ }
+ }
+ }
+ printf(")");
+ if(top) printf("\n");
+}
+
+void parser_process(struct Node* node) {
+ if(node->ident != NULL) {
+ int i;
+ print_node(node, true);
+ }
+}
+
int __dw_parser_parse(const char* str, bool top) {
#ifdef PARSER_DEBUG
yydebug = 1;
#endif
void* buf = yy_scan_string(str);
- if(yyparse() != 0){
+ if(yyparse() != 0) {
yy_delete_buffer(buf);
return DW_ERR_PARSER_FAIL;
+ } else {
+ extern YYSTYPE yyval;
+ parser_process(&yyval.node);
}
yy_delete_buffer(buf);