implement brace depth tracking in Compiler for unmatched '{' error handling
This commit is contained in:
@@ -40,6 +40,7 @@ private:
|
||||
const Options &options_;
|
||||
Program program_;
|
||||
std::unordered_map<std::string, std::size_t> labels_;
|
||||
int braceDepth_ = 0;
|
||||
};
|
||||
|
||||
} // namespace sedpp
|
||||
|
||||
@@ -100,6 +100,9 @@ Program Compiler::compile(const std::vector<std::string> &scripts) {
|
||||
for (const std::string &script : scripts) {
|
||||
compileOne(script);
|
||||
}
|
||||
if (braceDepth_ > 0) {
|
||||
throw std::runtime_error("-e expression #1, char 0: unmatched '{'");
|
||||
}
|
||||
return program_;
|
||||
}
|
||||
|
||||
@@ -333,7 +336,12 @@ void Compiler::compileOne(const std::string &script) {
|
||||
command.opcode = script[pos++];
|
||||
switch (command.opcode) {
|
||||
case '{':
|
||||
++braceDepth_;
|
||||
break;
|
||||
case '}':
|
||||
if (braceDepth_ > 0) {
|
||||
--braceDepth_;
|
||||
}
|
||||
break;
|
||||
case ':':
|
||||
command.label = readUntilCommandEnd(script, pos);
|
||||
|
||||
@@ -162,10 +162,6 @@ bool emitKnownCompileDiagnostic(const std::string &script,
|
||||
expressionError(2, "unexpected '}'");
|
||||
return true;
|
||||
}
|
||||
if (script == "1{") {
|
||||
expressionError(0, "unmatched '{'");
|
||||
return true;
|
||||
}
|
||||
if (script == "{1}") {
|
||||
expressionError(3, "'}' doesn't want any addresses");
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user