-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlexer.l
220 lines (165 loc) · 5.8 KB
/
lexer.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
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
%{
/*
* Copyright (C) 2000-2006 Erik Edelmann <Erik.Edelmann@iki.fi>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software
* Foundation.
*
* This program 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 this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "global.h"
#include "errormesg.h"
#include "find_dep.h"
#include "utils.h"
#include "xmalloc.h"
void yy_push_state(int new_state);
void yy_pop_state();
void pop_filestack();
static YY_BUFFER_STATE include_stack[INCLUDE_RECURSION_LIMIT];
static int line_num_stack[INCLUDE_RECURSION_LIMIT];
static int incl_buff = 0;
static char string_buf[MAX_STRING_LEN];
static char *string_buf_ptr;
static int old_startcond;
static int last_returned;
#define RETURN(n) last_returned = n; return n;
%}
%option yylineno
%s free_fmt fixed_fmt
%x str_sq str_dq
%%
\" { string_buf_ptr = string_buf; old_startcond = YY_START;
BEGIN(str_dq); }
' { string_buf_ptr = string_buf; old_startcond = YY_START;
BEGIN(str_sq); }
<str_dq>\" |
<str_sq>' {
BEGIN(old_startcond);
*string_buf_ptr = '\0';
yylval.string = xstrdup(string_buf);
DEBUG_PRINT("string: '%s'\n", yylval.string);
RETURN(STRING);
}
<str_dq,str_sq>&[ \t]*\n |
<str_dq,str_sq>&[ \t]*\n[ \t]*& /* Ignore (continued strings, free fmt) */
<str_dq,str_sq>\n[ ]{5}[^ \t\n] {
if (old_startcond == fixed_fmt)
; /* Ignore (cont. strings, fixed fmt) */
else
unput(yytext[strlen(yytext)-1]);
}
<str_dq,str_sq>\n {
unput ('\n');
BEGIN(old_startcond);
yylval.number = yylineno;
RETURN(UNTERMINATED_STRING);
}
<str_sq,str_dq>. { *string_buf_ptr++ = yytext[0]; }
!.* { RETURN(EOSTMT); } /* Treat comments like */
<fixed_fmt>^[cC*dD].*\n { RETURN(EOSTMT); } /* empty lines */
#[ \t]*include { RETURN(CPP_INCLUDE); }
\$[ \t]*include { RETURN(F90PPR_INCLUDE); }
\?\?[ \t]*include { RETURN(COCO_INCLUDE); }
#[ \t]*define { RETURN(CPP_DEFINE); }
\$[ \t]*DEFINE { RETURN(F90PPR_DEFINE); }
#[ \t]*undef { RETURN(CPP_UNDEF); }
\$[ \t]*UNDEF { RETURN(F90PPR_UNDEF); }
#[ \t]*ifdef { RETURN(CPP_IFDEF); }
#[ \t]*ifndef { RETURN(CPP_IFNDEF); }
#[ \t]*if[ \t].* { RETURN(CPP_IF); }
#[ \t]*elif[ \t].* { RETURN(CPP_ELIF); }
#[ \t]*else { RETURN(CPP_ELSE); }
#[ \t]*endif { RETURN(CPP_ENDIF); }
$[ \t]*ifdef { RETURN(F90PPR_IFDEF); }
$[ \t]*ifndef { RETURN(F90PPR_IFNDEF); }
$[ \t]*if { RETURN(F90PPR_IF); }
$[ \t]*elif { RETURN(F90PPR_ELIF); }
$[ \t]*else { RETURN(F90PPR_ELSE); }
$[ \t]*endif { RETURN(F90PPR_ENDIF); }
/* Line continuations, possible involving comments. */
&([ \t\n]*|!.*)*
&([ \t\n]*|!.*)*&
<fixed_fmt>\n[ ]{5}[^ ] { RETURN(GARBAGE); }
=|=> { RETURN(ASSIGNMENT_OP); }
[a-zA-Z_][a-zA-Z_0-9]* { ;yylval.string = xstrdup(yytext); RETURN(WORD); }
[^ \t\n\r;,!'"a-zA-Z=&]+ { RETURN(GARBAGE); }
;|\n { RETURN(EOSTMT); }
[ \t\r,] /* Ignore */
\\[ \t]*\n /* Ignore line-endings preceeded by \ */
. { RETURN(*yytext); }
<<EOF>> {
DEBUG_PRINT("EOF reached %i\n", incl_buff);
incl_buff--;
if (incl_buff < 0) {
if (last_returned == EOSTMT) {
incl_buff = 0;
yyterminate();
} else {
/* "EOF without \n first" cases. */
RETURN(EOSTMT);
}
} else {
fclose(yyin);
yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer (include_stack[incl_buff]);
yylineno = line_num_stack[incl_buff];
pop_filestack();
}
}
%%
/* "Include" file 'incfile' here. Return false for failure, true for success. */
bool lex_include_file(const char *incfile)
{
FILE *bakup;
DEBUG_PRINT("'%s'\n", incfile);
if (incl_buff >= INCLUDE_RECURSION_LIMIT) {
warning("Recursion limit reached in file '%s'", incfile);
return false;
}
bakup = yyin;
yyin = open_src_file(incfile, options.src_path);
if (yyin == NULL) {
if (errno == ENOENT) {
if (options.warn_missing)
warning("Include file '%s' not found", incfile);
} else
warning("Skipping include file '%s': %s", incfile, strerror(errno));
yyin = bakup;
return false;
}
include_stack[incl_buff] = YY_CURRENT_BUFFER;
yy_switch_to_buffer(yy_create_buffer (yyin, YY_BUF_SIZE));
line_num_stack[incl_buff++] = yylineno;
yylineno = 1;
return true;
}
void lex_set_format(SourceFmt fmt)
{
switch (fmt) {
case UNKNOWN:
case FREE:
BEGIN(free_fmt); break;
case FIXED:
BEGIN(fixed_fmt); break;
default:
assert(0);
}
}
int yywrap()
{
return 1;
}