File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11#include " tokenizer.hpp"
2+ #include < iostream>
23
34static bool isWhitespace (char c)
45{
@@ -22,8 +23,9 @@ static bool isAlphanumeric(char c)
2223
2324void Tokenizer::skipWhitespace ()
2425{
25- for (char c = stream.get (); !stream.eof () && isWhitespace (c); c = stream.get ())
26+ for (char c = stream.peek (); !stream.eof () && isWhitespace (c); c = stream.peek ())
2627 {
28+ stream.get (); // We know it's whitespace, so we read the character
2729 if (c == ' \n ' || c == ' \r ' )
2830 line++;
2931 }
@@ -34,6 +36,8 @@ void Tokenizer::parseToken()
3436 skipWhitespace ();
3537
3638 char c = stream.get ();
39+ if (stream.eof ())
40+ return ;
3741
3842 std::string raw;
3943 raw += c;
@@ -90,27 +94,36 @@ void Tokenizer::parseToken()
9094 return ;
9195 }
9296
97+ std::cout << " (multi-char)" ;
98+
9399 // Multi-char tokens
94100
95101 if (c == ' "' )
96102 {
97103 // String
98104 for (c = stream.get (); raw.at (raw.size () - 1 ) != ' "' && raw.at (raw.size () - 2 ) != ' \\ ' ; c = stream.get ())
105+ {
99106 raw += c;
107+ }
100108
101109 token.type = TokenType::Literal;
102110 }
103111 else if (isNumber (c))
104112 {
113+ // Number
105114 for (c = stream.get (); !stream.eof () && isNumber (c); c = stream.get ())
115+ {
106116 raw += c;
117+ }
107118
108119 token.type = TokenType::Literal;
109120 }
110121 else
111122 {
112123 for (c = stream.get (); !stream.eof () && isAlphanumeric (c); c = stream.get ())
124+ {
113125 raw += c;
126+ }
114127 token.type = TokenType::Identifier;
115128 }
116129
Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ TEST(Tokenizer, identifiesSingleCharTokenTypes)
2020 EXPECT_EQ (tokens.size (), singleCharTypes.size ());
2121 for (int i = 0 ; i < tokens.size (); i++)
2222 {
23+ if (i >= singleCharTypes.size ())
24+ break ;
2325 EXPECT_EQ (tokens[i].type , singleCharTypes[i]);
2426 }
2527
You can’t perform that action at this time.
0 commit comments