以下是我为行和块注释定义的语法
if (currentChar == '/')
{
if (miniLangInput[lookAheadIndex + 1] == '/') //line comment
{
while (currentChar != '\n')
{
lookAheadIndex += 1;
currentChar = miniLangInput[lookAheadIndex];
}
return Lexer::token(TOK_LINECOMMENT);
}
else if (miniLangInput[lookAheadIndex + 1] == '*') //block comment
{
bool reached = false;
do
{
lookAheadIndex += 1;
currentChar = miniLangInput[lookAheadIndex];
if (currentChar == '*' && miniLangInput[lookAheadIndex + 1] == '/')
{
lookAheadIndex += 1;
currentChar = miniLangInput[lookAheadIndex];
reached = true;
}
} while (reached == false); //while (lastChar != '/');
lookAheadIndex += 1;
return Lexer::token(TOK_BLOCKCOMMENT);
}
else
{
lookAheadIndex += 1;
return Lexer::token(TOK_SLASH);
}
}
但是,如果在输入字符串的末尾遇到没有关闭块的开放块注释(即(/ *)),程序崩溃导致以下错误:
调试断言失败,字符串下标超出范围。
这是因为未满足while子句,因此导致无限循环。
是否有可用于防止程序崩溃的异常类?我尝试在尝试捕获时封闭了do,但这没有任何效果。