aboutsummaryrefslogtreecommitdiffstats
path: root/parser.cpp
blob: 828b574b35d70ec440d96650687a251187c2a685 (plain)
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
#include "parser.h"
#include <iostream>
#include <string>
#include <vector>
#include "token.h"

Parser::Parser(std::string& input) {
    std::vector<Token> result;

    bool isString = false;
    std::string tmp;
    for (auto& c : input) {
        if (std::isspace(c)) {
            continue;
        }

        if (c == '"' && !isString) {
            isString = true;
            continue;
        }

        if (c == '"' && isString) {
            Token token(tmp, true);
            result.push_back(token);
            tmp = "";
            isString = false;
            continue;
        }

        if (!isString) {
            std::string parsed(1, c);
            Token token(parsed, false);
            result.push_back(token);
        }
        else {
            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;
}