-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathosc_scanner.l
192 lines (163 loc) · 4.71 KB
/
osc_scanner.l
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
/*
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_scanner.l
\author John MacCallum
*/
%{
#include "osc_parser.h"
#include "osc_mem.h"
#include "osc_timetag.h"
int osc_scanner_wrap(yyscan_t scanner){
return 1;
}
%}
dseq ([[:digit:]]+)
dseq_opt ([[:digit:]]*)
frac (({dseq_opt}"."{dseq})|{dseq}".")
exp ([eE][+-]?{dseq})
exp_opt ({exp}?)
fsuff [flFL]
fsuff_opt ({fsuff}?)
hpref (0[xX])
hdseq ([[:xdigit:]]+)
hdseq_opt ([[:xdigit:]]*)
hfrac (({hdseq_opt}"."{hdseq})|({hdseq}"."))
bexp ([pP][+-]?{dseq})
dfc (({frac}{exp_opt}{fsuff_opt})|({dseq}{exp}{fsuff_opt}))
hfc (({hpref}{hfrac}{bexp}{fsuff_opt})|({hpref}{hdseq}{bexp}{fsuff_opt}))
c99_floating_point_constant ({dfc}|{hfc})
string [^ \t\"\n\\\{\}\'\[\],]*
quoted_string [^\"\\]
%x quote escape
%option reentrant
%option bison-bridge
%option bison-locations
%option noinput
%option nounput
%%
\" {
BEGIN(quote);
}
<quote>{quoted_string}+ {
if(*buf){
*buf = osc_mem_resize(*buf, *buflen + yyleng + 1);
*buflen += yyleng + 1;
strcat(*buf, yytext);
}else{
yylval->atom = osc_atom_u_allocWithString(yytext);
return OSCVALUE;
}
}
<quote>\" {
BEGIN(0);
if(*buf){
yylval->atom = osc_atom_u_allocWithString(*buf);
*buf = NULL;
*buflen = 0;
return OSCVALUE;
}
}
<quote>{quoted_string}*\\. {
if(*buf){
*buf = osc_mem_resize(*buf, *buflen + yyleng);
memcpy(*buf + *buflen, yytext, yyleng - 1);
*buflen += yyleng - 1;
(*buf)[*buflen - 1] = yytext[yyleng - 1];
(*buf)[*buflen] = '\0';
}else{
*buf = osc_mem_alloc(yyleng);
memcpy(*buf, yytext, yyleng - 1);
*buflen = yyleng - 1;
(*buf)[*buflen - 1] = yytext[yyleng - 1];
(*buf)[*buflen] = '\0';
}
}
\"\" {
yylval->atom = osc_atom_u_alloc();
osc_atom_u_setString(yylval->atom, "");
return OSCVALUE;
}
-?{c99_floating_point_constant} {
char *endp = NULL;
double d = strtod(yytext, &endp);
yylval->atom = osc_atom_u_allocWithDouble(d);
return OSCVALUE;
}
true {
yylval->atom = osc_atom_u_alloc();
osc_atom_u_setTrue(yylval->atom);
return OSCVALUE;
}
false {
yylval->atom = osc_atom_u_alloc();
osc_atom_u_setFalse(yylval->atom);
return OSCVALUE;
}
nil {
yylval->atom = osc_atom_u_alloc();
osc_atom_u_setNil(yylval->atom);
return OSCVALUE;
}
:|\{|\}|, {
return *yytext;
}
\[|\] {
return *yytext;
}
\'.\' {
yylval->atom = osc_atom_u_alloc();
osc_atom_u_setInt8(yylval->atom, yytext[1]);
return OSCVALUE;
}
(-?([[:digit:]]{-}[0])[[:digit:]]*|0) {
char *endp = NULL;
long i = strtol(yytext, &endp, 10);
yylval->atom = osc_atom_u_allocWithInt32(i);
return OSCVALUE;
}
0[xX][[:digit:]abcdefABCDEF]+ {
// hex int
char *endptr = NULL;
yylval->atom = osc_atom_u_alloc();
if(strlen(yytext) > 10){
int64_t i = strtoull(yytext, &endptr, 16);
osc_atom_u_setUInt64(yylval->atom, i);
}else{
int32_t i = strtoul(yytext, &endptr, 16);
osc_atom_u_setUInt32(yylval->atom, i);
}
return OSCVALUE;
}
\/[^ \t\n#]+ {
if(yytext[yyleng - 1] == ','){
yyless(yyleng - 1);
}
yylval->atom = osc_atom_u_allocWithString(yytext);
return OSCADDRESS;
}
(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])?T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)??(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])? { /* ISO 8601 from Regular Expressions Cookbook */
t_osc_timetag t;
osc_timetag_fromISO8601(yytext, &t);
yylval->atom = osc_atom_u_allocWithTimetag(t);
return OSCVALUE;
}
[ \t\n]+
. {return *yytext;}
%%