diff --git a/include/plasp/pddl/expressions/Binary.h b/include/plasp/pddl/expressions/Binary.h index 4aafbab..da3455f 100644 --- a/include/plasp/pddl/expressions/Binary.h +++ b/include/plasp/pddl/expressions/Binary.h @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace plasp @@ -48,7 +49,7 @@ std::unique_ptr Binary::parse(Context &context, const auto position = parser.position(); if (!parser.probe("(") - || !parser.probeIdentifier(Derived::Identifier)) + || !parser.probeIdentifier(Derived::Identifier, isIdentifier)) { parser.seek(position); return nullptr; diff --git a/include/plasp/pddl/expressions/NAry.h b/include/plasp/pddl/expressions/NAry.h index c14ff9a..8d1ca6c 100644 --- a/include/plasp/pddl/expressions/NAry.h +++ b/include/plasp/pddl/expressions/NAry.h @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace plasp @@ -46,7 +47,7 @@ std::unique_ptr NAry::parse(Context &context, const auto position = parser.position(); if (!parser.probe("(") - || !parser.probeIdentifier(Derived::Identifier)) + || !parser.probeIdentifier(Derived::Identifier, isIdentifier)) { parser.seek(position); return nullptr; diff --git a/include/plasp/pddl/expressions/Not.h b/include/plasp/pddl/expressions/Not.h index 80c60aa..c008bf0 100644 --- a/include/plasp/pddl/expressions/Not.h +++ b/include/plasp/pddl/expressions/Not.h @@ -3,6 +3,7 @@ #include #include +#include namespace plasp { @@ -44,7 +45,7 @@ NotPointer Not::parse(Context &context, ExpressionContext &expressionContext, const auto position = parser.position(); if (!parser.probe("(") - || !parser.probeIdentifier("not")) + || !parser.probeIdentifier("not", isIdentifier)) { parser.seek(position); return nullptr; diff --git a/include/plasp/utils/Parser.h b/include/plasp/utils/Parser.h index 89e7914..beb3ca0 100644 --- a/include/plasp/utils/Parser.h +++ b/include/plasp/utils/Parser.h @@ -67,7 +67,8 @@ class Parser template bool probe(const Type &expectedValue); - bool probeIdentifier(const std::string &identifier); + template + bool probeIdentifier(const std::string &identifier, CharacterPredicate characterPredicate); template void expect(const Type &expectedValue); @@ -130,6 +131,14 @@ std::string Parser::parseIdentifier(CharacterPredicate characterPredicate) //////////////////////////////////////////////////////////////////////////////////////////////////// +template +bool Parser::probeIdentifier(const std::string &expectedValue, CharacterPredicate characterPredicate) +{ + return probe(expectedValue) && !characterPredicate(currentCharacter()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + template void Parser::skipWhiteSpace(WhiteSpacePredicate whiteSpacePredicate) { diff --git a/src/plasp/pddl/Action.cpp b/src/plasp/pddl/Action.cpp index 4cc1d98..53dfe07 100644 --- a/src/plasp/pddl/Action.cpp +++ b/src/plasp/pddl/Action.cpp @@ -42,9 +42,9 @@ void Action::parseDeclaration(Context &context, Domain &domain) { context.parser.expect(":"); - if (context.parser.probeIdentifier("precondition")) + if (context.parser.probeIdentifier("precondition", isIdentifier)) action->m_precondition = parsePreconditionExpression(context, expressionContext); - else if (context.parser.probeIdentifier("effect")) + else if (context.parser.probeIdentifier("effect", isIdentifier)) action->m_effect = parseEffectExpression(context, expressionContext); context.parser.skipWhiteSpace(); diff --git a/src/plasp/pddl/Domain.cpp b/src/plasp/pddl/Domain.cpp index 9f19ea5..b275c02 100644 --- a/src/plasp/pddl/Domain.cpp +++ b/src/plasp/pddl/Domain.cpp @@ -75,23 +75,23 @@ void Domain::findSections() const auto sectionIdentifierPosition = parser.position(); // Save the parser position of the individual sections for later parsing - if (parser.probeIdentifier("requirements")) + if (parser.probeIdentifier("requirements", isIdentifier)) setSectionPosition("requirements", m_requirementsPosition, position, true); - else if (parser.probeIdentifier("types")) + else if (parser.probeIdentifier("types", isIdentifier)) setSectionPosition("types", m_typesPosition, position, true); - else if (parser.probeIdentifier("constants")) + else if (parser.probeIdentifier("constants", isIdentifier)) setSectionPosition("constants", m_constantsPosition, position, true); - else if (parser.probeIdentifier("predicates")) + else if (parser.probeIdentifier("predicates", isIdentifier)) setSectionPosition("predicates", m_predicatesPosition, position, true); - else if (parser.probeIdentifier("action")) + else if (parser.probeIdentifier("action", isIdentifier)) { m_actionPositions.emplace_back(-1); setSectionPosition("action", m_actionPositions.back(), position); } - else if (parser.probeIdentifier("functions") - || parser.probeIdentifier("constraints") - || parser.probeIdentifier("durative-action") - || parser.probeIdentifier("derived")) + else if (parser.probeIdentifier("functions", isIdentifier) + || parser.probeIdentifier("constraints", isIdentifier) + || parser.probeIdentifier("durative-action", isIdentifier) + || parser.probeIdentifier("derived", isIdentifier)) { parser.seek(sectionIdentifierPosition); diff --git a/src/plasp/pddl/Expression.cpp b/src/plasp/pddl/Expression.cpp index 455c8b8..eff798b 100644 --- a/src/plasp/pddl/Expression.cpp +++ b/src/plasp/pddl/Expression.cpp @@ -57,8 +57,8 @@ ExpressionPointer parsePreconditionExpression(Context &context, const auto expressionIdentifierPosition = parser.position(); - if (parser.probeIdentifier("forall") - || parser.probeIdentifier("preference")) + if (parser.probeIdentifier("forall", isIdentifier) + || parser.probeIdentifier("preference", isIdentifier)) { // TODO: refactor parser.seek(expressionIdentifierPosition); @@ -102,19 +102,19 @@ ExpressionPointer parseExpression(Context &context, ExpressionContext &expressio const auto expressionIdentifierPosition = parser.position(); - if (parser.probeIdentifier("exists") - || parser.probeIdentifier("forall") - || parser.probeIdentifier("-") - || parser.probeIdentifier("=") - || parser.probeIdentifier("*") - || parser.probeIdentifier("+") - || parser.probeIdentifier("-") - || parser.probeIdentifier("/") - || parser.probeIdentifier(">") - || parser.probeIdentifier("<") - || parser.probeIdentifier("=") - || parser.probeIdentifier(">=") - || parser.probeIdentifier("<=")) + if (parser.probeIdentifier("exists", isIdentifier) + || parser.probeIdentifier("forall", isIdentifier) + || parser.probeIdentifier("-", isIdentifier) + || parser.probeIdentifier("=", isIdentifier) + || parser.probeIdentifier("*", isIdentifier) + || parser.probeIdentifier("+", isIdentifier) + || parser.probeIdentifier("-", isIdentifier) + || parser.probeIdentifier("/", isIdentifier) + || parser.probeIdentifier(">", isIdentifier) + || parser.probeIdentifier("<", isIdentifier) + || parser.probeIdentifier("=", isIdentifier) + || parser.probeIdentifier(">=", isIdentifier) + || parser.probeIdentifier("<=", isIdentifier)) { parser.seek(expressionIdentifierPosition); const auto expressionIdentifier = parser.parseIdentifier(isIdentifier); @@ -152,8 +152,8 @@ ExpressionPointer parseEffectExpression(Context &context, ExpressionContext &exp const auto expressionIdentifierPosition = parser.position(); - if (parser.probeIdentifier("forall") - || parser.probeIdentifier("when")) + if (parser.probeIdentifier("forall", isIdentifier) + || parser.probeIdentifier("when", isIdentifier)) { parser.seek(expressionIdentifierPosition); const auto expressionIdentifier = parser.parseIdentifier(isIdentifier); @@ -191,12 +191,12 @@ ExpressionPointer parseEffectBodyExpression(Context &context, ExpressionContext const auto expressionIdentifierPosition = parser.position(); - if (parser.probeIdentifier("=") - || parser.probeIdentifier("assign") - || parser.probeIdentifier("scale-up") - || parser.probeIdentifier("scale-down") - || parser.probeIdentifier("increase") - || parser.probeIdentifier("decrease")) + if (parser.probeIdentifier("=", isIdentifier) + || parser.probeIdentifier("assign", isIdentifier) + || parser.probeIdentifier("scale-up", isIdentifier) + || parser.probeIdentifier("scale-down", isIdentifier) + || parser.probeIdentifier("increase", isIdentifier) + || parser.probeIdentifier("decrease", isIdentifier)) { parser.seek(expressionIdentifierPosition); const auto expressionIdentifier = parser.parseIdentifier(isIdentifier); diff --git a/src/plasp/pddl/InitialState.cpp b/src/plasp/pddl/InitialState.cpp index a203d29..30e0c31 100644 --- a/src/plasp/pddl/InitialState.cpp +++ b/src/plasp/pddl/InitialState.cpp @@ -45,9 +45,9 @@ std::unique_ptr InitialState::parseDeclaration(Context &context, c const auto expressionIdentifierPosition = parser.position(); - if (parser.probeIdentifier("at") - || parser.probeIdentifier("=") - || parser.probeIdentifier("not")) + if (parser.probeIdentifier("at", isIdentifier) + || parser.probeIdentifier("=", isIdentifier) + || parser.probeIdentifier("not", isIdentifier)) { parser.seek(expressionIdentifierPosition); const auto expressionIdentifier = parser.parseIdentifier(isIdentifier); diff --git a/src/plasp/pddl/Problem.cpp b/src/plasp/pddl/Problem.cpp index 96ac88e..e419c70 100644 --- a/src/plasp/pddl/Problem.cpp +++ b/src/plasp/pddl/Problem.cpp @@ -71,18 +71,18 @@ void Problem::findSections() const auto sectionIdentifierPosition = parser.position(); // TODO: check order of the sections - if (parser.probeIdentifier("domain")) + if (parser.probeIdentifier("domain", isIdentifier)) setSectionPosition("domain", m_domainPosition, position, true); - else if (parser.probeIdentifier("requirements")) + else if (parser.probeIdentifier("requirements", isIdentifier)) setSectionPosition("requirements", m_requirementsPosition, position, true); - else if (parser.probeIdentifier("objects")) + else if (parser.probeIdentifier("objects", isIdentifier)) setSectionPosition("objects", m_objectsPosition, position, true); - else if (parser.probeIdentifier("init")) + else if (parser.probeIdentifier("init", isIdentifier)) setSectionPosition("init", m_initialStatePosition, position, true); - else if (parser.probeIdentifier("goal") - || parser.probeIdentifier("constraints") - || parser.probeIdentifier("metric") - || parser.probeIdentifier("length")) + else if (parser.probeIdentifier("goal", isIdentifier) + || parser.probeIdentifier("constraints", isIdentifier) + || parser.probeIdentifier("metric", isIdentifier) + || parser.probeIdentifier("length", isIdentifier)) { parser.seek(sectionIdentifierPosition); diff --git a/src/plasp/utils/Parser.cpp b/src/plasp/utils/Parser.cpp index 25f341a..cbf7906 100644 --- a/src/plasp/utils/Parser.cpp +++ b/src/plasp/utils/Parser.cpp @@ -284,13 +284,6 @@ bool Parser::probe(const std::string &expectedValue) //////////////////////////////////////////////////////////////////////////////////////////////////// -bool Parser::probeIdentifier(const std::string &expectedValue) -{ - return probe(expectedValue) && std::iswspace(currentCharacter()); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - template<> void Parser::expect(const std::string &expectedValue) {