载入中...
搜索中...
未找到
ScriptStructure.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cctype>
4#include <map>
5#include <set>
6#include <stdexcept>
7
8namespace eve::script {
9namespace {
10struct Token {
11 std::string kind, text;
12 unsigned line = 1, column = 1;
13 size_t begin = 0, end = 0;
14};
15struct Signature {
16 std::string name;
17 size_t begin, end;
18};
19struct Parser {
20 std::string_view source;
21 std::vector<Token> tokens;
22 std::map<size_t, size_t> pairs;
23 ScriptStructure out;
24 std::map<size_t, ScriptCallSyntax> calls;
25 std::vector<Signature> signatures;
26
27 void lex() {
28 unsigned line = 1, column = 1;
29 size_t i = 0;
30 auto advance = [&]() {
31 if (source[i++] == '\n') {
32 ++line;
33 column = 1;
34 } else
35 ++column;
36 };
37 while (i < source.size()) {
38 const auto c = static_cast<unsigned char>(source[i]);
39 if (std::isspace(c)) {
40 advance();
41 continue;
42 }
43 if (source.substr(i, 2) == "//") {
44 while (i < source.size() && source[i] != '\n') advance();
45 continue;
46 }
47 if (source.substr(i, 2) == "/*") {
48 advance();
49 advance();
50 while (i < source.size() && source.substr(i, 2) != "*/") advance();
51 if (i == source.size()) throw std::runtime_error("Unterminated block comment");
52 advance();
53 advance();
54 continue;
55 }
56 Token token{"punctuation", "", line, column, i, i};
57 bool verbatim = source[i] == '@' && i + 1 < source.size() && source[i + 1] == '"';
58 if (source[i] == '"' || source[i] == '\'' || verbatim) {
59 token.kind = "string";
60 if (verbatim) advance();
61 char quote = source[i];
62 advance();
63 bool closed = false;
64 while (i < source.size()) {
65 char value = source[i];
66 advance();
67 if (value == quote) {
68 if (verbatim && i < source.size() && source[i] == quote) {
69 advance();
70 token.text += quote;
71 continue;
72 }
73 closed = true;
74 break;
75 }
76 if (!verbatim && value == '\\') {
77 if (i == source.size()) break;
78 value = source[i];
79 advance();
80 if (value == 'n')
81 value = '\n';
82 else if (value == 'r')
83 value = '\r';
84 else if (value == 't')
85 value = '\t';
86 else if (value != '\\' && value != '"' && value != '\'')
87 token.kind = "dynamic";
88 }
89 token.text += value;
90 }
91 if (!closed) throw std::runtime_error("Unterminated string");
92 } else if (std::isalpha(c) || c == '_') {
93 token.kind = "identifier";
94 while (i < source.size() && (std::isalnum(static_cast<unsigned char>(source[i])) || source[i] == '_')) {
95 token.text += source[i];
96 advance();
97 }
98 } else if (std::isdigit(c)) {
99 token.kind = "number";
100 while (i < source.size() && (std::isalnum(static_cast<unsigned char>(source[i])) || source[i] == '.')) {
101 token.text += source[i];
102 advance();
103 }
104 } else {
105 token.text += source[i];
106 advance();
107 if (i < source.size() && (token.text + source[i] == "<-" || token.text + source[i] == "==" ||
108 token.text + source[i] == "!=" || token.text + source[i] == "::" ||
109 token.text + source[i] == "<=" || token.text + source[i] == ">=")) {
110 token.text += source[i];
111 advance();
112 }
113 }
114 token.end = i;
115 tokens.push_back(std::move(token));
116 if (tokens.size() > 65536) throw std::runtime_error("Script token budget exceeded");
117 }
118 std::vector<size_t> stack;
119 for (size_t t = 0; t < tokens.size(); ++t) {
120 auto text = tokens[t].text;
121 if (tokens[t].kind != "punctuation") continue;
122 if (text == "(" || text == "[" || text == "{") {
123 stack.push_back(t);
124 if (stack.size() > 64) throw std::runtime_error("Script nesting budget exceeded");
125 } else if (text == ")" || text == "]" || text == "}") {
126 if (stack.empty()) throw std::runtime_error("Unbalanced script delimiter");
127 auto open = tokens[stack.back()].text;
128 if ((open == "(" && text != ")") || (open == "[" && text != "]") || (open == "{" && text != "}"))
129 throw std::runtime_error("Mismatched script delimiter");
130 pairs[stack.back()] = t;
131 stack.pop_back();
132 }
133 }
134 if (!stack.empty()) throw std::runtime_error("Unbalanced script delimiter");
135 }
136 ScriptExpression atom(size_t t, std::string kind = {}) const {
137 const auto& token = tokens[t];
138 return {kind.empty() ? token.kind : kind, token.text, {}, token.line, token.column, token.begin, token.end};
139 }
140 std::string scope(size_t offset) const {
141 const ScriptFunctionSyntax* owner = nullptr;
142 for (const auto& function : out.functions)
144 owner = &function;
145 if (owner) return owner->name;
146 for (const auto& signature : signatures)
147 if (signature.begin <= offset && offset < signature.end) return signature.name;
148 return classScope(offset);
149 }
150 std::string classScope(size_t offset) const {
151 const ScriptClassSyntax* owner = nullptr;
152 for (const auto& value : out.classes)
153 if (value.begin <= offset && offset < value.end && (!owner || value.begin >= owner->begin)) owner = &value;
154 return owner ? owner->name + "." : "";
155 }
156 ScriptExpression expression(size_t begin, size_t end, unsigned depth = 0, bool complete = true) {
157 if (begin >= end || depth >= 64) return {"dynamic", "", {}, 1, 1, 0, 0};
158 size_t p = begin;
159 auto value = atom(p++);
160 if (value.value == "(" && pairs.contains(begin) && pairs.at(begin) < end) {
161 value = expression(begin + 1, pairs.at(begin), depth + 1);
162 p = pairs.at(begin) + 1;
163 } else if (value.kind != "identifier" && value.kind != "string" && value.kind != "number")
164 value.kind = "dynamic";
165 while (p < end) {
166 if (tokens[p].text == "." && p + 1 < end && tokens[p + 1].kind == "identifier") {
167 auto member = atom(p + 1, "member");
168 member.begin = value.begin;
169 member.operands.push_back(std::move(value));
170 value = std::move(member);
171 p += 2;
172 } else if (tokens[p].text == "(" && pairs.contains(p) && pairs.at(p) < end) {
173 const auto close = pairs.at(p);
174 ScriptExpression call{"call", "", {}, value.line, value.column, value.begin, tokens[close].end};
175 call.operands.push_back(std::move(value));
176 size_t argument = p + 1;
177 for (size_t k = argument; k <= close; ++k) {
178 if (k < close && pairs.contains(k)) {
179 k = pairs.at(k);
180 continue;
181 }
182 if (k == close || tokens[k].text == ",") {
183 if (argument < k) call.operands.push_back(expression(argument, k, depth + 1));
184 argument = k + 1;
185 }
186 }
187 ScriptCallSyntax record{scope(call.begin), "", call, {"dynamic", "", {}, 1, 1, 0, 0}};
188 if (close + 2 < tokens.size() && (tokens[close + 1].text == "==" || tokens[close + 1].text == "!=")) {
189 record.comparison = tokens[close + 1].text;
190 record.comparedValue = atom(close + 2);
191 }
192 calls[call.begin] = std::move(record);
193 if (calls.size() > 4096) throw std::runtime_error("Script call budget exceeded");
194 value = std::move(call);
195 p = close + 1;
196 } else
197 break;
198 }
199 if (complete && p != end) return atom(begin, "dynamic");
200 return value;
201 }
202 void functions() {
203 for (size_t i = 0; i + 1 < tokens.size(); ++i) {
204 if (tokens[i].kind != "identifier" || tokens[i].text != "class" || tokens[i + 1].kind != "identifier")
205 continue;
206 size_t body = i + 2;
207 while (body < tokens.size() && tokens[body].text != "{" && tokens[body].text != ";") ++body;
208 if (body < tokens.size() && tokens[body].text == "{" && pairs.contains(body))
209 out.classes.push_back(
210 {tokens[i + 1].text, tokens[i].line, tokens[body].begin, tokens[pairs.at(body)].end});
211 if (out.classes.size() > 512) throw std::runtime_error("Script class budget exceeded");
212 }
213 for (size_t i = 0; i < tokens.size(); ++i) {
214 if (tokens[i].kind != "identifier" || tokens[i].text != "function") continue;
215 size_t p = i + 1;
216 std::string name;
217 if (p < tokens.size() && tokens[p].kind == "identifier")
218 name = tokens[p++].text;
219 else if (i >= 2 && (tokens[i - 1].text == "<-" || tokens[i - 1].text == "=") &&
220 tokens[i - 2].kind == "identifier")
221 name = tokens[i - 2].text;
222 if (name.empty() || p >= tokens.size() || tokens[p].text != "(" || !pairs.contains(p)) continue;
223 size_t close = pairs.at(p), body = close + 1;
224 while (body < tokens.size() && tokens[body].text != "{" && tokens[body].text != ";") ++body;
225 if (body == tokens.size() || tokens[body].text != "{" || !pairs.contains(body)) continue;
226 ScriptFunctionSyntax fn{
227 classScope(tokens[i].begin) + name, {}, tokens[i].line, tokens[body].begin, tokens[pairs.at(body)].end};
228 for (size_t k = p + 1; k < close;) {
229 if (tokens[k].kind == "identifier") {
230 ScriptParameterSyntax parameter{tokens[k].text, ""};
231 if (k + 2 < close && tokens[k + 1].text == ":") parameter.type = tokens[k + 2].text;
232 fn.parameters.push_back(std::move(parameter));
233 }
234 while (k < close && tokens[k].text != ",") {
235 if (pairs.contains(k)) k = pairs.at(k);
236 ++k;
237 }
238 ++k;
239 }
240 signatures.push_back({fn.name, tokens[p].begin, tokens[body].begin});
241 out.functions.push_back(std::move(fn));
242 if (out.functions.size() > 512) throw std::runtime_error("Script function budget exceeded");
243 }
244 }
245 void analyze() {
246 lex();
247 functions();
248 const std::set<std::string> keywords{"if", "while", "for", "foreach", "switch", "catch",
249 "function", "class", "typeof", "return", "throw"};
250 for (size_t i = 0; i < tokens.size(); ++i) {
251 if (tokens[i].kind != "identifier" || keywords.contains(tokens[i].text)) continue;
252 if (i && (tokens[i - 1].text == "." || tokens[i - 1].text == "function" || tokens[i - 1].text == ":"))
253 continue;
254 expression(i, tokens.size(), 0, false);
255 if (std::any_of(signatures.begin(), signatures.end(), [&](const Signature& signature) {
256 return signature.begin <= tokens[i].begin && tokens[i].begin < signature.end;
257 }))
258 continue;
259 size_t assign = i + 1;
260 std::string type;
261 if (assign + 1 < tokens.size() && tokens[assign].text == ":") {
262 type = tokens[assign + 1].text;
263 assign += 2;
264 }
265 if (assign >= tokens.size() || (tokens[assign].text != "=" && tokens[assign].text != "<-")) continue;
266 size_t end = assign + 1;
267 while (end < tokens.size() && tokens[end].text != ";" && tokens[end].text != "}" &&
268 tokens[end].line == tokens[assign + 1].line) {
269 if (pairs.contains(end)) end = pairs.at(end);
270 ++end;
271 }
272 auto value = expression(assign + 1, end);
273 out.bindings.push_back({tokens[i].text, scope(tokens[i].begin), type,
274 !classScope(tokens[i].begin).empty() ||
275 (i && (tokens[i - 1].text == "local" || tokens[i - 1].text == "const")),
276 std::move(value)});
277 if (out.bindings.size() > 2048) throw std::runtime_error("Script binding budget exceeded");
278 }
279 for (auto& [offset, call] : calls) {
280 (void)offset;
281 out.calls.push_back(std::move(call));
282 }
283 }
284};
285} // namespace
287 Parser parser{source, {}, {}, {}, {}, {}};
288 try {
289 parser.analyze();
290 } catch (const std::exception& error) {
291 parser.out = {};
292 parser.out.diagnostics.push_back(error.what());
293 }
294 return std::move(parser.out);
295}
296} // namespace eve::script
glm::vec4 p[6]
ScriptComponentRecord record
Definition ECS.cpp:228
std::string name
Definition ECS.cpp:154
std::int32_t c
JobScope scope
size_t offset
bool verbatim
char quote
OceanGpuFrameToken token
std::shared_ptr< OceanGpuSpectrum > owner
std::string error
Definition Package.cpp:174
std::map< std::string, std::vector< Function > > functions
std::string body
Object::Ptr value
float t
std::string parameter
MigrationFunction function
unsigned line
std::vector< Signature > signatures
std::string text
ScriptStructure out
std::string_view source
std::map< size_t, ScriptCallSyntax > calls
size_t begin
std::map< size_t, size_t > pairs
unsigned column
size_t end
std::string kind
std::vector< Token > tokens
float(ui::Theme::* member)[4]
std::uint32_t depth
ScriptStructure analyzeScriptStructure(std::string_view source)
Parse named functions, lexical bindings and call/member/literal expressions.
SettlementPipeline::Stage fn
Bounded structural analysis, independent of VM execution and binding resolution.
std::vector< std::string > diagnostics