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