This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.cpp
More file actions
68 lines (57 loc) · 1.98 KB
/
Copy pathtoken.cpp
File metadata and controls
68 lines (57 loc) · 1.98 KB
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
67
68
#include "./token.h"
#include <iomanip>
#include <sstream>
using namespace std::literals;
TokenPtr Token::fromChar(char c) {
TokenType type;
switch (c) {
case '(': type = TokenType::LEFT_PAREN; break;
case ')': type = TokenType::RIGHT_PAREN; break;
case '\'': type = TokenType::QUOTE; break;
case '`': type = TokenType::QUASIQUOTE; break;
case ',': type = TokenType::UNQUOTE; break;
// DOT not listed here, because it can be part of identifier/literal.
default: return nullptr;
}
return TokenPtr(new Token(type));
}
TokenPtr Token::dot() {
return TokenPtr(new Token(TokenType::DOT));
}
std::string Token::toString() const {
switch (type) {
case TokenType::LEFT_PAREN: return "(LEFT_PAREN)"; break;
case TokenType::RIGHT_PAREN: return "(RIGHT_PAREN)"; break;
case TokenType::QUOTE: return "(QUOTE)"; break;
case TokenType::QUASIQUOTE: return "(QUASIQUOTE)"; break;
case TokenType::UNQUOTE: return "(UNQUOTE)"; break;
case TokenType::DOT: return "(DOT)"; break;
default: return "(UNKNOWN)";
}
}
std::unique_ptr<BooleanLiteralToken> BooleanLiteralToken::fromChar(char c) {
if (c == 't') {
return std::make_unique<BooleanLiteralToken>(true);
} else if (c == 'f') {
return std::make_unique<BooleanLiteralToken>(false);
} else {
return nullptr;
}
}
std::string BooleanLiteralToken::toString() const {
return "(BOOLEAN_LITERAL "s + (value ? "true" : "false") + ")";
}
std::string NumericLiteralToken::toString() const {
return "(NUMERIC_LITERAL " + std::to_string(value) + ")";
}
std::string StringLiteralToken::toString() const {
std::ostringstream ss;
ss << "(STRING_LITERAL " << std::quoted(value) << ")";
return ss.str();
}
std::string IdentifierToken::toString() const {
return "(IDENTIFIER " + name + ")";
}
std::ostream& operator<<(std::ostream& os, const Token& token) {
return os << token.toString();
}