Search in sources :

Example 6 with ConfigCompileGroupException

use of com.laytonsmith.core.exceptions.ConfigCompileGroupException in project CommandHelper by EngineHub.

the class MSLPMaker method start.

public static void start(String path) throws IOException {
    File start = new File(path);
    if (!start.exists()) {
        StreamUtils.GetSystemErr().println("The specified file does not exist!");
        return;
    }
    File output = new File(start.getParentFile(), start.getName() + ".mslp");
    if (output.exists()) {
        pl("The file " + output.getName() + " already exists, would you like to overwrite? (Y/N)");
        String overwrite = prompt();
        if (!overwrite.equalsIgnoreCase("y")) {
            return;
        }
    }
    // First attempt to compile it, and make sure it doesn't fail
    AliasCore.LocalPackage localPackage = new AliasCore.LocalPackage();
    AliasCore.GetAuxAliases(start, localPackage);
    boolean error = false;
    for (AliasCore.LocalPackage.FileInfo fi : localPackage.getMSFiles()) {
        try {
            MethodScriptCompiler.compile(MethodScriptCompiler.lex(fi.contents(), fi.file(), true));
        } catch (ConfigCompileException e) {
            error = true;
            ConfigRuntimeException.HandleUncaughtException(e, "Compile error in script. Compilation will attempt to continue, however.", null);
        } catch (ConfigCompileGroupException ex) {
            error = true;
            ConfigRuntimeException.HandleUncaughtException(ex, null);
        }
    }
    List<Script> allScripts = new ArrayList<>();
    for (AliasCore.LocalPackage.FileInfo fi : localPackage.getMSAFiles()) {
        List<Script> tempScripts;
        try {
            tempScripts = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(fi.contents(), fi.file(), false));
            for (Script s : tempScripts) {
                try {
                    s.compile();
                    s.checkAmbiguous(allScripts);
                    allScripts.add(s);
                } catch (ConfigCompileException e) {
                    error = true;
                    ConfigRuntimeException.HandleUncaughtException(e, "Compile error in script. Compilation will attempt to continue, however.", null);
                } catch (ConfigCompileGroupException e) {
                    error = true;
                    ConfigRuntimeException.HandleUncaughtException(e, "Compile errors in script. Compilation will attempt to continue, however.", null);
                }
            }
        } catch (ConfigCompileException e) {
            error = true;
            ConfigRuntimeException.HandleUncaughtException(e, "Could not compile file " + fi.file() + " compilation will halt.", null);
        }
    }
    if (!error) {
        ZipMaker.MakeZip(start, output.getName());
        pl(GREEN + "The MSLP file has been created at " + output.getAbsolutePath() + reset());
    } else {
        pl(RED + "MSLP file has not been created due to compile errors. Correct the errors, and try again." + reset());
    }
}
Also used : Script(com.laytonsmith.core.Script) AliasCore(com.laytonsmith.core.AliasCore) ArrayList(java.util.ArrayList) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) File(java.io.File) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException)

Example 7 with ConfigCompileGroupException

use of com.laytonsmith.core.exceptions.ConfigCompileGroupException in project CommandHelper by EngineHub.

the class CompositeFunction method exec.

@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
    ParseTree tree;
    if (!cachedScripts.containsKey(this.getClass())) {
        try {
            String script = script();
            tree = MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, true)).getChildAt(0);
        } catch (ConfigCompileException | ConfigCompileGroupException ex) {
            // This is really bad.
            throw new Error(ex);
        }
        if (cacheCompile()) {
            cachedScripts.put(this.getClass(), tree);
        }
    } else {
        tree = cachedScripts.get(this.getClass());
    }
    GlobalEnv env = environment.getEnv(GlobalEnv.class);
    IVariableList oldVariables = env.GetVarList();
    IVariableList newVariables = new IVariableList();
    newVariables.set(new IVariable(CClassType.get("array"), "@arguments", new CArray(t, args.length, args), t));
    env.SetVarList(newVariables);
    Construct ret = CVoid.VOID;
    try {
        env.GetScript().eval(tree, environment);
    } catch (FunctionReturnException ex) {
        ret = ex.getReturn();
    }
    env.SetVarList(oldVariables);
    return ret;
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) IVariableList(com.laytonsmith.core.constructs.IVariableList) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 8 with ConfigCompileGroupException

use of com.laytonsmith.core.exceptions.ConfigCompileGroupException in project CommandHelper by EngineHub.

the class ExampleScript method getOutput.

public String getOutput() throws IOException, DataSourceException, URISyntaxException {
    if (output != null) {
        return output;
    }
    Script s = Script.GenerateScript(script, Static.GLOBAL_PERMISSION);
    Environment env;
    try {
        env = Static.GenerateStandaloneEnvironment();
    } catch (Profiles.InvalidProfileException ex) {
        throw new RuntimeException(ex);
    }
    Class[] interfaces = new Class[] { MCPlayer.class };
    MCPlayer p = (MCPlayer) Proxy.newProxyInstance(ExampleScript.class.getClassLoader(), interfaces, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("getName") || method.getName().equals("getDisplayName")) {
                return "Player";
            }
            if (method.getName().equals("sendMessage")) {
                playerOutput.append(args[0].toString()).append("\n");
            }
            if (method.getName().equals("isOnline")) {
                return true;
            }
            return genericReturn(method.getReturnType());
        }
    });
    // TODO: Remove this dependency. Make MCPlayer implement a generic "User" and make that
    // part of the GlobalEnv.
    env.getEnv(CommandHelperEnvironment.class).SetPlayer(p);
    final StringBuilder finalOutput = new StringBuilder();
    String thrown = null;
    try {
        List<Variable> vars = new ArrayList<>();
        try {
            MethodScriptCompiler.execute(originalScript, new File("/" + functionName + ".ms"), true, env, new MethodScriptComplete() {

                @Override
                public void done(String output) {
                    if (output != null) {
                        finalOutput.append(output);
                    }
                }
            }, null, vars);
        } catch (ConfigCompileException | ConfigCompileGroupException ex) {
        // We already checked for compile errors, so this won't happen
        }
    } catch (ConfigRuntimeException e) {
        String name = e.getClass().getName();
        if (e instanceof AbstractCREException) {
            name = ((AbstractCREException) e).getName();
        }
        thrown = "\n(Throws " + name + ": " + e.getMessage() + ")";
    }
    String playerOut = playerOutput.toString().trim();
    String finalOut = finalOutput.toString().trim();
    String out = (playerOut.isEmpty() ? "" : playerOut) + (finalOut.isEmpty() || !playerOut.trim().isEmpty() ? "" : ":" + finalOut);
    if (thrown != null) {
        out += thrown;
    }
    return out;
}
Also used : Variable(com.laytonsmith.core.constructs.Variable) MCPlayer(com.laytonsmith.abstraction.MCPlayer) ArrayList(java.util.ArrayList) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) Profiles(com.laytonsmith.core.Profiles) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Script(com.laytonsmith.core.Script) AbstractCREException(com.laytonsmith.core.exceptions.CRE.AbstractCREException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) MethodScriptComplete(com.laytonsmith.core.MethodScriptComplete) Environment(com.laytonsmith.core.environments.Environment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) File(java.io.File)

Example 9 with ConfigCompileGroupException

use of com.laytonsmith.core.exceptions.ConfigCompileGroupException in project CommandHelper by EngineHub.

the class IncludeCache method get.

public static ParseTree get(File file, Target t) {
    CHLog.GetLogger().Log(TAG, LogLevel.DEBUG, "Loading " + file, t);
    if (cache.containsKey(file)) {
        CHLog.GetLogger().Log(TAG, LogLevel.INFO, "Returning " + file + " from cache", t);
        return cache.get(file);
    }
    CHLog.GetLogger().Log(TAG, LogLevel.VERBOSE, "Cache does not already contain file. Compiling and caching.", t);
    // We have to pull the file from the FS, and compile it.
    if (!Security.CheckSecurity(file)) {
        throw new CRESecurityException("The script cannot access " + file + " due to restrictions imposed by the base-dir setting.", t);
    }
    CHLog.GetLogger().Log(TAG, LogLevel.VERBOSE, "Security check passed", t);
    try {
        String s = new ZipReader(file).getFileContents();
        ParseTree tree = MethodScriptCompiler.compile(MethodScriptCompiler.lex(s, file, true));
        CHLog.GetLogger().Log(TAG, LogLevel.VERBOSE, "Compilation succeeded, adding to cache.", t);
        IncludeCache.add(file, tree);
        return tree;
    } catch (ConfigCompileException ex) {
        throw new CREIncludeException("There was a compile error when trying to include the script at " + file + "\n" + ex.getMessage() + " :: " + file.getName() + ":" + ex.getLineNum(), t);
    } catch (ConfigCompileGroupException ex) {
        StringBuilder b = new StringBuilder();
        b.append("There were compile errors when trying to include the script at ").append(file).append("\n");
        for (ConfigCompileException e : ex.getList()) {
            b.append(e.getMessage()).append(" :: ").append(e.getFile().getName()).append(":").append(e.getLineNum());
        }
        throw new CREIncludeException(b.toString(), t);
    } catch (IOException ex) {
        throw new CREIOException("The script at " + file + " could not be found or read in.", t);
    }
}
Also used : CREIncludeException(com.laytonsmith.core.exceptions.CRE.CREIncludeException) ZipReader(com.laytonsmith.PureUtilities.ZipReader) IOException(java.io.IOException) CREIOException(com.laytonsmith.core.exceptions.CRE.CREIOException) CREIOException(com.laytonsmith.core.exceptions.CRE.CREIOException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree) CRESecurityException(com.laytonsmith.core.exceptions.CRE.CRESecurityException)

Example 10 with ConfigCompileGroupException

use of com.laytonsmith.core.exceptions.ConfigCompileGroupException in project CommandHelper by EngineHub.

the class MethodScriptCompilerTest method ambigousCommandRegistrationHelper.

/**
 * Checks if cmd1 and cmd2 would cause a ConfigCompileException due to being ambigous. Calls "fail(message)" when
 * compile success != expectFail. The order of cmd1 and cmd2 does not matter, this method checks both orders.
 *
 * @param cmd1 - The first command.
 * @param cmd2 - The second command.
 * @param expectFail - True if cmd1 and cmd2 are expected to be ambigous, false otherwise.
 */
private void ambigousCommandRegistrationHelper(String cmd1, String cmd2, boolean expectFail) {
    Script s1, s2;
    try {
        List<Script> scripts = MethodScriptCompiler.preprocess(MethodScriptCompiler.lex(cmd1 + "\n" + cmd2, null, false));
        s1 = scripts.get(0);
        s2 = scripts.get(1);
        s1.compile();
        s2.compile();
    } catch (ConfigCompileGroupException | ConfigCompileException | IndexOutOfBoundsException e) {
        fail("Encountered unexpected " + e.getClass().getSimpleName() + " with message: " + e.getMessage());
        return;
    }
    // Check scripts 1 and 2 against eachother.
    ConfigCompileException e1 = null, e2 = null;
    try {
        s2.checkAmbiguous(Arrays.asList(new Script[] { s1 }));
    } catch (ConfigCompileException e) {
        e1 = e;
    }
    try {
        s1.checkAmbiguous(Arrays.asList(new Script[] { s2 }));
    } catch (ConfigCompileException e) {
        e2 = e;
    }
    // Fail if the behaviour of script 1 VS 2 did not match script 2 VS 1.
    if ((e1 == null) != (e2 == null)) {
        fail("Commands '" + cmd1 + "' and '" + cmd2 + "' are both ambigous and non-ambigous" + " depending on the command order. Since command order does not matter, this is a bug.");
    }
    // Fail if the result differs from the expected result.
    if (expectFail) {
        if (e1 == null) {
            fail("Commands '" + cmd1 + "' and '" + cmd2 + "' are expected to be ambigous," + " but they did not cause the expected ConfigCompileException.");
        }
    } else {
        if (e1 != null) {
            fail("Commands '" + cmd1 + "' and '" + cmd2 + "' are expected to be non-ambigous," + " but they did cause a ConfigCompileException with message: " + e1.getMessage());
        }
    }
}
Also used : ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException)

Aggregations

ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)11 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)11 File (java.io.File)5 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)4 ArrayList (java.util.ArrayList)4 ParseTree (com.laytonsmith.core.ParseTree)3 CString (com.laytonsmith.core.constructs.CString)3 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)3 Environment (com.laytonsmith.core.environments.Environment)3 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)3 MethodScriptComplete (com.laytonsmith.core.MethodScriptComplete)2 Profiles (com.laytonsmith.core.Profiles)2 Script (com.laytonsmith.core.Script)2 Construct (com.laytonsmith.core.constructs.Construct)2 IVariable (com.laytonsmith.core.constructs.IVariable)2 Variable (com.laytonsmith.core.constructs.Variable)2 FunctionList (com.laytonsmith.core.functions.FunctionList)2 TaskManager (com.laytonsmith.core.taskmanager.TaskManager)2 IOException (java.io.IOException)2 ArgumentParser (com.laytonsmith.PureUtilities.ArgumentParser)1