| 1 |
(* bnflex.mod provides a simple lexical package for pg. |
| 2 |
|
| 3 |
Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with GNU Modula-2; see the file COPYING3. If not see |
| 20 |
<http://www.gnu.org/licenses/>. *) |
| 21 |
|
| 22 |
IMPLEMENTATION MODULE bnflex ; |
| 23 |
|
| 24 |
|
| 25 |
FROM PushBackInput IMPORT GetCh, PutCh, PutString, WarnError ; |
| 26 |
FROM SymbolKey IMPORT SymbolTree, InitTree, PutSymKey, GetSymKey ; |
| 27 |
FROM ASCII IMPORT tab, lf, nul ; |
| 28 |
FROM Debug IMPORT Halt ; |
| 29 |
FROM NameKey IMPORT Name, LengthKey, MakeKey, GetKey, WriteKey, NulName ; |
| 30 |
FROM StrLib IMPORT StrEqual, StrLen ; |
| 31 |
FROM FIO IMPORT File, IsNoError ; |
| 32 |
FROM StrCase IMPORT Lower ; |
| 33 |
FROM StdIO IMPORT Write ; |
| 34 |
|
| 35 |
IMPORT PushBackInput ; |
| 36 |
|
| 37 |
|
| 38 |
CONST |
| 39 |
MaxNameLength = 8192 ; |
| 40 |
|
| 41 |
VAR |
| 42 |
f : File ; |
| 43 |
ReservedWords: SymbolTree ; |
| 44 |
CurrentToken : Name ; |
| 45 |
CurrentType : TokenType ; |
| 46 |
Debugging , |
| 47 |
InQuote : BOOLEAN ; |
| 48 |
QuoteChar : CHAR ; |
| 49 |
|
| 50 |
|
| 51 |
(* |
| 52 |
OpenSource - Attempts to open the source file, a. |
| 53 |
The success of the operation is returned. |
| 54 |
*) |
| 55 |
|
| 56 |
PROCEDURE OpenSource (a: ARRAY OF CHAR) : BOOLEAN ; |
| 57 |
BEGIN |
| 58 |
f := PushBackInput.Open(a) ; |
| 59 |
RETURN( IsNoError(f) ) |
| 60 |
END OpenSource ; |
| 61 |
|
| 62 |
|
| 63 |
(* |
| 64 |
CloseSource - Closes the current open file. |
| 65 |
*) |
| 66 |
|
| 67 |
PROCEDURE CloseSource ; |
| 68 |
BEGIN |
| 69 |
PushBackInput.Close(f) |
| 70 |
END CloseSource ; |
| 71 |
|
| 72 |
|
| 73 |
(* |
| 74 |
GetChar - returns the current character on the input stream. |
| 75 |
*) |
| 76 |
|
| 77 |
PROCEDURE GetChar () : CHAR ; |
| 78 |
BEGIN |
| 79 |
RETURN( PushBackInput.GetCh(f) ) |
| 80 |
END GetChar ; |
| 81 |
|
| 82 |
|
| 83 |
(* |
| 84 |
PutChar - pushes a character onto the push back stack, it also |
| 85 |
returns the character which has been pushed. |
| 86 |
*) |
| 87 |
|
| 88 |
PROCEDURE PutChar (ch: CHAR) : CHAR ; |
| 89 |
BEGIN |
| 90 |
RETURN( PushBackInput.PutCh(ch) ) |
| 91 |
END PutChar ; |
| 92 |
|
| 93 |
|
| 94 |
(* |
| 95 |
EatChar - consumes the next character in the input. |
| 96 |
*) |
| 97 |
|
| 98 |
PROCEDURE EatChar ; |
| 99 |
BEGIN |
| 100 |
IF PushBackInput.GetCh(f)=nul |
| 101 |
THEN |
| 102 |
END |
| 103 |
END EatChar ; |
| 104 |
|
| 105 |
|
| 106 |
(* |
| 107 |
IsWhite - returns TRUE if, ch, is a space or a tab. |
| 108 |
*) |
| 109 |
|
| 110 |
PROCEDURE IsWhite (ch: CHAR) : BOOLEAN ; |
| 111 |
BEGIN |
| 112 |
RETURN( (ch=' ') OR (ch=tab) OR (ch=lf) ) |
| 113 |
END IsWhite ; |
| 114 |
|
| 115 |
|
| 116 |
(* |
| 117 |
SkipWhite - skips all white space. |
| 118 |
*) |
| 119 |
|
| 120 |
PROCEDURE SkipWhite ; |
| 121 |
BEGIN |
| 122 |
WHILE IsWhite(PutChar(GetChar())) DO |
| 123 |
EatChar |
| 124 |
END |
| 125 |
END SkipWhite ; |
| 126 |
|
| 127 |
|
| 128 |
(* |
| 129 |
SkipUntilEoln - skips until a lf is seen. It consumes the lf. |
| 130 |
*) |
| 131 |
|
| 132 |
PROCEDURE SkipUntilEoln ; |
| 133 |
BEGIN |
| 134 |
WHILE (PutChar(GetChar())#lf) AND (PutChar(GetChar())#nul) DO |
| 135 |
EatChar |
| 136 |
END ; |
| 137 |
IF PutChar(GetChar())=lf |
| 138 |
THEN |
| 139 |
EatChar |
| 140 |
END |
| 141 |
END SkipUntilEoln ; |
| 142 |
|
| 143 |
|
| 144 |
(* |
| 145 |
SkipUntilWhite - skips all characters until white space is seen. |
| 146 |
*) |
| 147 |
|
| 148 |
PROCEDURE SkipUntilWhite ; |
| 149 |
BEGIN |
| 150 |
WHILE ((NOT IsWhite(PutChar(GetChar()))) AND (PutChar(GetChar())#nul)) OR |
| 151 |
(PutChar(GetChar())=lf) DO |
| 152 |
EatChar |
| 153 |
END |
| 154 |
END SkipUntilWhite ; |
| 155 |
|
| 156 |
|
| 157 |
(* |
| 158 |
IsReserved - returns TRUE if the name is a reserved word. |
| 159 |
*) |
| 160 |
|
| 161 |
PROCEDURE IsReserved (name: Name) : BOOLEAN ; |
| 162 |
BEGIN |
| 163 |
RETURN (GetSymKey(ReservedWords, name)#0) |
| 164 |
END IsReserved ; |
| 165 |
|
| 166 |
|
| 167 |
(* |
| 168 |
GetCurrentTokenType - returns the type of current token. |
| 169 |
*) |
| 170 |
|
| 171 |
PROCEDURE GetCurrentTokenType () : TokenType ; |
| 172 |
BEGIN |
| 173 |
RETURN( CurrentType ) |
| 174 |
END GetCurrentTokenType ; |
| 175 |
|
| 176 |
|
| 177 |
(* |
| 178 |
GetCurrentToken - returns the NameKey of the current token. |
| 179 |
*) |
| 180 |
|
| 181 |
PROCEDURE GetCurrentToken () : Name ; |
| 182 |
BEGIN |
| 183 |
RETURN( CurrentToken ) |
| 184 |
END GetCurrentToken ; |
| 185 |
|
| 186 |
|
| 187 |
(* |
| 188 |
SkipComments - consumes comments. |
| 189 |
*) |
| 190 |
|
| 191 |
PROCEDURE SkipComments ; |
| 192 |
BEGIN |
| 193 |
SkipWhite ; |
| 194 |
WHILE PutChar(GetChar())='-' DO |
| 195 |
IF (GetChar()='-') AND (PutChar(GetChar())='-') |
| 196 |
THEN |
| 197 |
(* found comment, skip it *) |
| 198 |
SkipUntilEoln ; |
| 199 |
SkipWhite |
| 200 |
ELSE |
| 201 |
(* no second '-' found thus restore first '-' *) |
| 202 |
IF PutChar('-')='-' |
| 203 |
THEN |
| 204 |
END ; |
| 205 |
RETURN |
| 206 |
END |
| 207 |
END |
| 208 |
END SkipComments ; |
| 209 |
|
| 210 |
|
| 211 |
(* |
| 212 |
WriteToken - |
| 213 |
*) |
| 214 |
|
| 215 |
PROCEDURE WriteToken ; |
| 216 |
BEGIN |
| 217 |
WriteKey(CurrentToken) ; Write(' ') |
| 218 |
END WriteToken ; |
| 219 |
|
| 220 |
|
| 221 |
(* |
| 222 |
AdvanceToken - advances to the next token. |
| 223 |
*) |
| 224 |
|
| 225 |
PROCEDURE AdvanceToken ; |
| 226 |
VAR |
| 227 |
a: ARRAY [0..MaxNameLength] OF CHAR ; |
| 228 |
i: CARDINAL ; |
| 229 |
BEGIN |
| 230 |
i := 0 ; |
| 231 |
IF InQuote |
| 232 |
THEN |
| 233 |
IF CurrentType=literaltok |
| 234 |
THEN |
| 235 |
IF PutChar(GetChar())=QuoteChar |
| 236 |
THEN |
| 237 |
a[i] := GetChar() ; |
| 238 |
InQuote := FALSE ; |
| 239 |
INC(i) ; |
| 240 |
a[i] := nul ; |
| 241 |
CurrentToken := MakeKey(a) ; |
| 242 |
CurrentType := VAL(TokenType, GetSymKey(ReservedWords, CurrentToken)) |
| 243 |
ELSE |
| 244 |
IF QuoteChar='"' |
| 245 |
THEN |
| 246 |
WarnError('missing " at the end of a literal') |
| 247 |
ELSE |
| 248 |
WarnError("missing ' at the end of a literal") |
| 249 |
END ; |
| 250 |
InQuote := FALSE (* to avoid a contineous list of the same error message *) |
| 251 |
END |
| 252 |
ELSE |
| 253 |
WHILE (i<MaxNameLength) AND (PutChar(GetChar())#nul) AND |
| 254 |
(PutChar(GetChar())#lf) AND (PutChar(GetChar())#QuoteChar) DO |
| 255 |
a[i] := GetChar() ; |
| 256 |
INC(i) |
| 257 |
END ; |
| 258 |
IF PutChar(GetChar())=QuoteChar |
| 259 |
THEN |
| 260 |
CurrentType := literaltok ; |
| 261 |
a[i] := nul ; |
| 262 |
CurrentToken := MakeKey(a) |
| 263 |
ELSE |
| 264 |
IF QuoteChar='"' |
| 265 |
THEN |
| 266 |
WarnError('missing " at the end of a literal') |
| 267 |
ELSE |
| 268 |
WarnError("missing ' at the end of a literal") |
| 269 |
END ; |
| 270 |
InQuote := FALSE (* to avoid a contineous list of the same error message *) |
| 271 |
END |
| 272 |
END |
| 273 |
ELSE |
| 274 |
SkipComments ; |
| 275 |
|
| 276 |
IF (PutChar(GetChar())='"') OR (PutChar(GetChar())="'") |
| 277 |
THEN |
| 278 |
a[i] := GetChar() ; |
| 279 |
QuoteChar := a[i] ; |
| 280 |
INC(i) ; |
| 281 |
InQuote := TRUE ; |
| 282 |
a[i] := nul ; |
| 283 |
CurrentToken := MakeKey(a) ; |
| 284 |
CurrentType := VAL(TokenType, GetSymKey(ReservedWords, CurrentToken)) |
| 285 |
ELSE |
| 286 |
WHILE (i<MaxNameLength) AND (PutChar(GetChar())#nul) AND |
| 287 |
(PutChar(GetChar())#lf) AND (PutChar(GetChar())#QuoteChar) AND |
| 288 |
(NOT IsWhite(PutChar(GetChar()))) DO |
| 289 |
a[i] := GetChar() ; |
| 290 |
INC(i) |
| 291 |
END ; |
| 292 |
a[i] := nul ; |
| 293 |
CurrentToken := MakeKey(a) ; |
| 294 |
IF GetSymKey(ReservedWords, CurrentToken)=0 |
| 295 |
THEN |
| 296 |
CurrentType := identtok |
| 297 |
ELSE |
| 298 |
CurrentType := VAL(TokenType, GetSymKey(ReservedWords, CurrentToken)) |
| 299 |
END |
| 300 |
END |
| 301 |
END ; |
| 302 |
IF Debugging |
| 303 |
THEN |
| 304 |
WriteToken |
| 305 |
END |
| 306 |
END AdvanceToken ; |
| 307 |
|
| 308 |
|
| 309 |
(* |
| 310 |
SymIs - if t is equal to the current token the next token is read |
| 311 |
and true is returned, otherwise false is returned. |
| 312 |
*) |
| 313 |
|
| 314 |
PROCEDURE SymIs (t: TokenType) : BOOLEAN ; |
| 315 |
BEGIN |
| 316 |
IF CurrentType=t |
| 317 |
THEN |
| 318 |
AdvanceToken ; |
| 319 |
RETURN( TRUE ) |
| 320 |
ELSE |
| 321 |
RETURN( FALSE ) |
| 322 |
END |
| 323 |
END SymIs ; |
| 324 |
|
| 325 |
|
| 326 |
(* |
| 327 |
IsSym - returns the result of the comparison between the current token |
| 328 |
type and t. |
| 329 |
*) |
| 330 |
|
| 331 |
PROCEDURE IsSym (t: TokenType) : BOOLEAN ; |
| 332 |
BEGIN |
| 333 |
RETURN( t=CurrentType ) |
| 334 |
END IsSym ; |
| 335 |
|
| 336 |
|
| 337 |
(* |
| 338 |
PushBackToken - pushes a token back onto input. |
| 339 |
*) |
| 340 |
|
| 341 |
PROCEDURE PushBackToken (t: Name) ; |
| 342 |
VAR |
| 343 |
a: ARRAY [0..MaxNameLength] OF CHAR ; |
| 344 |
BEGIN |
| 345 |
GetKey(t, a) ; |
| 346 |
PutString(a) |
| 347 |
END PushBackToken ; |
| 348 |
|
| 349 |
|
| 350 |
(* |
| 351 |
SetDebugging - sets the debugging flag. |
| 352 |
*) |
| 353 |
|
| 354 |
PROCEDURE SetDebugging (flag: BOOLEAN) ; |
| 355 |
BEGIN |
| 356 |
Debugging := flag |
| 357 |
END SetDebugging ; |
| 358 |
|
| 359 |
|
| 360 |
(* |
| 361 |
Init - initialize the modules global variables. |
| 362 |
*) |
| 363 |
|
| 364 |
PROCEDURE Init ; |
| 365 |
VAR |
| 366 |
a: ARRAY [0..1] OF CHAR ; |
| 367 |
BEGIN |
| 368 |
InitTree(ReservedWords) ; |
| 369 |
Debugging := FALSE ; |
| 370 |
|
| 371 |
a[0] := nul ; |
| 372 |
PutSymKey(ReservedWords, MakeKey(a) , ORD(eoftok)) ; |
| 373 |
PutSymKey(ReservedWords, MakeKey('%') , ORD(codetok)) ; |
| 374 |
PutSymKey(ReservedWords, MakeKey(':=') , ORD(lbecomestok)) ; |
| 375 |
PutSymKey(ReservedWords, MakeKey('=:') , ORD(rbecomestok)) ; |
| 376 |
PutSymKey(ReservedWords, MakeKey('|') , ORD(bartok)) ; |
| 377 |
PutSymKey(ReservedWords, MakeKey('[') , ORD(lsparatok)) ; |
| 378 |
PutSymKey(ReservedWords, MakeKey(']') , ORD(rsparatok)) ; |
| 379 |
PutSymKey(ReservedWords, MakeKey('{') , ORD(lcparatok)) ; |
| 380 |
PutSymKey(ReservedWords, MakeKey('}') , ORD(rcparatok)) ; |
| 381 |
PutSymKey(ReservedWords, MakeKey('(') , ORD(lparatok)) ; |
| 382 |
PutSymKey(ReservedWords, MakeKey(')') , ORD(rparatok)) ; |
| 383 |
PutSymKey(ReservedWords, MakeKey('<') , ORD(lesstok)) ; |
| 384 |
PutSymKey(ReservedWords, MakeKey('>') , ORD(gretok)) ; |
| 385 |
PutSymKey(ReservedWords, MakeKey('error') , ORD(errortok)) ; |
| 386 |
PutSymKey(ReservedWords, MakeKey('tokenfunc') , ORD(tfunctok)) ; |
| 387 |
PutSymKey(ReservedWords, MakeKey('symfunc') , ORD(symfunctok)) ; |
| 388 |
PutSymKey(ReservedWords, MakeKey("'") , ORD(squotetok)) ; |
| 389 |
PutSymKey(ReservedWords, MakeKey('"') , ORD(dquotetok)) ; |
| 390 |
PutSymKey(ReservedWords, MakeKey('module') , ORD(moduletok)) ; |
| 391 |
PutSymKey(ReservedWords, MakeKey('begin') , ORD(begintok)) ; |
| 392 |
PutSymKey(ReservedWords, MakeKey('rules') , ORD(rulestok)) ; |
| 393 |
PutSymKey(ReservedWords, MakeKey('end') , ORD(endtok)) ; |
| 394 |
PutSymKey(ReservedWords, MakeKey('declaration'), ORD(declarationtok)) ; |
| 395 |
PutSymKey(ReservedWords, MakeKey('token') , ORD(tokentok)) ; |
| 396 |
PutSymKey(ReservedWords, MakeKey('special') , ORD(specialtok)) ; |
| 397 |
PutSymKey(ReservedWords, MakeKey('first') , ORD(firsttok)) ; |
| 398 |
PutSymKey(ReservedWords, MakeKey('follow') , ORD(followtok)) ; |
| 399 |
PutSymKey(ReservedWords, MakeKey('epsilon') , ORD(epsilontok)) ; |
| 400 |
PutSymKey(ReservedWords, MakeKey('BNF') , ORD(BNFtok)) ; |
| 401 |
PutSymKey(ReservedWords, MakeKey('FNB') , ORD(FNBtok)) ; |
| 402 |
|
| 403 |
CurrentToken := NulName ; |
| 404 |
CurrentType := identtok ; |
| 405 |
InQuote := FALSE |
| 406 |
|
| 407 |
END Init ; |
| 408 |
|
| 409 |
|
| 410 |
BEGIN |
| 411 |
Init |
| 412 |
END bnflex. |
| 413 |
(* |
| 414 |
* Local variables: |
| 415 |
* compile-command: "../bin2/m2f -quiet -g -verbose -M \"../libs ../gm2s\" bnflex.mod" |
| 416 |
* End: |
| 417 |
*) |