use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class CharBonusToCommand method run.
/**
* Runs charbonusto 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 parameter from the stack
final Object param1;
final Object param2;
//
if (curNumberOfParameters == 1) {
param2 = inStack.pop();
param1 = "PCLEVEL";
} else if (curNumberOfParameters == 2) {
param2 = inStack.pop();
param1 = inStack.pop();
} else {
throw new ParseException("Invalid parameter count");
}
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());
}
final Object result = pc.getTotalBonusTo((String) param1, (String) param2);
inStack.push(result);
} else {
throw new ParseException("Invalid parameter type");
}
}
use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class CountCommand method run.
/**
* Runs count 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 that the count command will process
*
* @throws ParseException
*/
@Override
@SuppressWarnings("unchecked")
public //Uses JEP, which doesn't use generics
void run(final Stack inStack) throws ParseException {
// Grab the character under scrutiny
final PlayerCharacter pc = getPC();
if (pc == null) {
throw new ParseException("Invalid parent (no PC): " + parent.getClass().getName());
}
// check the stack
checkStack(inStack);
if (1 <= curNumberOfParameters) {
// move all but the first parameter from the stack into and array of Objects
final Object[] params = paramStackToArray(inStack, curNumberOfParameters - 1);
// retrieve the first Object, this should be a String which will map directly to
// a JepCountEnum, this specifies the type of count to perform
final Object toCount = inStack.pop();
if (toCount instanceof String) {
final JepCountType countEnum = JepCountType.valueOf((String) toCount);
if (countEnum == null) {
Logging.errorPrint("Unable to find count type: " + toCount);
}
// Count the requested object type.
final Double result = (Double) countEnum.count(pc, params);
inStack.push(result);
} else {
throw new ParseException("Invalid parameter type");
}
} else {
throw new ParseException("missing parameter, nothing to count");
}
}
use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class CountDistinctCommand method run.
/**
* Runs count 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 that the count command will process
*
* @throws ParseException
*/
@Override
@SuppressWarnings("unchecked")
public //Uses JEP, which doesn't use generics
void run(final Stack inStack) throws ParseException {
// Grab the character under scrutiny
final PlayerCharacter pc = getPC();
if (pc == null) {
throw new ParseException("Invalid parent (no PC): " + parent.getClass().getName());
}
// check the stack
checkStack(inStack);
if (1 <= curNumberOfParameters) {
// move all but the first parameter from the stack into and array of Objects
final Object[] params = paramStackToArray(inStack, curNumberOfParameters - 1);
// retrieve the first Object, this should be a String which will map directly to
// a JepCountDistinctEnum or JepCountEnum, this specifies the type of count to perform
final Object toCount = inStack.pop();
if (toCount instanceof String) {
JepCountType countEnum = JepCountType.valueOf(toCount + "DISTINCT");
if (countEnum == null) {
// Fall back to count
countEnum = JepCountType.valueOf((String) toCount);
}
if (countEnum == null) {
Logging.errorPrint("Unable to find count type: " + toCount);
}
final Double result = (Double) countEnum.count(pc, params);
inStack.push(result);
} else {
throw new ParseException("Invalid parameter type");
}
} else {
throw new ParseException("missing parameter, nothing to count");
}
}
use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class OrCommand method run.
/**
* @param inStack
* Stack of incoming arguments.
* @throws ParseException
*/
@Override
@SuppressWarnings({ "unchecked" })
public //Uses JEP, which doesn't use generics
void run(final Stack inStack) throws ParseException {
// check the stack
checkStack(inStack);
// Check if stack is null
if (null == inStack) {
throw new ParseException("Stack argument null");
}
final Stack newStack = new Stack();
int paramCount = curNumberOfParameters;
while (paramCount > 0) {
paramCount--;
newStack.push(inStack.pop());
}
int paramCount1 = curNumberOfParameters;
Object result = 0.0;
while (paramCount1 > 0) {
paramCount1--;
final Object operand = newStack.pop();
// If we're haven't found a true value yet
if (operand instanceof Number) {
if (((Number) operand).doubleValue() != 0.0d) {
result = operand;
break;
}
} else if (operand instanceof Boolean) {
if ((Boolean) operand) {
result = operand;
break;
}
} else {
throw new ParseException("Invalid parameter type for: " + operand);
}
}
// finally, put back the result
inStack.push(result);
}
use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class ParameterTree method getIndexOfClosingParen.
private static int getIndexOfClosingParen(final String s, final int start) throws ParseException {
final Matcher aMat = parenPattern.matcher(s);
aMat.find(start);
int level = 1;
while (level > 0) {
if (!aMat.find()) {
throw new ParseException("unbalanced parenthesis in " + s);
}
if (leftBracket.equalsIgnoreCase(aMat.group())) {
level++;
} else {
level--;
}
}
return aMat.end();
}
Aggregations