Add new lex() function to streamline lexing, change main to reflect this
This commit is contained in:
@@ -40,3 +40,5 @@ public:
|
|||||||
std::vector<Token> collect();
|
std::vector<Token> collect();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// when you don't want to construct the object
|
||||||
|
std::vector<Token> lex(std::string);
|
||||||
|
14
src/lex.cpp
14
src/lex.cpp
@@ -135,5 +135,19 @@ Token Lexer::next() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<Token> Lexer::collect() {
|
||||||
|
vector<Token> v;
|
||||||
|
while (true) {
|
||||||
|
Token t = next();
|
||||||
|
if (t.type == TokenType::End)
|
||||||
|
break;
|
||||||
|
|
||||||
|
v.push_back(t);
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Token> lex(std::string s) {
|
||||||
|
Lexer l(s);
|
||||||
|
return l.collect();
|
||||||
|
}
|
||||||
|
11
src/main.cpp
11
src/main.cpp
@@ -1,14 +1,17 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <lex.hpp>
|
#include <lex.hpp>
|
||||||
#include <sstream>
|
#include <string>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
string s;
|
string s;
|
||||||
cin >> s;
|
getline(cin, s);
|
||||||
|
cout << s << endl;
|
||||||
|
|
||||||
Lexer l(s);
|
for (auto t : lex(s)) {
|
||||||
cout << l.next() << endl;
|
cout << t << " ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Reference in New Issue
Block a user