|
| 1 | +// Template by Ali A. Kooshesh on 1/27/21. |
| 2 | +// Additional Code by Garret Mook on 02/06/21. |
| 3 | + |
| 4 | +//READER/LEXICAL ANALYZER |
| 5 | +//Implemenation Of Operations |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include "Tokenizer.hpp" |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | +// |
| 12 | +#include <cstdio> |
| 13 | +#include <cmath> |
| 14 | +#include <stdio.h> |
| 15 | +#include <ctype.h> |
| 16 | + |
| 17 | +//Initializer List |
| 18 | +Tokenizer::Tokenizer(std::string name): lineNumber{1}, |
| 19 | + charPosition{1}, |
| 20 | + inputFileName{name} { |
| 21 | + inputStream.open(inputFileName, std::ios::in); // open the input file. We will make sure that it is open in getToken. |
| 22 | +} |
| 23 | +//This defines what a Character of interest is to the reader. |
| 24 | +bool Tokenizer::charOfInterest(char c) { |
| 25 | + // is c the initial (or the sole) character of a token? |
| 26 | + return c == '<' || c == '>' || c == '/'; |
| 27 | +} |
| 28 | + |
| 29 | +Token Tokenizer::getToken() { |
| 30 | + char c; |
| 31 | + //Checks to see if the file we are trying to read is open |
| 32 | + if( ! inputStream.is_open()) { |
| 33 | + std::cout << "Tokenizer::getToken() called with a stream that is not open." << std::endl; |
| 34 | + std::cout << "Make sure that " << inputFileName << " exists and is readable. Terminating."; |
| 35 | + exit(2); |
| 36 | + } |
| 37 | + |
| 38 | + //GM: This while loop executes until we find a charOfInterest |
| 39 | + while( inputStream.get(c) && ! charOfInterest(c) ) { |
| 40 | + // keep track of the line number and the character position here. |
| 41 | + if (c == '\n') { |
| 42 | + lineNumber++; |
| 43 | + |
| 44 | + } |
| 45 | + else { |
| 46 | + charPosition++; |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + //GM: After loop stops this executes |
| 53 | + Token token(lineNumber, charPosition); |
| 54 | + |
| 55 | + if( inputStream.eof() ) { |
| 56 | + token.endOfFile() = true; |
| 57 | + } |
| 58 | +// if < is found |
| 59 | + else if( c == '<' && inputStream.peek() != '/' && inputStream.peek() != '>') { |
| 60 | + if( isalpha(inputStream.peek()) ) { |
| 61 | + charPosition++; |
| 62 | + std::string needthisforPeek; |
| 63 | + while (inputStream.get(c) && isalpha(c) && !isspace(c) && inputStream.peek() != '/') { |
| 64 | + needthisforPeek = needthisforPeek + c; |
| 65 | + charPosition++; |
| 66 | + } |
| 67 | + |
| 68 | + inputStream.putback(c); |
| 69 | + token.makeOpenTag(needthisforPeek); |
| 70 | + return token; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /* |
| 75 | + // if /> is found |
| 76 | + else if (c == '/' && inputStream.peek() == '>') { |
| 77 | + inputStream.get(c); |
| 78 | + token.isCloseStandAloneTag() = true; |
| 79 | + } |
| 80 | + */ |
| 81 | + |
| 82 | + |
| 83 | + else if (c == '/') { |
| 84 | + if (c == '/' && isalpha(inputStream.peek()) ) { |
| 85 | + token.isSlash() = true; |
| 86 | + charPosition++; |
| 87 | + |
| 88 | + } else if (c == '/' && inputStream.peek() == '>') { |
| 89 | + inputStream.get(c); |
| 90 | + token.isCloseStandAloneTag() = true; |
| 91 | + charPosition++; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +// if > is found |
| 104 | + else if( c == '>' ) { |
| 105 | + token.isCloseAngleBracket() = true; |
| 106 | + charPosition++; |
| 107 | + } |
| 108 | + // if <tagname is found then it is NOT a closed standalone tag |
| 109 | + else if (isalpha(inputStream.peek())) { |
| 110 | + token.isCloseStandAloneTag() = false; |
| 111 | + charPosition++; |
| 112 | + } |
| 113 | + |
| 114 | + //IF </ is found |
| 115 | + else if ( inputStream.good() && c == '<' && inputStream.peek() == '/') { |
| 116 | + inputStream.get(c); |
| 117 | + charPosition++; |
| 118 | + std::string neededToReadPeek; |
| 119 | + if (c== '/' && isalpha(inputStream.peek())) { |
| 120 | + while (inputStream.good() && inputStream.get(c) && isalpha(c)) { |
| 121 | + neededToReadPeek += c; |
| 122 | + charPosition++; |
| 123 | + } |
| 124 | + inputStream.putback(c); |
| 125 | + token.makeCloseTag(neededToReadPeek); |
| 126 | + charPosition++; |
| 127 | + return token; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + else { |
| 132 | + std::cout << "Unknown Character: " << c; |
| 133 | + exit(1); |
| 134 | +} |
| 135 | + |
| 136 | + return token; |
| 137 | + |
| 138 | +} |
0 commit comments