| 1 |
%{ |
| 2 |
/* Copyright (C) 2022-2025 Free Software Foundation, Inc. |
| 3 |
This file is part of GNU Modula-2. |
| 4 |
|
| 5 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License as published by the Free |
| 7 |
Software Foundation; either version 2, or (at your option) any later |
| 8 |
version. |
| 9 |
|
| 10 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License along |
| 16 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 17 |
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* adv.flex - provides a lexical analyser for Dungeon |
| 22 |
*/ |
| 23 |
|
| 24 |
struct lineInfo { |
| 25 |
char *linebuf; /* line contents */ |
| 26 |
int linelen; /* length */ |
| 27 |
int tokenpos; /* start position of token within line */ |
| 28 |
int toklen; /* a copy of yylen (length of token) */ |
| 29 |
int nextpos; /* position after token */ |
| 30 |
int actualline; /* line number of this line */ |
| 31 |
}; |
| 32 |
|
| 33 |
static int lineno =1; /* a running count of the file line number */ |
| 34 |
static char *filename =NULL; |
| 35 |
static struct lineInfo *currentLine =NULL; |
| 36 |
|
| 37 |
void advflex_error (const char *); |
| 38 |
static void finishedLine (void); |
| 39 |
static void resetpos (void); |
| 40 |
static void consumeLine (void); |
| 41 |
static void updatepos (void); |
| 42 |
static void skippos (void); |
| 43 |
static void poperrorskip (const char *); |
| 44 |
int advflex_OpenSource (char *s); |
| 45 |
int advflex_GetLineNo (void); |
| 46 |
void advflex_CloseSource(void); |
| 47 |
char *advflex_GetToken (void); |
| 48 |
void _M2_advflex_init (int, char *, char *); |
| 49 |
void _M2_advflex_finish (int, char *, char *); |
| 50 |
void _M2_advflex_ctor (void); |
| 51 |
extern void yylex (void); |
| 52 |
|
| 53 |
#if !defined(TRUE) |
| 54 |
# define TRUE (1==1) |
| 55 |
#endif |
| 56 |
#if !defined(FALSE) |
| 57 |
# define FALSE (1==0) |
| 58 |
#endif |
| 59 |
|
| 60 |
typedef enum {eoftok, roomtok, doortok, walltok, treasuretok, attok, |
| 61 |
leadstok, totok, statustok, closedtok, opentok, secrettok, |
| 62 |
istok, endtok, enddottok, integertok, randomizetok} toktype ; |
| 63 |
|
| 64 |
toktype advflex_currenttoken; |
| 65 |
char *advflex_currentident; |
| 66 |
int advflex_currentinteger; |
| 67 |
|
| 68 |
|
| 69 |
#define YY_DECL void yylex (void) |
| 70 |
%} |
| 71 |
|
| 72 |
%% |
| 73 |
|
| 74 |
\n.* { consumeLine(); /* printf("found: %s\n", currentLine->linebuf); */ } |
| 75 |
[0-9]+ { updatepos(); |
| 76 |
advflex_currenttoken = integertok; |
| 77 |
advflex_currentinteger = atoi(yytext); |
| 78 |
return; } |
| 79 |
[ \t]* { updatepos(); } |
| 80 |
ROOM { updatepos(); advflex_currenttoken = roomtok; return; } |
| 81 |
END { updatepos(); advflex_currenttoken = endtok; return; } |
| 82 |
WALL { updatepos(); advflex_currenttoken = walltok; return; } |
| 83 |
DOOR { updatepos(); advflex_currenttoken = doortok; return; } |
| 84 |
STATUS { updatepos(); advflex_currenttoken = statustok; return; } |
| 85 |
CLOSED { updatepos(); advflex_currenttoken = closedtok; return; } |
| 86 |
OPEN { updatepos(); advflex_currenttoken = opentok; return; } |
| 87 |
SECRET { updatepos(); advflex_currenttoken = secrettok; return; } |
| 88 |
LEADS { updatepos(); advflex_currenttoken = leadstok; return; } |
| 89 |
TO { updatepos(); advflex_currenttoken = totok; return; } |
| 90 |
TREASURE { updatepos(); advflex_currenttoken = treasuretok; return; } |
| 91 |
AT { updatepos(); advflex_currenttoken = attok; return; } |
| 92 |
IS { updatepos(); advflex_currenttoken = istok; return; } |
| 93 |
END. { updatepos(); advflex_currenttoken = enddottok; return; } |
| 94 |
RANDOMIZE { updatepos(); advflex_currenttoken = randomizetok; return; } |
| 95 |
<<EOF>> { updatepos(); advflex_currenttoken = eoftok; return; } |
| 96 |
|
| 97 |
%% |
| 98 |
|
| 99 |
/* |
| 100 |
* consumeLine - reads a line into a buffer, it then pushes back the whole |
| 101 |
* line except the initial \n. |
| 102 |
*/ |
| 103 |
|
| 104 |
static void consumeLine (void) |
| 105 |
{ |
| 106 |
if (currentLine->linelen<yyleng) { |
| 107 |
currentLine->linebuf = (char *)realloc (currentLine->linebuf, yyleng); |
| 108 |
currentLine->linelen = yyleng; |
| 109 |
} |
| 110 |
strcpy(currentLine->linebuf, yytext+1); /* copy all except the initial \n */ |
| 111 |
lineno++; |
| 112 |
currentLine->actualline = lineno; |
| 113 |
currentLine->tokenpos=0; |
| 114 |
currentLine->nextpos=0; |
| 115 |
yyless(1); /* push back all but the \n */ |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* updatepos - updates the current token position. |
| 120 |
* Should be used when a rule matches a token. |
| 121 |
*/ |
| 122 |
|
| 123 |
static void updatepos (void) |
| 124 |
{ |
| 125 |
currentLine->nextpos = currentLine->tokenpos+yyleng; |
| 126 |
currentLine->toklen = yyleng; |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* skippos - skips over this token. This function should be called |
| 131 |
* if we are not returning and thus not calling getToken. |
| 132 |
*/ |
| 133 |
|
| 134 |
static void skippos (void) |
| 135 |
{ |
| 136 |
currentLine->tokenpos = currentLine->nextpos; |
| 137 |
} |
| 138 |
|
| 139 |
/* |
| 140 |
* initLine - initializes a currentLine |
| 141 |
*/ |
| 142 |
|
| 143 |
static void initLine (void) |
| 144 |
{ |
| 145 |
currentLine = (struct lineInfo *)malloc (sizeof(struct lineInfo)); |
| 146 |
|
| 147 |
if (currentLine == NULL) |
| 148 |
perror("malloc"); |
| 149 |
currentLine->linebuf = NULL; |
| 150 |
currentLine->linelen = 0; |
| 151 |
currentLine->tokenpos = 0; |
| 152 |
currentLine->toklen = 0; |
| 153 |
currentLine->nextpos = 0; |
| 154 |
currentLine->actualline = lineno; |
| 155 |
} |
| 156 |
|
| 157 |
/* |
| 158 |
* resetpos - resets the position of the next token to the start of the line. |
| 159 |
*/ |
| 160 |
|
| 161 |
static void resetpos (void) |
| 162 |
{ |
| 163 |
if (currentLine != NULL) |
| 164 |
currentLine->nextpos = 0; |
| 165 |
} |
| 166 |
|
| 167 |
/* |
| 168 |
* advflex_GetToken - returns a new token. |
| 169 |
*/ |
| 170 |
|
| 171 |
char *advflex_GetToken (void) |
| 172 |
{ |
| 173 |
if (currentLine == NULL) |
| 174 |
initLine(); |
| 175 |
currentLine->tokenpos = currentLine->nextpos; |
| 176 |
yylex(); |
| 177 |
} |
| 178 |
|
| 179 |
void advflex_error (const char *s) |
| 180 |
{ |
| 181 |
if (currentLine != NULL) { |
| 182 |
printf("%s:%d:%s\n", filename, currentLine->actualline, s); |
| 183 |
printf("%s\n", currentLine->linebuf); |
| 184 |
# if 0 |
| 185 |
printf("%*s%*s\n", currentLine->nextpos, " ", currentLine->toklen, "^"); |
| 186 |
# endif |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/* |
| 191 |
* OpenSource - returns TRUE if file, s, can be opened and |
| 192 |
* all tokens are taken from this file. |
| 193 |
*/ |
| 194 |
|
| 195 |
int advflex_OpenSource (char *s) |
| 196 |
{ |
| 197 |
FILE *f = fopen(s, "r"); |
| 198 |
|
| 199 |
if (f == NULL) |
| 200 |
return FALSE; |
| 201 |
else { |
| 202 |
yy_delete_buffer(YY_CURRENT_BUFFER); |
| 203 |
yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE)); |
| 204 |
filename = strdup(s); |
| 205 |
lineno =1; |
| 206 |
if (currentLine != NULL) |
| 207 |
currentLine->actualline = lineno; |
| 208 |
return TRUE; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/* |
| 213 |
* CloseSource - provided for semantic sugar |
| 214 |
*/ |
| 215 |
|
| 216 |
void advflex_CloseSource (void) |
| 217 |
{ |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* advflex_GetLineNo - returns the current line number. |
| 222 |
*/ |
| 223 |
|
| 224 |
int advflex_GetLineNo (void) |
| 225 |
{ |
| 226 |
if (currentLine != NULL) |
| 227 |
return currentLine->actualline; |
| 228 |
else |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
/* |
| 233 |
* yywrap is called when end of file is seen. We push an eof token |
| 234 |
* and tell the lexical analysis to stop. |
| 235 |
*/ |
| 236 |
|
| 237 |
int yywrap (void) |
| 238 |
{ |
| 239 |
updatepos(); return 1; |
| 240 |
} |
| 241 |
|
| 242 |
void _M2_advflex_init (int, char *, char *) |
| 243 |
{ |
| 244 |
} |
| 245 |
|
| 246 |
void _M2_advflex_finish (int, char *, char *) |
| 247 |
{ |
| 248 |
} |
| 249 |
|
| 250 |
void _M2_advflex_ctor (void) |
| 251 |
{ |
| 252 |
} |
| 253 |
|
| 254 |
#if 0 |
| 255 |
main () { |
| 256 |
char *s; |
| 257 |
|
| 258 |
if (advflex_OpenSource("../maps/glover")) { |
| 259 |
s = (char *)advflex_GetToken(); |
| 260 |
while (s != NULL) { |
| 261 |
advflex_error(s); |
| 262 |
s = (char *)advflex_GetToken(); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
#endif |