Search in sources :

Example 56 with ParseResult

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

the class RoleToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCClass pcc, String value) {
    ParseResult pr = checkForIllegalSeparator('.', value);
    if (!pr.passed()) {
        return pr;
    }
    StringTokenizer aTok = new StringTokenizer(value, Constants.DOT, false);
    while (aTok.hasMoreTokens()) {
        String role = aTok.nextToken();
        if (SettingsHandler.getGame().getMonsterRoleList().contains(role)) {
            context.getObjectContext().addToList(pcc, ListKey.MONSTER_ROLES, role);
        } else {
            return new ParseResult.Fail(getTokenName() + " '" + role + "' is not a known monster role for this game mode.", context);
        }
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult)

Example 57 with ParseResult

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

the class VisibleToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCClass pcc, String value) {
    Visibility vis;
    if (value.equals("NO")) {
        vis = Visibility.HIDDEN;
    } else if (value.equalsIgnoreCase("Y") || value.equals("YES")) {
        vis = Visibility.DEFAULT;
    } else {
        return new ParseResult.Fail("Can't understand Visibility: " + value, context);
    }
    context.getObjectContext().put(pcc, ObjectKey.VISIBILITY, vis);
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) Visibility(pcgen.util.enumeration.Visibility)

Example 58 with ParseResult

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

the class RoleToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Race race, String value) {
    ParseResult pr = checkForIllegalSeparator('.', value);
    if (!pr.passed()) {
        return pr;
    }
    StringTokenizer aTok = new StringTokenizer(value, Constants.DOT, false);
    while (aTok.hasMoreTokens()) {
        String role = aTok.nextToken();
        if (SettingsHandler.getGame().getMonsterRoleList().contains(role)) {
            context.getObjectContext().addToList(race, ListKey.MONSTER_ROLES, role);
        } else {
            return new ParseResult.Fail(getTokenName() + " '" + role + "' is not a known monster role for this game mode.", context);
        }
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult)

Example 59 with ParseResult

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

the class CskillToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClassLevel obj, String value) {
    boolean first = true;
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
    while (tok.hasMoreTokens()) {
        String tokText = tok.nextToken();
        if (Constants.LST_DOT_CLEAR.equals(tokText)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
            }
            context.getObjectContext().removeList(obj, ListKey.LOCALCSKILL);
        } else if (tokText.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            String clearText = tokText.substring(7);
            if (Constants.LST_ALL.equals(clearText)) {
                context.getObjectContext().removeFromList(obj, ListKey.LOCALCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
            } else {
                CDOMReference<Skill> ref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, clearText);
                if (ref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
                }
                context.getObjectContext().removeFromList(obj, ListKey.LOCALCSKILL, ref);
            }
        } else {
            /*
				 * Note this HAS to be done one-by-one, because the
				 * .clearChildNodeOfClass method above does NOT recognize the
				 * C/CC Skill object and therefore doesn't know how to search
				 * the sublists
				 */
            if (Constants.LST_ALL.equals(tokText)) {
                foundAny = true;
                context.getObjectContext().addToList(obj, ListKey.LOCALCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
            } else {
                foundOther = true;
                CDOMReference<Skill> ref = getSkillReference(context, tokText);
                if (ref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
                }
                context.getObjectContext().addToList(obj, ListKey.LOCALCSKILL, ref);
            }
        }
        first = false;
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 60 with ParseResult

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

the class SpellsLst method parseNonEmptyToken.

/**
	 * {@literal 
	 * SPELLS:<spellbook name>|[<optional parameters, pipe deliminated>] |<spell
	 * name>[,<formula for DC>] |<spell name2>[,<formula2 for DC>] |PRExxx
	 * |PRExxx
	 *
	 * CASTERLEVEL=<formula> Casterlevel of spells TIMES=<formula> Cast Times
	 * per day, -1=At Will
	 *}
	 * @param sourceLine
	 *            Line from the LST file without the SPELLS:
	 * @return spells list
	 */
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String sourceLine) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    if ((sourceLine == null) || sourceLine.isEmpty()) {
        return new ParseResult.Fail("Argument in " + getTokenName() + " cannot be empty", context);
    }
    if (sourceLine.equals(Constants.LST_DOT_CLEAR_ALL)) {
        context.getListContext().removeAllFromList(getTokenName(), obj, Spell.SPELLS);
        return ParseResult.SUCCESS;
    }
    ParsingSeparator sep = new ParsingSeparator(sourceLine, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    String spellBook = sep.next();
    if (spellBook.isEmpty()) {
        return new ParseResult.Fail("SpellBook in " + getTokenName() + " cannot be empty", context);
    }
    // Formula casterLevel = null;
    String casterLevel = null;
    String times = null;
    String timeunit = null;
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + ": minimally requires a Spell Name", context);
    }
    String token = sep.next();
    while (true) {
        if (token.startsWith("TIMES=")) {
            if (times != null) {
                return new ParseResult.Fail("Found two TIMES entries in " + getTokenName() + ": invalid: " + sourceLine, context);
            }
            times = token.substring(6);
            if (times.isEmpty()) {
                return new ParseResult.Fail("Error in Times in " + getTokenName() + ": argument was empty", context);
            }
            if (!sep.hasNext()) {
                return new ParseResult.Fail(getTokenName() + ": minimally requires " + "a Spell Name (after TIMES=)", context);
            }
            token = sep.next();
        } else if (token.startsWith("TIMEUNIT=")) {
            if (timeunit != null) {
                return new ParseResult.Fail("Found two TIMEUNIT entries in " + getTokenName() + ": invalid: " + sourceLine, context);
            }
            timeunit = token.substring(9);
            if (timeunit.isEmpty()) {
                return new ParseResult.Fail("Error in TimeUnit in " + getTokenName() + ": argument was empty", context);
            }
            if (!sep.hasNext()) {
                return new ParseResult.Fail(getTokenName() + ": minimally requires " + "a Spell Name (after TIMEUNIT=)", context);
            }
            token = sep.next();
        } else if (token.startsWith("CASTERLEVEL=")) {
            if (casterLevel != null) {
                return new ParseResult.Fail("Found two CASTERLEVEL entries in " + getTokenName() + ": invalid: " + sourceLine, context);
            }
            casterLevel = token.substring(12);
            if (casterLevel.isEmpty()) {
                return new ParseResult.Fail("Error in Caster Level in " + getTokenName() + ": argument was empty", context);
            }
            if (!sep.hasNext()) {
                return new ParseResult.Fail(getTokenName() + ": minimally requires a " + "Spell Name (after CASTERLEVEL=)", context);
            }
            token = sep.next();
        } else {
            break;
        }
    }
    if (times == null) {
        times = "1";
    }
    if (token.isEmpty()) {
        return new ParseResult.Fail("Spell arguments may not be empty in " + getTokenName() + ": " + sourceLine, context);
    }
    if (token.charAt(0) == ',') {
        return new ParseResult.Fail(getTokenName() + " Spell arguments may not start with , : " + token, context);
    }
    if (token.charAt(token.length() - 1) == ',') {
        return new ParseResult.Fail(getTokenName() + " Spell arguments may not end with , : " + token, context);
    }
    if (token.indexOf(",,") != -1) {
        return new ParseResult.Fail(getTokenName() + " Spell arguments uses double separator ,, : " + token, context);
    }
    /*
		 * CONSIDER This is currently order enforcing the reference fetching to
		 * match the integration tests that we perform, and their current
		 * behavior. Not sure if this is really tbe best solution?
		 *
		 * See CDOMObject.
		 */
    DoubleKeyMap<CDOMReference<Spell>, AssociationKey<?>, Object> dkm = new DoubleKeyMap<>(LinkedHashMap.class, HashMap.class);
    while (true) {
        if (token.isEmpty()) {
            return new ParseResult.Fail("Spell arguments may not end with comma or pipe in " + getTokenName() + ": " + sourceLine, context);
        }
        int commaLoc = token.indexOf(',');
        String name = commaLoc == -1 ? token : token.substring(0, commaLoc);
        CDOMReference<Spell> spell = context.getReferenceContext().getCDOMReference(Spell.class, name);
        dkm.put(spell, AssociationKey.CASTER_LEVEL, casterLevel);
        Formula timesFormula = FormulaFactory.getFormulaFor(times);
        if (!timesFormula.isValid()) {
            return new ParseResult.Fail("Times in " + getTokenName() + " was not valid: " + timesFormula.toString(), context);
        }
        dkm.put(spell, AssociationKey.TIMES_PER_UNIT, timesFormula);
        if (timeunit != null) {
            dkm.put(spell, AssociationKey.TIME_UNIT, timeunit);
        }
        dkm.put(spell, AssociationKey.SPELLBOOK, spellBook);
        if (commaLoc != -1) {
            dkm.put(spell, AssociationKey.DC_FORMULA, token.substring(commaLoc + 1));
        }
        if (!sep.hasNext()) {
            // No prereqs, so we're done
            finish(context, obj, dkm, null);
            return ParseResult.SUCCESS;
        }
        token = sep.next();
        if (looksLikeAPrerequisite(token)) {
            break;
        }
    }
    List<Prerequisite> prereqs = new ArrayList<>();
    while (true) {
        Prerequisite prereq = getPrerequisite(token);
        if (prereq == null) {
            return new ParseResult.Fail("   (Did you put spells after the " + "PRExxx tags in SPELLS:?)", context);
        }
        prereqs.add(prereq);
        if (!sep.hasNext()) {
            break;
        }
        token = sep.next();
    }
    finish(context, obj, dkm, prereqs);
    return ParseResult.SUCCESS;
}
Also used : AssociationKey(pcgen.cdom.enumeration.AssociationKey) ParseResult(pcgen.rules.persistence.token.ParseResult) ArrayList(java.util.ArrayList) Ungranted(pcgen.cdom.base.Ungranted) Spell(pcgen.core.spell.Spell) Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) CDOMObject(pcgen.cdom.base.CDOMObject) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) DoubleKeyMap(pcgen.base.util.DoubleKeyMap) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

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