-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.h
More file actions
174 lines (137 loc) · 4.21 KB
/
parser.h
File metadata and controls
174 lines (137 loc) · 4.21 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
166
167
168
169
170
171
172
173
174
#pragma once
#include <string_view>
#include <string>
#include <optional>
#include <variant>
#include <vector>
#include <map>
#include "id.h"
#include "file_range.h"
#include "syntax_error.h"
#include "expression.h"
#include "syntax.h"
#include "namespace.h"
using std::string_literals::operator""s;
using std::string_view_literals::operator""sv;
using std::string_view;
using std::optional;
using std::pair;
using std::variant;
using std::map;
using std::vector;
struct Statement;
struct Proof;
[[nodiscard]] constexpr bool isKeywordChar(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
[[nodiscard]] constexpr bool isOperatorChar(char c) {
return (c >= '!' && c <= '\'') || (c >= '*' && c <= '/') || c == ':' || (c >= '<' && c <= '@') || (c >= '[' && c <= '^') || c == '`' || (c >= '{' && c <= '~');
}
[[nodiscard]] constexpr bool isWhitespaceChar(char c) {
return c == ' ' || c == '\r' || c == '\n' || c == '\t';
}
[[nodiscard]] constexpr bool isIdentifierChar(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
}
[[nodiscard]] constexpr bool isKeyword(string_view str) {
return
str == "scope"sv ||
str == "proves"sv ||
str == "by"sv ||
str == "in"sv ||
str == "substitute"sv ||
str == "unwrap"sv ||
str == "wrap"sv ||
str == "require"sv ||
str == "assume"sv ||
str == "atom"sv ||
str == "define"sv ||
str == "forany"sv;
}
struct Parser {
string_view fullStr;
string_view str;
unsigned int line = 1;
unsigned int col = 1;
constexpr FilePos currentFilePos() const noexcept {
return {
.index = (unsigned int)fullStr.size() - (unsigned int)str.size(),
.line = line,
.col = col,
};
}
void rewindTo(FilePos pos) noexcept {
str = fullStr.substr(pos.index);
line = pos.line;
col = pos.col;
}
constexpr bool isAtEnd() const noexcept { return str.size() == 0; }
constexpr Parser(string_view _str): fullStr(_str), str(_str) { }
// Skips whitespace, single-line comments and multi-line comments.
void skipWhitespace();
[[nodiscard]] constexpr bool areAtEnd() const {
return str.size() == 0;
}
[[nodiscard]] constexpr bool tryPeekChar(char c) const {
return str.size() != 0 && str[0] == c;
}
[[nodiscard]] constexpr bool tryReadChar(char c) {
if (str.size() != 0 && str[0] == c) {
str = str.substr(1);
col++;
return true;
} else {
return false;
}
}
constexpr void readChar(char c, string_view errorMessage) {
auto maybe = tryReadChar(c);
if (!maybe) throw SyntaxError(errorMessage, currentFilePos());
}
[[nodiscard]] constexpr bool tryReadChars(string_view chars) {
if (chars.size() > str.size()) return false;
if (str.substr(0, chars.size()) == chars) {
str = str.substr(chars.size());
col += chars.size();
return true;
}
return false;
}
[[nodiscard]] constexpr bool tryReadKeyword(string_view keyword) {
if (keyword.size() > str.size()) return false;
if (str.substr(0, keyword.size()) != keyword) return false;
if (keyword.size() == str.size() || !isIdentifierChar(str[keyword.size()])) {
str = str.substr(keyword.size());
col += keyword.size();
return true;
}
return false;
}
void readKeyword(string_view keyword, string_view errorMessage) {
auto maybe = tryReadKeyword(keyword);
if (!maybe) throw SyntaxError(errorMessage, currentFilePos());
}
long readInteger(string_view errorMessage);
[[nodiscard]] optional<string_view> tryReadIdentifier();
[[nodiscard]] inline string_view readIdentifier(string_view errorMessage) {
auto maybe = tryReadIdentifier();
if (maybe.has_value()) { return maybe.value(); }
else { throw SyntaxError(errorMessage, currentFilePos()); }
}
[[nodiscard]] vector<std::variant<Id, char, Space, pair<Id, Id>>> readSyntaxPieces(Namespace& ns);
void skipParenEnclosedStuff();
void skipToAndIncluding(char to);
[[nodiscard]] pair<vector<shared_ptr<const Statement>>, shared_ptr<const Proof>> readStatementsAndMaybeOneProof(
Namespace& ns
);
[[nodiscard]] shared_ptr<const Proof> readProof_level0(
Namespace& ns
);
[[nodiscard]] shared_ptr<const Proof> readProof(
Namespace& ns
);
[[nodiscard]] shared_ptr<const Expression> readExpression(
Namespace& ns,
bool stopBeforeComma
);
};