Refactored tokenizer to lazily evaluate file locations.
This commit is contained in:
@@ -23,7 +23,7 @@ struct Context
|
||||
}
|
||||
|
||||
// TODO: replace std::string with char *
|
||||
using WarningCallback = std::function<void (tokenize::Location, const std::string &)>;
|
||||
using WarningCallback = std::function<void (tokenize::Location &&, const std::string &)>;
|
||||
|
||||
Context() = default;
|
||||
~Context() = default;
|
||||
|
@@ -2,9 +2,10 @@
|
||||
#define __PDDL_PARSE__EXCEPTION_H
|
||||
|
||||
#include <exception>
|
||||
#include <experimental/optional>
|
||||
#include <string>
|
||||
|
||||
#include <tokenize/Stream.h>
|
||||
#include <tokenize/Location.h>
|
||||
|
||||
namespace pddl
|
||||
{
|
||||
@@ -18,48 +19,45 @@ namespace pddl
|
||||
class Exception: public std::exception
|
||||
{
|
||||
public:
|
||||
explicit Exception()
|
||||
Exception()
|
||||
: Exception("unspecified parser error")
|
||||
{
|
||||
}
|
||||
|
||||
explicit Exception(const char *message)
|
||||
Exception(const char *message)
|
||||
: Exception(static_cast<std::string>(message))
|
||||
{
|
||||
}
|
||||
|
||||
explicit Exception(const std::string &message)
|
||||
Exception(const std::string &message)
|
||||
: m_message{message}
|
||||
{
|
||||
}
|
||||
|
||||
explicit Exception(const tokenize::Location &location)
|
||||
: Exception(location, "unspecified parser error")
|
||||
Exception(tokenize::Location &&location)
|
||||
: Exception(std::forward<tokenize::Location>(location), "unspecified parser error")
|
||||
{
|
||||
}
|
||||
|
||||
explicit Exception(const tokenize::Location &location, const char *message)
|
||||
: Exception(location, static_cast<std::string>(message))
|
||||
Exception(tokenize::Location &&location, const char *message)
|
||||
: Exception(std::forward<tokenize::Location>(location), static_cast<std::string>(message))
|
||||
{
|
||||
}
|
||||
|
||||
explicit Exception(const tokenize::Location &location, const std::string &message)
|
||||
: m_location{location},
|
||||
m_message{message},
|
||||
// TODO: refactor
|
||||
m_plainMessage{std::string(m_location.sectionStart) + ":" + std::to_string(m_location.rowStart)
|
||||
+ ":" + std::to_string(m_location.columnStart) + " " + m_message}
|
||||
Exception(tokenize::Location &&location, const std::string &message)
|
||||
: m_location{std::move(location)},
|
||||
m_message{message}
|
||||
{
|
||||
}
|
||||
|
||||
~Exception() noexcept = default;
|
||||
|
||||
const char *what() const throw()
|
||||
const char *what() const noexcept
|
||||
{
|
||||
return m_plainMessage.c_str();
|
||||
return m_message.c_str();
|
||||
}
|
||||
|
||||
const tokenize::Location &location() const
|
||||
const std::experimental::optional<tokenize::Location> &location() const
|
||||
{
|
||||
return m_location;
|
||||
}
|
||||
@@ -70,9 +68,8 @@ class Exception: public std::exception
|
||||
}
|
||||
|
||||
private:
|
||||
tokenize::Location m_location;
|
||||
std::experimental::optional<tokenize::Location> m_location;
|
||||
std::string m_message;
|
||||
std::string m_plainMessage;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@@ -34,12 +34,12 @@ class ActionParser
|
||||
Context &m_context;
|
||||
ast::Domain &m_domain;
|
||||
|
||||
tokenize::Stream::Position m_parametersPosition;
|
||||
tokenize::Stream::Position m_preconditionPosition;
|
||||
tokenize::Stream::Position m_effectPosition;
|
||||
tokenize::StreamPosition m_parametersPosition;
|
||||
tokenize::StreamPosition m_preconditionPosition;
|
||||
tokenize::StreamPosition m_effectPosition;
|
||||
|
||||
// For compatibility with old PDDL versions
|
||||
tokenize::Stream::Position m_varsPosition;
|
||||
tokenize::StreamPosition m_varsPosition;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@@ -27,8 +27,8 @@ class DescriptionParser
|
||||
void findSections();
|
||||
|
||||
Context &m_context;
|
||||
tokenize::Stream::Position m_domainPosition;
|
||||
tokenize::Stream::Position m_problemPosition;
|
||||
tokenize::StreamPosition m_domainPosition;
|
||||
tokenize::StreamPosition m_problemPosition;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@@ -35,11 +35,11 @@ class DomainParser
|
||||
|
||||
Context &m_context;
|
||||
|
||||
tokenize::Stream::Position m_requirementsPosition;
|
||||
tokenize::Stream::Position m_typesPosition;
|
||||
tokenize::Stream::Position m_constantsPosition;
|
||||
tokenize::Stream::Position m_predicatesPosition;
|
||||
std::vector<tokenize::Stream::Position> m_actionPositions;
|
||||
tokenize::StreamPosition m_requirementsPosition;
|
||||
tokenize::StreamPosition m_typesPosition;
|
||||
tokenize::StreamPosition m_constantsPosition;
|
||||
tokenize::StreamPosition m_predicatesPosition;
|
||||
std::vector<tokenize::StreamPosition> m_actionPositions;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@@ -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.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
|
||||
throw ParserException(tokenizer, "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
|
||||
|
||||
auto argumentRight = parseArgumentRight(context, astContext, variableStack);
|
||||
|
||||
if (!argumentRight)
|
||||
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
|
||||
throw ParserException(tokenizer, "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.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
|
||||
throw ParserException(tokenizer, "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.location(), "“" + std::string(Derived::Identifier) + "” expressions should not be empty");
|
||||
context.warningCallback(tokenizer, "“" + 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.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
|
||||
throw ParserException(tokenizer, "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.location(), "could not parse argument of “not” expression");
|
||||
throw ParserException(tokenizer, "could not parse argument of “not” expression");
|
||||
|
||||
tokenizer.expect<std::string>(")");
|
||||
|
||||
|
@@ -36,11 +36,11 @@ class ProblemParser
|
||||
Context &m_context;
|
||||
ast::Domain &m_domain;
|
||||
|
||||
tokenize::Stream::Position m_domainPosition;
|
||||
tokenize::Stream::Position m_requirementsPosition;
|
||||
tokenize::Stream::Position m_objectsPosition;
|
||||
tokenize::Stream::Position m_initialStatePosition;
|
||||
tokenize::Stream::Position m_goalPosition;
|
||||
tokenize::StreamPosition m_domainPosition;
|
||||
tokenize::StreamPosition m_requirementsPosition;
|
||||
tokenize::StreamPosition m_objectsPosition;
|
||||
tokenize::StreamPosition m_initialStatePosition;
|
||||
tokenize::StreamPosition m_goalPosition;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
Reference in New Issue
Block a user