aboutsummaryrefslogtreecommitdiff
path: root/shared/common/lex.c
blob: aafed9dbc76bc2dd0110250bd8a18393d08f5ecd (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* Copyright (c) 1997-2004 Miller Puckette, krzYszcz, and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef MIXED_STANDALONE
#include "unstable/standalone.h"
#else
#include "m_pd.h"
#endif
#include "lex.h"

static int lex_nextbyte(t_lex *lx, unsigned char *buf)
{
    int ich;
    if (lx->l_fp)
    {
	if ((ich = fgetc(lx->l_fp)) == EOF)
	    return (0);
    }
    else if (lx->l_buf)
    {
	if (lx->l_bufndx < lx->l_bufsize)
	    ich = lx->l_buf[lx->l_bufndx++];
	else
	    return (0);
    }
    else return (0);
    if (ich)
    {
	*buf = (unsigned char)ich;
	return (1);
    }
    else
    {
	lx->l_errbinary = 1;
	return (0);
    }
}

static void lex_ungetbyte(t_lex *lx, unsigned char ch)
{
    if (lx->l_fp)
    {
	ungetc(ch, lx->l_fp);
    }
    else if (lx->l_buf)
    {
	if (lx->l_bufndx > 0)
	    lx->l_buf[--lx->l_bufndx] = ch;
    }
}

/* single pass of binbuf_text(), optionally int-preserving version */
int lex_nextatom(t_lex *lx, t_atom *ap)
{
    char buf[1001], *bufp, *ebuf = buf + 1000;
    int ready;
    unsigned char ch;
    ap->a_type = A_NULL;
    while ((ready = lex_nextbyte(lx, &ch)) &&
	   (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'));
    if (!ready)
    {
	/* ??? */
	if (lx->l_lasttype == A_SEMI)
	    return (0);
	else
	    ap->a_type = A_SEMI;
    }
    else if (ch == ';')
	ap->a_type = A_SEMI;
    else if (ch == ',')
	ap->a_type = A_COMMA;
    else
    {
	int floatstate = 0, slash = 0, lastslash = 0, firstslash = (ch == '\\');
	bufp = buf;
	do
	{
	    *bufp = ch;
	    lastslash = slash;
	    slash = (ch == '\\');

	    if (floatstate >= 0)
	    {
		int digit = (ch >= '0' && ch <= '9'),
		    dot = (ch == '.'), minus = (ch == '-'),
		    plusminus = (minus || (ch == '+')),
		    expon = (ch == 'e' || ch == 'E');
		if (floatstate == 0)    /* beginning */
		{
		    if (minus) floatstate = 1;
		    else if (digit) floatstate = 2;
		    else if (dot) floatstate = 3;
		    else floatstate = -1;
		}
		else if (floatstate == 1)	/* got minus */
		{
		    if (digit) floatstate = 2;
		    else if (dot) floatstate = 3;
		    else floatstate = -1;
		}
		else if (floatstate == 2)	/* got digits */
		{
		    if (dot) floatstate = 4;
		    else if (expon) floatstate = 6;
		    else if (!digit) floatstate = -1;
		}
		else if (floatstate == 3)	/* got '.' without digits */
		{
		    if (digit) floatstate = 5;
		    else floatstate = -1;
		}
		else if (floatstate == 4)	/* got '.' after digits */
		{
		    if (digit) floatstate = 5;
		    else if (expon) floatstate = 6;
		    else floatstate = -1;
		}
		else if (floatstate == 5)	/* got digits after . */
		{
		    if (expon) floatstate = 6;
		    else if (!digit) floatstate = -1;
		}
		else if (floatstate == 6)	/* got 'e' */
		{
		    if (plusminus) floatstate = 7;
		    else if (digit) floatstate = 8;
		    else floatstate = -1;
		}
		else if (floatstate == 7)	/* got plus or minus */
		{
		    if (digit) floatstate = 8;
		    else floatstate = -1;
		}
		else if (floatstate == 8)	/* got digits */
		{
		    if (!digit) floatstate = -1;
		}
	    }
	    if (!slash) bufp++;
	}
	while ((ready = lex_nextbyte(lx, &ch)) && bufp != ebuf
	       && (slash || (ch != ' ' && ch != '\n' && ch != '\r'
			     && ch != '\t' && ch != ',' && ch != ';')));
	if (ready && (ch == ',' || ch == ';'))
	    lex_ungetbyte(lx, ch);
	*bufp = 0;
#if 0
	fprintf(stderr, "buf %s\n", buf);
#endif
	if (*buf == '$' && buf[1] >= '0' && buf[1] <= '9' && !firstslash)
	{
	    for (bufp = buf+2; *bufp; bufp++)
	    {
		if (*bufp < '0' || *bufp > '9')
		{
		    ap->a_type = A_DOLLSYM;
		    ap->a_w.w_symbol = gensym(buf+1);
		    break;
		}
	    }
	    if (ap->a_type == A_NULL)
	    {
		ap->a_type = A_DOLLAR;
		ap->a_w.w_index = atoi(buf+1);
	    }
	}
	else if (floatstate == 2)
	{
	    if (lx->l_inttype == A_FLOAT)
	    {
		ap->a_type = A_FLOAT;
		ap->a_w.w_float = (float)atof(buf);
	    }
	    else
	    {
		ap->a_type = lx->l_inttype;
		ap->a_w.w_index = atoi(buf);
	    }
	}
	else if (floatstate == 4 || floatstate == 5 || floatstate == 8)
	{
	    ap->a_type = A_FLOAT;
	    ap->a_w.w_float = (float)atof(buf);
	}
	else
	{
	    ap->a_type = A_SYMBOL;
	    ap->a_w.w_symbol = gensym(buf);
	}
    }
    lx->l_lasttype = ap->a_type;
    return (1);
}

void lex_atomstring(t_atom *ap, char *buf, int bufsize, t_atomtype inttype)
{
    char *sp, *bp, *ep;
    switch(ap->a_type)
    {
    case A_SEMI:
	strcpy(buf, ";"); break;
    case A_COMMA:
	strcpy(buf, ","); break;
    case A_FLOAT:
	sprintf(buf, "%#f", ap->a_w.w_float);
	ep = buf + strlen(buf) - 1;
	while (ep > buf && *ep == '0') *ep-- = 0;
	break;
    case A_SYMBOL:
    	sp = ap->a_w.w_symbol->s_name;
	bp = buf;
	ep = buf + (bufsize-5);
	while (bp < ep && *sp)
	{
	    if (*sp == ';' || *sp == ',' || *sp == '\\' ||
		(*sp == '$' && bp == buf && sp[1] >= '0' && sp[1] <= '9'))
		*bp++ = '\\';
	    if ((unsigned char)*sp < 127)
		*bp++ = *sp++;
	    else
		/* FIXME this is temporary -- codepage horror */
		sprintf(bp, "\\%.3o", (unsigned char)*sp++), bp += 4;
	}
	if (*sp) *bp++ = '*';
	*bp = 0;
	break;
    case A_DOLLAR:
    	sprintf(buf, "$%d", ap->a_w.w_index);
    	break;
    case A_DOLLSYM:
    	sprintf(buf, "$%s", ap->a_w.w_symbol->s_name);
    	break;
    default:
	if (ap->a_type == inttype)
	    sprintf(buf, "%d", ap->a_w.w_index);
	else
	{
#ifdef MIXED_STANDALONE
	    fprintf(stderr, "BUG (lex): bad atom type\n");
#else
	    bug("lex_atomstring (bad atom type)");
#endif
	    strcpy(buf, "???");
	}
    }
}

int lex_isbinary(t_lex *lx)
{
    return (lx->l_errbinary);
}

void lex_free(t_lex *lx)
{
    freebytes(lx, sizeof(*lx));
}

t_lex *lex_new(FILE *fp, t_atomtype inttype)
{
    t_lex *lx = (t_lex *)getbytes(sizeof(*lx));
    lx->l_fp = fp;
    lx->l_buf = 0;  /* FIXME */
    lx->l_inttype = inttype;
    lx->l_lasttype = A_SEMI;
    lx->l_errbinary = 0;
    return (lx);
}