-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.cpp
More file actions
165 lines (151 loc) · 5.71 KB
/
Copy pathtoken.cpp
File metadata and controls
165 lines (151 loc) · 5.71 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "token.hpp"
#include <array>
#include <utility>
namespace rs {
namespace {
// Anahtar kelime tablosu. SPEC §7.1'deki 29 + 4 operatör anahtar kelimesi.
// Sıralı tutuluyor ki gözle denetlenebilsin; arama doğrusal ama tablo 33 girişlik.
constexpr std::array<std::pair<std::string_view, Tok>, 33> KEYWORDS{{
{"and", Tok::KwAnd},
{"as", Tok::KwAs},
{"async", Tok::KwAsync},
{"await", Tok::KwAwait},
{"break", Tok::KwBreak},
{"catch", Tok::KwCatch},
{"class", Tok::KwClass},
{"continue", Tok::KwContinue},
{"elif", Tok::KwElif},
{"else", Tok::KwElse},
{"false", Tok::KwFalse},
{"finally", Tok::KwFinally},
{"fn", Tok::KwFn},
{"for", Tok::KwFor},
{"if", Tok::KwIf},
{"import", Tok::KwImport},
{"in", Tok::KwIn},
{"include", Tok::KwInclude},
{"is", Tok::KwIs},
{"nil", Tok::KwNil},
{"not", Tok::KwNot},
{"or", Tok::KwOr},
{"outer", Tok::KwOuter},
{"pass", Tok::KwPass},
{"return", Tok::KwReturn},
{"self", Tok::KwSelf},
{"super", Tok::KwSuper},
{"throw", Tok::KwThrow},
{"trait", Tok::KwTrait},
{"true", Tok::KwTrue},
{"try", Tok::KwTry},
{"view", Tok::KwView},
{"while", Tok::KwWhile},
}};
} // namespace
std::optional<Tok> keywordLookup(std::string_view s) noexcept {
for (const auto& [ad, tok] : KEYWORDS) {
if (ad == s) {
return tok;
}
}
return std::nullopt;
}
std::string_view tokName(Tok t) noexcept {
switch (t) {
case Tok::Newline: return "satır sonu";
case Tok::Indent: return "girinti";
case Tok::Dedent: return "girinti çıkışı";
case Tok::Eof: return "dosya sonu";
case Tok::Ident: return "tanımlayıcı";
case Tok::Int: return "tam sayı";
case Tok::Float: return "ondalık sayı";
case Tok::Str: return "metin";
case Tok::RawStr: return "ham metin";
case Tok::FStr: return "biçimli metin";
default: break;
}
const std::string_view lx = tokLexeme(t);
return lx.empty() ? "bilinmeyen" : lx;
}
std::string_view tokLexeme(Tok t) noexcept {
switch (t) {
case Tok::KwFn: return "fn";
case Tok::KwClass: return "class";
case Tok::KwTrait: return "trait";
case Tok::KwView: return "view";
case Tok::KwImport: return "import";
case Tok::KwInclude: return "include";
case Tok::KwAs: return "as";
case Tok::KwOuter: return "outer";
case Tok::KwIf: return "if";
case Tok::KwElif: return "elif";
case Tok::KwElse: return "else";
case Tok::KwWhile: return "while";
case Tok::KwFor: return "for";
case Tok::KwIn: return "in";
case Tok::KwBreak: return "break";
case Tok::KwContinue: return "continue";
case Tok::KwReturn: return "return";
case Tok::KwTry: return "try";
case Tok::KwCatch: return "catch";
case Tok::KwFinally: return "finally";
case Tok::KwThrow: return "throw";
case Tok::KwAsync: return "async";
case Tok::KwAwait: return "await";
case Tok::KwSelf: return "self";
case Tok::KwSuper: return "super";
case Tok::KwTrue: return "true";
case Tok::KwFalse: return "false";
case Tok::KwNil: return "nil";
case Tok::KwPass: return "pass";
case Tok::KwAnd: return "and";
case Tok::KwOr: return "or";
case Tok::KwNot: return "not";
case Tok::KwIs: return "is";
case Tok::Plus: return "+";
case Tok::Minus: return "-";
case Tok::Star: return "*";
case Tok::Slash: return "/";
case Tok::SlashSlash: return "//";
case Tok::Percent: return "%";
case Tok::StarStar: return "**";
case Tok::Assign: return "=";
case Tok::PlusEq: return "+=";
case Tok::MinusEq: return "-=";
case Tok::StarEq: return "*=";
case Tok::SlashEq: return "/=";
case Tok::SlashSlashEq: return "//=";
case Tok::PercentEq: return "%=";
case Tok::StarStarEq: return "**=";
case Tok::EqEq: return "==";
case Tok::BangEq: return "!=";
case Tok::Lt: return "<";
case Tok::Gt: return ">";
case Tok::LtEq: return "<=";
case Tok::GtEq: return ">=";
case Tok::Dot: return ".";
case Tok::DotDot: return "..";
case Tok::DotDotEq: return "..=";
case Tok::QuestionDot: return "?.";
case Tok::Question: return "?";
case Tok::Arrow: return "->";
case Tok::FatArrow: return "=>";
case Tok::Comma: return ",";
case Tok::Colon: return ":";
case Tok::Semicolon: return ";";
case Tok::At: return "@";
case Tok::Tilde: return "~";
case Tok::Amp: return "&";
case Tok::Pipe: return "|";
case Tok::Caret: return "^";
case Tok::LtLt: return "<<";
case Tok::GtGt: return ">>";
case Tok::LParen: return "(";
case Tok::RParen: return ")";
case Tok::LBracket: return "[";
case Tok::RBracket: return "]";
case Tok::LBrace: return "{";
case Tok::RBrace: return "}";
default: return {};
}
}
} // namespace rs