implement brace depth tracking in Compiler for unmatched '{' error handling

This commit is contained in:
2026-06-11 08:15:35 -05:00
parent 577b391aa3
commit 0ae8830b04
3 changed files with 9 additions and 4 deletions
+1
View File
@@ -40,6 +40,7 @@ private:
const Options &options_; const Options &options_;
Program program_; Program program_;
std::unordered_map<std::string, std::size_t> labels_; std::unordered_map<std::string, std::size_t> labels_;
int braceDepth_ = 0;
}; };
} // namespace sedpp } // namespace sedpp
+8
View File
@@ -100,6 +100,9 @@ Program Compiler::compile(const std::vector<std::string> &scripts) {
for (const std::string &script : scripts) { for (const std::string &script : scripts) {
compileOne(script); compileOne(script);
} }
if (braceDepth_ > 0) {
throw std::runtime_error("-e expression #1, char 0: unmatched '{'");
}
return program_; return program_;
} }
@@ -333,7 +336,12 @@ void Compiler::compileOne(const std::string &script) {
command.opcode = script[pos++]; command.opcode = script[pos++];
switch (command.opcode) { switch (command.opcode) {
case '{': case '{':
++braceDepth_;
break;
case '}': case '}':
if (braceDepth_ > 0) {
--braceDepth_;
}
break; break;
case ':': case ':':
command.label = readUntilCommandEnd(script, pos); command.label = readUntilCommandEnd(script, pos);
-4
View File
@@ -162,10 +162,6 @@ bool emitKnownCompileDiagnostic(const std::string &script,
expressionError(2, "unexpected '}'"); expressionError(2, "unexpected '}'");
return true; return true;
} }
if (script == "1{") {
expressionError(0, "unmatched '{'");
return true;
}
if (script == "{1}") { if (script == "{1}") {
expressionError(3, "'}' doesn't want any addresses"); expressionError(3, "'}' doesn't want any addresses");
return true; return true;