Search in sources :

Example 21 with ParseException

use of org.nfunk.jep.ParseException in project pcgen by PCGen.

the class ParameterTreeTest method testMakeTree7.

@Test
public final void testMakeTree7() {
    //		verbose = true;
    //		Logging.errorPrint("\n\n --- Start Test Make tree 7 --- \n\n");
    final String s = "TYPE=Foo[or](TYPE=Bar[and]String3)";
    final Matcher mat = ParameterTree.pat.matcher(s);
    mat.find();
    ParameterTree t = new ParameterTree("Foo");
    try {
        t = ParameterTree.makeTree(s);
    } catch (ParseException e) {
        e.printStackTrace();
        fail("Threw a parse exception");
    }
    final ParameterTree tl = t.getLeftTree();
    final ParameterTree tr = t.getRightTree();
    final ParameterTree trl = tr.getLeftTree();
    final ParameterTree trr = tr.getRightTree();
    // expected branch nodes
    is(t, not(eqnull()), "t  not null");
    is(tr, not(eqnull()), "tr not null");
    is(t.getContents(), strEq("[or]"), "t  has correct contents '[or]'");
    is(tr.getContents(), strEq("[and]"), "tr has correct contents '[and]'");
    // expected leaf nodes
    is(tl, not(eqnull()), "tl  not null");
    is(trl, not(eqnull()), "trl not null");
    is(trr, not(eqnull()), "trr not null");
    is(tl.getContents(), strEq("TYPE=Foo"), "tl  has correct contents 'TYPE=Foo'");
    is(trl.getContents(), strEq("TYPE=Bar"), "trl has correct contents 'TYPE=Bar'");
    is(trr.getContents(), strEq("String3"), "trr has correct contents 'String3'");
    // check that leaves really are leaves
    is(tl.getLeftTree(), eqnull(), "tl left tree is null (i.e. is a leaf node)");
    is(tl.getRightTree(), eqnull(), "tl right tree is null (i.e. is a leaf node)");
    is(trl.getLeftTree(), eqnull(), "trl left tree is null (i.e. is a leaf node)");
    is(trl.getRightTree(), eqnull(), "trl right tree is null (i.e. is a leaf node)");
    is(trr.getLeftTree(), eqnull(), "trr left tree is null (i.e. is a leaf node)");
    is(trr.getRightTree(), eqnull(), "trr right tree is null (i.e. is a leaf node)");
}
Also used : Matcher(java.util.regex.Matcher) ParseException(org.nfunk.jep.ParseException) Test(org.junit.Test)

Example 22 with ParseException

use of org.nfunk.jep.ParseException in project pcgen by PCGen.

the class ParameterTreeTest method testMakeTree1.

@Test
public final void testMakeTree1() {
    final String s = "TYPE=Foo";
    final Matcher mat = ParameterTree.pat.matcher(s);
    mat.find();
    ParameterTree t1 = new ParameterTree("Foo");
    try {
        t1 = ParameterTree.makeTree(s);
    } catch (ParseException e) {
        e.printStackTrace();
        fail("Threw a parse exception");
    }
    is(t1.getContents(), strEq(s), "New ParamterTree has correct contents");
}
Also used : Matcher(java.util.regex.Matcher) ParseException(org.nfunk.jep.ParseException) Test(org.junit.Test)

Example 23 with ParseException

use of org.nfunk.jep.ParseException in project pcgen by PCGen.

the class ParameterTree method makeTree.

private static ParameterTree makeTree(final String source, final boolean operatorNeeded) throws ParseException {
    final Matcher pM = parenPattern.matcher(source);
    final boolean hasP = pM.find();
    ParameterTree t;
    if (hasP) {
        final String pre = source.substring(0, pM.start());
        final int end = getIndexOfClosingParen(source, pM.start());
        if (pre.isEmpty()) {
            final String inside = source.substring(pM.end(), end - 1);
            t = makeTree(inside, false);
        } else {
            t = toTree(pre, operatorNeeded);
            final Matcher rM = operatorPattern.matcher(t.getContents());
            if (rM.find()) {
                if (t.getRightTree() == null) {
                    // Since we found an operator in the root of the tree, but
                    // the right subtree is null, then the parens must contain
                    // a complete expression (or the whole thing is illegal)
                    // so make a tree from the contents and append it here
                    // remember to strip off the outer parens.
                    final String inside = source.substring(pM.end(), end - 1);
                    t.setRightTree(makeTree(inside, false));
                } else {
                    // The root of the tree generated from the first section of the
                    // string has something in its right sub tree. That means the
                    // parenthesised expression is a part of that string (since
                    // if it ended with an operator that would be in a separate
                    // "root")
                    final StringBuilder rNodeContents = new StringBuilder();
                    rNodeContents.append(t.getRightTree().getContents());
                    rNodeContents.append(source.substring(pM.start(), end));
                }
            } else {
                // root of the generated tree doesn't contain an operator, so
                // the paren expression should be tacked onto it.
                final String parenExp = source.substring(pM.end() - 1, end);
                final StringBuilder rNodeContents = new StringBuilder();
                rNodeContents.append(t.getContents()).append(parenExp);
                t.setContents(rNodeContents.toString());
            }
        }
        if (end < source.length()) {
            final String sEnd = source.substring(end);
            final ParameterTree r = makeTree(sEnd, true);
            ParameterTree c = r;
            final Matcher cM = operatorPattern.matcher(r.getContents());
            if (!cM.find()) {
                throw new ParseException("expected \"" + source.substring(end) + "\" to begin with an operator");
            }
            while (c.getLeftTree() != null) {
                c = c.getLeftTree();
            }
            c.setLeftTree(t);
            t = r;
        }
    } else {
        t = toTree(source, operatorNeeded);
    }
    return t;
}
Also used : Matcher(java.util.regex.Matcher) ParseException(org.nfunk.jep.ParseException)

Example 24 with ParseException

use of org.nfunk.jep.ParseException in project dhis2-core by dhis2.

the class VectorSum method run.

// nFunk's JEP run() method uses the raw Stack type
@SuppressWarnings({ "rawtypes", "unchecked" })
public void run(Stack inStack) throws ParseException {
    checkStack(inStack);
    Object param = inStack.pop();
    if (param instanceof List) {
        List<Double> vals = CustomFunctions.checkVector(param);
        int n = vals.size();
        if (n == 0) {
            inStack.push(new Double(0));
        } else {
            double sum = 0;
            for (Double v : vals) {
                sum = sum + v;
            }
            inStack.push(new Double(sum));
        }
    } else {
        throw new ParseException("Invalid aggregate value in expression");
    }
}
Also used : Object(java.lang.Object) List(java.util.List) ParseException(org.nfunk.jep.ParseException)

Aggregations

ParseException (org.nfunk.jep.ParseException)24 Matcher (java.util.regex.Matcher)12 Test (org.junit.Test)9 PlayerCharacter (pcgen.core.PlayerCharacter)7 Object (java.lang.Object)2 List (java.util.List)2 VariableProcessor (pcgen.core.VariableProcessor)2 JepCountType (pcgen.util.JepCountType)2 Stack (java.util.Stack)1 PostfixMathCommandI (org.nfunk.jep.function.PostfixMathCommandI)1 Equipment (pcgen.core.Equipment)1 GameMode (pcgen.core.GameMode)1 Skill (pcgen.core.Skill)1 VariableProcessorEq (pcgen.core.VariableProcessorEq)1 VariableProcessorPC (pcgen.core.VariableProcessorPC)1