| 1 |
(* M2LexBuf.def provides a buffer for m2.flex. |
| 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 |
DEFINITION MODULE M2LexBuf ; |
| 23 |
|
| 24 |
(* |
| 25 |
Title : M2LexBuf |
| 26 |
Author : Gaius Mulley |
| 27 |
System : UNIX (GNU Modula-2) |
| 28 |
Date : Fri Jul 27 12:42:13 2001 |
| 29 |
Description: provides a buffer for the all the tokens created by m2.lex. |
| 30 |
*) |
| 31 |
|
| 32 |
FROM SYSTEM IMPORT ADDRESS ; |
| 33 |
FROM M2Reserved IMPORT toktype ; |
| 34 |
FROM DynamicStrings IMPORT String ; |
| 35 |
FROM gcctypes IMPORT location_t ; |
| 36 |
FROM NameKey IMPORT Name ; |
| 37 |
|
| 38 |
|
| 39 |
CONST |
| 40 |
UnknownTokenNo = 0 ; |
| 41 |
BuiltinTokenNo = 1 ; |
| 42 |
|
| 43 |
VAR |
| 44 |
currenttoken : toktype ; |
| 45 |
currentstring : ADDRESS ; |
| 46 |
currentcolumn : CARDINAL ; |
| 47 |
currentinteger: INTEGER ; |
| 48 |
|
| 49 |
|
| 50 |
(* |
| 51 |
OpenSource - Attempts to open the source file, s. |
| 52 |
The success of the operation is returned. |
| 53 |
*) |
| 54 |
|
| 55 |
PROCEDURE OpenSource (s: String) : BOOLEAN ; |
| 56 |
|
| 57 |
|
| 58 |
(* |
| 59 |
CloseSource - closes the current open file. |
| 60 |
*) |
| 61 |
|
| 62 |
PROCEDURE CloseSource ; |
| 63 |
|
| 64 |
|
| 65 |
(* |
| 66 |
ReInitialize - re-initialize the all the data structures. |
| 67 |
*) |
| 68 |
|
| 69 |
PROCEDURE ReInitialize ; |
| 70 |
|
| 71 |
|
| 72 |
(* |
| 73 |
ResetForNewPass - reset the buffer pointers to the beginning ready for |
| 74 |
a new pass |
| 75 |
*) |
| 76 |
|
| 77 |
PROCEDURE ResetForNewPass ; |
| 78 |
|
| 79 |
|
| 80 |
(* |
| 81 |
GetToken - gets the next token into currenttoken. |
| 82 |
*) |
| 83 |
|
| 84 |
PROCEDURE GetToken ; |
| 85 |
|
| 86 |
|
| 87 |
(* |
| 88 |
InsertToken - inserts a symbol, token, infront of the current token |
| 89 |
ready for the next pass. |
| 90 |
*) |
| 91 |
|
| 92 |
PROCEDURE InsertToken (token: toktype) ; |
| 93 |
|
| 94 |
|
| 95 |
(* |
| 96 |
InsertTokenAndRewind - inserts a symbol, token, infront of the current token |
| 97 |
and then moves the token stream back onto the inserted token. |
| 98 |
*) |
| 99 |
|
| 100 |
PROCEDURE InsertTokenAndRewind (token: toktype) ; |
| 101 |
|
| 102 |
|
| 103 |
(* |
| 104 |
GetPreviousTokenLineNo - returns the line number of the previous token. |
| 105 |
*) |
| 106 |
|
| 107 |
PROCEDURE GetPreviousTokenLineNo () : CARDINAL ; |
| 108 |
|
| 109 |
|
| 110 |
(* |
| 111 |
GetLineNo - returns the current line number where the symbol occurs in |
| 112 |
the source file. |
| 113 |
*) |
| 114 |
|
| 115 |
PROCEDURE GetLineNo () : CARDINAL ; |
| 116 |
|
| 117 |
|
| 118 |
(* |
| 119 |
GetTokenNo - returns the current token number. |
| 120 |
*) |
| 121 |
|
| 122 |
PROCEDURE GetTokenNo () : CARDINAL ; |
| 123 |
|
| 124 |
|
| 125 |
(* |
| 126 |
GetTokenName - returns the token name given the tokenno. |
| 127 |
*) |
| 128 |
|
| 129 |
PROCEDURE GetTokenName (tokenno: CARDINAL) : Name ; |
| 130 |
|
| 131 |
|
| 132 |
(* |
| 133 |
TokenToLineNo - returns the line number of the current file for the |
| 134 |
tokenno. The depth refers to the include depth. |
| 135 |
A depth of 0 is the current file, depth of 1 is the file |
| 136 |
which included the current file. Zero is returned if the |
| 137 |
depth exceeds the file nesting level. |
| 138 |
*) |
| 139 |
|
| 140 |
PROCEDURE TokenToLineNo (tokenno: CARDINAL; depth: CARDINAL) : CARDINAL ; |
| 141 |
|
| 142 |
|
| 143 |
(* |
| 144 |
GetColumnNo - returns the current column where the symbol occurs in |
| 145 |
the source file. |
| 146 |
*) |
| 147 |
|
| 148 |
PROCEDURE GetColumnNo () : CARDINAL ; |
| 149 |
|
| 150 |
|
| 151 |
(* |
| 152 |
TokenToColumnNo - returns the column number of the current file for the |
| 153 |
tokenno. The depth refers to the include depth. |
| 154 |
A depth of 0 is the current file, depth of 1 is the file |
| 155 |
which included the current file. Zero is returned if the |
| 156 |
depth exceeds the file nesting level. |
| 157 |
*) |
| 158 |
|
| 159 |
PROCEDURE TokenToColumnNo (tokenno: CARDINAL; depth: CARDINAL) : CARDINAL ; |
| 160 |
|
| 161 |
|
| 162 |
(* |
| 163 |
TokenToLocation - returns the location_t corresponding to tokenno. |
| 164 |
*) |
| 165 |
|
| 166 |
PROCEDURE TokenToLocation (tokenno: CARDINAL) : location_t ; |
| 167 |
|
| 168 |
|
| 169 |
(* |
| 170 |
FindFileNameFromToken - returns the complete FileName for the appropriate |
| 171 |
source file yields the token number tokenno. |
| 172 |
The, Depth, indicates the include level: 0..n |
| 173 |
Level 0 is the current. NIL is returned if n+1 |
| 174 |
is requested. |
| 175 |
*) |
| 176 |
|
| 177 |
PROCEDURE FindFileNameFromToken (tokenno: CARDINAL; depth: CARDINAL) : String ; |
| 178 |
|
| 179 |
|
| 180 |
(* |
| 181 |
GetFileName - returns a String defining the current file. |
| 182 |
*) |
| 183 |
|
| 184 |
PROCEDURE GetFileName () : String ; |
| 185 |
|
| 186 |
|
| 187 |
(* |
| 188 |
MakeVirtualTok - providing caret, left, right are associated with a source file |
| 189 |
and exist on the same src line then |
| 190 |
create and return a new tokenno which is created from |
| 191 |
tokenno left and right. Otherwise return caret. |
| 192 |
If caret is UnknownTokenNo then it is replaced with left or right |
| 193 |
in sequence to avoid an UnknownTokenNo. |
| 194 |
*) |
| 195 |
|
| 196 |
PROCEDURE MakeVirtualTok (caret, left, right: CARDINAL) : CARDINAL ; |
| 197 |
|
| 198 |
|
| 199 |
(* |
| 200 |
MakeVirtual2Tok - creates and return a new tokenno which is created from |
| 201 |
two tokens left and right. It tries to avoid UnknownTokenNo |
| 202 |
and will fall back to left or right if necessary. |
| 203 |
*) |
| 204 |
|
| 205 |
PROCEDURE MakeVirtual2Tok (left, right: CARDINAL) : CARDINAL ; |
| 206 |
|
| 207 |
|
| 208 |
(* *********************************************************************** |
| 209 |
* |
| 210 |
* These functions allow m2.lex to deliver tokens into the buffer |
| 211 |
* |
| 212 |
************************************************************************* *) |
| 213 |
|
| 214 |
(* |
| 215 |
AddTok - adds a token to the buffer. |
| 216 |
*) |
| 217 |
|
| 218 |
PROCEDURE AddTok (t: toktype) ; |
| 219 |
|
| 220 |
|
| 221 |
(* |
| 222 |
AddTokCharStar - adds a token to the buffer and an additional string, s. |
| 223 |
A copy of string, s, is made. |
| 224 |
*) |
| 225 |
|
| 226 |
PROCEDURE AddTokCharStar (t: toktype; s: ADDRESS) ; |
| 227 |
|
| 228 |
|
| 229 |
(* |
| 230 |
AddTokInteger - adds a token and an integer to the buffer. |
| 231 |
*) |
| 232 |
|
| 233 |
PROCEDURE AddTokInteger (t: toktype; i: INTEGER) ; |
| 234 |
|
| 235 |
|
| 236 |
(* |
| 237 |
SetFile - sets the current filename to, filename. |
| 238 |
*) |
| 239 |
|
| 240 |
PROCEDURE SetFile (filename: ADDRESS) ; |
| 241 |
|
| 242 |
|
| 243 |
(* |
| 244 |
PushFile - indicates that, filename, has just been included. |
| 245 |
*) |
| 246 |
|
| 247 |
PROCEDURE PushFile (filename: ADDRESS) ; |
| 248 |
|
| 249 |
|
| 250 |
(* |
| 251 |
PopFile - indicates that we are returning to, filename, having finished |
| 252 |
an include. |
| 253 |
*) |
| 254 |
|
| 255 |
PROCEDURE PopFile (filename: ADDRESS) ; |
| 256 |
|
| 257 |
|
| 258 |
(* |
| 259 |
PrintTokenNo - displays token and the location of the token. |
| 260 |
*) |
| 261 |
|
| 262 |
PROCEDURE PrintTokenNo (tokenno: CARDINAL) ; |
| 263 |
|
| 264 |
|
| 265 |
(* |
| 266 |
DisplayToken - display the token name using printf0 no newline is emitted. |
| 267 |
*) |
| 268 |
|
| 269 |
PROCEDURE DisplayToken (tok: toktype) ; |
| 270 |
|
| 271 |
|
| 272 |
(* |
| 273 |
DumpTokens - displays all tokens. |
| 274 |
*) |
| 275 |
|
| 276 |
PROCEDURE DumpTokens ; |
| 277 |
|
| 278 |
|
| 279 |
END M2LexBuf. |