Search in sources :

Example 16 with ParsingSeparator

use of pcgen.base.text.ParsingSeparator in project pcgen by PCGen.

the class DefineLst method parseToken.

@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " may not be empty", context);
    }
    String firstItem = sep.next();
    if (firstItem.startsWith("UNLOCK.")) {
        return new ParseResult.Fail("DEFINE:UNLOCK. has been deprecated, " + "please use DEFINESTAT:STAT| or DEFINESTAT:UNLOCK|", context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " varName|varFormula" + "or LOCK.<stat>|value syntax requires an argument", context);
    }
    String var = firstItem;
    if (var.isEmpty()) {
        return new ParseResult.Fail("Empty Variable Name found in " + getTokenName() + ": " + value, context);
    }
    try {
        Formula f = FormulaFactory.getFormulaFor(sep.next());
        if (!f.isValid()) {
            return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
        }
        if ((!f.isStatic() || f.resolveStatic().intValue() != 0) && !(var.startsWith("MAXLEVELSTAT="))) {
            Logging.deprecationPrint("DEFINE with a non zero value has been deprecated, " + "please use a DEFINE of 0 and an appropriate bonus. Tag was DEFINE:" + value + " in " + obj, context);
        }
        if (sep.hasNext()) {
            return new ParseResult.Fail(getTokenName() + ' ' + firstItem + " syntax requires only one argument: " + value, context);
        }
        if (value.startsWith("LOCK.")) {
            return new ParseResult.Fail("DEFINE:LOCK. has been deprecated, " + "please use DEFINESTAT:LOCL| or DEFINESTAT:NONSTAT|", context);
        } else {
            context.getObjectContext().put(obj, VariableKey.getConstant(var), f);
        }
        return ParseResult.SUCCESS;
    } catch (IllegalArgumentException e) {
        return new ParseResult.Fail("Illegal Formula found in " + getTokenName() + ": " + value + ' ' + e.getLocalizedMessage(), context);
    }
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult) Ungranted(pcgen.cdom.base.Ungranted)

Example 17 with ParsingSeparator

use of pcgen.base.text.ParsingSeparator in project pcgen by PCGen.

the class DefineStatLst method parseToken.

@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " may not be empty", context);
    }
    String firstItem = sep.next();
    DefineStatSubToken subToken;
    try {
        subToken = DefineStatSubToken.valueOf(firstItem);
    } catch (IllegalArgumentException e1) {
        return new ParseResult.Fail("Found unexpected sub tag " + firstItem + " in " + getTokenName() + Constants.COLON + value + ". Must be one of " + StringUtils.join(DefineStatSubToken.values(), ", ") + Constants.DOT, context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by a stat.", context);
    }
    String statKey = sep.next();
    CDOMSingleRef<PCStat> stat = context.getReferenceContext().getCDOMReference(PCSTAT_CLASS, statKey);
    Formula f = null;
    if (subToken == DefineStatSubToken.LOCK || subToken == DefineStatSubToken.MINVALUE || subToken == DefineStatSubToken.MAXVALUE) {
        if (!sep.hasNext()) {
            return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by both a stat and a value.", context);
        }
        String formula = sep.next();
        f = FormulaFactory.getFormulaFor(formula);
        if (!f.isValid()) {
            return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
        }
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + Constants.COLON + value + " has too many pipe separated item.", context);
    }
    switch(subToken) {
        case LOCK:
            context.getObjectContext().addToList(obj, ListKey.STAT_LOCKS, new StatLock(stat, f));
            break;
        case UNLOCK:
            context.getObjectContext().addToList(obj, ListKey.UNLOCKED_STATS, stat);
            break;
        case NONSTAT:
            context.getObjectContext().addToList(obj, ListKey.NONSTAT_STATS, stat);
            break;
        case STAT:
            context.getObjectContext().addToList(obj, ListKey.NONSTAT_TO_STAT_STATS, stat);
            break;
        case MINVALUE:
            context.getObjectContext().addToList(obj, ListKey.STAT_MINVALUE, new StatLock(stat, f));
            break;
        case MAXVALUE:
            context.getObjectContext().addToList(obj, ListKey.STAT_MAXVALUE, new StatLock(stat, f));
            break;
    }
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult) PCStat(pcgen.core.PCStat) Ungranted(pcgen.cdom.base.Ungranted) StatLock(pcgen.cdom.helper.StatLock)

Example 18 with ParsingSeparator

use of pcgen.base.text.ParsingSeparator in project pcgen by PCGen.

the class FollowersLst method parseToken.

@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    if ((value == null) || value.isEmpty()) {
        return new ParseResult.Fail("Argument in " + getTokenName() + " cannot be empty", context);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String followerType = sep.next();
    if (followerType.isEmpty()) {
        return new ParseResult.Fail("Follower Type in " + getTokenName() + " cannot be empty", context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " has no PIPE character: " + "Must be of the form <follower type>|<formula>", context);
    }
    String followerNumber = sep.next();
    if (sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " has too many PIPE characters: " + "Must be of the form <follower type>|<formula", context);
    }
    if (followerNumber.isEmpty()) {
        return new ParseResult.Fail("Follower Count in " + getTokenName() + " cannot be empty", context);
    }
    CDOMSingleRef<CompanionList> cl = context.getReferenceContext().getCDOMReference(CompanionList.class, followerType);
    Formula num = FormulaFactory.getFormulaFor(followerNumber);
    if (!num.isValid()) {
        return new ParseResult.Fail("Number of Followers in " + getTokenName() + " was not valid: " + num.toString(), context);
    }
    context.getObjectContext().addToList(obj, ListKey.FOLLOWERS, new FollowerLimit(cl, num));
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) CompanionList(pcgen.cdom.list.CompanionList) FollowerLimit(pcgen.cdom.helper.FollowerLimit) Ungranted(pcgen.cdom.base.Ungranted)

Example 19 with ParsingSeparator

use of pcgen.base.text.ParsingSeparator in project pcgen by PCGen.

the class AddFeatToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String addType = sep.next();
    if (!"FEAT".equals(addType)) {
        return new ParseResult.Fail("Incompatible with ADD:FEAT:" + value);
    }
    String activeValue = sep.next();
    Formula count;
    if (!sep.hasNext()) {
        count = FormulaFactory.ONE;
    } else {
        count = FormulaFactory.getFormulaFor(activeValue);
        if (!count.isValid()) {
            return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
        }
        if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
            return new ParseResult.Fail("Count in ADD:FEAT must be > 0", context);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail("ADD:FEAT had too many pipe separated items: " + value, context);
    }
    try {
        if (!context.processToken(obj, "ADD", "ABILITY|" + count.toString() + "|FEAT|NORMAL|" + activeValue)) {
            Logging.replayParsedMessages();
            return new ParseResult.Fail("Delegation Error from ADD:FEAT");
        }
    } catch (PersistenceLayerException e) {
        return new ParseResult.Fail("Delegation Error from ADD:FEAT: " + e.getLocalizedMessage());
    }
    return ParseResult.SUCCESS;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult)

Example 20 with ParsingSeparator

use of pcgen.base.text.ParsingSeparator in project pcgen by PCGen.

the class AddVFeatToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String addType = sep.next();
    if (!"VFEAT".equals(addType)) {
        return new ParseResult.Fail("Incompatible with ADD:VFEAT: " + value);
    }
    String activeValue = sep.next();
    Formula count;
    if (!sep.hasNext()) {
        count = FormulaFactory.ONE;
    } else {
        count = FormulaFactory.getFormulaFor(activeValue);
        if (!count.isValid()) {
            return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
        }
        if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
            return new ParseResult.Fail("Count in ADD:VFEAT must be > 0", context);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail("ADD:VFEAT had too many pipe separated items: " + value, context);
    }
    try {
        if (!context.processToken(obj, "ADD", "ABILITY|" + count.toString() + "|FEAT|VIRTUAL|" + activeValue)) {
            Logging.replayParsedMessages();
            return new ParseResult.Fail("Delegation Error from ADD:VFEAT");
        }
    } catch (PersistenceLayerException e) {
        return new ParseResult.Fail("Delegation Error from ADD:VFEAT: " + e.getLocalizedMessage());
    }
    return ParseResult.SUCCESS;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult)

Aggregations

ParsingSeparator (pcgen.base.text.ParsingSeparator)35 Formula (pcgen.base.formula.Formula)26 ParseResult (pcgen.rules.persistence.token.ParseResult)18 ArrayList (java.util.ArrayList)15 CDOMReference (pcgen.cdom.base.CDOMReference)10 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)9 StringTokenizer (java.util.StringTokenizer)8 ChoiceSet (pcgen.cdom.base.ChoiceSet)7 ReferenceChoiceSet (pcgen.cdom.choiceset.ReferenceChoiceSet)6 PCClass (pcgen.core.PCClass)5 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)5 Ungranted (pcgen.cdom.base.Ungranted)4 Prerequisite (pcgen.core.prereq.Prerequisite)4 Skill (pcgen.core.Skill)3 LegalScope (pcgen.base.formula.base.LegalScope)2 PrimitiveCollection (pcgen.cdom.base.PrimitiveCollection)2 AbilityRefChoiceSet (pcgen.cdom.choiceset.AbilityRefChoiceSet)2 CNAbility (pcgen.cdom.content.CNAbility)2 LevelCommandFactory (pcgen.cdom.content.LevelCommandFactory)2 Nature (pcgen.cdom.enumeration.Nature)2