Search in sources :

Example 21 with ParsingSeparator

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

the class MonsterclassToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Race race, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, ':');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String classString = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " must have a colon: " + value, context);
    }
    String numLevels = sep.next();
    if (sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " must have only one colon: " + value, context);
    }
    CDOMSingleRef<PCClass> cl = context.getReferenceContext().getCDOMReference(PCCLASS_CLASS, classString);
    try {
        int lvls = Integer.parseInt(numLevels);
        if (lvls <= 0) {
            return new ParseResult.Fail("Number of levels in " + getTokenName() + " must be greater than zero: " + value, context);
        }
        LevelCommandFactory cf = new LevelCommandFactory(cl, FormulaFactory.getFormulaFor(lvls));
        context.getObjectContext().put(race, ObjectKey.MONSTER_CLASS, cf);
        return ParseResult.SUCCESS;
    } catch (NumberFormatException nfe) {
        return new ParseResult.Fail("Number of levels in " + getTokenName() + " must be an integer greater than zero: " + value, context);
    }
}
Also used : LevelCommandFactory(pcgen.cdom.content.LevelCommandFactory) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult) PCClass(pcgen.core.PCClass)

Example 22 with ParsingSeparator

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

the class KnownToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClassLevel level, String value) {
    context.getObjectContext().removeList(level, ListKey.KNOWN);
    ParsingSeparator sep = new ParsingSeparator(value, ',');
    sep.addGroupingPair('(', ')');
    while (sep.hasNext()) {
        String tok = sep.next();
        try {
            if (Integer.parseInt(tok) < 0) {
                return new ParseResult.Fail("Invalid Spell Count: " + tok + " is less than zero", context);
            }
        } catch (NumberFormatException e) {
        // OK, it must be a formula...
        }
        Formula formula = FormulaFactory.getFormulaFor(tok);
        if (!formula.isValid()) {
            return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + formula.toString(), context);
        }
        context.getObjectContext().addToList(level, ListKey.KNOWN, formula);
    }
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator)

Example 23 with ParsingSeparator

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

the class ChoiceSetLoadUtilities method getPrimitive.

public static <T extends CDOMObject> PrimitiveCollection<T> getPrimitive(LoadContext context, SelectionCreator<T> sc, String joinedOr) {
    if (joinedOr.isEmpty() || hasIllegalSeparator('|', joinedOr)) {
        return null;
    }
    List<PrimitiveCollection<T>> pcfOrList = new ArrayList<>();
    ParsingSeparator pipe = new ParsingSeparator(joinedOr, '|');
    pipe.addGroupingPair('[', ']');
    pipe.addGroupingPair('(', ')');
    for (; pipe.hasNext(); ) {
        String joinedAnd = pipe.next();
        if (joinedAnd.isEmpty() || hasIllegalSeparator(',', joinedAnd)) {
            return null;
        }
        List<PrimitiveCollection<T>> pcfAndList = new ArrayList<>();
        ParsingSeparator comma = new ParsingSeparator(joinedAnd, ',');
        comma.addGroupingPair('[', ']');
        comma.addGroupingPair('(', ')');
        for (; comma.hasNext(); ) {
            String primitive = comma.next();
            if (primitive == null || primitive.isEmpty()) {
                Logging.addParseMessage(Logging.LST_ERROR, "Choice argument was null or empty: " + primitive);
                return null;
            }
            PrimitiveCollection<T> pcf = getSimplePrimitive(context, sc, primitive);
            if (pcf == null) {
                Logging.addParseMessage(Logging.LST_ERROR, "Choice argument was not valid: " + primitive);
                return null;
            } else {
                pcfAndList.add(pcf);
            }
        }
        if (pcfAndList.size() == 1) {
            pcfOrList.add(pcfAndList.get(0));
        } else {
            pcfOrList.add(new CompoundAndPrimitive<>(pcfAndList));
        }
    }
    if (pcfOrList.size() == 1) {
        return pcfOrList.get(0);
    } else {
        return new CompoundOrPrimitive<>(pcfOrList);
    }
}
Also used : CompoundOrPrimitive(pcgen.cdom.primitive.CompoundOrPrimitive) ParsingSeparator(pcgen.base.text.ParsingSeparator) ArrayList(java.util.ArrayList) PrimitiveCollection(pcgen.cdom.base.PrimitiveCollection)

Example 24 with ParsingSeparator

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

the class ValuesToken method processRange.

private boolean processRange(KitTable kitTable, KitGear optionInfo, String range) {
    if (isEmpty(range) || hasIllegalSeparator(',', range)) {
        return false;
    }
    ParsingSeparator sep = new ParsingSeparator(range, ',');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String minString = sep.next();
    String maxString;
    if (sep.hasNext()) {
        maxString = sep.next();
    } else {
        maxString = range;
    }
    if (sep.hasNext()) {
        return false;
    }
    Formula min = FormulaFactory.getFormulaFor(minString);
    if (!min.isValid()) {
        Logging.errorPrint("Min Formula in " + getTokenName() + " was not valid: " + min.toString());
        return false;
    }
    Formula max = FormulaFactory.getFormulaFor(maxString);
    if (!max.isValid()) {
        Logging.errorPrint("Max Formula in " + getTokenName() + " was not valid: " + max.toString());
        return false;
    }
    kitTable.addGear(optionInfo, min, max);
    return true;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator)

Example 25 with ParsingSeparator

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

the class AbilityToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    if (isEmpty(value)) {
        return new ParseResult.Fail("Value in " + getFullName() + " may not be empty", context);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String first = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail("Syntax of ADD:" + getTokenName() + " requires 3 to 4 |: " + value, context);
    }
    String second = sep.next();
    if (!sep.hasNext()) {
        return new ParseResult.Fail("Syntax of ADD:" + getTokenName() + " requires a minimum of three | : " + value, context);
    }
    String third = sep.next();
    Formula count;
    if (sep.hasNext()) {
        count = FormulaFactory.getFormulaFor(first);
        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 " + getFullName() + " must be > 0", context);
        }
        first = second;
        second = third;
        third = sep.next();
    } else {
        count = FormulaFactory.ONE;
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail("Syntax of ADD:" + getTokenName() + " has max of four | when a count is not present: " + value, context);
    }
    CDOMSingleRef<AbilityCategory> acRef = context.getReferenceContext().getCDOMReference(ABILITY_CATEGORY_CLASS, first);
    Nature nature = Nature.valueOf(second);
    if (nature == null) {
        return new ParseResult.Fail(getFullName() + ": Invalid ability nature: " + second, context);
    }
    if (Nature.ANY.equals(nature)) {
        return new ParseResult.Fail(getTokenName() + " refers to ANY Ability Nature, cannot be used in " + getTokenName() + ": " + value);
    }
    if (Nature.AUTOMATIC.equals(nature)) {
        return new ParseResult.Fail(getTokenName() + " refers to AUTOMATIC Ability Nature, cannot be used in " + getTokenName() + ": " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', third);
    if (!pr.passed()) {
        return pr;
    }
    List<CDOMReference<Ability>> refs = new ArrayList<>();
    ParsingSeparator tok = new ParsingSeparator(third, ',');
    tok.addGroupingPair('[', ']');
    tok.addGroupingPair('(', ')');
    boolean allowStack = false;
    int dupChoices = 0;
    ReferenceManufacturer<Ability> rm = context.getReferenceContext().getManufacturer(ABILITY_CLASS, ABILITY_CATEGORY_CLASS, first);
    if (rm == null) {
        return new ParseResult.Fail("Could not get Reference Manufacturer for Category: " + first, context);
    }
    while (tok.hasNext()) {
        CDOMReference<Ability> ab;
        String token = tok.next();
        if ("STACKS".equals(token)) {
            if (allowStack) {
                return new ParseResult.Fail(getFullName() + " found second stacking specification in value: " + value, context);
            }
            allowStack = true;
            continue;
        } else if (token.startsWith("STACKS=")) {
            if (allowStack) {
                return new ParseResult.Fail(getFullName() + " found second stacking specification in value: " + value, context);
            }
            allowStack = true;
            try {
                dupChoices = Integer.parseInt(token.substring(7));
            } catch (NumberFormatException nfe) {
                return new ParseResult.Fail("Invalid Stack number in " + getFullName() + ": " + value, context);
            }
            if (dupChoices <= 0) {
                return new ParseResult.Fail("Invalid (less than 1) Stack number in " + getFullName() + ": " + value, context);
            }
            continue;
        } else {
            if (Constants.LST_ALL.equals(token)) {
                ab = rm.getAllReference();
            } else {
                ab = TokenUtilities.getTypeOrPrimitive(rm, token);
            }
        }
        if (ab == null) {
            return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName() + ": " + value + " had an invalid reference: " + token, context);
        }
        refs.add(ab);
    }
    if (refs.isEmpty()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains no ability reference: " + value, context);
    }
    AbilityRefChoiceSet rcs = new AbilityRefChoiceSet(acRef, refs, nature);
    if (!rcs.getGroupingState().isValid()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    AbilityChoiceSet cs = new AbilityChoiceSet(getTokenName(), rcs);
    StringBuilder title = new StringBuilder(50);
    if (!Nature.NORMAL.equals(nature)) {
        title.append(nature.toString());
        title.append(' ');
    }
    title.append(first);
    title.append(" Choice");
    cs.setTitle(title.toString());
    PersistentTransitionChoice<CNAbilitySelection> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    tc.allowStack(allowStack);
    if (dupChoices != 0) {
        tc.setStackLimit(dupChoices);
    }
    tc.setChoiceActor(this);
    return ParseResult.SUCCESS;
}
Also used : Nature(pcgen.cdom.enumeration.Nature) Ability(pcgen.core.Ability) CNAbility(pcgen.cdom.content.CNAbility) ParseResult(pcgen.rules.persistence.token.ParseResult) ArrayList(java.util.ArrayList) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) AbilityChoiceSet(pcgen.cdom.base.ChoiceSet.AbilityChoiceSet) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) AbilityCategory(pcgen.core.AbilityCategory) CDOMReference(pcgen.cdom.base.CDOMReference) AbilityRefChoiceSet(pcgen.cdom.choiceset.AbilityRefChoiceSet)

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