-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinclex.lex
153 lines (131 loc) · 2.78 KB
/
inclex.lex
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
%{
#include <stdlib.h>
#include <inctok.h>
#include <dbg.h>
#include <cfgdbg.h>
#ifdef DEBUG
#include <memall.h>
#include <dbmem.h>
static char * local_strdup(const char * s)
{
char * retVal = malloc(strlen(s) + 1);
strcpy(retVal, s);
return retVal;
}
#else
#define local_strdup(s) strdup(s)
#endif
%}
whitespace [\t ]
alpha [a-zA-Z]
digit [0-9]
firstchar [a-zA-Z_]
followchar [a-zA-Z0-9_]
quote [\"]
string [^\"]
langle "<"
rangle ">"
astring [^<^>^\n]
hex [xX]
%s PREPROC
%%
{whitespace} /* ignore */;
^"#include" {
ConfigSpew("lexspew", ("include\n"));
BEGIN PREPROC;
return INCLUDE;
};
^"#define" {
ConfigSpew("lexspew", ("define\n"));
BEGIN PREPROC;
return DEFINE;
};
<PREPROC>\n {
ConfigSpew("lexspew", ("return\n"));
BEGIN INITIAL;
};
<PREPROC>{firstchar}{followchar}* {
ConfigSpew("lexspew", ("ident\n"));
inclval.strval = local_strdup(inctext);
return IDENT;
}
<PREPROC>-?{digit}+ {
ConfigSpew("lexspew", ("int\n"));
inclval.ival = atoi(inctext);
return INT;
}
<PREPROC>0{hex}-?{digit}+ {
ConfigSpew("lexspew", ("hex\n"));
inclval.ival = strtol(inctext, NULL, 16);
return INT;
}
<PREPROC>{quote}{string}*{quote} {
ConfigSpew("lexspew", ("string\n"));
inclval.strval = local_strdup(inctext);
return STRING;
}
<PREPROC>{langle}{astring}*{rangle} {
ConfigSpew("lexspew", ("string\n"));
inclval.strval = local_strdup(inctext);
return STRING;
}
.|\n /* ignore */;
<PREPROC>. /* ignore */;
%%
#define MAX_FILES_OPEN 10
YY_SAVED *file_stack[MAX_FILES_OPEN];
int files_in_stack = 0;
BOOL IncludeFileOpen(char *string, BOOL push)
{
FILE *file;
char file_name[100];
strncpy(file_name, &string[1], strlen(string)-2);
file_name[strlen(string)-2] = '\0';
if (push && (files_in_stack >= MAX_FILES_OPEN))
{
Warning(("Max open files exceeded\n"));
return FALSE;
}
if ((file = fopen(file_name, "r")) == NULL)
{
Warning(("Can't open file %s\n", file_name));
return FALSE;
}
if (push)
{
file_stack[files_in_stack] = incSaveScan(incin);
files_in_stack++;
}
incin = file;
BEGIN INITIAL;
return TRUE;
}
BOOL IncludeFilePop()
{
BEGIN INITIAL;
if (files_in_stack>0)
{
if (incin)
fclose(incin);
--files_in_stack;
incRestoreScan(file_stack[files_in_stack]);
return TRUE;
}
return FALSE;
}
int incwrap(void)
{
if (IncludeFilePop())
return 0;
return 1;
}
int incparse();
void IncParseFile(char *fileName)
{
files_in_stack = 0;
YY_INIT;
if (IncludeFileOpen(fileName, FALSE))
while (!feof(incin))
incparse();
fclose(incin);
}