78 lines
1.9 KiB
C++
78 lines
1.9 KiB
C++
#include "scan.h"
|
|
|
|
using namespace std;
|
|
|
|
vector<Token> scan(string s) {
|
|
size_t pt = 0;
|
|
int line = 1;
|
|
vector<Token> tokens;
|
|
auto skipSpace = [&]() {
|
|
while (pt < s.size() && isspace(s[pt])) {
|
|
if (s[pt] == '\n') {
|
|
line++;
|
|
}
|
|
pt++;
|
|
}
|
|
};
|
|
for (skipSpace(); pt < s.size(); skipSpace()) {
|
|
TokenType type;
|
|
string t;
|
|
if (s[pt] == ',') {
|
|
type = TokenType::COMMA, pt++;
|
|
} else if (s[pt] == ';') {
|
|
type = TokenType::SEMI, pt++;
|
|
} else if (s[pt] == '{') {
|
|
type = TokenType::LB, pt++;
|
|
} else if (s[pt] == '}') {
|
|
type = TokenType::RB, pt++;
|
|
} else if (s[pt] == '(') {
|
|
type = TokenType::LP, pt++;
|
|
} else if (s[pt] == ')') {
|
|
type = TokenType::RP, pt++;
|
|
} else if (s[pt] == '<') {
|
|
type = TokenType::LT, pt++;
|
|
} else if (s[pt] == '>') {
|
|
type = TokenType::RT, pt++;
|
|
} else if (s[pt] == '=') {
|
|
type = TokenType::ASSIGN, pt++;
|
|
} else if (s[pt] == '.') {
|
|
type = TokenType::DOT, pt++;
|
|
} else if (s[pt] == ':') {
|
|
if (pt + 1 < s.size() && s[pt + 1] == ':') {
|
|
type = TokenType::SCOPE, pt += 2;
|
|
} else {
|
|
type = TokenType::COLON, pt++;
|
|
}
|
|
} else if (s[pt] == '-') {
|
|
if (pt + 1 < s.size() && s[pt + 1] == '>') {
|
|
type = TokenType::IMPLY, pt += 2;
|
|
} else {
|
|
printf("error on line %d", line), exit(1);
|
|
}
|
|
} else if (isalpha(s[pt]) || s[pt] == '_') {
|
|
size_t r = pt + 1;
|
|
while (r < s.size() && (isalpha(s[r]) || s[r] == '_' || isdigit(s[r]))) {
|
|
r++;
|
|
}
|
|
t = s.substr(pt, r - pt);
|
|
if (t == "struct") {
|
|
type = TokenType::STRUCT, t.clear();
|
|
} else if (t == "Fn") {
|
|
type = TokenType::FN, t.clear();
|
|
} else if (t == "return") {
|
|
type = TokenType::RETURN, t.clear();
|
|
} else if (t == "typeof") {
|
|
type = TokenType::TYPEOF, t.clear();
|
|
} else if (t == "public") {
|
|
type = TokenType::PUBLIC, t.clear();
|
|
} else {
|
|
type = TokenType::ID;
|
|
}
|
|
pt = r;
|
|
} else {
|
|
printf("error on line %d", line), exit(1);
|
|
}
|
|
tokens.push_back({line, type, t});
|
|
}
|
|
return tokens;
|
|
} |