Skip to content

Commit

Permalink
Improve bare string exceptions
Browse files Browse the repository at this point in the history
This prints the bare string that caused the exception, which is useful for debugging from logs, and removes the extra and less useful "Not a statement" exception in these cases.
  • Loading branch information
PseudoKnight committed Mar 25, 2024
1 parent 5979012 commit 84290d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/laytonsmith/core/MethodScriptCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1944,11 +1944,12 @@ private static void checkLinearComponents(ParseTree tree, Environment env,
for(ParseTree m : tree.getAllNodes()) {
if(m.getData() instanceof CBareString && !(m.getData() instanceof CKeyword)) {
if(m.getFileOptions().isStrict()) {
compilerErrors.add(new ConfigCompileException("Use of bare strings in strict mode is not"
+ " allowed.", m.getTarget()));
compilerErrors.add(new ConfigCompileException("Use of bare (unquoted) string: "
+ m.getData().val(), m.getTarget()));
} else {
env.getEnv(CompilerEnvironment.class).addCompilerWarning(m.getFileOptions(),
new CompilerWarning("Use of bare string", m.getTarget(),
new CompilerWarning("Use of bare (unquoted) string: "
+ m.getData().val(), m.getTarget(),
FileOptions.SuppressWarning.UseBareStrings));
return; // for now, only one warning per file
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/laytonsmith/core/functions/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import com.laytonsmith.core.ParseTree;
import com.laytonsmith.core.Script;
import com.laytonsmith.core.compiler.FileOptions;
import com.laytonsmith.core.constructs.CBareString;
import com.laytonsmith.core.constructs.CBracket;
import com.laytonsmith.core.constructs.CClassType;
import com.laytonsmith.core.constructs.CEntry;
import com.laytonsmith.core.constructs.CFunction;
import com.laytonsmith.core.constructs.CKeyword;
import com.laytonsmith.core.constructs.CLabel;
import com.laytonsmith.core.constructs.CNull;
import com.laytonsmith.core.constructs.CString;
Expand Down Expand Up @@ -698,7 +700,10 @@ public boolean preResolveVariables() {
public ParseTree postParseRewrite(ParseTree ast, Environment env,
Set<Class<? extends Environment.EnvironmentImpl>> envs, Set<ConfigCompileException> exceptions) {
for(ParseTree child : ast.getChildren()) {
if(!(child.getData() instanceof CFunction)) {
// We only expect functions here.
// Bare strings already have a better exception from MethodScriptCompiler.checkLinearComponents()
if(!(child.getData() instanceof CFunction)
&& (!(child.getData() instanceof CBareString) || child.getData() instanceof CKeyword)) {
exceptions.add(new ConfigCompileException("Not a statement.", child.getTarget()));
}
}
Expand Down

0 comments on commit 84290d0

Please sign in to comment.