Search in sources :

Example 11 with ConfigCompileException

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

the class NotInstanceofKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    if (keywordPosition == 0) {
        throw new ConfigCompileException("Expected value to proceed \"notinstanceof\" keyword, but no identifiers were found.", list.get(keywordPosition).getTarget());
    }
    if (list.size() <= keywordPosition + 1) {
        throw new ConfigCompileException("Expected type to follow \"notinstanceof\" keyword, but no type was found.", list.get(keywordPosition).getTarget());
    }
    ParseTree node = new ParseTree(new CFunction(INSTANCEOF, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
    node.addChild(list.get(keywordPosition - 1));
    node.addChild(list.get(keywordPosition + 1));
    ParseTree not = new ParseTree(new CFunction(NOT, list.get(keywordPosition).getTarget()), list.get(keywordPosition).getFileOptions());
    not.addChild(node);
    // Overwrite the LHS
    list.set(keywordPosition - 1, not);
    // Remove the keyword
    list.remove(keywordPosition);
    // Remove the RHS
    list.remove(keywordPosition);
    return keywordPosition;
}
Also used : CFunction(com.laytonsmith.core.constructs.CFunction) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 12 with ConfigCompileException

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

the class ClosureKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    try {
        if (list.get(keywordPosition).getData() instanceof CFunction) {
            // It's a function, so do the old processing
            SimpleBlockKeywordFunction.doProcess(this.getKeywordName(), null, true, list, keywordPosition);
            // easiest if we do the conversion here.
            try {
                if (list.get(keywordPosition - 1).getData() instanceof CClassType) {
                    ParseTree type = list.remove(keywordPosition - 1);
                    List<ParseTree> children = list.get(keywordPosition - 1).getChildren();
                    children.add(0, type);
                    list.get(keywordPosition - 1).setChildren(children);
                    return keywordPosition - 1;
                }
            } catch (IndexOutOfBoundsException ex) {
            // Ignore, it's not a typed closure
            }
            return keywordPosition;
        } else {
            // Else it's standalone, so this should be treated as the closure ClassType
            list.set(keywordPosition, new ParseTree(CClosure.TYPE, list.get(keywordPosition).getFileOptions()));
            return keywordPosition;
        }
    } catch (IndexOutOfBoundsException ex) {
        throw new ConfigCompileException("Unexpected \"closure\" reference", list.get(keywordPosition).getTarget());
    }
}
Also used : CFunction(com.laytonsmith.core.constructs.CFunction) CClassType(com.laytonsmith.core.constructs.CClassType) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 13 with ConfigCompileException

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

the class DoKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    // We expect the format to be "do" __cbracket__ while, so if this is not the case, we will
    // always throw an exception.
    Target t = list.get(keywordPosition).getTarget();
    try {
        ParseTree code = list.get(keywordPosition + 1);
        ParseTree _while = list.get(keywordPosition + 2);
        this.validateCodeBlock(code, "Missing brace following \"do\" keyword");
        if (!(_while.getData() instanceof CFunction) || !_while.getData().val().equals(WHILE)) {
            throw new ConfigCompileException("Missing while clause following \"do\" keyword", t);
        }
        if (_while.getChildren().isEmpty()) {
            throw new ConfigCompileException("Missing argument to while clause", _while.getTarget());
        }
        ParseTree dowhile = new ParseTree(new CFunction(DOWHILE, t), list.get(keywordPosition).getFileOptions());
        dowhile.addChild(this.getArgumentOrNull(code));
        dowhile.addChild(_while.getChildAt(0));
        list.set(keywordPosition, dowhile);
        list.remove(keywordPosition + 2);
        list.remove(keywordPosition + 1);
    } catch (IndexOutOfBoundsException ex) {
        throw new ConfigCompileException("Unexpected keyword \"do\"", t);
    }
    return keywordPosition;
}
Also used : Target(com.laytonsmith.core.constructs.Target) CFunction(com.laytonsmith.core.constructs.CFunction) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 14 with ConfigCompileException

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

the class MethodScriptStaticCompiler method go.

private static void go(ParseTree node, StringBuilder b, api.Platforms platform) throws ConfigCompileException {
    if (node.hasChildren()) {
        FunctionBase f = FunctionList.getFunction(node.getData(), platform);
        if (!(f instanceof CompiledFunction)) {
            throw new ConfigCompileException("The function " + f.getName() + " is unknown in this platform.", node.getData().getTarget());
        }
        CompiledFunction cf = (CompiledFunction) f;
        List<String> children = new ArrayList<String>();
        for (ParseTree baby : node.getChildren()) {
            StringBuilder bb = new StringBuilder();
            go(baby, bb, platform);
            children.add(bb.toString());
        }
        b.append(cf.compile(node.getData().getTarget(), children.toArray(new String[children.size()])));
    } else {
        if (platform.getResolver() == null) {
            b.append(node.getData().val());
        } else {
            b.append(platform.getResolver().outputConstant(node.getData()));
        }
    }
}
Also used : CompiledFunction(com.laytonsmith.core.functions.CompiledFunction) FunctionBase(com.laytonsmith.core.functions.FunctionBase) ArrayList(java.util.ArrayList) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 15 with ConfigCompileException

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

the class CommandHelperInterpreterListener method execute.

public void execute(String script, final MCPlayer p) throws ConfigCompileException, ConfigCompileGroupException {
    TokenStream stream = MethodScriptCompiler.lex(script, new File("Interpreter"), true);
    ParseTree tree = MethodScriptCompiler.compile(stream);
    interpreterMode.remove(p.getName());
    GlobalEnv gEnv = new GlobalEnv(plugin.executionQueue, plugin.profiler, plugin.persistenceNetwork, CommandHelperFileLocations.getDefault().getConfigDirectory(), plugin.profiles, new TaskManager());
    gEnv.SetDynamicScriptingMode(true);
    CommandHelperEnvironment cEnv = new CommandHelperEnvironment();
    cEnv.SetPlayer(p);
    Environment env = Environment.createEnvironment(gEnv, cEnv);
    try {
        MethodScriptCompiler.registerAutoIncludes(env, null);
        MethodScriptCompiler.execute(tree, env, new MethodScriptComplete() {

            @Override
            public void done(String output) {
                output = output.trim();
                if (output.isEmpty()) {
                    Static.SendMessage(p, ":");
                } else {
                    if (output.startsWith("/")) {
                        // Run the command
                        Static.SendMessage(p, ":" + MCChatColor.YELLOW + output);
                        p.chat(output);
                    } else {
                        // output the results
                        Static.SendMessage(p, ":" + MCChatColor.GREEN + output);
                    }
                }
                interpreterMode.add(p.getName());
            }
        }, null);
    } catch (CancelCommandException e) {
        interpreterMode.add(p.getName());
    } catch (ConfigRuntimeException e) {
        ConfigRuntimeException.HandleUncaughtException(e, env);
        Static.SendMessage(p, MCChatColor.RED + e.toString());
        interpreterMode.add(p.getName());
    } catch (Exception e) {
        Static.SendMessage(p, MCChatColor.RED + e.toString());
        Logger.getLogger(CommandHelperInterpreterListener.class.getName()).log(Level.SEVERE, null, e);
        interpreterMode.add(p.getName());
    }
}
Also used : TokenStream(com.laytonsmith.core.compiler.TokenStream) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) MethodScriptComplete(com.laytonsmith.core.MethodScriptComplete) TaskManager(com.laytonsmith.core.taskmanager.TaskManager) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Environment(com.laytonsmith.core.environments.Environment) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) File(java.io.File) ParseTree(com.laytonsmith.core.ParseTree)

Aggregations

ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)37 ParseTree (com.laytonsmith.core.ParseTree)16 CFunction (com.laytonsmith.core.constructs.CFunction)14 ArrayList (java.util.ArrayList)13 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)12 CString (com.laytonsmith.core.constructs.CString)11 Construct (com.laytonsmith.core.constructs.Construct)8 Token (com.laytonsmith.core.constructs.Token)8 IVariable (com.laytonsmith.core.constructs.IVariable)7 Target (com.laytonsmith.core.constructs.Target)6 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)6 Function (com.laytonsmith.core.functions.Function)6 Variable (com.laytonsmith.core.constructs.Variable)5 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)5 Environment (com.laytonsmith.core.environments.Environment)5 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)5 FunctionBase (com.laytonsmith.core.functions.FunctionBase)5 File (java.io.File)5 List (java.util.List)5 FunctionList (com.laytonsmith.core.functions.FunctionList)4