diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2016-08-31 19:15:13 +0000 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2016-08-31 19:15:13 +0000 |
| commit | b7eeb6676b8f67be7cc1560d9586f2672a5ad0eb (patch) | |
| tree | 651f5c1dd84041b076b20ca2f4370ba6999b3d5f | |
| parent | 251a9af5e8c4976373b71935457d1ce17c6888a1 (diff) | |
Print parsed tokens
| -rw-r--r-- | main.cpp | 3 | ||||
| -rw-r--r-- | parser.cpp | 18 | ||||
| -rw-r--r-- | parser.h | 6 | ||||
| -rw-r--r-- | token.cpp | 3 | ||||
| -rw-r--r-- | token.h | 1 |
5 files changed, 31 insertions, 0 deletions
@@ -21,5 +21,8 @@ int main(int argc, char** argv) { std::string input = readFile(filename); std::cout << input; + Parser parser(input); + parser.printTokens(); + return 0; } @@ -1,4 +1,5 @@ #include "parser.h" +#include <iostream> #include <string> #include <vector> #include "token.h" @@ -9,6 +10,10 @@ Parser::Parser(std::string& input) { bool isString = false; std::string tmp; for (auto& c : input) { + if (std::isspace(c)) { + continue; + } + if (c == '"' && !isString) { isString = true; continue; @@ -31,4 +36,17 @@ Parser::Parser(std::string& input) { tmp += c; } } + + m_tokens = result; +} + +std::vector<Token>& Parser::getTokens() { + return m_tokens; +} + +void Parser::printTokens() { + for (auto& token : getTokens()) { + std::cout << "Token[" << token.getLexeme() << "] "; + } + std::cout << std::endl; } @@ -1,7 +1,13 @@ #pragma once #include <string> +#include "token.h" +#include <vector> class Parser { + private: + std::vector<Token> m_tokens; public: Parser(std::string&); + std::vector<Token>& getTokens(); + void printTokens(); }; @@ -5,3 +5,6 @@ Token::Token(std::string& lexeme, bool isString) : } +const std::string& Token::getLexeme() const { + return m_lexeme; +} @@ -8,4 +8,5 @@ class Token { public: Token(std::string& lexeme, bool isString); + const std::string& getLexeme() const; }; |
