summaryrefslogtreecommitdiffhomepage
path: root/minicc.cpp
blob: 9f5e90632d05e868e1ae26c00d905374061d60ab (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "bnf.h"
#include "lexer.h"
#include "grammer.h"
#include "minicc.h"

#include <boost/algorithm/string.hpp>

#include <algorithm>
#include <cctype>
#include <deque>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>

using namespace std::string_literals;

std::vector<std::string> split(std::string s)
{
 std::vector<std::string> result;
 boost::algorithm::split(result, s, boost::algorithm::is_any_of(s), boost::algorithm::token_compress_on);
 while (result.size() > 0 && result.back() == ""s)
  result.pop_back();
 return result;
}

bool operator==(const Location &a, const Location &b)
{
 return (a.line == b.line && a.column == b.column);
}

bool operator==(const Token &a, const Token &b)
{
 return (a.type == b.type && a.value == b.value && a.location == b.location);
}

std::ostream& operator<<(std::ostream& os, const Token& token) {
 return os << token.type << ": " << token.value << "(" << token.location.line << ":" << token.location.column << ")";
}

void Location::advance(bool newline)
{
 pos++;
 if (newline) {
  line++;
  column = 1;
 } else {
  column++;
 }
}

std::string Location::toString() const
{
 return std::to_string(line) + ":"s + std::to_string(column);
}

std::string Token::toString() const
{
 return location.toString() + ": "s + value + " ("s + type + ")"s;
}

std::string demangle(const std::type_info& type)
{
 return boost::core::demangle(type.name());
}