aboutsummaryrefslogtreecommitdiffstats
path: root/pic/picl.l
blob: bffecb429081d9ba662a029be673981049e35987 (plain) (blame)
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
%{
/*
 * Changes by Gunnar Ritter, Freiburg i. Br., Germany, October 2005.
 *
 * Derived from Plan 9 v4 /sys/src/cmd/pic/
 *
 * Copyright (C) 2003, Lucent Technologies Inc. and others.
 * All Rights Reserved.
 *
 * Distributed under the terms of the Lucent Public License Version 1.02.
 */

/*	Sccsid @(#)picl.l	1.9 (gritter) 8/6/06	*/
%}
%Start A str def xsc br thru sh
%e 1700
%k 150
%a 1800
%o 1600
%p 5000
%n 700

%{
#ifndef	FLEX_SCANNER
#undef	input
#undef	unput
#endif	/* !FLEX_SCANNER */
/* #include <stdio.h> lex puts one out for us */
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include "pic.h"
#include "y.tab.h"

#ifdef	FLEX_SCANNER
#undef	YY_INPUT
#define	YY_INPUT(buf, result, max_size) { \
	int	c = xxinput(); \
	result = (c == EOF || c == 0) ? YY_NULL : (buf[0] = c, 1); \
}
#define	witchcraft	YY_START
#else	/* !FLEX_SCANNER */
#define	witchcraft	yybgin-yysvec-1
#endif	/* !FLEX_SCANNER */

extern	struct	symtab	symtab[];

int	yyback(int *, int);
int	yylook(void);
int	yywrap(void);

#define	CADD	cbuf[clen++]=yytext[0]; \
		if (clen>=CBUFLEN-1) { WARNING("string too long", cbuf); BEGIN A; }
#define	CBUFLEN	500
static char	cbuf[CBUFLEN];
static int	c, clen, delim;
static int	ifsw	= 0;	/* 1 if if statement in progress */
%}

A	[a-zA-Z_]
B	[a-zA-Z0-9_]
D	[0-9]
WS	[ \t]
FWS	([ \t]|\\\n)

%%
	switch (witchcraft) {
	case 0:
		BEGIN A;
		break;
	case xsc:
		BEGIN A;
		return('}');
	case br:
		BEGIN A;
		return(']');
	}

<A>{WS}		;
<A>"\\"\n	;
<A>\n		{ return(ST); }
<A>";"		{ return(ST); }
<A>"}"		{ BEGIN xsc; return(ST); }
<A>"]"		{ BEGIN br; return(ST); }
<A>"{"{WS}*(#.*)?\n+	{ return(yylval.i = yytext[0]); }

<A>^"."{WS}*"PS".*	{ if (curfile == infile) WARNING(".PS found inside .PS/.PE"); }
<A>^"."{WS}*"PE".*	{ if (curfile == infile) {
			yylval.p = PEstring = tostring(yytext);
			return(EOF);
		  }
		}
<A>^['.].*	{ yylval.p = tostring(yytext); return(TROFF); }

<A>print	return(yylval.i = PRINT);
<A>box		return(yylval.i = BOX);
<A>circle	return(yylval.i = CIRCLE);
<A>arc		return(yylval.i = ARC);
<A>ellipse	return(yylval.i = ELLIPSE);
<A>arrow	return(yylval.i = ARROW);
<A>spline	return(yylval.i = SPLINE);
<A>line		return(yylval.i = LINE);
<A>move		return(yylval.i = MOVE);
<A>"[]"		return(yylval.i = BLOCK);
<A>reset	return(RESET);
<A>sprintf	return(SPRINTF);

<A>same		return(SAME);
<A>between	return(BETWEEN);
<A>and		return(AND);

<A>of		;
<A>the		;
<A>way		;

<A>"."(e|east)		{ yylval.i = EAST; return(CORNER); }
<A>"."(r|right)		{ yylval.i = EAST; return(CORNER); }
<A>"."(w|west)		{ yylval.i = WEST; return(CORNER); }
<A>"."(l|left)		{ yylval.i = WEST; return(CORNER); }
<A>"."(n|north)		{ yylval.i = NORTH; return(CORNER); }
<A>"."(t|top)		{ yylval.i = NORTH; return(CORNER); }
<A>"."(s|south)		{ yylval.i = SOUTH; return(CORNER); }
<A>"."(b|bot|bottom)	{ yylval.i = SOUTH; return(CORNER); }
<A>"."(c|center)	{ yylval.i = CENTER; return(CORNER); }
<A>".start"		{ yylval.i = START; return(CORNER); }
<A>".end"		{ yylval.i = END; return(CORNER); }
<A>".ne"		{ yylval.i = NE; return(CORNER); }
<A>".se"		{ yylval.i = SE; return(CORNER); }
<A>".nw"		{ yylval.i = NW; return(CORNER); }
<A>".sw"		{ yylval.i = SW; return(CORNER); }

<A>top{FWS}+of		{ yylval.i = NORTH; return(CORNER); }
<A>north{FWS}+of	{ yylval.i = NORTH; return(CORNER); }
<A>bottom{FWS}+of	{ yylval.i = SOUTH; return(CORNER); }
<A>south{FWS}+of	{ yylval.i = SOUTH; return(CORNER); }
<A>left{FWS}+of		{ yylval.i = WEST; return(CORNER); }
<A>west{FWS}+of		{ yylval.i = WEST; return(CORNER); }
<A>right{FWS}+of	{ yylval.i = EAST; return(CORNER); }
<A>east{FWS}+of		{ yylval.i = EAST; return(CORNER); }
<A>center{FWS}+of	{ yylval.i = CENTER; return(CORNER); }
<A>start{FWS}+of	{ yylval.i = START; return(CORNER); }
<A>end{FWS}+of		{ yylval.i = END; return(CORNER); }
<A>upper{FWS}right{FWS}+of	{ yylval.i = NE; return(CORNER); }
<A>lower{FWS}right{FWS}+of	{ yylval.i = SE; return(CORNER); }
<A>upper{FWS}left{FWS}+of	{ yylval.i = NW; return(CORNER); }
<A>lower{FWS}left{FWS}+of	{ yylval.i = SW; return(CORNER); }

<A>height|ht	{ yylval.i = HEIGHT; return(ATTR); }
<A>width|wid	{ yylval.i = WIDTH; return(ATTR); }
<A>radius|rad	{ yylval.i = RADIUS; return(ATTR); }
<A>diameter|diam { yylval.i = DIAMETER; return(ATTR); }
<A>size		{ yylval.i = SIZE; return(ATTR); }
<A>left		{ yylval.i = LEFT; return(DIR); }
<A>right	{ yylval.i = RIGHT; return(DIR); }
<A>up		{ yylval.i = UP; return(DIR); }
<A>down		{ yylval.i = DOWN; return(DIR); }
<A>cw		{ yylval.i = CW; return(ATTR); }
<A>clockwise	{ yylval.i = CW; return(ATTR); }
<A>ccw		{ yylval.i = CCW; return(ATTR); }
<A>invis(ible)?	{ yylval.i = INVIS; return(ATTR); }
<A>noedge	{ yylval.i = INVIS; return ATTR; }
<A>fill		return(yylval.i = FILL);
<A>solid	;
<A>dot(ted)?	return(yylval.i = DOT);
<A>dash(ed)?	return(yylval.i = DASH);
<A>chop		return(yylval.i = CHOP);

<A>spread	{ yylval.i = SPREAD; return TEXTATTR; }
<A>ljust	{ yylval.i = LJUST; return TEXTATTR; }
<A>rjust	{ yylval.i = RJUST; return TEXTATTR; }
<A>above	{ yylval.i = ABOVE; return TEXTATTR; }
<A>below	{ yylval.i = BELOW; return TEXTATTR; }
<A>center	{ yylval.i = CENTER; return TEXTATTR; }

<A>"<-"		{ yylval.i = HEAD1; return(HEAD); }
<A>"->"		{ yylval.i = HEAD2; return(HEAD); }
<A>"<->"	{ yylval.i = HEAD12; return(HEAD); }

<A>".x"			return(yylval.i = DOTX);
<A>".y"			return(yylval.i = DOTY);
<A>"."(ht|height)	return(yylval.i = DOTHT);
<A>"."(wid|width)	return(yylval.i = DOTWID);
<A>"."(rad|radius)	return(yylval.i = DOTRAD);

<A>from		return(yylval.i = FROM);
<A>to		return(yylval.i = TO);
<A>at		return(yylval.i = AT);
<A>by		return(yylval.i = BY);
<A>with		return(yylval.i = WITH);
<A>last		return(yylval.i = LAST);

<A>log		return(LOG);
<A>exp		return(EXP);
<A>sin		return(SIN);
<A>cos		return(COS);
<A>atan2	return(ATAN2);
<A>sqrt		return(SQRT);
<A>rand		return(RAND);
<A>max		return(MAX);
<A>min		return(MIN);
<A>int		return(INT);

<A>"=="		return(EQ);
<A>">="		return(GE);
<A>"<="		return(LE);
<A>"!="		return(NEQ);
<A>">"		return(GT);
<A>"<"		return(LT);
<A>"&&"		return(ANDAND);
<A>"||"		return(OROR);
<A>"!"		return(NOT);	

<A>Here		return(yylval.i = HERE);

<A>for		return(FOR);
<A>^Endfor\n	{ endfor(); }
<A>do		{ yylval.p = delimstr("loop body"); return(DOSTR); }

<A>copy|include		return(COPY);
<A>(thru|through){WS}+	{ BEGIN thru; return(THRU); }
<thru>{A}{B}*|.		{ yylval.st = copythru(yytext); BEGIN A; return(DEFNAME); }
<A>until		return(UNTIL);

<A>if		{ ifsw = 1; return(IF); }
<A>then		{ if (!ifsw) { yylval.i = THEN; return(ATTR); }
		  yylval.p = delimstr("then part"); ifsw = 0;
		  return(THENSTR); }
<A>else		{ yylval.p = delimstr("else part"); return(ELSESTR); }

<A>sh{WS}+	{ BEGIN sh;
		  if ((delim = input()) == '{') delim = '}';	/* no nested {} */
		  shell_init(); }
<sh>{A}{B}*	{ struct symtab *p;
		  if (yytext[0] == delim) {
			shell_exec();
			BEGIN A;
		  } else {
			p = lookup(yytext);
			if (p != NULL && p->s_type == DEFNAME) {
				c = input();
				xxunput(c);
				if (c == '(')
					dodef(p);
				else
					pbstr(p->s_val.p);
			} else
				shell_text(yytext);
		  }
		}
<sh>.|\n	{ if (yytext[0] == delim) {
			shell_exec();
			BEGIN A;
		  } else
			shell_text(yytext);
		}

<A>define{WS}+		{ BEGIN def; }
<def>{A}{B}*		{ definition(yytext); BEGIN A; }
<A>undef(ine)?{WS}+{A}{B}*	{ undefine(yytext); }

<A>first		{ yylval.i = 1; return(NTH); }
<A>{D}+(th|nd|rd|st)	{ yylval.i = atoi(yytext); return(NTH); }
<A>({D}+("."?){D}*|"."{D}+)((e|E)("+"|-)?{D}+)?i? {
		  	yylval.f = atof(yytext); return(NUMBER); }

<A>{A}{B}* {	struct symtab *p;
		p = lookup(yytext);
		if (p != NULL && p->s_type == DEFNAME) {
			c = input();
			xxunput(c);
			if (c == '(')	/* it's name(...) */
				dodef(p);
			else {	/* no argument list */
				pbstr(p->s_val.p);
				dprintf("pushing back `%s'\n", p->s_val.p);
			}
		} else if (islower((int)yytext[0])) {
			yylval.p = tostring(yytext);
			return(VARNAME);
		} else {
			yylval.p = tostring(yytext);
			return(PLACENAME);
		}
	}

<A>\"		{ BEGIN str; clen=0; }
<str>\"		{ cbuf[clen]=0; yylval.p = tostring(cbuf); BEGIN A; return(TEXT); }
<str>\n		{ cbuf[clen]=0; WARNING("missing quote in string \"%s\"", cbuf);
				BEGIN A; return(ST); }
<str>"\\\""	{ cbuf[clen++]='"'; }
<str>"\\"t	{ cbuf[clen++]='\t'; }
<str>"\\\\"	{ cbuf[clen++]='\\'; }
<str>.		{ CADD; }

<A>#.*		;

<A>.		return(yylval.i = yytext[0]);

%%