summaryrefslogtreecommitdiff
path: root/yy/kalmia.y
blob: 0d8b16f254f039841148ab03709749eb6d68c393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
%define api.pure full
%define api.prefix {kalmia}
%locations
%define parse.error verbose
%param { yyscan_t scanner }

%code top {
	#include <stdio.h>
	#include <stdlib.h>
	#include <stdarg.h>
}
%code requires {
	typedef void* yyscan_t;

	struct kalmia_t {
		void *current;
		int index;
	};

	struct ka_float_array_t {
		char *id;
		size_t count;
		double *buf;
	};
}
%parse-param { struct kalmia_t *result }
%code {
	int yylex(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t scanner);
	int yyerror(YYLTYPE *yyllocp, yyscan_t unused, struct kalmia_t *unused2, const char *msg);

	#define KAI_ABORT -1
	#define KAI_NOMEM -2
	
	#define KAI_ABORTF(...) \
	do { \
		kai_abortf_(&yylloc, __VA_ARGS__); \
		YYABORT; \
	} while(0)
	void kai_abortf_(YYLTYPE *yyllocp, const char *fmt, ...);

	/* float arrays */
	int kai_set_float_array_attrs(struct ka_float_array_t *a, char *id, char *count);

	#define KAI_WRAP(call) \
	do { \
		int err = call; \
		if (err == KAI_ABORT) { \
			KAI_ABORTF("call returned error: %s", #call); \
		} \
		else if (err == KAI_NOMEM) { \
			KAI_ABORTF("call ran out of memory: %s", #call); \
		} \
	} while(0)
}

%union {
	long lval;
	double dval;
	char *sval;
	char cval;
	struct ka_float_array_t float_array;
}

%token PROLOG
%token S_TAG_OPEN E_TAG_OPEN TAG_CLOSE EMPTY_TAG_CLOSE
%token <sval> NAME
%token <sval> ATTR
%token <sval> TEXT
%token <lval> INTEGER
%token <dval> DOUBLE
%token DATE;

%token <float_array> FLOAT_ARRAY
%type <float_array> float_array
%type <float_array> float_array_start
%type <float_array> float_array_attributes

%token ID_ATTR COUNT_ATTR
%type <sval> attr

%%


document: PROLOG element;

elements:
	element
	| elements element
	;

element:
	empty_tag
	| start_tag end_tag
	| start_tag content end_tag
	| float_array
	;

content:
	elements
	| integers
	| doubles
	| DATE
	;

empty_tag:
	S_TAG_OPEN NAME attributes EMPTY_TAG_CLOSE { printf("empty tag: %s\n", $2); }
	;

start_tag:
	S_TAG_OPEN NAME attributes TAG_CLOSE 
	{
		printf("enter tag: %s\n", $2);
		result->current = NULL;
	}
	;

end_tag:
	E_TAG_OPEN NAME TAG_CLOSE { printf("exit tag: %s\n", $2); }
	;

attributes:
	| attribute
	| attributes attribute
	;

attribute:
	ATTR attr { printf("attribute: %s=%s\n", $1, $2); }
	| ID_ATTR attr
	| COUNT_ATTR attr
	;



float_array:
	float_array_start doubles float_array_close
	{ 
		$$ = $1;
		printf("float_array[id='%s', count=%lu]{ ", $$.id, $$.count);
		for (size_t i=0; i<$$.count; i++) {
			printf("%f, ", $$.buf[i]);
		}
		printf("}\n");
	}
	;

float_array_start:
	S_TAG_OPEN FLOAT_ARRAY float_array_attributes TAG_CLOSE
	{ 
		$$ = $3; 
		result->current = $$.buf;
		result->index = 0;
	}
	;

float_array_close:
	E_TAG_OPEN FLOAT_ARRAY TAG_CLOSE
	;

float_array_attributes:
	COUNT_ATTR attr 
		{ KAI_WRAP(kai_set_float_array_attrs(&$$, NULL, $2)); }
	| COUNT_ATTR attr ID_ATTR attr
		{ KAI_WRAP(kai_set_float_array_attrs(&$$, $4, $2)); }
	| ID_ATTR attr COUNT_ATTR attr
		{ KAI_WRAP(kai_set_float_array_attrs(&$$, $2, $4)); }
	;

attr:
	'=' '"' TEXT '"' { $$ = $3; }
	;


integers:
	INTEGER { printf("%ld\n", $1); }
	| integers INTEGER { printf("%ld\n", $2); }
	;

doubles:
	DOUBLE 
	{ 
		if (result->current != NULL) {
			((double*) result->current)[result->index] = $1;
			result->index += 1; 
		}
	}
	| doubles DOUBLE
	{ 
		if (result->current != NULL) {
			((double*) result->current)[result->index] = $2;
			result->index += 1; 
		}
	}

	;


%%


int yyerror(
	YYLTYPE *yyllocp, 
	yyscan_t unused, 
	struct kalmia_t *unused2, 
	const char *msg)
{
	fprintf(
		stderr, "[%d:%d]: %s\n",
		yyllocp->first_line, yyllocp->first_column, msg
	);
	return 1;
}


void kai_abortf_(YYLTYPE *yyllocp, const char *fmt, ...)
{
	va_list args, args_len;
	va_start(args, fmt);
	va_copy(args_len, args);
	size_t len = vsnprintf(NULL, 0, fmt, args_len) + 1;
	va_end(args_len);

	char *buf = malloc(sizeof(char) * len);
	if (buf == NULL) {
		fprintf(stderr, "failed to allocate buffer for error message!\n");
		return;
	}
	vsnprintf(buf, len, fmt, args);
	va_end(args);

	yyerror(yyllocp, NULL, NULL, buf);
}


/* float arrays */
int kai_set_float_array_attrs(
	struct ka_float_array_t *a, char *id, char *count)
{
	a->id = id;
	char *end = NULL;
	a->count = strtoll(count, &end, 10);
	if (end == count) {
		return -1;
	}

	a->buf = malloc(sizeof(double) * a->count);
	if (a->buf == NULL) {
		return -2;
	}
	return 0;
}