-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlex.l
More file actions
274 lines (243 loc) · 5.6 KB
/
Copy pathlex.l
File metadata and controls
274 lines (243 loc) · 5.6 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
%{
/*
Copyright (C) 2006-2011, Bruce Ediger
This file is part of lc.
lc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
lc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with lc; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: lex.l,v 1.26 2011/11/12 04:50:28 bediger Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <parser.h>
#include <hashtable.h>
#include <atom.h>
#include "y.tab.h"
int lineno = 0;
const char *current_input_stream;
char *unescape_string(char *s);
void push_and_open(const char *filename);
struct stream_node {
#ifdef FLEX_SCANNER
YY_BUFFER_STATE stream;
#else
FILE *stream;
#endif
struct stream_node *next;
const char *old_filename;
int old_lineno;
};
struct stream_node *file_stack = NULL;
void set_yyin_stdin(void);
void reset_yyin(void);
void set_yyin(const char *filename);
extern int looking_for_filename; /* from grammar.y */
extern int found_binary_command; /* from grammar.y */
extern FILE *yyin;
extern const char *current_input_stream;
%}
%%
\#..*$ { return TK_EOL; }
"def" { return TK_DEF; }
"define" { return TK_DEF; }
"normalize" { return TK_NORMALIZE; }
"godelize" { return TK_GOEDELIZE; }
"load" { return TK_LOAD; }
"free" { return TK_FREE; }
"bound" { return TK_BOUND; }
"timer" { return TK_TIMER; }
"==" { return TK_LEXICALLY_EQUIVALENT; }
"=" { return TK_ALPHA_EQUIVALENT; }
"step" { return TK_STEP; }
"trace" { return TK_TRACE; }
"eta" { return TK_ETA; }
"print" { return TK_PRINT; }
\"(\\.|[^\\"])*\" {
char *tmp;
tmp = unescape_string(yytext);
yylval.string_constant = Atom_string(tmp);
free(tmp);
return FILE_NAME;
}
"on"|"off" {
const char *p = Atom_string(yytext);
yylval.string_constant = p;
if (found_binary_command)
return BINARY_MODIFIER;
else if (looking_for_filename)
return FILE_NAME;
else {
yylval.identifier = p;
return TK_IDENTIFIER;
}
}
[a-zA-Z_][a-zA-Z0-9_-]* {
if (looking_for_filename)
{
yylval.string_constant = Atom_string(yytext);
return FILE_NAME;
} else {
yylval.identifier = Atom_string(yytext);
return TK_IDENTIFIER;
}
}
"$$" {
return TK_RESULT;
}
[1-9][0-9]* {
/* So, 1 is a NUMBER, 2 is a NUMBER, but 0 doesn't fit.
* This keeps the de-parameterization code in lambda_expression.c
* from having to deal with expr{0} as some weird-beard special case.
*/
yylval.number = strtol(yytext, NULL, 10);
return NUMBER;
}
\* { return TK_STAR; }
\n { return TK_EOL; }
\( { return TK_LPAREN; }
\) { return TK_RPAREN; }
\. { return TK_DOT; }
"->" { return TK_DOT; }
\% { return TK_LAMBDA; }
\$ { return TK_LAMBDA; }
\^ { return TK_LAMBDA; }
\\ { return TK_LAMBDA; }
\{ { return TK_LBRACE; }
\} { return TK_RBRACE; }
. { /* Just eat it. */ }
%%
char *
unescape_string(char *s)
{
char *n = NULL;
if (s)
{
int i, j, l;
if ('"' == s[0])
++s;
if ('"' == s[strlen(s) - 1])
s[strlen(s) - 1] = '\0';
n = malloc(strlen(s) + 1);
l = strlen(s);
for (i = 0, j = 0; i < l; ++i)
{
if ('\\' == s[i])
{
++i;
switch (s[i])
{
/* XXX - doesn't do '\0' or other numerical escapes */
case 't': n[j++] = '\t'; break;
case 'r': n[j++] = '\r'; break;
case 'n': n[j++] = '\n'; break;
case '\\': n[j++] = '\\'; break;
default:
n[j++] = s[i];
break;
}
} else {
n[j++] = s[i];
}
}
n[j] = '\0';
}
return n;
}
void
push_and_open(const char *filename)
{
FILE *fin;
if (NULL != (fin = fopen(filename, "r")))
{
struct stream_node *n;
n = malloc(sizeof(*n));
#ifdef FLEX_SCANNER
n->stream = YY_CURRENT_BUFFER;
yyin = fin;
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
#else
n->stream = yyin;
yyin = fin;
#endif
n->next = file_stack;
n->old_filename = current_input_stream;
n->old_lineno = lineno;
current_input_stream = filename;
file_stack = n;
lineno = 0;
} else {
fprintf(stderr, "Could not open \"%s\" for read: %s\n",
filename, strerror(errno));
}
}
void
set_yyin_stdin(void)
{
yyin = stdin;
#ifdef FLEX_SCANNER
yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
#endif
}
void
set_yyin(const char *filename)
{
FILE *fin;
if (NULL != (fin = fopen(filename, "r")))
{
yyin = fin;
#ifdef FLEX_SCANNER
yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
#endif
current_input_stream = filename;
lineno = 0;
} else {
fprintf(stderr, "Could not open \"%s\" for read: %s\n",
filename, strerror(errno));
}
}
void
reset_yyin(void)
{
if (yyin)
fclose(yyin);
yyin = NULL;
#ifdef FLEX_SCANNER
yy_delete_buffer(YY_CURRENT_BUFFER);
#endif
}
int
yywrap()
{
int r = 1;
if (file_stack)
{
struct stream_node *tmp = file_stack->next;
fclose(yyin);
#ifdef FLEX_SCANNER
yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer(file_stack->stream);
#else
yyin = file_stack->stream;
#endif
current_input_stream = file_stack->old_filename;
lineno = file_stack->old_lineno;
file_stack->stream = NULL;
file_stack->next = NULL;
free(file_stack);
file_stack = tmp;
r = 0;
}
return r;
}