|
| 1 | +package lexer; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.StringTokenizer; |
| 6 | + |
| 7 | +import lexer.Token.TokenType; |
| 8 | + |
| 9 | +/** |
| 10 | + * Wrapper class that is used to pull a text file from hard disk into memory. The TextFile class |
| 11 | + * gives functionality to that text file in memory so that it can be accessed and written to. |
| 12 | + * |
| 13 | + * @author Michael Pinnegar |
| 14 | + * |
| 15 | + */ |
| 16 | +public class TextFile { |
| 17 | + |
| 18 | + |
| 19 | + /** The main storage body for each string that represents the text file in memory*/ |
| 20 | + private ArrayList<String> body; |
| 21 | + /** The horizontal depth into a string of the body array*/ |
| 22 | + private int posPointer; |
| 23 | + /** The vertical depth down the rows of strings in the body array*/ |
| 24 | + private int rowPointer; |
| 25 | + |
| 26 | + |
| 27 | + /** |
| 28 | + * Returns the line number that is currently ready to be accessed. |
| 29 | + * @return line number |
| 30 | + */ |
| 31 | + public Integer getReport() |
| 32 | + { |
| 33 | + return rowPointer; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructor for TextFile |
| 38 | + * |
| 39 | + * @param inputFilename Name of the file to be wrapped by the TextFile class |
| 40 | + * @throws IOException Unable to access file |
| 41 | + */ |
| 42 | + public TextFile(String inputFilename) throws IOException |
| 43 | + { |
| 44 | + body = new ArrayList<String>(); |
| 45 | + posPointer = 0; |
| 46 | + rowPointer = 0; |
| 47 | + |
| 48 | + BufferedReader in = new BufferedReader(new FileReader(inputFilename)); |
| 49 | + |
| 50 | + while (in.ready()) //While not end of file, take in another line |
| 51 | + { |
| 52 | + String currentInput = in.readLine(); |
| 53 | + body.add(currentInput); |
| 54 | + } |
| 55 | + |
| 56 | + in.close(); |
| 57 | + } |
| 58 | + /** |
| 59 | + * Constructs an empty textFile |
| 60 | + */ |
| 61 | + public TextFile() |
| 62 | + { |
| 63 | + body = new ArrayList<String>(); |
| 64 | + posPointer = 0; |
| 65 | + rowPointer = 0; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Removes all comments from a textFile object. |
| 70 | + */ |
| 71 | + public void stripCommentsAndWhiteSpace() |
| 72 | + { |
| 73 | + ArrayList<String> tmpBody = new ArrayList<String>(); |
| 74 | + |
| 75 | + int line = 0; |
| 76 | + while(tmpBody.size() < body.size()) |
| 77 | + { |
| 78 | + String original = body.get(line); |
| 79 | + int pos = original.indexOf('#'); |
| 80 | + |
| 81 | + if (pos > -1)//a comment exists in the original line |
| 82 | + { |
| 83 | + original = original.substring(0, pos); //Remove the comment and everything after it |
| 84 | + } |
| 85 | + |
| 86 | + original = original.trim(); //Remove whitespace |
| 87 | + if(original.isEmpty()) //Tack on a space if the line is empty |
| 88 | + { |
| 89 | + original += " "; |
| 90 | + } |
| 91 | + tmpBody.add(original); |
| 92 | + line++; |
| 93 | + } |
| 94 | + body = tmpBody; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Inserts a line of text at the specified row |
| 99 | + * @param row specified row |
| 100 | + * @param input line of text |
| 101 | + */ |
| 102 | + public void insertLine(int row, String input) |
| 103 | + { |
| 104 | + body.add(row, input); |
| 105 | + rowPointer += 1; |
| 106 | + } |
| 107 | + |
| 108 | + public String getCharacter() |
| 109 | + { |
| 110 | + String character; |
| 111 | + while(!isEndOfFile() && posPointer > (body.get(rowPointer).length()-1)) |
| 112 | + { |
| 113 | + rowPointer++; |
| 114 | + posPointer = 0; |
| 115 | + } |
| 116 | + |
| 117 | + if(!isEndOfFile()) |
| 118 | + { |
| 119 | + posPointer++; |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + if(!isEndOfFile()) |
| 124 | + { |
| 125 | + posPointer--; |
| 126 | + character = body.get(rowPointer).substring(posPointer, posPointer+1); |
| 127 | + posPointer++; |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + character = ""; |
| 132 | + } |
| 133 | + |
| 134 | + return character; |
| 135 | + } |
| 136 | + |
| 137 | + public Token getToken() |
| 138 | + { |
| 139 | + String rVal = body.get(rowPointer); //Get the string out of the array located at the rowPointer |
| 140 | + rVal = rVal.substring(posPointer); //Get the portion of the string to the right of the posPointer |
| 141 | + for(int i = rowPointer+1; i < body.size(); i++) |
| 142 | + rVal = rVal + body.get(i); |
| 143 | + |
| 144 | + String returnTokText = null; |
| 145 | + TokenType returnTokType = null; |
| 146 | + StringTokenizer st = new StringTokenizer(rVal," \t;(){}=!<>+-/*%",true); |
| 147 | + String temp = st.nextToken(); |
| 148 | + String lookAhead = null; |
| 149 | + |
| 150 | + if(st.hasMoreTokens()) //Grab the lookahead token if there is one left |
| 151 | + { |
| 152 | + lookAhead = st.nextToken(); |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + if(TokenType.matchesToken(TokenType.WS, temp)) |
| 157 | + { |
| 158 | + returnTokText = temp; |
| 159 | + returnTokType = TokenType.WS; |
| 160 | + } |
| 161 | + else if(TokenType.matchesToken(TokenType.KEYWORD, temp)) |
| 162 | + {//Keywords |
| 163 | + returnTokText = temp; |
| 164 | + returnTokType = TokenType.KEYWORD; |
| 165 | + } |
| 166 | + else if(TokenType.matchesToken(TokenType.OPERATOR, temp)) |
| 167 | + { |
| 168 | + returnTokText = temp; |
| 169 | + returnTokType = TokenType.OPERATOR; |
| 170 | + } |
| 171 | + else if(TokenType.matchesToken(TokenType.GAMEFUNCTION, temp)) |
| 172 | + { |
| 173 | + returnTokText = temp; |
| 174 | + returnTokType = TokenType.GAMEFUNCTION; |
| 175 | + } |
| 176 | + else if(TokenType.matchesToken(TokenType.GAMEORDER, temp)) |
| 177 | + { |
| 178 | + returnTokText = temp; |
| 179 | + returnTokType = TokenType.GAMEORDER; |
| 180 | + } |
| 181 | + else if(TokenType.matchesToken(TokenType.SYMBOL, temp)) |
| 182 | + { |
| 183 | + if(temp.matches("=") && lookAhead == null) //This is the last token, so it must just be the = symbol |
| 184 | + { |
| 185 | + returnTokText = temp; |
| 186 | + returnTokType = TokenType.SYMBOL; |
| 187 | + } |
| 188 | + else if(temp.matches("=") && lookAhead != null) //This is not the last token, so something must come after it |
| 189 | + { |
| 190 | + if(lookAhead.matches("="))//Check to see if you actually found == instead of just = |
| 191 | + { |
| 192 | + returnTokText = temp.concat(lookAhead); |
| 193 | + returnTokType = TokenType.SYMBOL; |
| 194 | + } |
| 195 | + else //Wasn't ==, so it must just be the symbol operator for assignment |
| 196 | + { |
| 197 | + returnTokText = temp; |
| 198 | + returnTokType = TokenType.SYMBOL; |
| 199 | + } |
| 200 | + } |
| 201 | + else //Is one of the other symbols in the symbol type that is only one length long |
| 202 | + { |
| 203 | + returnTokText = temp; |
| 204 | + returnTokType = TokenType.SYMBOL; |
| 205 | + } |
| 206 | + |
| 207 | + } |
| 208 | + else if(temp.matches("!") ||temp.matches("<") ||temp.matches(">")) //Must be a condition |
| 209 | + { |
| 210 | + if(temp.matches("!"))//Must have = after it, or it's an error |
| 211 | + { |
| 212 | + if(lookAhead.matches("=")) |
| 213 | + { |
| 214 | + returnTokText = temp + lookAhead; |
| 215 | + returnTokType = TokenType.CONDITION; |
| 216 | + } |
| 217 | + else //Is error, no token can begin with ! and not have = after it |
| 218 | + { |
| 219 | + returnTokText = temp; |
| 220 | + returnTokType = TokenType.ERROR; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + if(temp.matches("<") || temp.matches(">")) |
| 225 | + { |
| 226 | + if(lookAhead == null || !lookAhead.matches("=")) |
| 227 | + { |
| 228 | + returnTokText = temp; |
| 229 | + returnTokType = TokenType.CONDITION; |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + returnTokText = temp + lookAhead; |
| 234 | + returnTokType = TokenType.CONDITION; |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + else if(TokenType.matchesToken(TokenType.DIGITS, temp)) //If it is a series of digits only |
| 239 | + { |
| 240 | + returnTokText = temp; |
| 241 | + returnTokType = TokenType.DIGITS; |
| 242 | + } |
| 243 | + else if(TokenType.matchesToken(TokenType.IDENTIFIER, temp)) //If it is a properly formed identifier |
| 244 | + { |
| 245 | + returnTokText = temp; |
| 246 | + returnTokType = TokenType.IDENTIFIER; |
| 247 | + } |
| 248 | + else //If it doesn't fit any of the above classifications, it must be an error |
| 249 | + { |
| 250 | + returnTokText = temp; |
| 251 | + returnTokType = TokenType.ERROR; |
| 252 | + } |
| 253 | + |
| 254 | + posPointer += returnTokText.length(); |
| 255 | + |
| 256 | + while(body.get(rowPointer).length() < posPointer) //While the pos pointer is greater than the size of the current row, take out the pos pointer |
| 257 | + { |
| 258 | + posPointer -= body.get(rowPointer).length(); |
| 259 | + rowPointer++; |
| 260 | + } |
| 261 | +// System.out.println("ROW:"+rowPointer); |
| 262 | +// System.out.println("POS:"+posPointer); |
| 263 | + return new Token(returnTokType, returnTokText); |
| 264 | + |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * Returns the remaining text from the pointer's position to the end of the line. Increments the pointer to the next line afterwards. |
| 269 | + * |
| 270 | + * @return Returns the remaining text to the end of the line without any carriage return. |
| 271 | + */ |
| 272 | + public String getLine() |
| 273 | + { |
| 274 | + String rVal = body.get(rowPointer); //Get the string out of the array located at the rowPointer |
| 275 | + rVal = rVal.substring(posPointer); //Get the portion of the string to the right of the posPointer |
| 276 | + |
| 277 | + rowPointer++; |
| 278 | + posPointer = 0; |
| 279 | + |
| 280 | + return rVal; |
| 281 | + } |
| 282 | + |
| 283 | + |
| 284 | + /** |
| 285 | + * Writes the textfile to disk, line by line, starting from the first line regardless of the pointer's position. |
| 286 | + * @param outputFilename Name given to the file to be written to disk |
| 287 | + * @throws IOException Can not write file to disk |
| 288 | + */ |
| 289 | + public void write(String outputFilename) throws IOException |
| 290 | + { |
| 291 | + int i = 0; |
| 292 | + |
| 293 | + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFilename))); |
| 294 | + while(i < body.size()) //Print out each line in the body representation of the text file |
| 295 | + { |
| 296 | + out.println(body.get(i)); |
| 297 | + i++; |
| 298 | + } |
| 299 | + out.close(); |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Inserts a line of text at the specified line in the textfile. Bumps the line of text that was there down one line. |
| 304 | + * @param entry Should not be longer than the number of rows in the textfile |
| 305 | + */ |
| 306 | + public void input(String entry) |
| 307 | + { |
| 308 | + body.add(entry); |
| 309 | + } |
| 310 | + |
| 311 | + /** |
| 312 | + * Prints the textfile line by line to the console. |
| 313 | + */ |
| 314 | + public void display() |
| 315 | + { |
| 316 | + int i = 0; |
| 317 | + |
| 318 | + while(i < body.size()) |
| 319 | + { |
| 320 | + System.out.println(body.get(i)); |
| 321 | + i++; |
| 322 | + } |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * Moves the pointer back to the very beginning of the textfile |
| 327 | + */ |
| 328 | + public void reset() |
| 329 | + { |
| 330 | + posPointer = 0; |
| 331 | + rowPointer = 0; |
| 332 | + } |
| 333 | + |
| 334 | + /** |
| 335 | + * Checks to see if the pointer is pointing at the end of file |
| 336 | + * @return True if the pointer is at the last line, last spot of the file |
| 337 | + */ |
| 338 | + public boolean isEndOfFile() |
| 339 | + { |
| 340 | + if (body.size() == 0 || (body.size() == 1 && body.get(0).equals(""))) //If file is empty, you're automatically at EoF |
| 341 | + { |
| 342 | + return true; |
| 343 | + } |
| 344 | + else if(body.size() - 1 == rowPointer && body.get(rowPointer).length() == posPointer) //If the file isn't empty, is the horizontal marker too far? |
| 345 | + { |
| 346 | + return true; |
| 347 | + } |
| 348 | + else //If the file isn't empty, and the marker isn't at the last spot in the file, then you must not be at EoF |
| 349 | + { |
| 350 | + return false; |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | +} |
0 commit comments