-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathosc_parser.y
More file actions
520 lines (477 loc) · 12.4 KB
/
osc_parser.y
File metadata and controls
520 lines (477 loc) · 12.4 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
Written by John MacCallum, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2011-14, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
/** \file osc_parser.y
\author John MacCallum
*/
%{
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#ifndef WIN_VERSION
//#include <Carbon.h>
//#include <CoreServices.h>
//#endif
#include "osc.h"
#include "osc_mem.h"
#include "osc_error.h"
#include "osc_bundle_u.h"
#include "osc_message_u.h"
#include "osc_message_u.r"
#include "osc_atom_u.h"
#include "osc_atom_u.r"
#include "osc_parser.h"
#include "osc_legacy_parser.h"
#include "osc_scanner.h"
#include "osc_timetag.h"
//#define OSC_PARSER_DEBUG
#ifdef OSC_PARSER_DEBUG
#define PP(fmt, ...)printf("%d: "fmt, __LINE__, ##__VA_ARGS__)
#else
#define PP(fmt, ...)
#endif
#ifdef __cplusplus
extern "C" int osc_scanner_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param, yyscan_t yyscanner, long *buflen, char **buf);
#else
int osc_scanner_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param, yyscan_t yyscanner, long *buflen, char **buf);
#endif
%}
%code requires{
#include "osc.h"
#include "osc_error.h"
#include "osc_message_u.h"
#ifdef YY_DECL
#undef YY_DECL
#endif
#ifdef __cplusplus
#define YY_DECL extern "C" int osc_scanner_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param, yyscan_t yyscanner, long *buflen, char **buf)
#else
#define YY_DECL int osc_scanner_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param, yyscan_t yyscanner, long *buflen, char **buf)
#endif
#ifndef __HAVE_OSC_BNDL_LIST__
#define __HAVE_OSC_BNDL_LIST__
typedef struct _osc_parser_bndl_list{
struct _osc_bundle_u *bndl;
struct _osc_parser_bndl_list *next;
} t_osc_parser_bndl_list;
#endif
#ifndef __HAVE_OSC_PARSER_PARSESTRING__
#define __HAVE_OSC_PARSER_PARSESTRING__
#ifdef __cplusplus
extern "C"{
#endif
t_osc_err osc_parser_parseString(long len, char *ptr, struct _osc_bundle_u **bndl);
struct _osc_bundle_u *osc_parser_parseString_r(long len, char *ptr);
#ifdef __cplusplus
}
#endif
#endif
}
%{
int osc_parser_lex(YYSTYPE * yylval_param,YYLTYPE * yylloc_param ,yyscan_t yyscanner, long *buflen, char **buf){
return osc_scanner_lex(yylval_param, yylloc_param, yyscanner, buflen, buf);
}
t_osc_err osc_parser_parseString(long len, char *ptr, t_osc_bndl_u **bndl)
{
/*
t_osc_err e = osc_legacy_parser_parseString(len, ptr, bndl);
if(!e && *bndl){
return e;
}
*/
yyscan_t scanner;
osc_scanner_lex_init(&scanner);
YY_BUFFER_STATE buf_state = osc_scanner__scan_string(ptr, scanner);
osc_scanner_set_out(NULL, scanner);
long buflen = 0;
char *buf = NULL;
int ret = osc_parser_parse(bndl, scanner, &buflen, &buf);
osc_scanner__delete_buffer(buf_state, scanner);
osc_scanner_lex_destroy(scanner);
/*
if(!ret){
*bndl = bl->bndl;
}
if(bl){
osc_mem_free(bl);
}
*/
if(ret){
return OSC_ERR_PARSER;
}
return OSC_ERR_NONE;
}
struct _osc_bundle_u *osc_parser_parseString_r(long len, char *ptr)
{
t_osc_bndl_u *bndl = NULL;
osc_parser_parseString(len, ptr, &bndl);
return bndl;
}
void yyerror (YYLTYPE *yylloc, t_osc_bndl_u **bndl, void *scanner, long *buflen, char **buf, char const *e){
printf("osc_parser: error: %s\n", e);
//printf("%d: %s at %s\n", yylineno, e, yytext);
}
%}
%define api.pure full
%locations
%require "2.4.2"
%parse-param{t_osc_bndl_u **bndl}
%parse-param{void *scanner}
%parse-param{long *buflen}
%parse-param{char **buf}
%lex-param{void *scanner}
%lex-param{long *buflen}
%lex-param{char **buf}
%union{
t_osc_bndl_u *bundle;
t_osc_msg_u *message;
t_osc_atom_u *atom;
}
%type <bundle>bundle
%type <message>message messages
%type <atom>data datum list subbundle
%token <atom>OSCADDRESS OSCVALUE
%%
bundle:
messages {
t_osc_bndl_u *b = osc_bundle_u_alloc();
t_osc_msg_u *m = $1;
osc_bundle_u_addMsgList(b, m);
$$ = b;
*bndl = b;
}
;
subbundle:
'{' '}' {
t_osc_bndl_u *b = osc_bundle_u_alloc();
$$ = osc_atom_u_allocWithBndl(b);
}
| '{' messages '}' {
t_osc_bndl_u *b = osc_bundle_u_alloc();
t_osc_msg_u *m = $2;
osc_bundle_u_addMsgList(b, m);
$$ = osc_atom_u_allocWithBndl(b);
}
;
messages:
message
| messages ',' message {
osc_message_u_append($1, $3);
$$ = $1;
}
;
message:
OSCADDRESS {
char *st = NULL;
osc_atom_u_getString($1, 0, &st);
$$ = osc_message_u_alloc();
osc_message_u_setAddressPtr($$, st, NULL);
osc_atom_u_free($1);
}
| OSCADDRESS ':' datum {
char *st = NULL;
osc_atom_u_getString($1, 0, &st);
$$ = osc_message_u_alloc();
osc_message_u_setAddressPtr($$, st, NULL);
osc_message_u_appendAtom($$, $3);
osc_atom_u_free($1);
}
| OSCADDRESS ':' list {
char *st = NULL;
osc_atom_u_getString($1, 0, &st);
$$ = osc_message_u_alloc();
osc_message_u_setAddressPtr($$, st, NULL);
osc_atom_u_free($1);
$$->arghead = $3;
t_osc_atom_u *a = $3;
int i = 1;
while(a->next){
a = a->next;
i++;
}
$$->argtail = a;
$$->argc = i;
}
;
data:
datum
| data ',' datum {
osc_atom_u_append($1, $3);
$$ = $1;
}
;
datum:
OSCADDRESS {
$$ = $1;
}
| OSCVALUE {
$$ = $1;
}
| subbundle
;
list:
'[' data ']' {
$$ = $2;
}
;
/*
bundle: {
t_osc_parser_bndl_list *b = osc_mem_alloc(sizeof(t_osc_parser_bndl_list));
b->bndl = osc_bundle_u_alloc();
if(*bndl){
PP("push BNDL %p->%p\n", b->bndl, (*bndl)->bndl);
}else{
PP("push BNDL %p->%p\n", b->bndl, NULL);
}
b->next = *bndl;
*bndl = b;
}
| bundle msg {
PP("pop MSG %p<-%p\n", *msg, (*msg)->next);
PP("add MSG to BNDL %p := %p\n", (*bndl)->bndl, *msg);
t_osc_msg_u *m = (*msg)->next;
(*msg)->next = NULL;
osc_bundle_u_addMsg((*bndl)->bndl, *msg);
*msg = m;
}
;
arglist:
STRING {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add STRING to MSG %p := %s\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendStringPtr(*msg, $1);
}
| OSCADDRESS {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add OSCADDRESS to MSG %p := %s\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendStringPtr(*msg, $1);
}
| OSCFLOAT {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add OSCFLOAT to MSG %p := %f\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendDouble(*msg, $1);
}
| OSCINT32 {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add OSCINT32 to MSG %p := %d\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendInt32(*msg, $1);
}
| OSCUINT32 {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add OSCUINT32 to MSG %p := %d\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendUInt32(*msg, $1);
}
| OSCINT64 {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add OSCINT64 to MSG %p := %d\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendInt64(*msg, $1);
}
| OSCUINT64 {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add OSCUINT64 to MSG %p := %d\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendUInt64(*msg, $1);
}
| OSCCHAR {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add OSCCHAR to MSG %p := %c\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendInt8(*msg, $1);
}
| OSCBOOL {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add OSCBOOL to MSG %p := %c\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendBool(*msg, (int)$1);
}
| OSCTIMETAG {
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
PP("add OSCTIMETAG to MSG %p := %llu\n", m, $1);
m->next = *msg;
*msg = m;
osc_message_u_appendTimetag(*msg, $1);
}
| arglist STRING {
PP("add STRING to MSG %p := %s\n", *msg, $2);
osc_message_u_appendStringPtr(*msg, $2);
}
| arglist OSCADDRESS {
PP("add OSCADDRESS to MSG %p := %s\n", *msg, $2);
osc_message_u_appendStringPtr(*msg, $2);
}
| arglist OSCFLOAT {
PP("add OSCFLOAT to MSG %p := %f\n", *msg, $2);
osc_message_u_appendDouble(*msg, $2);
}
| arglist OSCINT32 {
PP("add OSCINT32 to MSG %p := %d\n", *msg, $2);
osc_message_u_appendInt32(*msg, $2);
}
| arglist OSCUINT32 {
PP("add OSCUINT32 to MSG %p := %d\n", *msg, $2);
osc_message_u_appendUInt32(*msg, $2);
}
| arglist OSCINT64 {
PP("add OSCINT64 to MSG %p := %d\n", *msg, $2);
osc_message_u_appendInt64(*msg, $2);
}
| arglist OSCUINT64 {
PP("add OSCUINT64 to MSG %p := %d\n", *msg, $2);
osc_message_u_appendUInt64(*msg, $2);
}
| arglist OSCCHAR {
PP("add OSCCHAR to MSG %p := %c\n", *msg, $2);
osc_message_u_appendInt8(*msg, $2);
}
| arglist OSCBOOL {
PP("add OSCTIMETAG to MSG %p := %c\n", *msg, $2);
osc_message_u_appendBool(*msg, (int)$2);
}
| arglist OSCTIMETAG {
PP("add OSCTIMETAG to MSG %p := %llu\n", *msg, $2);
osc_message_u_appendTimetag(*msg, $2);
}
| '[' bundle ']' {
//if(!(*msg)){
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
m->next = *msg;
*msg = m;
//}
PP("add BNDL to MSG %p := %p\n", *msg, (*bndl)->bndl);
long len = 0;
char *ptr = NULL;
osc_bundle_u_serialize((*bndl)->bndl, &len, &ptr);
if(ptr){
osc_message_u_appendBndl(*msg, len, ptr);
osc_mem_free(ptr);
}
PP("pop BNDL %p<-%p\n", (*bndl), (*bndl)->next);
t_osc_parser_bndl_list *b = (*bndl)->next;
osc_bundle_u_free((*bndl)->bndl);
osc_mem_free(*bndl);
*bndl = b;
}
| '[' '\n' bundle ']' {
//if(!(*msg)){
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
m->next = *msg;
*msg = m;
//}
PP("add BNDL to MSG %p := %p\n", *msg, (*bndl)->bndl);
long len = 0;
char *ptr = NULL;
osc_bundle_u_serialize((*bndl)->bndl, &len, &ptr);
if(ptr){
osc_message_u_appendBndl(*msg, len, ptr);
osc_mem_free(ptr);
}
PP("pop BNDL %p<-%p\n", (*bndl), (*bndl)->next);
t_osc_parser_bndl_list *b = (*bndl)->next;
osc_bundle_u_free((*bndl)->bndl);
osc_mem_free(*bndl);
*bndl = b;
}
| arglist '[' bundle ']' {
if(!(*msg)){
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
m->next = *msg;
*msg = m;
}
PP("add BNDL to MSG %p := %p\n", *msg, (*bndl)->bndl);
long len = 0;
char *ptr = NULL;
osc_bundle_u_serialize((*bndl)->bndl, &len, &ptr);
if(ptr){
osc_message_u_appendBndl(*msg, len, ptr);
osc_mem_free(ptr);
}
PP("pop BNDL %p<-%p\n", (*bndl), (*bndl)->next);
t_osc_parser_bndl_list *b = (*bndl)->next;
osc_bundle_u_free((*bndl)->bndl);
osc_mem_free(*bndl);
*bndl = b;
}
| arglist '[' '\n' bundle ']' {
if(!(*msg)){
t_osc_msg_u *m = osc_message_u_alloc();
PP("push MSG %p->%p\n", m, *msg);
m->next = *msg;
*msg = m;
}
PP("add BNDL to MSG %p := %p\n", *msg, (*bndl)->bndl);
long len = 0;
char *ptr = NULL;
osc_bundle_u_serialize((*bndl)->bndl, &len, &ptr);
if(ptr){
osc_message_u_appendBndl(*msg, len, ptr);
osc_mem_free(ptr);
}
PP("pop BNDL %p<-%p\n", (*bndl), (*bndl)->next);
t_osc_parser_bndl_list *b = (*bndl)->next;
osc_bundle_u_free((*bndl)->bndl);
osc_mem_free(*bndl);
*bndl = b;
}
;
msg:
OSCADDRESS arglist '\n' {
PP("set ADDRESS %p := %s\n", *msg, $1);
osc_message_u_setAddressPtr(*msg, $1, NULL);
}
| OSCADDRESS '\n' {
t_osc_msg_u *m = osc_message_u_alloc();
PP("set ADDRESS %p := %s\n", *msg, $1);
PP("push MSG %p->%p\n", m, *msg);
PP("have message with address and no arguments\n");
m->next = *msg;
*msg = m;
osc_message_u_setAddressPtr(*msg, $1, NULL);
}
;
*/