-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.cpp
86 lines (80 loc) · 2.04 KB
/
lex.cpp
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
//
// Created by alexwang on 11/24/17.
//
#include "lex.h"
bool al::Lexer::parseQuoteString(std::string &str, std::string eos) {
string content;
bool escaping = false;
while (!input.empty()) {
string cp = nextUtf8CodePoint();
if (escaping) {
escaping = false;
if (cp == "n") {
content += "\n";
}
else {
content += cp;
}
continue;
}
if (cp == "\\") {
escaping = true;
continue;
}
if (cp == eos) {
str = content;
return true;
}
content += cp;
}
str = content;
return false;
}
al::Parser::symbol_type al::Lexer::lex() {
string regs[] = {
"\\s+",
"\\(",
R"(\))",
"'",
R"(\w(\w|\d|[-_+=?!@#$%^&*])*)",
"\\d+",
};
std::function<Parser::symbol_type (const std::string &s)> fns[] = {
nullptr,
[](const std::string &s) -> Parser::symbol_type {
return Parser::make_LEFTPAR(Parser::location_type());
},
[](const std::string &s) -> Parser::symbol_type {
return Parser::make_RIGHTPAR(Parser::location_type());
},
[this](const std::string &s) -> Parser::symbol_type {
std::string result;
if (!this->parseQuoteString(result, "'"))
throw "failed to parse quote string";
auto p = std::make_shared<ast::StringLiteral>(result);
return Parser::make_STRING(p, Parser::location_type());
},
[](const std::string &s) -> Parser::symbol_type {
auto p = std::make_shared<ast::Symbol>(s);
return Parser::make_SYMBOL(p, Parser::location_type());
},
[](const std::string &s) -> Parser::symbol_type {
return Parser::make_INT(s, Parser::location_type());
},
};
if (input.empty()) {
return al::Parser::make_END(Parser::location_type());
}
int i = 0;
for (const auto ®: regs) {
string var;
int value;
if (RE2::Consume(&input, "(" + reg + ")", &var)) {
// FIXME: i == 0 for blank characters
if (i != 0)
return fns[i](var);
}
i++;
}
throw "wtf";
}