Search in sources :

Example 6 with Target

use of com.laytonsmith.core.constructs.Target in project CommandHelper by EngineHub.

the class IfKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    ParseTree node = list.get(keywordPosition);
    Target t = node.getTarget();
    if (list.size() > keywordPosition + 1) {
        if (this.isValidCodeBlock(list.get(keywordPosition + 1))) {
            // is a compile error.
            if (node.getChildren().size() != 1) {
                throw new ConfigCompileException("Unexpected parameters passed to \"if\" clause, exactly 1 argument" + " must be provided", t);
            }
            // use ifelse from the outset
            try {
                if (nodeIsElseKeyword(list.get(keywordPosition + 2)) && nodeIsIfFunction(list.get(keywordPosition + 3))) {
                    // It is, convert this into an ifelse
                    ParseTree newNode = new ParseTree(new CFunction(IFELSE, t), node.getFileOptions());
                    newNode.setChildren(node.getChildren());
                    list.set(keywordPosition, newNode);
                    node = newNode;
                }
            } catch (IndexOutOfBoundsException ex) {
            // Doesn't matter, we're apparently at the end of the stream
            }
            node.addChild(getArgumentOrNull(list.get(keywordPosition + 1)));
            list.remove(keywordPosition + 1);
        }
        while (list.size() > keywordPosition + 1) {
            // Now check for elses. Since we've removed the cbrace following the if from the tree, we can continue from keywordPostion + 1
            if (nodeIsElseKeyword(list.get(keywordPosition + 1))) {
                try {
                    if (isCodeBlock(list.get(keywordPosition + 2))) {
                        // So ends the chain
                        validateCodeBlock(list.get(keywordPosition + 2), "");
                        node.addChild(getArgumentOrNull(list.get(keywordPosition + 2)));
                        // remove the else keyword + the brace
                        list.remove(keywordPosition + 1);
                        list.remove(keywordPosition + 1);
                        break;
                    } else if (nodeIsIfFunction(list.get(keywordPosition + 2))) {
                        // if(@a){ } else if(@b, @c)
                        if (list.get(keywordPosition + 2).getChildren().size() != 1) {
                            throw new ConfigCompileException("Unexpected parameters passed to \"if\" clause, exactly 1 argument" + " must be provided", list.get(keywordPosition + 2).getTarget());
                        }
                        if (!isCodeBlock(list.get(keywordPosition + 3))) {
                            throw new ConfigCompileException("Expecting braces after \"if\" clause", list.get(keywordPosition + 3).getTarget());
                        }
                        // Ok, checks are complete, so we can actually construct the arguments now
                        node.addChild(list.get(keywordPosition + 2).getChildAt(0));
                        node.addChild(getArgumentOrNull(list.get(keywordPosition + 3)));
                        // Remove the else, if function, and braces
                        list.remove(keywordPosition + 1);
                        list.remove(keywordPosition + 1);
                        list.remove(keywordPosition + 1);
                    } else {
                        // Anything else is unexpected.
                        throw new IndexOutOfBoundsException();
                    }
                } catch (IndexOutOfBoundsException ex) {
                    throw new ConfigCompileException("Expecting either braces, or continuing if statement after \"else\" keyword", list.get(keywordPosition + 1).getTarget());
                }
            } else {
                // Done with the if else chain
                break;
            }
        }
    }
    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 7 with Target

use of com.laytonsmith.core.constructs.Target 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 8 with Target

use of com.laytonsmith.core.constructs.Target in project CommandHelper by EngineHub.

the class ForKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    ParseTree _for = list.get(keywordPosition);
    Target t = _for.getTarget();
    if (list.size() > keywordPosition + 1) {
        // This portion handles the initial code block, i.e. foreach(...){ }
        ParseTree codeBlock = list.get(keywordPosition + 1);
        if (isCodeBlock(codeBlock)) {
            validateCodeBlock(codeBlock, "");
            _for.addChild(getArgumentOrNull(codeBlock));
            list.remove(keywordPosition + 1);
        }
    }
    if (list.size() > keywordPosition + 1) {
        // This part handles the else keyword, i.e. foreach(...){ } else { }
        ParseTree elseKeyword = list.get(keywordPosition + 1);
        // If it's not an else keyword, then we'll leave it alone, and be done.
        if (elseKeyword.getData() instanceof CKeyword && elseKeyword.getData().val().equals("else")) {
            list.remove(keywordPosition + 1);
            ParseTree codeBlock = list.get(keywordPosition + 1);
            if (isCodeBlock(codeBlock)) {
                validateCodeBlock(codeBlock, "");
                _for.addChild(getArgumentOrNull(codeBlock));
            }
            // We also have to refactor this into a foreachelse, instead of a foreach.
            list.get(keywordPosition).setData(new CFunction(FORELSE, t));
            list.remove(keywordPosition + 1);
        }
    }
    return keywordPosition;
}
Also used : Target(com.laytonsmith.core.constructs.Target) CFunction(com.laytonsmith.core.constructs.CFunction) CKeyword(com.laytonsmith.core.constructs.CKeyword) ParseTree(com.laytonsmith.core.ParseTree)

Example 9 with Target

use of com.laytonsmith.core.constructs.Target in project CommandHelper by EngineHub.

the class ForeachKeyword method process.

@Override
public int process(List<ParseTree> list, int keywordPosition) throws ConfigCompileException {
    ParseTree foreach = list.get(keywordPosition);
    Target t = foreach.getTarget();
    if (list.size() > keywordPosition + 1) {
        // This portion handles the initial code block, i.e. foreach(...){ }
        ParseTree codeBlock = list.get(keywordPosition + 1);
        if (isCodeBlock(codeBlock)) {
            validateCodeBlock(codeBlock, "");
            foreach.addChild(getArgumentOrNull(codeBlock));
            list.remove(keywordPosition + 1);
        }
    }
    if (list.size() > keywordPosition + 1) {
        // This part handles the else keyword, i.e. foreach(...){ } else { }
        ParseTree elseKeyword = list.get(keywordPosition + 1);
        // If it's not an else keyword, then we'll leave it alone, and be done.
        if (elseKeyword.getData() instanceof CKeyword && elseKeyword.getData().val().equals("else")) {
            list.remove(keywordPosition + 1);
            ParseTree codeBlock = list.get(keywordPosition + 1);
            if (isCodeBlock(codeBlock)) {
                validateCodeBlock(codeBlock, "");
                foreach.addChild(getArgumentOrNull(codeBlock));
            }
            // We also have to refactor this into a foreachelse, instead of a foreach.
            list.get(keywordPosition).setData(new CFunction(FOREACHELSE, t));
            list.remove(keywordPosition + 1);
        }
    }
    return keywordPosition;
}
Also used : Target(com.laytonsmith.core.constructs.Target) CFunction(com.laytonsmith.core.constructs.CFunction) CKeyword(com.laytonsmith.core.constructs.CKeyword) ParseTree(com.laytonsmith.core.ParseTree)

Example 10 with Target

use of com.laytonsmith.core.constructs.Target in project CommandHelper by EngineHub.

the class RandomTests method testJSONEscapeString.

@Test
public void testJSONEscapeString() throws MarshalException {
    CArray ca = new CArray(Target.UNKNOWN);
    final Target t = Target.UNKNOWN;
    ca.push(C.Int(1), t);
    ca.push(C.Double(2.2), t);
    ca.push(C.String("string"), t);
    ca.push(C.String("\"Quote\""), t);
    ca.push(C.Boolean(true), t);
    ca.push(C.Boolean(false), t);
    ca.push(C.Null(), t);
    ca.push(C.Void(), t);
    ca.push(new Command("/Command", Target.UNKNOWN), t);
    ca.push(new CArray(Target.UNKNOWN, new CInt(1, Target.UNKNOWN)), t);
    // [1, 2.2, "string", "\"Quote\"", true, false, null, "", "/Command", [1]]
    assertEquals("[1,2.2,\"string\",\"\\\"Quote\\\"\",true,false,null,\"\",\"\\/Command\",[1]]", Construct.json_encode(ca, Target.UNKNOWN));
}
Also used : Target(com.laytonsmith.core.constructs.Target) CInt(com.laytonsmith.core.constructs.CInt) Command(com.laytonsmith.core.constructs.Command) CArray(com.laytonsmith.core.constructs.CArray) Test(org.junit.Test)

Aggregations

Target (com.laytonsmith.core.constructs.Target)17 CString (com.laytonsmith.core.constructs.CString)6 Construct (com.laytonsmith.core.constructs.Construct)6 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)6 ParseTree (com.laytonsmith.core.ParseTree)5 CArray (com.laytonsmith.core.constructs.CArray)5 CFunction (com.laytonsmith.core.constructs.CFunction)5 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)4 ArrayList (java.util.ArrayList)4 CKeyword (com.laytonsmith.core.constructs.CKeyword)3 Token (com.laytonsmith.core.constructs.Token)3 CClosure (com.laytonsmith.core.constructs.CClosure)2 CInt (com.laytonsmith.core.constructs.CInt)2 IVariable (com.laytonsmith.core.constructs.IVariable)2 Variable (com.laytonsmith.core.constructs.Variable)2 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)2 CancelCommandException (com.laytonsmith.core.exceptions.CancelCommandException)2 File (java.io.File)2 List (java.util.List)2