Search in sources :

Example 51 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class CrToken method parseNonEmptyToken.

@Override
public ParseResult parseNonEmptyToken(LoadContext context, Race race, String value) {
    try {
        int intRating = Integer.parseInt(value.startsWith("1/") ? value.substring(2) : value);
        if (intRating < 0) {
            return new ParseResult.Fail(getTokenName() + " Challenge Rating cannot be negative", context);
        }
    } catch (NumberFormatException e) {
        return new ParseResult.Fail(getTokenName() + "Challenge Rating must be a positive integer i or 1/i", context);
    }
    Formula formula = FormulaFactory.getFormulaFor(value);
    if (!formula.isValid()) {
        return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
    }
    ChallengeRating cr = new ChallengeRating(formula);
    context.getObjectContext().put(race, ObjectKey.CHALLENGE_RATING, cr);
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ChallengeRating(pcgen.cdom.content.ChallengeRating) ParseResult(pcgen.rules.persistence.token.ParseResult)

Example 52 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class FaceToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Race race, String value) {
    if (ControlUtilities.hasControlToken(context, CControl.FACE)) {
        return new ParseResult.Fail("FACE: LST Token is disabled when FACE: control is used", context);
    }
    if (value.indexOf(',') == -1) {
        value = value + ',' + 0;
    }
    FormatManager<OrderedPair> formatManager = (FormatManager<OrderedPair>) context.getReferenceContext().getFormatManager("ORDEREDPAIR");
    ScopeInstance scopeInst = context.getActiveScope();
    LegalScope scope = scopeInst.getLegalScope();
    PCGenModifier<OrderedPair> modifier;
    try {
        modifier = context.getVariableContext().getModifier(MOD_IDENTIFICATION, value, MOD_PRIORITY, scope, formatManager);
    } catch (IllegalArgumentException iae) {
        return new ParseResult.Fail(getTokenName() + " Modifier " + MOD_IDENTIFICATION + " had value " + value + " but it was not valid: " + iae.getMessage(), context);
    }
    OrderedPair pair = modifier.process(null);
    if (pair.getPreciseX().doubleValue() < 0.0) {
        return new ParseResult.Fail(getTokenName() + " had value " + value + " but first item cannot be negative", context);
    }
    if (pair.getPreciseY().doubleValue() < 0.0) {
        return new ParseResult.Fail(getTokenName() + " had value " + value + " but second item cannot be negative", context);
    }
    if (!context.getVariableContext().isLegalVariableID(scope, VAR_NAME)) {
        return new ParseResult.Fail(getTokenName() + " internal error: found invalid var name: " + VAR_NAME + ", Modified on " + race.getClass().getSimpleName() + ' ' + race.getKeyName(), context);
    }
    VarModifier<OrderedPair> vm = new VarModifier<>(VAR_NAME, scope, modifier);
    context.getObjectContext().addToList(race, ListKey.MODIFY, vm);
    return ParseResult.SUCCESS;
}
Also used : ScopeInstance(pcgen.base.formula.base.ScopeInstance) ParseResult(pcgen.rules.persistence.token.ParseResult) LegalScope(pcgen.base.formula.base.LegalScope) VarModifier(pcgen.cdom.content.VarModifier) OrderedPair(pcgen.base.math.OrderedPair) FormatManager(pcgen.base.util.FormatManager)

Example 53 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class HitdiceadvancementToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Race race, String value) {
    final StringTokenizer commaTok = new StringTokenizer(value, Constants.COMMA);
    context.getObjectContext().removeList(race, ListKey.HITDICE_ADVANCEMENT);
    int last = 0;
    while (commaTok.hasMoreTokens()) {
        String tok = commaTok.nextToken();
        int hd;
        if ("*".equals(tok)) {
            if (commaTok.hasMoreTokens()) {
                return new ParseResult.Fail("Found * in " + getTokenName() + " but was not at end of list", context);
            }
            hd = Integer.MAX_VALUE;
        } else {
            try {
                hd = Integer.parseInt(tok);
                if (hd < last) {
                    return new ParseResult.Fail("Found " + hd + " in " + getTokenName() + " but was < 1 " + "or the previous value in the list: " + value, context);
                }
                last = hd;
            } catch (NumberFormatException nfe) {
                return new ParseResult.Fail(nfe.getMessage(), context);
            }
        }
        context.getObjectContext().addToList(race, ListKey.HITDICE_ADVANCEMENT, hd);
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult)

Example 54 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class HitdieToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Race race, String value) {
    try {
        String lock = value;
        int pipeLoc = lock.indexOf(Constants.PIPE);
        if (pipeLoc != lock.lastIndexOf(Constants.PIPE)) {
            return new ParseResult.Fail(getTokenName() + " has more than one pipe, " + "is not of format: <int>[|<prereq>]", context);
        }
        // Do not initialize, null is significant
        CDOMReference<PCClass> owner = null;
        if (pipeLoc != -1) {
            // Has a limitation
            String lockPre = lock.substring(pipeLoc + 1);
            if (lockPre.startsWith("CLASS.TYPE=")) {
                String substring = lock.substring(pipeLoc + 12);
                if (substring.isEmpty()) {
                    return new ParseResult.Fail("Cannot have Empty Type Limitation in " + getTokenName() + ": " + value, context);
                }
                ParseResult pr = checkForIllegalSeparator('.', substring);
                if (!pr.passed()) {
                    return pr;
                }
                owner = context.getReferenceContext().getCDOMTypeReference(PCCLASS_CLASS, substring.split("\\."));
            } else if (lockPre.startsWith(Constants.LST_CLASS_EQUAL)) {
                String substring = lock.substring(pipeLoc + 7);
                if (substring.isEmpty()) {
                    return new ParseResult.Fail("Cannot have Empty Class Limitation in " + getTokenName() + ": " + value, context);
                }
                owner = context.getReferenceContext().getCDOMReference(PCCLASS_CLASS, substring);
            } else {
                return new ParseResult.Fail("Invalid Limitation in HITDIE: " + lockPre, context);
            }
            lock = lock.substring(0, pipeLoc);
        }
        Processor<HitDie> hdm;
        if (lock.startsWith("%/")) {
            // HITDIE:%/num --- divides the classes hit die by num.
            int denom = Integer.parseInt(lock.substring(2));
            if (denom <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive Integer " + "for dividing Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new DividingFormula(denom));
        } else if (lock.startsWith("%*")) {
            // HITDIE:%*num --- multiplies the classes hit die by num.
            int mult = Integer.parseInt(lock.substring(2));
            if (mult <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for multiplying Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new MultiplyingFormula(mult));
        } else if (lock.startsWith("%+")) {
            // possibly redundant with BONUS:HD MAX|num
            // HITDIE:%+num --- adds num to the classes hit die.
            int add = Integer.parseInt(lock.substring(2));
            if (add <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for adding Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new AddingFormula(add));
        } else if (lock.startsWith("%-")) {
            // HITDIE:%-num --- subtracts num from the classes hit die.
            // possibly redundant with BONUS:HD MAX|num if that will
            // take negative numbers.
            int sub = Integer.parseInt(lock.substring(2));
            if (sub <= 0) {
                return new ParseResult.Fail(getTokenName() + " was expecting a Positive " + "Integer for subtracting Lock, was : " + lock.substring(2), context);
            }
            hdm = new HitDieFormula(new SubtractingFormula(sub));
        } else if (lock.startsWith("%up")) {
            // HITDIE:%upnum --- moves the hit die num steps up the die size
            // list d4,d6,d8,d10,d12. Stops at d12.
            int steps = Integer.parseInt(lock.substring(3));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " up (must be positive)", context);
            }
            if (steps >= 5) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " up (too large)", context);
            }
            hdm = new HitDieStep(steps, new HitDie(12));
        } else if (lock.startsWith("%Hup")) {
            // HITDIE:%upnum --- moves the hit die num steps up the die size
            // list d4,d6,d8,d10,d12. Stops at d12.
            int steps = Integer.parseInt(lock.substring(4));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName(), context);
            }
            hdm = new HitDieStep(steps, null);
        } else if (lock.startsWith("%down")) {
            // HITDIE:%downnum --- moves the hit die num steps down the die
            // size
            // list d4,d6,d8,d10,d12. Stops at d4.
            int steps = Integer.parseInt(lock.substring(5));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " down (must be positive)", context);
            }
            if (steps >= 5) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName() + " down (too large)", context);
            }
            hdm = new HitDieStep(-steps, new HitDie(4));
        } else if (lock.startsWith("%Hdown")) {
            // HITDIE:%downnum --- moves the hit die num steps down the die
            // size
            // list. No limit.
            int steps = Integer.parseInt(lock.substring(6));
            if (steps <= 0) {
                return new ParseResult.Fail("Invalid Step Count: " + steps + " in " + getTokenName(), context);
            }
            hdm = new HitDieStep(-steps, null);
        } else {
            int i = Integer.parseInt(lock);
            if (i <= 0) {
                return new ParseResult.Fail("Invalid HitDie: " + i + " in " + getTokenName(), context);
            }
            // HITDIE:num --- sets the hit die to num regardless of class.
            hdm = new HitDieLock(new HitDie(i));
        }
        Processor<HitDie> mod = owner == null ? hdm : new ContextProcessor<>(hdm, owner);
        context.getObjectContext().put(race, ObjectKey.HITDIE, mod);
        return ParseResult.SUCCESS;
    } catch (NumberFormatException nfe) {
        return new ParseResult.Fail("Invalid Number (must be an Integer) in " + getTokenName() + ": " + nfe.getLocalizedMessage(), context);
    }
}
Also used : HitDieFormula(pcgen.cdom.processor.HitDieFormula) ParseResult(pcgen.rules.persistence.token.ParseResult) PCClass(pcgen.core.PCClass) MultiplyingFormula(pcgen.base.formula.MultiplyingFormula) HitDieLock(pcgen.cdom.processor.HitDieLock) AddingFormula(pcgen.base.formula.AddingFormula) HitDieStep(pcgen.cdom.processor.HitDieStep) HitDie(pcgen.cdom.content.HitDie) DividingFormula(pcgen.base.formula.DividingFormula) SubtractingFormula(pcgen.base.formula.SubtractingFormula)

Example 55 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class CrModToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Race race, String value) {
    int pipeLoc = value.indexOf(Constants.PIPE);
    if (pipeLoc == -1) {
        return new ParseResult.Fail(getTokenName() + " expecting '|', format is: " + "ClassTypes|CRMod was: " + value, context);
    }
    if (pipeLoc != value.lastIndexOf(Constants.PIPE)) {
        return new ParseResult.Fail(getTokenName() + " expecting only one '|', " + "format is: ClassTypes|CRMod was: " + value, context);
    }
    String keys = value.substring(0, pipeLoc);
    if (keys.isEmpty()) {
        return new ParseResult.Fail(getTokenName() + " expecting non-empty class type, " + "format is: ClassTypes|CRMod was: " + value, context);
    }
    String val = value.substring(pipeLoc + 1);
    if (val.isEmpty()) {
        return new ParseResult.Fail(getTokenName() + " expecting non-empty CR mod, " + "format is: ClassTypes|CRMod was: " + value, context);
    }
    try {
        StringTokenizer aTok = new StringTokenizer(keys, Constants.DOT, false);
        while (aTok.hasMoreTokens()) {
            context.getObjectContext().put(race, MapKey.CRMOD, aTok.nextToken(), Integer.valueOf(val));
        }
    } catch (NumberFormatException e) {
        return new ParseResult.Fail(getTokenName() + " expecting number CR mod, " + "format is: ClassTypes|CRMod was: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult)

Aggregations

ParseResult (pcgen.rules.persistence.token.ParseResult)210 StringTokenizer (java.util.StringTokenizer)68 Test (org.junit.Test)45 CDOMReference (pcgen.cdom.base.CDOMReference)30 AbstractTokenModelTest (tokenmodel.testsupport.AbstractTokenModelTest)26 Ability (pcgen.core.Ability)21 ArrayList (java.util.ArrayList)18 Formula (pcgen.base.formula.Formula)18 ParsingSeparator (pcgen.base.text.ParsingSeparator)18 Prerequisite (pcgen.core.prereq.Prerequisite)18 PCClass (pcgen.core.PCClass)17 Ungranted (pcgen.cdom.base.Ungranted)15 PCTemplate (pcgen.core.PCTemplate)14 CNAbility (pcgen.cdom.content.CNAbility)13 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)11 ComplexParseResult (pcgen.rules.persistence.token.ComplexParseResult)11 BigDecimal (java.math.BigDecimal)10 Race (pcgen.core.Race)10 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)9 LegalScope (pcgen.base.formula.base.LegalScope)8