Refactored tokenizer to lazily evaluate file locations.

This commit is contained in:
2017-06-18 18:15:04 +02:00
parent 04dffdb09e
commit 4c0583c91f
30 changed files with 420 additions and 260 deletions

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.location(), "requirement “" + std::string(toString(requirement)) + "” used but never declared, silently adding requirement");
context.warningCallback(context.tokenizer, "requirement “" + std::string(toString(requirement)) + "” used but never declared, silently adding requirement");
else
throw ParserException(context.tokenizer.location(), "requirement “" + std::string(toString(requirement)) + "” used but never declared");
throw ParserException(context.tokenizer, "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.location(), "requirement “" + std::string(toString(requirement)) + "” used but never declared, silently adding requirement");
context.warningCallback(context.tokenizer, "requirement “" + std::string(toString(requirement)) + "” used but never declared, silently adding requirement");
else
throw ParserException(context.tokenizer.location(), "requirement “" + std::string(toString(requirement)) + "” used but never declared");
throw ParserException(context.tokenizer, "requirement “" + std::string(toString(requirement)) + "” used but never declared");
problem.requirements.push_back(requirement);
}

View File

@@ -23,10 +23,10 @@ namespace detail
ActionParser::ActionParser(Context &context, ast::Domain &domain)
: m_context{context},
m_domain{domain},
m_parametersPosition{tokenize::Stream::InvalidPosition},
m_preconditionPosition{tokenize::Stream::InvalidPosition},
m_effectPosition{tokenize::Stream::InvalidPosition},
m_varsPosition{tokenize::Stream::InvalidPosition}
m_parametersPosition{tokenize::InvalidStreamPosition},
m_preconditionPosition{tokenize::InvalidStreamPosition},
m_effectPosition{tokenize::InvalidStreamPosition},
m_varsPosition{tokenize::InvalidStreamPosition}
{
}
@@ -40,26 +40,26 @@ ast::ActionPointer ActionParser::parse()
auto &tokenizer = m_context.tokenizer;
if (m_parametersPosition != tokenize::Stream::InvalidPosition)
if (m_parametersPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_parametersPosition);
parseParameterSection(*action);
}
// For compatibility with old PDDL versions, vars sections are parsed in addition to parameters
if (m_varsPosition != tokenize::Stream::InvalidPosition)
if (m_varsPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_varsPosition);
parseVarsSection(*action);
}
if (m_preconditionPosition != tokenize::Stream::InvalidPosition)
if (m_preconditionPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_preconditionPosition);
parsePreconditionSection(*action);
}
if (m_effectPosition != tokenize::Stream::InvalidPosition)
if (m_effectPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_effectPosition);
parseEffectSection(*action);
@@ -84,10 +84,10 @@ void ActionParser::findSections(ast::Action &action)
const auto setSectionPosition =
[&](const std::string &sectionName, auto &sectionPosition, const auto value, bool unique = false)
{
if (unique && sectionPosition != tokenize::Stream::InvalidPosition)
if (unique && sectionPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(value);
throw ParserException(tokenizer.location(), "only one “:" + sectionName + "” section allowed");
throw ParserException(tokenizer, "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.location(), "unknown action section “" + sectionIdentifier + "");
throw ParserException(tokenizer, "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.location(), "“vars” section is not part of the PDDL 3.1 specification, treating it like additional “parameters” section");
m_context.warningCallback(tokenizer, "“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.location(), "undeclared constant “" + constantName + "");
throw ParserException(tokenizer, "undeclared constant “" + constantName + "");
return std::move(constant.value());
}

View File

@@ -19,8 +19,8 @@ namespace detail
DescriptionParser::DescriptionParser(Context &context)
: m_context{context},
m_domainPosition{tokenize::Stream::InvalidPosition},
m_problemPosition{tokenize::Stream::InvalidPosition}
m_domainPosition{tokenize::InvalidStreamPosition},
m_problemPosition{tokenize::InvalidStreamPosition}
{
}
@@ -33,7 +33,7 @@ ast::Description DescriptionParser::parse()
findSections();
if (m_domainPosition == tokenize::Stream::InvalidPosition)
if (m_domainPosition == tokenize::InvalidStreamPosition)
throw ParserException("no PDDL domain specified");
tokenizer.seek(m_domainPosition);
@@ -41,7 +41,7 @@ ast::Description DescriptionParser::parse()
auto domain = DomainParser(m_context).parse();
// If no problem is given, return just the domain
if (m_problemPosition == tokenize::Stream::InvalidPosition)
if (m_problemPosition == tokenize::InvalidStreamPosition)
return {std::move(domain), std::experimental::nullopt};
tokenizer.seek(m_problemPosition);
@@ -73,7 +73,7 @@ void DescriptionParser::findSections()
if (m_context.mode == Mode::Compatibility && tokenizer.testAndReturn<std::string>("in-package"))
{
m_context.warningCallback(tokenizer.location(), "“in-package” section is not part of the PDDL 3.1 specification, ignoring section");
m_context.warningCallback(tokenizer, "“in-package” section is not part of the PDDL 3.1 specification, ignoring section");
skipSection(tokenizer);
tokenizer.skipWhiteSpace();
@@ -86,8 +86,8 @@ void DescriptionParser::findSections()
if (tokenizer.testAndSkip<std::string>("domain"))
{
if (m_domainPosition != tokenize::Stream::InvalidPosition)
throw ParserException(tokenizer.location(), "PDDL description may not contain two domains");
if (m_domainPosition != tokenize::InvalidStreamPosition)
throw ParserException(tokenizer, "PDDL description may not contain two domains");
m_domainPosition = position;
skipSection(tokenizer);
@@ -95,7 +95,7 @@ void DescriptionParser::findSections()
}
else if (m_context.tokenizer.testAndSkip<std::string>("problem"))
{
if (m_problemPosition != tokenize::Stream::InvalidPosition)
if (m_problemPosition != tokenize::InvalidStreamPosition)
throw ParserException("PDDL description may not contain two problems currently");
m_problemPosition = position;
@@ -105,7 +105,7 @@ void DescriptionParser::findSections()
else
{
const auto sectionIdentifier = tokenizer.get<std::string>();
throw ParserException(tokenizer.location(), "unknown PDDL section “" + sectionIdentifier + "");
throw ParserException(tokenizer, "unknown PDDL section “" + sectionIdentifier + "");
}
tokenizer.skipWhiteSpace();

View File

@@ -22,10 +22,10 @@ namespace detail
DomainParser::DomainParser(Context &context)
: m_context{context},
m_requirementsPosition{tokenize::Stream::InvalidPosition},
m_typesPosition{tokenize::Stream::InvalidPosition},
m_constantsPosition{tokenize::Stream::InvalidPosition},
m_predicatesPosition{tokenize::Stream::InvalidPosition}
m_requirementsPosition{tokenize::InvalidStreamPosition},
m_typesPosition{tokenize::InvalidStreamPosition},
m_constantsPosition{tokenize::InvalidStreamPosition},
m_predicatesPosition{tokenize::InvalidStreamPosition}
{
}
@@ -39,32 +39,32 @@ ast::DomainPointer DomainParser::parse()
auto &tokenizer = m_context.tokenizer;
if (m_requirementsPosition != tokenize::Stream::InvalidPosition)
if (m_requirementsPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_requirementsPosition);
parseRequirementSection(*domain);
}
if (m_typesPosition != tokenize::Stream::InvalidPosition)
if (m_typesPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_typesPosition);
parseTypeSection(*domain);
}
if (m_constantsPosition != tokenize::Stream::InvalidPosition)
if (m_constantsPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_constantsPosition);
parseConstantSection(*domain);
}
if (m_predicatesPosition != tokenize::Stream::InvalidPosition)
if (m_predicatesPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_predicatesPosition);
parsePredicateSection(*domain);
}
for (size_t i = 0; i < m_actionPositions.size(); i++)
if (m_actionPositions[i] != tokenize::Stream::InvalidPosition)
if (m_actionPositions[i] != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_actionPositions[i]);
parseActionSection(*domain);
@@ -93,10 +93,10 @@ void DomainParser::findSections(ast::Domain &domain)
const auto setSectionPosition =
[&](const std::string &sectionName, auto &sectionPosition, const auto value, bool unique = false)
{
if (unique && sectionPosition != tokenize::Stream::InvalidPosition)
if (unique && sectionPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(value);
throw ParserException(tokenizer.location(), "only one “:" + sectionName + "” section allowed");
throw ParserException(tokenizer, "only one “:" + sectionName + "” section allowed");
}
sectionPosition = value;
@@ -125,7 +125,7 @@ void DomainParser::findSections(ast::Domain &domain)
setSectionPosition("predicates", m_predicatesPosition, position, true);
else if (tokenizer.testIdentifierAndSkip("action"))
{
m_actionPositions.emplace_back(tokenize::Stream::InvalidPosition);
m_actionPositions.emplace_back(tokenize::InvalidStreamPosition);
setSectionPosition("action", m_actionPositions.back(), position);
}
else if (tokenizer.testIdentifierAndSkip("functions")
@@ -137,7 +137,7 @@ void DomainParser::findSections(ast::Domain &domain)
const auto sectionIdentifier = tokenizer.getIdentifier();
m_context.warningCallback(tokenizer.location(), "section type “" + sectionIdentifier + "” currently unsupported, ignoring section");
m_context.warningCallback(tokenizer, "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.location(), "unknown domain section “" + sectionIdentifier + "");
throw ParserException(tokenizer, "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.location(), "only primitive types are allowed in type section");
throw ParserException(tokenizer, "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.location(), "expression type “" + expressionIdentifier + "” unknown or not allowed in effect body");
throw ParserException(tokenizer, "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.location(), "expression type “" + expressionIdentifier + "” unknown or not allowed in conditional effect body");
throw ParserException(tokenizer, "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.location(), "invalid initial state fact");
throw ParserException(tokenizer, "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.location(), "expression type “" + expressionIdentifier + "” unknown or not allowed in precondition body");
throw ParserException(tokenizer, "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.location(), "no matching declaration found for predicate “" + name + "");
throw ParserException(tokenizer, "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.location(), "could not parse primitive type, expected identifier");
throw ParserException(tokenizer, "could not parse primitive type, expected identifier");
auto matchingType = std::find_if(types.begin(), types.end(),
[&](auto &primitiveTypeDeclaration)
@@ -34,9 +34,9 @@ ast::PrimitiveTypePointer parsePrimitiveType(Context &context, ast::Domain &doma
if (matchingType == types.end())
{
if (context.mode != Mode::Compatibility)
throw ParserException(tokenizer.location(), "primitive type “" + typeName + "” used without or before declaration");
throw ParserException(tokenizer, "primitive type “" + typeName + "” used without or before declaration");
context.warningCallback(tokenizer.location(), "primitive type “" + typeName + "” used without or before declaration, silently adding declaration");
context.warningCallback(tokenizer, "primitive type “" + typeName + "” used without or before declaration, silently adding declaration");
types.emplace_back(std::make_unique<ast::PrimitiveTypeDeclaration>(std::move(typeName)));

View File

@@ -22,11 +22,11 @@ namespace detail
ProblemParser::ProblemParser(Context &context, ast::Domain &domain)
: m_context{context},
m_domain{domain},
m_domainPosition{tokenize::Stream::InvalidPosition},
m_requirementsPosition{tokenize::Stream::InvalidPosition},
m_objectsPosition{tokenize::Stream::InvalidPosition},
m_initialStatePosition{tokenize::Stream::InvalidPosition},
m_goalPosition{tokenize::Stream::InvalidPosition}
m_domainPosition{tokenize::InvalidStreamPosition},
m_requirementsPosition{tokenize::InvalidStreamPosition},
m_objectsPosition{tokenize::InvalidStreamPosition},
m_initialStatePosition{tokenize::InvalidStreamPosition},
m_goalPosition{tokenize::InvalidStreamPosition}
{
}
@@ -40,32 +40,32 @@ ast::ProblemPointer ProblemParser::parse()
auto &tokenizer = m_context.tokenizer;
if (m_domainPosition == tokenize::Stream::InvalidPosition)
throw ParserException(tokenizer.location(), "problem description does not specify a corresponding domain");
if (m_domainPosition == tokenize::InvalidStreamPosition)
throw ParserException(tokenizer, "problem description does not specify a corresponding domain");
tokenizer.seek(m_domainPosition);
parseDomainSection(*problem);
if (m_requirementsPosition != tokenize::Stream::InvalidPosition)
if (m_requirementsPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_requirementsPosition);
parseRequirementSection(*problem);
}
if (m_objectsPosition != tokenize::Stream::InvalidPosition)
if (m_objectsPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(m_objectsPosition);
parseObjectSection(*problem);
}
if (m_initialStatePosition == tokenize::Stream::InvalidPosition)
throw ParserException(tokenizer.location(), "problem description does not specify an initial state");
if (m_initialStatePosition == tokenize::InvalidStreamPosition)
throw ParserException(tokenizer, "problem description does not specify an initial state");
tokenizer.seek(m_initialStatePosition);
parseInitialStateSection(*problem);
if (m_goalPosition == tokenize::Stream::InvalidPosition)
throw ParserException(tokenizer.location(), "problem description does not specify a goal");
if (m_goalPosition == tokenize::InvalidStreamPosition)
throw ParserException(tokenizer, "problem description does not specify a goal");
tokenizer.seek(m_goalPosition);
parseGoalSection(*problem);
@@ -91,10 +91,10 @@ void ProblemParser::findSections(ast::Problem &problem)
const auto setSectionPosition =
[&](const std::string &sectionName, auto &sectionPosition, const auto value, bool unique = false)
{
if (unique && sectionPosition != tokenize::Stream::InvalidPosition)
if (unique && sectionPosition != tokenize::InvalidStreamPosition)
{
tokenizer.seek(value);
throw ParserException(tokenizer.location(), "only one “:" + sectionName + "” section allowed");
throw ParserException(tokenizer, "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.location(), "section type “" + sectionIdentifier + "” currently unsupported, ignoring section");
m_context.warningCallback(tokenizer, "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.location(), "unknown problem section “" + sectionIdentifier + "");
throw ParserException(tokenizer, "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.location(), "domains do not match (“" + problem.domain->name + "” and “" + domainName + "”)");
throw ParserException(tokenizer, "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.location(), "" + requirementName + "” requirement is not part of the PDDL 3.1 specification, ignoring requirement");
context.warningCallback(tokenizer, "" + 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.location(), "expected primitive type or “either” expression");
throw ParserException(tokenizer, "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.location(), "expression type “" + expressionType + "” currently unsupported in this context, substituting it with placeholder");
context.warningCallback(tokenizer, "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.location(), "undeclared variable “" + variableName + "");
throw ParserException(tokenizer, "undeclared variable “" + variableName + "");
return std::make_unique<ast::Variable>(variableDeclaration.value());
}