Skip to content

Commit 4c42c14

Browse files
author
Matthew Stern
committed
a much needed update to parsing that fixes a lot and makes cleaner
1 parent 170c705 commit 4c42c14

File tree

6 files changed

+262
-44
lines changed

6 files changed

+262
-44
lines changed

src/topologic_parser.lex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ blanks [ \t\n]+
2626
"mem_option" return(MEM_OPT);
2727
"context" return(LEX_CONTEXT);
2828
"max_loop" return(MAX_LOOP);
29+
"request_flag" return(REQ_FLAG);
2930
[0-9]+ {yylval.val = atoi(yytext); return(VALUE);}
3031
%%

src/topologic_parser.y

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,47 @@ int edge_f(int id, void *args, void *glbl, const void *const edge_vars_a, const
88
void yyerror(struct graph** graph, const char *s);
99
extern FILE *yyin;
1010
int yylex(void);
11+
12+
int max_state_changes = -1;
13+
int snapshot_timestamp = START_STOP;
14+
int max_loop = MAX_LOOPS;
15+
unsigned int lvl_verbose = VERTICES | EDGES | FUNCTIONS | GLOBALS;
16+
enum CONTEXT context = SINGLE;
17+
enum MEM_OPTION mem_option = CONTINUE;
18+
enum REQUEST_FLAG request_flag = IGNORE_FAIL_REQUEST;
19+
20+
void parse_create_edge(struct graph *graph, int id_a, int id_b)
21+
{
22+
struct vertex *a = (struct vertex *) find(graph->vertices, id_a);
23+
struct vertex *b = (struct vertex *) find(graph->vertices, id_b);
24+
if (a && b)
25+
{
26+
if (create_edge(a, b, edge_f, NULL) == NULL)
27+
fprintf(stderr, "Failed to create Edge Between %d and %d\n", a->id, b->id);
28+
}
29+
else
30+
fprintf(stderr, "Invalid Vertices a:%p b:%p\n", a, b);
31+
}
32+
33+
void parse_create_bi_edge(struct graph *graph, int id_a, int id_b)
34+
{
35+
int val = 0;
36+
struct vertex *a = (struct vertex *) find(graph->vertices, id_a);
37+
struct vertex *b = (struct vertex *) find(graph->vertices, id_b);
38+
if (a && b)
39+
{
40+
if((val = create_bi_edge(a, b, edge_f, NULL, NULL, NULL) < 0))
41+
fprintf(stderr, "%d: Failed to bi create Edge Between %d and %d\n", val, a->id, b->id);
42+
}
43+
else
44+
fprintf(stderr, "Invalid Vertices a:%p(%d) b:%p(%d)\n", a, id_a, b, id_b);
45+
}
46+
47+
void parse_create_vertex(struct graph *graph, int id)
48+
{
49+
if (create_vertex(graph, f, id, NULL) < 0)
50+
fprintf(stderr, "Failed To Create Vertex %d\n", id);
51+
}
1152
%}
1253

1354
%union {
@@ -31,67 +72,75 @@ int yylex(void);
3172
%token LEX_CONTEXT
3273
%token MEM_OPT
3374
%token MAX_LOOP
75+
%token REQ_FLAG
3476
%token <val> VALUE
3577

3678
%start json
3779
%%
38-
json: L_BRACKET GRAPH {*graph = GRAPH_INIT(); if (!(*graph)){fprintf(stderr, "Can't create graph\n"); return -1;}}
39-
COLON L_BRACKET content R_BRACKET
40-
R_BRACKET
80+
json: L_BRACKET GRAPH
81+
COLON L_BRACKET params g R_BRACKET
82+
R_BRACKET
4183
;
42-
content: params g
43-
| g
44-
| params
45-
|
46-
;
4784
params: verb COMMA params
4885
| state COMMA params
4986
| mem_opt COMMA params
5087
| context COMMA params
5188
| max_loop COMMA params
89+
| req_flag COMMA params
5290
| verb
5391
| mem_opt
5492
| context
5593
| state
5694
| max_loop
57-
|
95+
| req_flag
96+
| {
97+
*graph = graph_init(max_state_changes, snapshot_timestamp, max_loop, lvl_verbose, context, mem_option, request_flag);
98+
if (!(*graph))
99+
{
100+
fprintf(stderr, "Can't create graph\n");
101+
return -1;
102+
}
103+
}
58104
;
59-
state: MAX_STATE COLON VALUE {(*graph)->max_state_changes = $3;}
105+
state: MAX_STATE COLON VALUE {max_state_changes = $3;}
60106
;
61-
verb: LVL_VERBOSE COLON VALUE {(*graph)->lvl_verbose = $3;}
107+
verb: LVL_VERBOSE COLON VALUE {lvl_verbose = $3;}
62108
;
63-
context: LEX_CONTEXT COLON VALUE {(*graph)->context = $3;}
109+
context: LEX_CONTEXT COLON VALUE {context = (enum CONTEXT) $3;}
64110
;
65-
mem_opt: MEM_OPT COLON VALUE {(*graph)->mem_option = $3;}
111+
mem_opt: MEM_OPT COLON VALUE {mem_option = (enum MEM_OPTION) $3;}
66112
;
67-
max_loop: MAX_LOOP COLON VALUE {(*graph)->max_loop = $3;}
113+
max_loop: MAX_LOOP COLON VALUE {max_loop = $3;}
68114
;
115+
req_flag: REQ_FLAG COLON VALUE {request_flag = (enum REQUEST_FLAG) $3;}
116+
;
69117
g: vs COMMA es COMMA bes
70118
| vs COMMA bes COMMA es
71119
| vs COMMA es
72120
| vs COMMA bes
73121
| vs
122+
|
74123
;
75124
vs: VERTICES_ COLON L_SQUARE v R_SQUARE
76125
;
77126
v: /* empty */
78-
| VALUE COMMA {if (create_vertex(*graph, f, $1, NULL) < 0) fprintf(stderr, "Failed To Create Vertex %d\n", $1);}
127+
| VALUE COMMA {parse_create_vertex(*graph, $1);}
79128
v
80-
| VALUE {if (create_vertex(*graph, f, $1, NULL) < 0) fprintf(stderr, "Failed To Create Vertex %d\n", $1);}
129+
| VALUE {parse_create_vertex(*graph, $1);}
81130
;
82131
es: EDGE_ COLON L_BRACKET e R_BRACKET
83132
;
84133
e: /* empty */
85-
| VALUE COLON VALUE COMMA {struct vertex *a = find((*graph)->vertices, $1); struct vertex *b = find((*graph)->vertices, $3); if (a && b) {if (create_edge(a, b, edge_f, NULL) == NULL) fprintf(stderr, "Failed to create Edge Between %d and %d\n", a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p b:%p\n", a, b);}
134+
| VALUE COLON VALUE COMMA {parse_create_edge(*graph, $1, $3);}
86135
e
87-
| VALUE COLON VALUE {struct vertex *a = find((*graph)->vertices, $1); struct vertex *b = find((*graph)->vertices, $3); if (a && b) {if (create_edge(a, b, edge_f, NULL) == NULL) fprintf(stderr, "Failed to create Edge Between %d and %d\n", a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p b:%p\n", a, b);}
136+
| VALUE COLON VALUE {parse_create_edge(*graph, $1, $3);}
88137
;
89138
bes:BI_EDGE_ COLON L_BRACKET be R_BRACKET
90139
;
91140
be: /* empty */
92-
| VALUE COLON VALUE COMMA {int val = 0; struct vertex *a = find((*graph)->vertices, $1); struct vertex *b = find((*graph)->vertices, $3); if (a && b) { if((val = create_bi_edge(a, b, edge_f, NULL, NULL, NULL) < 0)) fprintf(stderr, "%d: Failed to bi create Edge Between %d and %d\n", val, a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p(%d) b:%p(%d)\n", a, $1, b, $3);}
141+
| VALUE COLON VALUE COMMA {parse_create_bi_edge(*graph, $1, $3);}
93142
be
94-
| VALUE COLON VALUE {int val = 0; struct vertex *a = find((*graph)->vertices, $1); struct vertex *b = find((*graph)->vertices, $3); if (a && b) { if((val = create_bi_edge(a, b, edge_f, NULL, NULL, NULL) < 0)) fprintf(stderr, "%d: Failed to bi create Edge Between %d and %d\n", val, a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p(%d) b:%p(%d)\n", a, $1, b, $3);}
143+
| VALUE COLON VALUE {parse_create_bi_edge(*graph, $1, $3);}
95144
;
96145
%%
97146

@@ -113,6 +162,7 @@ struct graph *parse_json(const char *path) {
113162
yyparse(&graph);
114163
yyin = NULL;
115164
fclose(file);
165+
file = NULL;
116166
topologic_debug("%s;%s;%p", "parse_json", "success", graph);
117167
return graph;
118168
}

src/topologic_parser_cpp.lex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ blanks [ \t\n]+
2626
"mem_option" return(MEM_OPT);
2727
"context" return(LEX_CONTEXT);
2828
"max_loop" return(MAX_LOOP);
29+
"request_flag" return(REQ_FLAG);
2930
[0-9]+ {yylval.val = atoi(yytext); return(VALUE);}
3031
%%

src/topologic_parser_cpp.ypp

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,50 @@
55
#include <topologic/topologic.h>
66
void f(int id, struct graph *graph, struct vertex_result *args, void *glbl, void *edge_vars) {}
77
int edge_f(int id, void *args, void *glbl, const void *const edge_vars_a, const void *const edge_vars_b) {return 0;}
8-
int yylex();
98
void yyerror(struct graph** graph, const char *s);
109
extern FILE *yyin;
10+
int yylex(void);
11+
12+
int max_state_changes = -1;
13+
int snapshot_timestamp = START_STOP;
14+
int max_loop = MAX_LOOPS;
15+
unsigned int lvl_verbose = VERTICES | EDGES | FUNCTIONS | GLOBALS;
16+
enum CONTEXT context = SINGLE;
17+
enum MEM_OPTION mem_option = CONTINUE;
18+
enum REQUEST_FLAG request_flag = IGNORE_FAIL_REQUEST;
19+
20+
void parse_create_edge(struct graph *graph, int id_a, int id_b)
21+
{
22+
struct vertex *a = (struct vertex *) find(graph->vertices, id_a);
23+
struct vertex *b = (struct vertex *) find(graph->vertices, id_b);
24+
if (a && b)
25+
{
26+
if (create_edge(a, b, edge_f, NULL) == NULL)
27+
fprintf(stderr, "Failed to create Edge Between %d and %d\n", a->id, b->id);
28+
}
29+
else
30+
fprintf(stderr, "Invalid Vertices a:%p b:%p\n", a, b);
31+
}
32+
33+
void parse_create_bi_edge(struct graph *graph, int id_a, int id_b)
34+
{
35+
int val = 0;
36+
struct vertex *a = (struct vertex *) find(graph->vertices, id_a);
37+
struct vertex *b = (struct vertex *) find(graph->vertices, id_b);
38+
if (a && b)
39+
{
40+
if((val = create_bi_edge(a, b, edge_f, NULL, NULL, NULL) < 0))
41+
fprintf(stderr, "%d: Failed to bi create Edge Between %d and %d\n", val, a->id, b->id);
42+
}
43+
else
44+
fprintf(stderr, "Invalid Vertices a:%p(%d) b:%p(%d)\n", a, id_a, b, id_b);
45+
}
46+
47+
void parse_create_vertex(struct graph *graph, int id)
48+
{
49+
if (create_vertex(graph, f, id, NULL) < 0)
50+
fprintf(stderr, "Failed To Create Vertex %d\n", id);
51+
}
1152
%}
1253

1354
%union {
@@ -31,67 +72,75 @@ extern FILE *yyin;
3172
%token LEX_CONTEXT
3273
%token MEM_OPT
3374
%token MAX_LOOP
75+
%token REQ_FLAG
3476
%token <val> VALUE
3577

3678
%start json
3779
%%
38-
json: L_BRACKET GRAPH {*graph = GRAPH_INIT(); if (!(*graph)){fprintf(stderr, "Can't create graph\n"); return -1;}}
39-
COLON L_BRACKET content R_BRACKET
40-
R_BRACKET
80+
json: L_BRACKET GRAPH
81+
COLON L_BRACKET params g R_BRACKET
82+
R_BRACKET
4183
;
42-
content: params g
43-
| g
44-
| params
45-
|
46-
;
4784
params: verb COMMA params
4885
| state COMMA params
4986
| mem_opt COMMA params
5087
| context COMMA params
5188
| max_loop COMMA params
89+
| req_flag COMMA params
5290
| verb
5391
| mem_opt
5492
| context
5593
| state
5694
| max_loop
57-
|
95+
| req_flag
96+
| {
97+
*graph = graph_init(max_state_changes, snapshot_timestamp, max_loop, lvl_verbose, context, mem_option, request_flag);
98+
if (!(*graph))
99+
{
100+
fprintf(stderr, "Can't create graph\n");
101+
return -1;
102+
}
103+
}
58104
;
59-
state: MAX_STATE COLON VALUE {(*graph)->max_state_changes = $3;}
105+
state: MAX_STATE COLON VALUE {max_state_changes = $3;}
60106
;
61-
verb: LVL_VERBOSE COLON VALUE {(*graph)->lvl_verbose = (enum VERBOSITY) $3;}
107+
verb: LVL_VERBOSE COLON VALUE {lvl_verbose = $3;}
62108
;
63-
context: LEX_CONTEXT COLON VALUE {(*graph)->context = (enum CONTEXT) $3;}
109+
context: LEX_CONTEXT COLON VALUE {context = (enum CONTEXT) $3;}
64110
;
65-
mem_opt: MEM_OPT COLON VALUE {(*graph)->mem_option = (enum MEM_OPTION) $3;}
111+
mem_opt: MEM_OPT COLON VALUE {mem_option = (enum MEM_OPTION) $3;}
66112
;
67-
max_loop: MAX_LOOP COLON VALUE {(*graph)->max_loop = $3;}
113+
max_loop: MAX_LOOP COLON VALUE {max_loop = $3;}
68114
;
115+
req_flag: REQ_FLAG COLON VALUE {request_flag = (enum REQUEST_FLAG) $3;}
116+
;
69117
g: vs COMMA es COMMA bes
70118
| vs COMMA bes COMMA es
71119
| vs COMMA es
72120
| vs COMMA bes
73121
| vs
122+
|
74123
;
75124
vs: VERTICES_ COLON L_SQUARE v R_SQUARE
76125
;
77126
v: /* empty */
78-
| VALUE COMMA {if (create_vertex(*graph, f, $1, NULL) < 0) fprintf(stderr, "Failed To Create Vertex %d\n", $1);}
127+
| VALUE COMMA {parse_create_vertex(*graph, $1);}
79128
v
80-
| VALUE {if (create_vertex(*graph, f, $1, NULL) < 0) fprintf(stderr, "Failed To Create Vertex %d\n", $1);}
129+
| VALUE {parse_create_vertex(*graph, $1);}
81130
;
82131
es: EDGE_ COLON L_BRACKET e R_BRACKET
83132
;
84133
e: /* empty */
85-
| VALUE COLON VALUE COMMA {struct vertex *a = (struct vertex *) find((*graph)->vertices, $1); struct vertex *b = (struct vertex *) find((*graph)->vertices, $3); if (a && b) {if (create_edge(a, b, edge_f, NULL) == NULL) fprintf(stderr, "Failed to create Edge Between %d and %d\n", a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p b:%p\n", a, b);}
134+
| VALUE COLON VALUE COMMA {parse_create_edge(*graph, $1, $3);}
86135
e
87-
| VALUE COLON VALUE {struct vertex *a = (struct vertex *) find((*graph)->vertices, $1); struct vertex *b = (struct vertex *) find((*graph)->vertices, $3); if (a && b) {if (create_edge(a, b, edge_f, NULL) == NULL) fprintf(stderr, "Failed to create Edge Between %d and %d\n", a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p b:%p\n", a, b);}
136+
| VALUE COLON VALUE {parse_create_edge(*graph, $1, $3);}
88137
;
89138
bes:BI_EDGE_ COLON L_BRACKET be R_BRACKET
90139
;
91140
be: /* empty */
92-
| VALUE COLON VALUE COMMA {int val = 0; struct vertex *a = (struct vertex *) find((*graph)->vertices, $1); struct vertex *b = (struct vertex *) find((*graph)->vertices, $3); if (a && b) { if((val = create_bi_edge(a, b, edge_f, NULL, NULL, NULL) < 0)) fprintf(stderr, "%d: Failed to bi create Edge Between %d and %d\n", val, a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p(%d) b:%p(%d)\n", a, $1, b, $3);}
141+
| VALUE COLON VALUE COMMA {parse_create_bi_edge(*graph, $1, $3);}
93142
be
94-
| VALUE COLON VALUE {int val = 0; struct vertex *a = (struct vertex *) find((*graph)->vertices, $1); struct vertex *b = (struct vertex *) find((*graph)->vertices, $3); if (a && b) { if((val = create_bi_edge(a, b, edge_f, NULL, NULL, NULL) < 0)) fprintf(stderr, "%d: Failed to bi create Edge Between %d and %d\n", val, a->id, b->id);} else fprintf(stderr, "Invalid Vertices a:%p(%d) b:%p(%d)\n", a, $1, b, $3);}
143+
| VALUE COLON VALUE {parse_create_bi_edge(*graph, $1, $3);}
95144
;
96145
%%
97146

@@ -113,6 +162,7 @@ struct graph *parse_json(const char *path) {
113162
yyparse(&graph);
114163
yyin = NULL;
115164
fclose(file);
165+
file = NULL;
116166
topologic_debug("%s;%s;%p", "parse_json", "success", graph);
117167
return graph;
118168
}

0 commit comments

Comments
 (0)