Webserv
Loading...
Searching...
No Matches
Token.cpp
Go to the documentation of this file.
1#include "webserv.hpp"
2
3Tokenizer::Tokenizer(string fileName) {
4 file.open(fileName.c_str(), std::ios::in);
5 if (!file.good())
6 cout << "File open error"
7 << "\n";
8}
9bool Tokenizer::hasMoreTokens() { return !file.eof(); }
10
12 char c = ' ';
13 while ((c == ' ' || c == '\n') || c == '\t') {
14 file.get(c); // check
15
16 if ((c == ' ' || c == '\n') && !file.good()) {
17 // cout << file.eof() << " " << file.fail() << "\n";
18 throw std::logic_error("Ran out of tokens");
19 } else if (!file.good()) {
20 return c;
21 }
22 }
23
24 return c;
25}
27 if (file.eof())
28 file.clear();
29 file.seekg(prevPos);
30}
32 char c;
33 if (file.eof()) {
34 cout << "Exhaused tokens"
35 << "\n";
36 // throw std::exception("Exhausted tokens");
37 }
38 prevPos = file.tellg();
40
41 Token token;
42 token.type = NULL_TYPE;
43 if (c == '"') {
44 token.type = STRING;
45 token.value = "";
46 file.get(c);
47 while (c != '"') {
48 token.value += c;
49 file.get(c);
50 }
51 } else if (c == '{') {
52 token.type = CURLY_OPEN;
53 } else if (c == '}') {
54 token.type = CURLY_CLOSE;
55 } else if (c == '-' || (c >= '0' && c <= '9')) {
56 // Check if string is numeric
57 token.type = NUMBER;
58 token.value = "";
59 token.value += c;
60 std::streampos prevCharPos = file.tellg();
61 while ((c == '-') || (c >= '0' && c <= '9') || c == '.') {
62 prevCharPos = file.tellg();
63 file.get(c);
64
65 if (file.eof()) {
66 break;
67 } else {
68 if ((c == '-') || (c >= '0' && c <= '9') || (c == '.')) {
69 token.value += c;
70 } else {
71 file.seekg(prevCharPos);
72 }
73 }
74 }
75 } else if (c == 'f') {
76 token.type = BOOLEAN;
77 token.value = "False";
78 file.seekg(4, std::ios_base::cur);
79 } else if (c == 't') {
80 token.type = BOOLEAN;
81 token.value = "True";
82 file.seekg(3, std::ios_base::cur);
83 } else if (c == 'n') {
84 token.type = NULL_TYPE;
85 file.seekg(3, std::ios_base::cur);
86 } else if (c == '[') {
87 token.type = ARRAY_OPEN;
88 } else if (c == ']') {
89 token.type = ARRAY_CLOSE;
90 } else if (c == ':') {
91 token.type = COLON;
92 } else if (c == ',') {
93 token.type = COMMA;
94 }
95 // cout << token.type << " : " << token.value << "\n";
96 return token;
97}
@ NUMBER
Definition: Token.hpp:9
@ COLON
Definition: Token.hpp:7
@ CURLY_OPEN
Definition: Token.hpp:5
@ ARRAY_OPEN
Definition: Token.hpp:10
@ NULL_TYPE
Definition: Token.hpp:14
@ BOOLEAN
Definition: Token.hpp:13
@ ARRAY_CLOSE
Definition: Token.hpp:11
@ CURLY_CLOSE
Definition: Token.hpp:6
@ STRING
Definition: Token.hpp:8
@ COMMA
Definition: Token.hpp:12
Tokenizer(string fileName)
Definition: Token.cpp:3
void rollBackToken()
Definition: Token.cpp:26
size_t prevPos
Definition: Token.hpp:24
std::fstream file
Definition: Token.hpp:23
char getWithoutWhiteSpace()
Definition: Token.cpp:11
bool hasMoreTokens()
Definition: Token.cpp:9
Token getToken()
Definition: Token.cpp:31
string value
Definition: Token.hpp:18
TOKEN type
Definition: Token.hpp:19