File: lexer.l

package info (click to toggle)
nawm 0.0.20030130-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 360 kB
  • ctags: 496
  • sloc: ansic: 3,158; sh: 1,582; yacc: 408; lex: 406; makefile: 153
file content (406 lines) | stat: -rw-r--r-- 8,192 bytes parent folder | download | duplicates (2)
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
%{
/* lexer.l: the nawm tokenizer */

/* Copyright (C) 1999 by the Massachusetts Institute of Technology.
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation, and that the name of M.I.T. not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nawm.h"
#include "lang.h"
#include "parser.h"

int nawm_input(char *buf, int bufsize);
void *search_keys(char *name, int *kind);

#undef YY_INPUT
#define YY_INPUT(buf, result, size) { result = nawm_input(buf, size); }

char *process_string(char *str);
varbinding *mkvarbinding(char *, int, void *);

extern int yyparse(void);
extern dtype rettype;

extern Window root;
extern Display *dpy;

int kind, lineno;

typedef struct _lexscope {
  struct _lexscope *parent;
  int numvars;
  varbinding *vars;
  dtype *vtypes;
} lexscope;
static lexscope *scopestack;

%}

%option noyywrap

%%
include			{ return INCLUDE; }
option			{ return OPTION; }

begin			{ return BEGIN_; }
end			{ return END; }

command			{ return COMMAND; }
function		{ return FUNCTION; }

mode			{ return MODE; }

int			{ yylval.i = T_INT; return DTYPE; }
string			{ yylval.i = T_STR; return DTYPE; }
window			{ yylval.i = T_WIN; return DTYPE; }

key			{ return KEYPRESS; }
keypress		{ return KEYPRESS; }
keyrelease		{ return KEYRELEASE; }
button			{ return BUTTONPRESS; }
buttonpress		{ return BUTTONPRESS; }
buttonrelease		{ return BUTTONRELEASE; }
motion			{ return MOTION; }
enter			{ return ENTER; }
leave			{ return LEAVE; }

if			{ return IF; }
else			{ return ELSE; }
for			{ return FOR; }
in			{ return IN; }
while			{ return WHILE; }
do			{ return DO; }
break			{ return BREAK; }
continue		{ return CONTINUE; }
return			{ return RETURN; }
del			{ return DEL; }

[A-Za-z_][A-Za-z_0-9]*	{ yylval.n = lookup(yytext, &kind, 0);
			  if (yylval.n)
			    return kind;
			  else
			    {
			      yylval.s = xstrdup(yytext);
			      return SYM;
			    }
			}
[0-9]+			{ yylval.i = atoi(yytext); return NUM; }
\"([^"\\]|\\.)*\"	{ yylval.s = process_string(yytext); return STR; }

"+"			{ yylval.i = PLUS; return ADDOP; }
"-"			{ yylval.i = MINUS; return ADDOP; }
"*"			{ yylval.i = TIMES; return MULTOP; }
"/"			{ yylval.i = DIVIDE; return MULTOP; }
"%"			{ yylval.i = REMAINDER; return MULTOP; }
"=="			{ yylval.i = EQUAL; return COMPOP; }
"!="			{ yylval.i = NOTEQUAL; return COMPOP; }
"<="			{ yylval.i = LESSEQUAL; return COMPOP; }
"<"			{ yylval.i = LESS; return COMPOP; }
">="			{ yylval.i = GREATEREQUAL; return COMPOP; }
">"			{ yylval.i = GREATER; return COMPOP; }
"="			{ yylval.i = ASSIGN; return ASSIGNOP; }
"&&"			{ yylval.i = AND; return BOOLOP; }
"||"			{ yylval.i = OR; return BOOLOP; }
"!"			{ yylval.i = NOT; return UNOP; }

"("			{ return '('; }
")"			{ return ')'; }
"["			{ return '['; }
"]"			{ return ']'; }
"{"			{ return '{'; }
"}"			{ return '}'; }
"."			{ return '.'; }
","			{ return ','; }
";"			{ return ';'; }

[ \t]*			{ ; }
#.*$			{ ; }
\n			{ lineno++; }

.			{ die("Bad character `%c' in input at line %d.", *yytext, lineno); }

%%
char *pstr;
extern FILE *yyin;

void initlex(void)
{
  scopestack = NULL;
  pushlexscope(0);
}

void parse_file(char *name)
{
  lineno = 1;
  rettype = -1;
  if (!strcmp(name, "-"))
    yyin = stdin;
  else
    yyin = fopen(name, "r");
  if (!yyin)
    {
      die("Can't open file `%s'\n", name);
      return;
    }
  pstr = NULL;
  yyparse();
  if (strcmp(name, "-"))
    fclose(yyin);
  lineno = 0;
}

void parse_string(char *s)
{
  lineno = 0;
  rettype = -1;
  pstr = s;
  yyparse();
  lineno = 0;
}

int nawm_input(char *buf, int bufsize)
{
  int bytesread;

  if (pstr)
    {
      if (*pstr)
	{
	  buf[bufsize - 1] = '\0';
	  strncpy(buf, pstr, bufsize - 1);
	  bytesread = strlen(buf);
	  pstr += bytesread;
	}
      else
	bytesread = 0;
    }
  else
    bytesread = fread(buf, 1, bufsize, yyin);
  return bytesread;
}

char *process_string(char *str)
{
  char *newstr, *s, *d;
  int len = strlen(str) - 2;

  newstr = xmalloc(len + 1);
  for (s = str + 1, d = newstr; s < str + len + 1; s++, d++)
    {
      if (*s == '\\')
	{
	  switch (*++s)
	    {
	    case '\'':
	    case '"':
	    case '?':
	    case '\\':
	      *d = *s;
	      break;

	    case 'a':
	      *d = '\a';
	      break;
	    case 'b':
	      *d = '\b';
	      break;
	    case 'f':
	      *d = '\f';
	      break;
	    case 'n':
	      *d = '\n';
	      break;
	    case 'r':
	      *d = '\r';
	      break;
	    case 't':
	      *d = '\t';
	      break;
	    case 'v':
	      *d = '\v';
	      break;

	    case 'x':
#define xdigit(x) ( isdigit(x) ? x - '0' : x - 'a' + 10 )
	      if (isxdigit(s[1]) && isxdigit(s[2]))
		*d = xdigit(*++s) * 16 + xdigit(*++s);
	      else
		die("bad hex constant in \"%s\"", str);
	      break;

	    default:
#define isodigit(x) ( isdigit(x) && x != '8' && x != '9' )
	      if (!isodigit(s[0]) || !isodigit(s[1]) || !isodigit(s[2]))
		die("bad escape in \"%s\"", str);
	      else
		*d = xdigit(*++s) * 64 + xdigit(*++s) * 8 + xdigit(*++s);
	      break;
	    }
	  }
      else
	*d = *s;
    }

  *d = '\0';
  return newstr;
}


varbinding *mkvarbinding(char *name, int type, void *data)
{
  varbinding *b = xmalloc(sizeof(varbinding));

  b->name = name;
  b->type = type;
  b->data = data;
  b->next = NULL;
  return b;
}


void pushlexscope(int nestable)
{
  lexscope *tmp;

  tmp = xmalloc(sizeof(lexscope));
  tmp->numvars = nestable ? 0 : -1;
  tmp->vars = NULL;
  tmp->vtypes = malloc(0);
  tmp->parent = scopestack;
  scopestack = tmp;
}

dtype *getscopevartypes(void)
{
  return scopestack->vtypes;
}

int getscopenumvars(void)
{
  return scopestack->numvars;
}

void poplexscope(void)
{
  lexscope *tmp = scopestack;
  varbinding *v, *vtmp;

  scopestack = scopestack->parent;
  free(tmp->vtypes);
  for (v = tmp->vars; v; v = vtmp)
    {
      free(v->name);
      vtmp = v->next;
      free(v);
    }
  free(tmp);
}


variable *mkvar(dtype type)
{
  variable *v = xmalloc(sizeof(variable));

  v->type = type;
  if (scopestack->numvars == -1)
    {
      v->slot = -1;
      v->data = initial_value(type);
    }
  else
    {
      scopestack->vtypes = xrealloc(scopestack->vtypes,
				    (scopestack->numvars + 1) *
				    sizeof(dtype));
      scopestack->vtypes[scopestack->numvars] = type;
      v->slot = scopestack->numvars++;
    }
  return v;
}


void define(int type, char *name, void *data)
{
  varbinding *var;
  int tmp;

  if (!scopestack)
    die("Definition not allowed here.\n");

  if (search_builtins(name, &tmp))
    die("Attempted to redefine builtin `%s'\n", name);

  for (var = scopestack->vars; var; var = var->next)
    {
      if (!strcmp(var->name, name))
	die("Attempted to redefine %s `%s'.",
	    var->type == VAR ? "variable" :
	    var->type == FUN ? "function" : "command", name);
    }

  var = mkvarbinding(xstrdup(name), type, data);
  var->next = scopestack->vars;
  scopestack->vars = var;
}

void *lookup(char *name, int *type, int norecurse)
{
  lexscope *scope;
  varbinding *b;
  void *ans;

  ans = search_builtins(name, type);
  if (ans)
    return ans;

  for (scope = scopestack; scope; scope = scope->parent)
    {
      for (b = scope->vars; b; b = b->next)
	{
	  if (!strcmp(b->name, name))
	    {
	      *type = b->type;
	      return b->data;
	    }
	}
      if (norecurse)
	break;
    }
  return NULL;
}

char *nameof(void *data)
{
  lexscope *scope;
  char *ans;
  varbinding *b;

  ans = nameof_builtin(data);
  if (ans)
    return ans;

  for (scope = scopestack; scope; scope = scope->parent)
    {
      for (b = scope->vars; b; b = b->next)
	{
	  if (b->data == data)
	    return b->name;
	}
    }
  return "[unknown symbol]";
}