Search in sources :

Example 16 with ParseException

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

the class SkillInfoCommand method run.

/**
	 * Runs skill on the inStack. The parameter is popped
	 * off the {@code inStack}, and the variable's value is
	 * pushed back to the top of {@code inStack}.
	 * @param inStack the jep stack
	 * @throws ParseException
	 */
//Uses JEP, which doesn't use generics
@SuppressWarnings("unchecked")
@Override
public void run(final Stack inStack) throws ParseException {
    // check the stack
    checkStack(inStack);
    // get the parameters from the stack
    //
    // have to do this in reverse order...this is a stack afterall
    //
    final Object param2 = inStack.pop();
    final Object param1 = inStack.pop();
    if ((param1 instanceof String) && (param2 instanceof String)) {
        PlayerCharacter pc = null;
        if (parent instanceof VariableProcessor) {
            pc = ((VariableProcessor) parent).getPc();
        } else if (parent instanceof PlayerCharacter) {
            pc = (PlayerCharacter) parent;
        }
        if (pc == null) {
            throw new ParseException("Invalid parent (no PC): " + parent.getClass().getName());
        }
        Skill aSkill = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Skill.class, param2.toString());
        Object result = null;
        if (aSkill != null && pc.getDisplay().hasSkill(aSkill)) {
            if ("modifier".equalsIgnoreCase((String) param1)) {
                // aSkill.modifier() returns Integer
                result = (double) SkillModifier.modifier(aSkill, pc).intValue();
            } else if ("rank".equalsIgnoreCase((String) param1)) {
                // aSkill.getRank() returns Float
                result = pc.getDisplay().getRank(aSkill).doubleValue();
            } else if ("total".equalsIgnoreCase((String) param1)) {
                result = (double) SkillRankControl.getTotalRank(pc, aSkill).intValue() + SkillModifier.modifier(aSkill, pc);
            } else if ("totalrank".equalsIgnoreCase((String) param1)) {
                // aSkill.getTotalRank() returns Float
                result = SkillRankControl.getTotalRank(pc, aSkill).doubleValue();
            } else if ("stat".equalsIgnoreCase((String) param1)) {
                result = (double) SkillModifier.getStatMod(aSkill, pc);
            } else if ("misc".equalsIgnoreCase((String) param1)) {
                result = (double) (SkillModifier.modifier(aSkill, pc).intValue() - SkillModifier.getStatMod(aSkill, pc));
            } else {
                Logging.log(Logging.LST_ERROR, "Ignoring unknown parameter '" + param1 + "' in Skillinfo call: skillinfo(\"" + param1 + "\",\"" + param2 + "\")");
                result = (double) 0;
            }
        } else {
            result = (double) 0;
        }
        inStack.push(result);
    } else {
        throw new ParseException("Invalid parameter type");
    }
}
Also used : Skill(pcgen.core.Skill) PlayerCharacter(pcgen.core.PlayerCharacter) VariableProcessor(pcgen.core.VariableProcessor) ParseException(org.nfunk.jep.ParseException)

Example 17 with ParseException

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

the class ParameterTreeTest method testMakeTree5.

@Test
public final void testMakeTree5() {
    final String s = "(TYPE=Foo[or]TYPE=Bar)";
    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("[or]"), "New ParamterTree has correct contents");
    is(t1.getLeftTree().getContents(), strEq("TYPE=Foo"), "New ParamterTree has correct left tree contents");
    is(t1.getLeftTree().getLeftTree(), eqnull(), "New ParamterTree has correct left tree, left tree contents");
    is(t1.getLeftTree().getRightTree(), eqnull(), "New ParamterTree has correct left tree, right tree contents");
    is(t1.getRightTree().getContents(), strEq("TYPE=Bar"), "New ParamterTree has correct right tree contents");
    is(t1.getRightTree().getLeftTree(), eqnull(), "New ParamterTree has correct left tree, left tree contents");
    is(t1.getRightTree().getRightTree(), eqnull(), "New ParamterTree has correct left tree, right tree contents");
}
Also used : Matcher(java.util.regex.Matcher) ParseException(org.nfunk.jep.ParseException) Test(org.junit.Test)

Example 18 with ParseException

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

the class ParameterTreeTest method testMakeTree8.

@Test
public final void testMakeTree8() {
    //		verbose = true;
    //		Logging.errorPrint("\n\n --- Start Test Make tree 8 --- \n\n");
    final String s = "TYPE=Foo[or]((CATEGORY=FEAT[or]NATURE=AUTO)[and]TYPE=Bar)";
    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();
    final ParameterTree trll = trl.getLeftTree();
    final ParameterTree trlr = trl.getRightTree();
    // expected branch nodes
    is(t, not(eqnull()), "t  not null");
    is(tr, not(eqnull()), "tr not null");
    is(trl, not(eqnull()), "trl not null");
    is(t.getContents(), strEq("[or]"), "t  has correct contents '[or]'");
    is(tr.getContents(), strEq("[and]"), "tr has correct contents '[and]'");
    is(trl.getContents(), strEq("[or]"), "trl has correct contents '[or]'");
    // expected leaf nodes
    is(tl, not(eqnull()), "tl  not null");
    is(trr, not(eqnull()), "trr not null");
    is(trll, not(eqnull()), "trll not null");
    is(trlr, not(eqnull()), "trlr not null");
    is(tl.getContents(), strEq("TYPE=Foo"), "tl  has correct contents 'TYPE=Foo'");
    is(trr.getContents(), strEq("TYPE=Bar"), "trr has correct contents 'TYPE=Bar'");
    is(trll.getContents(), strEq("CATEGORY=FEAT"), "trl has correct contents 'CATEGORY=FEAT'");
    is(trlr.getContents(), strEq("NATURE=AUTO"), "trl has correct contents 'NATURE=AUTO'");
    // 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(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)");
    is(trll.getLeftTree(), eqnull(), "trl left tree is null (i.e. is a leaf node)");
    is(trll.getRightTree(), eqnull(), "trl right tree is null (i.e. is a leaf node)");
    is(trlr.getLeftTree(), eqnull(), "trll left tree is null (i.e. is a leaf node)");
    is(trlr.getRightTree(), eqnull(), "trlr 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 19 with ParseException

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

the class ParameterTreeTest method testMakeTree6.

@Test
public final void testMakeTree6() {
    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 tll = tl.getLeftTree();
    final ParameterTree tlr = tl.getRightTree();
    // expected branch nodes
    is(t.getContents(), strEq("[and]"), "t1 ParamterTree has correct contents");
    is(tl.getContents(), strEq("[or]"), "tl ParamterTree has correct contents");
    // expected leaf nodes
    is(tr.getContents(), strEq("String3"), "tr ParamterTree has correct contents");
    is(tll.getContents(), strEq("TYPE=Foo"), "tll ParamterTree has correct contents");
    is(tlr.getContents(), strEq("TYPE=Bar"), "tlr ParamterTree has correct contents");
    // check that leaves really are leaves
    is(tr.getLeftTree(), eqnull(), "tr left tree is null (i.e. is a leaf node)");
    is(tr.getRightTree(), eqnull(), "tr right tree is null (i.e. is a leaf node)");
    is(tll.getLeftTree(), eqnull(), "tll left tree is null (i.e. is a leaf node)");
    is(tll.getRightTree(), eqnull(), "tll right tree is null (i.e. is a leaf node)");
    is(tlr.getLeftTree(), eqnull(), "tlr left tree is null (i.e. is a leaf node)");
    is(tlr.getRightTree(), eqnull(), "tlr 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 20 with ParseException

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

the class ParameterTreeTest method testMakeTree9.

@Test
public final void testMakeTree9() {
    //		verbose = true;
    //		Logging.errorPrint("\n\n --- Start Test Make tree 9 --- \n\n");
    final String s = "TYPE=Foo[or]((CATEGORY=FEAT[or]NATURE=AUTO[or]CATEGORY=SA)[and]TYPE=Bar)";
    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();
    final ParameterTree trll = trl.getLeftTree();
    final ParameterTree trlr = trl.getRightTree();
    final ParameterTree trlll = trll.getLeftTree();
    final ParameterTree trllr = trll.getRightTree();
    // expected branch nodes
    is(t, not(eqnull()), "t  not null");
    is(tr, not(eqnull()), "tr not null");
    is(trl, not(eqnull()), "trl not null");
    is(trll, not(eqnull()), "trll not null");
    is(t.getContents(), strEq("[or]"), "t    has correct contents '[or]'");
    is(tr.getContents(), strEq("[and]"), "tr   has correct contents '[and]'");
    is(trl.getContents(), strEq("[or]"), "trl  has correct contents '[or]'");
    is(trll.getContents(), strEq("[or]"), "trll has correct contents '[or]'");
    // expected leaf nodes
    is(tl, not(eqnull()), "tl  not null");
    is(trr, not(eqnull()), "trr not null");
    is(trlr, not(eqnull()), "trlr not null");
    is(trlll, not(eqnull()), "trlll not null");
    is(trllr, not(eqnull()), "trllr not null");
    is(tl.getContents(), strEq("TYPE=Foo"), "tl  has correct contents 'TYPE=Foo'");
    is(trr.getContents(), strEq("TYPE=Bar"), "trr has correct contents 'TYPE=Bar'");
    is(trlr.getContents(), strEq("CATEGORY=SA"), "trlr has correct contents 'CATEGORY=SA'");
    is(trlll.getContents(), strEq("CATEGORY=FEAT"), "trlr has correct contents 'CATEGORY=FEAT'");
    is(trllr.getContents(), strEq("NATURE=AUTO"), "trlr has correct contents 'NATURE=AUTO'");
    // 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(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)");
    is(trlr.getLeftTree(), eqnull(), "trll left tree is null (i.e. is a leaf node)");
    is(trlr.getRightTree(), eqnull(), "trlr right tree is null (i.e. is a leaf node)");
    is(trlll.getLeftTree(), eqnull(), "trlll left tree is null (i.e. is a leaf node)");
    is(trlll.getRightTree(), eqnull(), "trlll right tree is null (i.e. is a leaf node)");
    is(trllr.getLeftTree(), eqnull(), "trlll left tree is null (i.e. is a leaf node)");
    is(trllr.getRightTree(), eqnull(), "trlll 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)

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