Search in sources :

Example 26 with ParsingSeparator

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

the class LanguageToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    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 " + getFullName() + " must be > 0", context);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
    if (!pr.passed()) {
        return pr;
    }
    List<CDOMReference<Language>> refs = new ArrayList<>();
    StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
    while (tok.hasMoreTokens()) {
        String tokText = tok.nextToken();
        CDOMReference<Language> lang = TokenUtilities.getReference(context, LANGUAGE_CLASS, tokText);
        if (lang == null) {
            return new ParseResult.Fail("  Error was encountered while parsing " + getFullName() + ": " + value + " had an invalid reference: " + tokText, context);
        }
        refs.add(lang);
    }
    ReferenceChoiceSet<Language> rcs = new ReferenceChoiceSet<>(refs);
    if (!rcs.getGroupingState().isValid()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    ChoiceSet<Language> cs = new ChoiceSet<>(getTokenName(), rcs);
    cs.setTitle("Language Choice");
    PersistentTransitionChoice<Language> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    tc.setChoiceActor(this);
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) ArrayList(java.util.ArrayList) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) ParsingSeparator(pcgen.base.text.ParsingSeparator) Language(pcgen.core.Language) CDOMReference(pcgen.cdom.base.CDOMReference) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet)

Example 27 with ParsingSeparator

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

the class SkillToken method parseToken.

@Override
public ParseResult parseToken(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 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 " + getFullName() + " must be > 0", context);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
    if (!pr.passed()) {
        return pr;
    }
    StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
    List<CDOMReference<Skill>> refs = new ArrayList<>();
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        CDOMReference<Skill> ref;
        if (Constants.LST_ANY.equalsIgnoreCase(token)) {
            ref = context.getReferenceContext().getCDOMAllReference(SKILL_CLASS);
        } else {
            ref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, token);
            if (ref == null) {
                return new ParseResult.Fail("  Error was encountered while parsing " + getFullName() + ": " + token + " is not a valid reference: " + value, context);
            }
        }
        refs.add(ref);
    }
    ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
    if (!rcs.getGroupingState().isValid()) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    ChoiceSet<Skill> cs = new ChoiceSet<>("SKILL", rcs, true);
    PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    tc.setChoiceActor(this);
    tc.allowStack(true);
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet) ChoiceSet(pcgen.cdom.base.ChoiceSet) ArrayList(java.util.ArrayList) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) Skill(pcgen.core.Skill) ParsingSeparator(pcgen.base.text.ParsingSeparator) CDOMReference(pcgen.cdom.base.CDOMReference) ReferenceChoiceSet(pcgen.cdom.choiceset.ReferenceChoiceSet)

Example 28 with ParsingSeparator

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

the class SpellCasterToken method parseToken.

@Override
public ParseResult parseToken(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 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 " + getFullName() + " must be > 0", context);
        }
        activeValue = sep.next();
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
    if (!pr.passed()) {
        return pr;
    }
    StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
    boolean foundAny = false;
    boolean foundOther = false;
    List<CDOMReference<PCClass>> groups = new ArrayList<>();
    List<CDOMReference<PCClass>> prims = new ArrayList<>();
    List<String> spelltypes = new ArrayList<>();
    CDOMGroupRef<PCClass> allRef = context.getReferenceContext().getCDOMAllReference(PCCLASS_CLASS);
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        if (Constants.LST_ANY.equalsIgnoreCase(token)) {
            foundAny = true;
            groups.add(allRef);
        } else {
            foundOther = true;
            if (token.equals("Arcane") || token.equals("Divine") || token.equals("Psionic")) {
                spelltypes.add(token);
            } else if (token.startsWith(Constants.LST_TYPE_DOT) || token.startsWith(Constants.LST_TYPE_EQUAL)) {
                CDOMReference<PCClass> ref = TokenUtilities.getTypeReference(context, PCCLASS_CLASS, token.substring(5));
                if (ref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getFullName() + ": " + token + " is not a valid reference: " + value, context);
                }
                groups.add(ref);
            } else {
                prims.add(context.getReferenceContext().getCDOMReference(PCCLASS_CLASS, token));
            }
        }
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    ReferenceChoiceSet<PCClass> grcs = groups.isEmpty() ? null : new ReferenceChoiceSet<>(groups);
    ReferenceChoiceSet<PCClass> prcs = prims.isEmpty() ? null : new ReferenceChoiceSet<>(prims);
    SelectableSet<PCClass> cs = new SpellCasterChoiceSet(allRef, spelltypes, grcs, prcs);
    cs.setTitle("Spell Caster Class Choice");
    PersistentTransitionChoice<PCClass> tc = new ConcretePersistentTransitionChoice<>(cs, count);
    context.getObjectContext().addToList(obj, ListKey.ADD, tc);
    tc.setChoiceActor(this);
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) SpellCasterChoiceSet(pcgen.cdom.choiceset.SpellCasterChoiceSet) ArrayList(java.util.ArrayList) PCClass(pcgen.core.PCClass) ConcretePersistentTransitionChoice(pcgen.cdom.base.ConcretePersistentTransitionChoice) Formula(pcgen.base.formula.Formula) StringTokenizer(java.util.StringTokenizer) ParsingSeparator(pcgen.base.text.ParsingSeparator) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 29 with ParsingSeparator

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

the class SpellLevelToken method parseTokenWithSeparator.

@Override
public ParseResult parseTokenWithSeparator(LoadContext context, CDOMObject obj, String value) {
    int pipeLoc = value.lastIndexOf('|');
    String activeValue;
    String title;
    if (pipeLoc == -1) {
        activeValue = value;
        title = getDefaultTitle();
    } else {
        String titleString = value.substring(pipeLoc + 1);
        if (titleString.startsWith("TITLE=")) {
            title = titleString.substring(6);
            if (title.startsWith("\"")) {
                title = title.substring(1, title.length() - 1);
            }
            activeValue = value.substring(0, pipeLoc);
        } else {
            activeValue = value;
            title = getDefaultTitle();
        }
    }
    pipeLoc = value.indexOf('|');
    ParsingSeparator sep = new ParsingSeparator(activeValue, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    if (!sep.hasNext()) {
        return new ParseResult.Fail("Found no arguments in " + getFullName() + ": " + value, context);
    }
    List<SpellLevelInfo> sliList = new ArrayList<>();
    while (sep.hasNext()) {
        String token = sep.next();
        PrimitiveCollection<PCClass> pcf = context.getPrimitiveChoiceFilter(context.getReferenceContext().getManufacturer(PCClass.class), token);
        if (!sep.hasNext()) {
            return new ParseResult.Fail("Expected minimum level argument after " + token + " in " + getFullName() + ": " + value, context);
        }
        String minLevelString = sep.next();
        Formula minLevel = FormulaFactory.getFormulaFor(minLevelString);
        if (!sep.hasNext()) {
            return new ParseResult.Fail("Expected maximum level argument after " + minLevelString + " in " + getFullName() + ": " + value, context);
        }
        String maxLevelString = sep.next();
        Formula maxLevel = FormulaFactory.getFormulaFor(maxLevelString);
        if (!maxLevel.isValid()) {
            return new ParseResult.Fail("Max Level Formula in " + getTokenName() + " was not valid: " + maxLevel.toString(), context);
        }
        SpellLevelInfo sli = new SpellLevelInfo(pcf, minLevel, maxLevel);
        sliList.add(sli);
    }
    SpellLevelChooseInformation tc = new SpellLevelChooseInformation(getTokenName(), sliList);
    tc.setTitle(title);
    tc.setChoiceActor(this);
    context.getObjectContext().put(obj, ObjectKey.CHOOSE_INFO, tc);
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) SpellLevelInfo(pcgen.cdom.helper.SpellLevelInfo) ArrayList(java.util.ArrayList) SpellLevelChooseInformation(pcgen.cdom.base.SpellLevelChooseInformation) PCClass(pcgen.core.PCClass)

Example 30 with ParsingSeparator

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

the class CSVUtilities method generateCSVSeparator.

/**
	 * Generates a new "naive" CSV separator. This is not formally CSV compliant
	 * because it ignores "embedded" new lines. For purposes of PCGen this is
	 * acceptable.
	 * 
	 * @param lstLine
	 *            The line to be processed by a CSV-like ParsingSeparator
	 * @return A ParsingSeparator for the given line
	 */
public static ParsingSeparator generateCSVSeparator(String lstLine) {
    ParsingSeparator ps = new ParsingSeparator(lstLine, ',');
    ps.addGroupingPair('"', '"');
    return ps;
}
Also used : ParsingSeparator(pcgen.base.text.ParsingSeparator)

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