Skip to content
Permalink
Browse files
Don't rewrite AST on try keyword process error
Rewrite the `try` keyword processor to not make changes to the AST unless it can finish parsing without exceptions.
  • Loading branch information
Pieter12345 committed Jul 21, 2020
1 parent f496a78 commit 1dcd0cb8e6e0a3d2cfefcf5c5f34067a07efd9ff
Showing 1 changed file with 14 additions and 12 deletions.
@@ -48,20 +48,20 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi

ParseTree complexTry = new ParseTree(new CFunction(COMPLEX_TRY, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
complexTry.addChild(getArgumentOrNull(list.get(keywordPosition + 1)));
// Remove the keyword and the try block
list.remove(keywordPosition);
list.remove(keywordPosition);

// For now, we won't allow try {}, so this must be followed by a catch keyword. This restriction is somewhat artificial, and
// if we want to remove it in the future, we can do so by removing this code block.
{
if(!(list.size() > keywordPosition && (nodeIsCatchKeyword(list.get(keywordPosition)) || nodeIsFinallyKeyword(list.get(keywordPosition))))) {
throw new ConfigCompileException("Expecting \"catch\" or \"finally\" keyword to follow try, but none found", complexTry.getTarget());
if(!(list.size() > keywordPosition + 2 && (nodeIsCatchKeyword(list.get(keywordPosition + 2))
|| nodeIsFinallyKeyword(list.get(keywordPosition + 2))))) {
throw new ConfigCompileException("Expecting \"catch\" or \"finally\" keyword to follow try,"
+ " but none found", complexTry.getTarget());
}
}

// We can have any number of catch statements after the try, so we loop through until we run out.
for(int i = keywordPosition; i < list.size(); i++) {
int numHandledChildren = 2; // The "try" keyword and try code block have already been handled.
for(int i = keywordPosition + 2; i < list.size(); i += 2) {
if(!nodeIsCatchKeyword(list.get(i)) && !nodeIsFinallyKeyword(list.get(i))) {
// End of the chain, stop processing.
break;
@@ -92,14 +92,16 @@ public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompi
// Passed the inspection.
complexTry.addChild(getArgumentOrNull(list.get(i + 1)));
}
// remove the catch keyword and the code block
list.remove(i);
list.remove(i);
--i;

// Mark catch keyword and code block as handled.
numHandledChildren += 2;
}

// Set the new function into place
list.add(keywordPosition, complexTry);
// Replace the "try" keyword, try block and all other handled blocks with the new function.
for(int i = 0; i < numHandledChildren - 1; i++) {
list.remove(keywordPosition);
}
list.set(keywordPosition, complexTry);

return keywordPosition;
}

0 comments on commit 1dcd0cb

Please sign in to comment.