Search in sources :

Example 71 with ParseResult

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

the class LevelToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCTemplate template, String value) {
    StringTokenizer tok = new StringTokenizer(value, Constants.COLON);
    String levelStr = tok.nextToken();
    int plusLoc = levelStr.indexOf('+');
    if (plusLoc == 0) {
        return new ParseResult.Fail("Malformed " + getTokenName() + " Level cannot start with +: " + value, context);
    }
    int lvl;
    try {
        /*
			 * Note this test of integer (even if it doesn't get used outside
			 * this try) is necessary for catching errors.
			 */
        lvl = Integer.parseInt(levelStr);
        if (lvl <= 0) {
            ComplexParseResult cpr = new ComplexParseResult();
            cpr.addErrorMessage("Malformed " + getTokenName() + " Token (Level was <= 0): " + lvl);
            cpr.addErrorMessage("  Line was: " + value);
            return cpr;
        }
    } catch (NumberFormatException ex) {
        return new ParseResult.Fail("Misunderstood Level value: " + levelStr + " in " + getTokenName(), context);
    }
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail("Invalid " + getTokenName() + ": requires 3 colon separated elements (has one): " + value, context);
    }
    String typeStr = tok.nextToken();
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail("Invalid " + getTokenName() + ": requires 3 colon separated elements (has two): " + value, context);
    }
    String argument = tok.nextToken();
    PCTemplate derivative = new PCTemplate();
    derivative.put(ObjectKey.VISIBILITY, Visibility.HIDDEN);
    derivative.put(IntegerKey.LEVEL, lvl);
    context.getReferenceContext().getManufacturer(PCTemplate.class).addDerivativeObject(derivative);
    context.getObjectContext().addToList(template, ListKey.LEVEL_TEMPLATES, derivative);
    try {
        if (context.processToken(derivative, typeStr, argument)) {
            return ParseResult.SUCCESS;
        }
    } catch (PersistenceLayerException e) {
        return new ParseResult.Fail(e.getMessage(), context);
    }
    return ParseResult.INTERNAL_ERROR;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringTokenizer(java.util.StringTokenizer) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) ParseResult(pcgen.rules.persistence.token.ParseResult) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) PCTemplate(pcgen.core.PCTemplate)

Example 72 with ParseResult

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

the class AcheckToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Skill skill, String value) {
    SkillArmorCheck aCheck;
    try {
        aCheck = SkillArmorCheck.valueOf(value);
    } catch (IllegalArgumentException iae) {
        /*
			 * TODO516 turn on deprecation
			 */
        // Logging.deprecationPrint("Misunderstood " + getTokenName() + ": "
        // + value + " is not an abbreviation");
        char first = value.charAt(0);
        if (first == 'N') {
            // Logging.deprecationPrint(" please use NONE");
            aCheck = SkillArmorCheck.NONE;
        } else if (first == 'Y') {
            // Logging.deprecationPrint(" please use YES");
            aCheck = SkillArmorCheck.YES;
        } else if (first == 'P') {
            // Logging.deprecationPrint(" please use NONPROF");
            aCheck = SkillArmorCheck.NONPROF;
        } else if (first == 'D') {
            // Logging.deprecationPrint(" please use DOUBLE");
            aCheck = SkillArmorCheck.DOUBLE;
        } else if (first == 'W') {
            // Logging.deprecationPrint(" please use WEIGHT");
            aCheck = SkillArmorCheck.WEIGHT;
        } else {
            return new ParseResult.Fail("Skill " + getTokenName() + " Did not understand: " + value, context);
        }
    }
    context.getObjectContext().put(skill, ObjectKey.ARMOR_CHECK, aCheck);
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) SkillArmorCheck(pcgen.cdom.enumeration.SkillArmorCheck)

Example 73 with ParseResult

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

the class CostToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Spell spell, String value) {
    if (Constants.LST_DOT_CLEAR.equals(value)) {
        context.getObjectContext().remove(spell, ObjectKey.COST);
    } else {
        try {
            BigDecimal cost = new BigDecimal(value);
            if (cost.compareTo(BigDecimal.ZERO) <= 0) {
                return new ParseResult.Fail(getTokenName() + " requires a positive Integer", context);
            }
            context.getObjectContext().put(spell, ObjectKey.COST, cost);
        } catch (NumberFormatException nfe) {
            return new ParseResult.Fail(getTokenName() + " expected an integer.  Tag must be of the form: " + getTokenName() + ":<int>", context);
        }
    }
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) BigDecimal(java.math.BigDecimal)

Example 74 with ParseResult

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

the class ArmorProfToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    String armorProf;
    // Do not initialize, null is significant!
    Prerequisite prereq = null;
    boolean isPre = false;
    if (value.indexOf("[") == -1) {
        // Supported version of PRExxx using |.  Needs to be at the front of the
        // Parsing code because many objects expect the pre to have been determined
        // Ahead of time.  Until deprecated code is removed, it will have to stay
        // like this.
        armorProf = value;
        StringTokenizer tok = new StringTokenizer(armorProf, Constants.PIPE);
        while (tok.hasMoreTokens()) {
            String token = tok.nextToken();
            if (PreParserFactory.isPreReqString(token)) {
                if (isPre) {
                    String errorText = "Invalid " + getTokenName() + ": " + value + "  PRExxx must be at the END of the Token";
                    Logging.errorPrint(errorText);
                    return new ParseResult.Fail(errorText, context);
                }
                prereq = getPrerequisite(token);
                if (prereq == null) {
                    return new ParseResult.Fail("Error generating Prerequisite " + prereq + " in " + getFullName(), context);
                }
                int preStart = value.indexOf(token) - 1;
                armorProf = value.substring(0, preStart);
                isPre = true;
            }
        }
    } else {
        return new ParseResult.Fail("Use of [] for Prerequisites has been removed. " + "Please use | based standard", context);
    }
    ParseResult pr = checkSeparatorsAndNonEmpty('|', armorProf);
    if (!pr.passed()) {
        return pr;
    }
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(armorProf, Constants.PIPE);
    List<CDOMReference<ArmorProf>> armorProfs = new ArrayList<>();
    List<CDOMReference<Equipment>> equipTypes = new ArrayList<>();
    while (tok.hasMoreTokens()) {
        String aProf = tok.nextToken();
        if (Constants.LST_PERCENT_LIST.equals(aProf)) {
            foundOther = true;
            ChooseSelectionActor<ArmorProf> cra;
            if (prereq == null) {
                cra = this;
            } else {
                ConditionalSelectionActor<ArmorProf> cca = new ConditionalSelectionActor<>(this);
                cca.addPrerequisite(prereq);
                cra = cca;
            }
            context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, cra);
        } else if (Constants.LST_ALL.equalsIgnoreCase(aProf)) {
            foundAny = true;
            armorProfs.add(context.getReferenceContext().getCDOMAllReference(ARMORPROF_CLASS));
        } else if (aProf.startsWith("ARMORTYPE.") || aProf.startsWith("ARMORTYPE=")) {
            foundOther = true;
            CDOMReference<Equipment> ref = TokenUtilities.getTypeReference(context, EQUIPMENT_CLASS, "ARMOR." + aProf.substring(10));
            if (ref == null) {
                return ParseResult.INTERNAL_ERROR;
            }
            equipTypes.add(ref);
        } else {
            foundOther = true;
            armorProfs.add(context.getReferenceContext().getCDOMReference(ARMORPROF_CLASS, aProf));
        }
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    if (!armorProfs.isEmpty() || !equipTypes.isEmpty()) {
        ArmorProfProvider pp = new ArmorProfProvider(armorProfs, equipTypes);
        if (prereq != null) {
            pp.addPrerequisite(prereq);
        }
        context.getObjectContext().addToList(obj, ListKey.AUTO_ARMORPROF, pp);
    }
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) ArrayList(java.util.ArrayList) StringTokenizer(java.util.StringTokenizer) ArmorProfProvider(pcgen.cdom.helper.ArmorProfProvider) ConditionalSelectionActor(pcgen.cdom.content.ConditionalSelectionActor) Equipment(pcgen.core.Equipment) ArmorProf(pcgen.core.ArmorProf) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 75 with ParseResult

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

the class ShieldProfToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    String shieldProf;
    // Do not initialize, null is significant!
    Prerequisite prereq = null;
    boolean isPre = false;
    if (value.indexOf("[") == -1) {
        // Supported version of PRExxx using |.  Needs to be at the front of the
        // Parsing code because many objects expect the pre to have been determined
        // Ahead of time.  Until deprecated code is removed, it will have to stay
        // like this.
        shieldProf = value;
        StringTokenizer tok = new StringTokenizer(shieldProf, Constants.PIPE);
        while (tok.hasMoreTokens()) {
            String token = tok.nextToken();
            if (PreParserFactory.isPreReqString(token)) {
                if (isPre) {
                    String errorText = "Invalid " + getTokenName() + ": " + value + "  PRExxx must be at the END of the Token";
                    Logging.errorPrint(errorText);
                    return new ParseResult.Fail(errorText, context);
                }
                prereq = getPrerequisite(token);
                if (prereq == null) {
                    return new ParseResult.Fail("Error generating Prerequisite " + prereq + " in " + getFullName(), context);
                }
                int preStart = value.indexOf(token) - 1;
                shieldProf = value.substring(0, preStart);
                isPre = true;
            }
        }
    } else {
        return new ParseResult.Fail("Use of [] for Prerequisites has been removed. " + "Please use | based standard", context);
    }
    ParseResult pr = checkForIllegalSeparator('|', shieldProf);
    if (!pr.passed()) {
        return pr;
    }
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(shieldProf, Constants.PIPE);
    List<CDOMReference<ShieldProf>> shieldProfs = new ArrayList<>();
    List<CDOMReference<Equipment>> equipTypes = new ArrayList<>();
    while (tok.hasMoreTokens()) {
        String aProf = tok.nextToken();
        if (Constants.LST_PERCENT_LIST.equals(aProf)) {
            foundOther = true;
            ChooseSelectionActor<ShieldProf> cra;
            if (prereq == null) {
                cra = this;
            } else {
                ConditionalSelectionActor<ShieldProf> cca = new ConditionalSelectionActor<>(this);
                cca.addPrerequisite(prereq);
                cra = cca;
            }
            context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, cra);
        } else if (Constants.LST_ALL.equalsIgnoreCase(aProf)) {
            foundAny = true;
            shieldProfs.add(context.getReferenceContext().getCDOMAllReference(SHIELDPROF_CLASS));
        } else if (aProf.startsWith("SHIELDTYPE.") || aProf.startsWith("SHIELDTYPE=")) {
            foundOther = true;
            CDOMReference<Equipment> ref = TokenUtilities.getTypeReference(context, EQUIPMENT_CLASS, "SHIELD." + aProf.substring(11));
            if (ref == null) {
                return ParseResult.INTERNAL_ERROR;
            }
            equipTypes.add(ref);
        } else {
            foundOther = true;
            shieldProfs.add(context.getReferenceContext().getCDOMReference(SHIELDPROF_CLASS, aProf));
        }
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    if (!shieldProfs.isEmpty() || !equipTypes.isEmpty()) {
        ShieldProfProvider pp = new ShieldProfProvider(shieldProfs, equipTypes);
        if (prereq != null) {
            pp.addPrerequisite(prereq);
        }
        context.getObjectContext().addToList(obj, ListKey.AUTO_SHIELDPROF, pp);
    }
    return ParseResult.SUCCESS;
}
Also used : ShieldProf(pcgen.core.ShieldProf) ParseResult(pcgen.rules.persistence.token.ParseResult) ArrayList(java.util.ArrayList) StringTokenizer(java.util.StringTokenizer) ConditionalSelectionActor(pcgen.cdom.content.ConditionalSelectionActor) Equipment(pcgen.core.Equipment) ShieldProfProvider(pcgen.cdom.helper.ShieldProfProvider) 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