OSDN Git Service

パーサーのコントロールクラスを定義
authorstarg <starg@users.osdn.me>
Wed, 6 Jul 2016 19:28:03 +0000 (04:28 +0900)
committerstarg <starg@users.osdn.me>
Wed, 6 Jul 2016 19:28:03 +0000 (04:28 +0900)
include/exceptions/parseexception.hpp [new file with mode: 0644]
src/parser/control.hpp [new file with mode: 0644]
src/parser/error_control.hpp [new file with mode: 0644]

diff --git a/include/exceptions/parseexception.hpp b/include/exceptions/parseexception.hpp
new file mode 100644 (file)
index 0000000..3b7195e
--- /dev/null
@@ -0,0 +1,39 @@
+
+#pragma once
+
+#include <string>
+
+#include <exceptions/exception.hpp>
+#include <message/id.hpp>
+
+namespace YAMML
+{
+
+namespace Exceptions
+{
+
+class ParseException : public Exception
+{
+public:
+    ParseException(
+        const std::string& source,
+        std::size_t line,
+        std::size_t column,
+        Message::MessageID id
+    ) : Exception("ParseException: MessageID=" + std::to_string(static_cast<int>(id))),
+        Source(source),
+        Line{line},
+        Column{column},
+        ID{id}
+    {
+    }
+
+    std::string Source;
+    std::size_t Line;
+    std::size_t Column;
+    Message::MessageID ID;
+};
+
+} // namespace Exceptions
+
+} // namespace YAMML
diff --git a/src/parser/control.hpp b/src/parser/control.hpp
new file mode 100644 (file)
index 0000000..98e4ddd
--- /dev/null
@@ -0,0 +1,22 @@
+
+#pragma once
+
+#include <pegtl.hh>
+#include <pegtl/contrib/changes.hh>
+
+#include "error_control.hpp"
+
+namespace YAMML
+{
+
+namespace Parser
+{
+
+template<typename TRule>
+class Control : public ErrorControl<TRule>
+{
+};
+
+} // namespace Parser
+
+} // namespace YAMML
diff --git a/src/parser/error_control.hpp b/src/parser/error_control.hpp
new file mode 100644 (file)
index 0000000..16a9716
--- /dev/null
@@ -0,0 +1,36 @@
+
+#pragma once
+
+#include <pegtl.hh>
+
+#include <exceptions/parseexception.hpp>
+#include <message/id.hpp>
+
+namespace YAMML
+{
+
+namespace Parser
+{
+
+template<typename TInput>
+void ThrowParseException(const TInput& in, Message::MessageID id)
+{
+    throw Exceptions::ParseException(in.source(), in.line(), in.column(), id);
+}
+
+template<typename TRule>
+class ErrorControl : public pegtl::normal<TRule>
+{
+public:
+    static const Message::MessageID ID;
+
+    template<typename TInput, typename... TStates>
+    static void raise(const TInput& in, TStates&&...)
+    {
+        ThrowParseException(in, ID);
+    }
+};
+
+} // namespace Parser
+
+} // namespace YAMML