Refactoring of tokenizer and stream classes for better efficiency and maintainability.

This commit is contained in:
2017-06-21 02:56:27 +02:00
parent 97c6e58355
commit 39c0e27cb2
38 changed files with 466 additions and 512 deletions

View File

@@ -64,12 +64,12 @@ std::experimental::optional<std::unique_ptr<Derived>> parseBinary(Context &conte
auto argumentLeft = parseArgumentLeft(context, astContext, variableStack);
if (!argumentLeft)
throw ParserException(tokenizer, "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
auto argumentRight = parseArgumentRight(context, astContext, variableStack);
if (!argumentRight)
throw ParserException(tokenizer, "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
tokenizer.expect<std::string>(")");
@@ -103,7 +103,7 @@ std::experimental::optional<std::unique_ptr<Derived>> parseNAry(Context &context
auto argument = parseArgument(context, astContext, variableStack);
if (!argument)
throw ParserException(tokenizer, "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
arguments.emplace_back(std::move(argument.value()));
@@ -111,7 +111,7 @@ std::experimental::optional<std::unique_ptr<Derived>> parseNAry(Context &context
}
if (arguments.empty())
context.warningCallback(tokenizer, "" + std::string(Derived::Identifier) + "” expressions should not be empty");
context.warningCallback(tokenizer.location(), "" + std::string(Derived::Identifier) + "” expressions should not be empty");
tokenizer.expect<std::string>(")");
@@ -147,7 +147,7 @@ std::experimental::optional<std::unique_ptr<Derived>> parseQuantified(Context &c
auto argument = parseArgument(context, astContext, variableStack);
if (!argument)
throw ParserException(tokenizer, "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
// Clean up variable stack
variableStack.pop();
@@ -221,7 +221,7 @@ std::experimental::optional<ast::NotPointer<Argument>> parseNot(Context &context
auto argument = parseArgument(context, astContext, variableStack);
if (!argument)
throw ParserException(tokenizer, "could not parse argument of “not” expression");
throw ParserException(tokenizer.location(), "could not parse argument of “not” expression");
tokenizer.expect<std::string>(")");

View File

@@ -27,7 +27,6 @@ set(sources
)
set(libraries
tokenize
)
add_library(${target} ${sources})

View File

@@ -61,9 +61,9 @@ void checkRequirement(ast::Domain &domain, ast::Requirement requirement, Context
return;
if (context.mode == Mode::Compatibility)
context.warningCallback(context.tokenizer, "requirement “" + std::string(toString(requirement)) + "” used but never declared, silently adding requirement");
context.warningCallback(context.tokenizer.location(), "requirement “" + std::string(toString(requirement)) + "” used but never declared, silently adding requirement");
else
throw ParserException(context.tokenizer, "requirement “" + std::string(toString(requirement)) + "” used but never declared");
throw ParserException(context.tokenizer.location(), "requirement “" + std::string(toString(requirement)) + "” used but never declared");
domain.requirements.push_back(requirement);
}
@@ -76,9 +76,9 @@ void checkRequirement(ast::Problem &problem, ast::Requirement requirement, Conte
return;
if (context.mode == Mode::Compatibility)
context.warningCallback(context.tokenizer, "requirement “" + std::string(toString(requirement)) + "” used but never declared, silently adding requirement");
context.warningCallback(context.tokenizer.location(), "requirement “" + std::string(toString(requirement)) + "” used but never declared, silently adding requirement");
else
throw ParserException(context.tokenizer, "requirement “" + std::string(toString(requirement)) + "” used but never declared");
throw ParserException(context.tokenizer.location(), "requirement “" + std::string(toString(requirement)) + "” used but never declared");
problem.requirements.push_back(requirement);
}

View File

@@ -87,7 +87,7 @@ void ActionParser::findSections(ast::Action &action)
if (unique && sectionPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(value);
throw ParserException(tokenizer, "only one “:" + sectionName + "” section allowed");
throw ParserException(tokenizer.location(), "only one “:" + sectionName + "” section allowed");
}
sectionPosition = value;
@@ -114,7 +114,7 @@ void ActionParser::findSections(ast::Action &action)
const auto sectionIdentifier = tokenizer.getIdentifier();
tokenizer.seek(position);
throw ParserException(tokenizer, "unknown action section “" + sectionIdentifier + "");
throw ParserException(tokenizer.location(), "unknown action section “" + sectionIdentifier + "");
}
tokenizer.expect<std::string>("(");
@@ -181,7 +181,7 @@ void ActionParser::parseVarsSection(ast::Action &action)
tokenizer.expect<std::string>(":vars");
tokenizer.expect<std::string>("(");
m_context.warningCallback(tokenizer, "“vars” section is not part of the PDDL 3.1 specification, treating it like additional “parameters” section");
m_context.warningCallback(tokenizer.location(), "“vars” section is not part of the PDDL 3.1 specification, treating it like additional “parameters” section");
parseAndAddVariableDeclarations(m_context, m_domain, action.parameters);

View File

@@ -73,7 +73,7 @@ ast::ConstantPointer parseConstant(Context &context, ASTContext &astContext)
auto constant = findConstant(constantName, astContext);
if (!constant)
throw ParserException(tokenizer, "undeclared constant “" + constantName + "");
throw ParserException(tokenizer.location(), "undeclared constant “" + constantName + "");
return std::move(constant.value());
}

View File

@@ -60,7 +60,7 @@ void parseAndAddConstantDeclarations(Context &context, ast::Domain &domain, ast:
const bool isTypingUsed = !domain.types.empty();
if (isTypingUsed && !constantDeclarations.empty() && !constantDeclarations.back()->type)
throw ParserException(tokenizer, "missing type declaration for constant “" + constantDeclarations.back()->name + "");
throw ParserException(tokenizer.location(), "missing type declaration for constant “" + constantDeclarations.back()->name + "");
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -73,7 +73,7 @@ void DescriptionParser::findSections()
if (m_context.mode == Mode::Compatibility && tokenizer.testAndReturn<std::string>("in-package"))
{
m_context.warningCallback(tokenizer, "“in-package” section is not part of the PDDL 3.1 specification, ignoring section");
m_context.warningCallback(tokenizer.location(), "“in-package” section is not part of the PDDL 3.1 specification, ignoring section");
skipSection(tokenizer);
tokenizer.skipWhiteSpace();
@@ -87,7 +87,7 @@ void DescriptionParser::findSections()
if (tokenizer.testAndSkip<std::string>("domain"))
{
if (m_domainPosition != tokenize::InvalidStreamPosition)
throw ParserException(tokenizer, "PDDL description may not contain two domains");
throw ParserException(tokenizer.location(), "PDDL description may not contain two domains");
m_domainPosition = position;
skipSection(tokenizer);
@@ -105,7 +105,7 @@ void DescriptionParser::findSections()
else
{
const auto sectionIdentifier = tokenizer.get<std::string>();
throw ParserException(tokenizer, "unknown PDDL section “" + sectionIdentifier + "");
throw ParserException(tokenizer.location(), "unknown PDDL section “" + sectionIdentifier + "");
}
tokenizer.skipWhiteSpace();

View File

@@ -96,7 +96,7 @@ void DomainParser::findSections(ast::Domain &domain)
if (unique && sectionPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(value);
throw ParserException(tokenizer, "only one “:" + sectionName + "” section allowed");
throw ParserException(tokenizer.location(), "only one “:" + sectionName + "” section allowed");
}
sectionPosition = value;
@@ -137,7 +137,7 @@ void DomainParser::findSections(ast::Domain &domain)
const auto sectionIdentifier = tokenizer.getIdentifier();
m_context.warningCallback(tokenizer, "section type “" + sectionIdentifier + "” currently unsupported, ignoring section");
m_context.warningCallback(tokenizer.location(), "section type “" + sectionIdentifier + "” currently unsupported, ignoring section");
tokenizer.seek(sectionIdentifierPosition);
}
@@ -146,7 +146,7 @@ void DomainParser::findSections(ast::Domain &domain)
const auto sectionIdentifier = tokenizer.getIdentifier();
tokenizer.seek(position);
throw ParserException(tokenizer, "unknown domain section “" + sectionIdentifier + "");
throw ParserException(tokenizer.location(), "unknown domain section “" + sectionIdentifier + "");
}
// Skip section for now and parse it later
@@ -246,7 +246,7 @@ void DomainParser::parseTypeSection(ast::Domain &domain)
while (tokenizer.currentCharacter() != ')')
{
if (tokenizer.currentCharacter() == '(')
throw ParserException(tokenizer, "only primitive types are allowed in type section");
throw ParserException(tokenizer.location(), "only primitive types are allowed in type section");
parseAndAddPrimitiveTypeDeclarations(m_context, domain);

View File

@@ -83,7 +83,7 @@ std::experimental::optional<ast::Effect> parseEffectBody(Context &context, ASTCo
const auto expressionIdentifier = tokenizer.getIdentifier();
tokenizer.seek(position);
throw ParserException(tokenizer, "expression type “" + expressionIdentifier + "” unknown or not allowed in effect body");
throw ParserException(tokenizer.location(), "expression type “" + expressionIdentifier + "” unknown or not allowed in effect body");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -144,7 +144,7 @@ std::experimental::optional<ast::ConditionalEffect> parseConditionalEffectBody(C
const auto expressionIdentifier = tokenizer.getIdentifier();
tokenizer.seek(position);
throw ParserException(tokenizer, "expression type “" + expressionIdentifier + "” unknown or not allowed in conditional effect body");
throw ParserException(tokenizer.location(), "expression type “" + expressionIdentifier + "” unknown or not allowed in conditional effect body");
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -28,7 +28,7 @@ ast::InitialState parseInitialState(Context &context, ASTContext &astContext, Va
auto fact = parseFact(context, astContext, variableStack);
if (!fact)
throw ParserException(tokenizer, "invalid initial state fact");
throw ParserException(tokenizer.location(), "invalid initial state fact");
initialState.facts.emplace_back(std::move(fact.value()));

View File

@@ -99,7 +99,7 @@ std::experimental::optional<ast::Precondition> parsePreconditionBody(Context &co
const auto expressionIdentifier = tokenizer.getIdentifier();
tokenizer.seek(position);
throw ParserException(tokenizer, "expression type “" + expressionIdentifier + "” unknown or not allowed in precondition body");
throw ParserException(tokenizer.location(), "expression type “" + expressionIdentifier + "” unknown or not allowed in precondition body");
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -78,7 +78,7 @@ std::experimental::optional<ast::PredicatePointer> parsePredicate(Context &conte
{
// TODO: enumerate candidates and why they are incompatible
tokenizer.seek(previousPosition);
throw ParserException(tokenizer, "no matching declaration found for predicate “" + name + "");
throw ParserException(tokenizer.location(), "no matching declaration found for predicate “" + name + "");
}
auto *declaration = matchingPredicateDeclaration->get();

View File

@@ -22,7 +22,7 @@ ast::PrimitiveTypePointer parsePrimitiveType(Context &context, ast::Domain &doma
auto typeName = tokenizer.getIdentifier();
if (typeName.empty())
throw ParserException(tokenizer, "could not parse primitive type, expected identifier");
throw ParserException(tokenizer.location(), "could not parse primitive type, expected identifier");
auto matchingType = std::find_if(types.begin(), types.end(),
[&](auto &primitiveTypeDeclaration)
@@ -37,9 +37,9 @@ ast::PrimitiveTypePointer parsePrimitiveType(Context &context, ast::Domain &doma
if (typeName != "object")
{
if (context.mode != Mode::Compatibility)
throw ParserException(tokenizer, "primitive type “" + typeName + "” used without or before declaration");
throw ParserException(tokenizer.location(), "primitive type “" + typeName + "” used without or before declaration");
context.warningCallback(tokenizer, "primitive type “" + typeName + "” used without or before declaration, silently adding declaration");
context.warningCallback(tokenizer.location(), "primitive type “" + typeName + "” used without or before declaration, silently adding declaration");
}
types.emplace_back(std::make_unique<ast::PrimitiveTypeDeclaration>(std::move(typeName)));

View File

@@ -41,7 +41,7 @@ ast::ProblemPointer ProblemParser::parse()
auto &tokenizer = m_context.tokenizer;
if (m_domainPosition == tokenize::InvalidStreamPosition)
throw ParserException(tokenizer, "problem description does not specify a corresponding domain");
throw ParserException(tokenizer.location(), "problem description does not specify a corresponding domain");
tokenizer.seek(m_domainPosition);
parseDomainSection(*problem);
@@ -59,13 +59,13 @@ ast::ProblemPointer ProblemParser::parse()
}
if (m_initialStatePosition == tokenize::InvalidStreamPosition)
throw ParserException(tokenizer, "problem description does not specify an initial state");
throw ParserException(tokenizer.location(), "problem description does not specify an initial state");
tokenizer.seek(m_initialStatePosition);
parseInitialStateSection(*problem);
if (m_goalPosition == tokenize::InvalidStreamPosition)
throw ParserException(tokenizer, "problem description does not specify a goal");
throw ParserException(tokenizer.location(), "problem description does not specify a goal");
tokenizer.seek(m_goalPosition);
parseGoalSection(*problem);
@@ -94,7 +94,7 @@ void ProblemParser::findSections(ast::Problem &problem)
if (unique && sectionPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(value);
throw ParserException(tokenizer, "only one “:" + sectionName + "” section allowed");
throw ParserException(tokenizer.location(), "only one “:" + sectionName + "” section allowed");
}
sectionPosition = value;
@@ -129,7 +129,7 @@ void ProblemParser::findSections(ast::Problem &problem)
const auto sectionIdentifier = tokenizer.getIdentifier();
m_context.warningCallback(tokenizer, "section type “" + sectionIdentifier + "” currently unsupported, ignoring section");
m_context.warningCallback(tokenizer.location(), "section type “" + sectionIdentifier + "” currently unsupported, ignoring section");
tokenizer.seek(sectionIdentifierPosition);
}
@@ -138,7 +138,7 @@ void ProblemParser::findSections(ast::Problem &problem)
const auto sectionIdentifier = tokenizer.getIdentifier();
tokenizer.seek(position);
throw ParserException(tokenizer, "unknown problem section “" + sectionIdentifier + "");
throw ParserException(tokenizer.location(), "unknown problem section “" + sectionIdentifier + "");
}
// Skip section for now and parse it later
@@ -165,7 +165,7 @@ void ProblemParser::parseDomainSection(ast::Problem &problem)
const auto domainName = tokenizer.getIdentifier();
if (problem.domain->name != domainName)
throw ParserException(tokenizer, "domains do not match (“" + problem.domain->name + "” and “" + domainName + "”)");
throw ParserException(tokenizer.location(), "domains do not match (“" + problem.domain->name + "” and “" + domainName + "”)");
tokenizer.expect<std::string>(")");
}

View File

@@ -67,7 +67,7 @@ std::experimental::optional<ast::Requirement> parseRequirement(Context &context)
return matchingRequirement->second;
if (context.mode == Mode::Compatibility && (requirementName == "goal-utilities" || requirementName == "domain-axioms"))
context.warningCallback(tokenizer, "" + requirementName + "” requirement is not part of the PDDL 3.1 specification, ignoring requirement");
context.warningCallback(tokenizer.location(), "" + requirementName + "” requirement is not part of the PDDL 3.1 specification, ignoring requirement");
return std::experimental::nullopt;
}

View File

@@ -37,7 +37,7 @@ ast::Type parseType(Context &context, ast::Domain &domain)
auto eitherType = parseEither<ast::PrimitiveTypePointer>(context, astContext, variableStack, parsePrimitiveTypeWrapper);
if (!eitherType)
throw ParserException(tokenizer, "expected primitive type or “either” expression");
throw ParserException(tokenizer.location(), "expected primitive type or “either” expression");
return std::move(eitherType.value());
}

View File

@@ -22,7 +22,7 @@ ast::UnsupportedPointer parseUnsupported(Context &context)
auto expressionType = tokenizer.getIdentifier();
context.warningCallback(tokenizer, "expression type “" + expressionType + "” currently unsupported in this context, substituting it with placeholder");
context.warningCallback(tokenizer.location(), "expression type “" + expressionType + "” currently unsupported in this context, substituting it with placeholder");
skipSection(tokenizer);

View File

@@ -64,7 +64,7 @@ ast::VariablePointer parseVariable(Context &context, VariableStack &variableStac
auto variableDeclaration = variableStack.findVariableDeclaration(variableName);
if (!variableDeclaration)
throw ParserException(tokenizer, "undeclared variable “" + variableName + "");
throw ParserException(tokenizer.location(), "undeclared variable “" + variableName + "");
return std::make_unique<ast::Variable>(variableDeclaration.value());
}

View File

@@ -60,7 +60,7 @@ void parseAndAddVariableDeclarations(Context &context, ast::Domain &domain, ast:
const bool isTypingUsed = !domain.types.empty();
if (isTypingUsed && !variableDeclarations.empty() && !variableDeclarations.back()->type)
throw ParserException(tokenizer, "missing type declaration for variable “?" + variableDeclarations.back()->name + "");
throw ParserException(tokenizer.location(), "missing type declaration for variable “?" + variableDeclarations.back()->name + "");
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -10,6 +10,7 @@ set(includes
)
set(libraries
stdc++fs
pddlparse
)