aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/parse/ParsingAlgorithm.hpp22
-rw-r--r--include/cru/parse/ParsingAlgorithmContext.hpp23
-rw-r--r--include/cru/parse/ParsingContext.hpp21
-rw-r--r--src/parse/CMakeLists.txt3
-rw-r--r--src/parse/ParsingAlgorithm.cpp1
-rw-r--r--src/parse/ParsingAlgorithmContext.cpp9
-rw-r--r--src/parse/ParsingContext.cpp13
7 files changed, 92 insertions, 0 deletions
diff --git a/include/cru/parse/ParsingAlgorithm.hpp b/include/cru/parse/ParsingAlgorithm.hpp
new file mode 100644
index 00000000..1ea30a80
--- /dev/null
+++ b/include/cru/parse/ParsingAlgorithm.hpp
@@ -0,0 +1,22 @@
+#pragma once
+#include "Grammar.hpp"
+
+namespace cru::parse {
+class ParsingAlgorithmContext;
+
+// Represents a parsing algorithm.
+// It does not relate to any specific grammar.
+// It is used to validate a grammar and create a parsing algorithm context.
+class ParsingAlgorithm {
+ public:
+ ParsingAlgorithm() = default;
+
+ CRU_DELETE_COPY(ParsingAlgorithm)
+ CRU_DELETE_MOVE(ParsingAlgorithm)
+
+ ~ParsingAlgorithm() = default;
+
+ virtual bool CanHandle(Grammar* grammar) const = 0;
+ virtual ParsingAlgorithmContext* CreateContext(Grammar* grammar) const = 0;
+};
+} // namespace cru::parse
diff --git a/include/cru/parse/ParsingAlgorithmContext.hpp b/include/cru/parse/ParsingAlgorithmContext.hpp
new file mode 100644
index 00000000..072203ec
--- /dev/null
+++ b/include/cru/parse/ParsingAlgorithmContext.hpp
@@ -0,0 +1,23 @@
+#pragma once
+#include "Grammar.hpp"
+
+namespace cru::parse {
+class ParsingAlgorithm;
+
+// A parsing algorithm context contains all data a parsing algorithm needs to
+// parse for a grammar. It does not relate to any input. For example, it can
+// contain any state machine.
+class ParsingAlgorithmContext {
+ public:
+ ParsingAlgorithmContext(Grammar* grammar, ParsingAlgorithm* algorithm);
+
+ CRU_DELETE_COPY(ParsingAlgorithmContext)
+ CRU_DELETE_MOVE(ParsingAlgorithmContext)
+
+ ~ParsingAlgorithmContext();
+
+ private:
+ Grammar* grammar_;
+ ParsingAlgorithm* algorithm_;
+};
+} // namespace cru::parse
diff --git a/include/cru/parse/ParsingContext.hpp b/include/cru/parse/ParsingContext.hpp
new file mode 100644
index 00000000..369acf41
--- /dev/null
+++ b/include/cru/parse/ParsingContext.hpp
@@ -0,0 +1,21 @@
+#pragma once
+#include "ParsingAlgorithmContext.hpp"
+
+namespace cru::parse {
+// A parsing context contains all info that a program needs to know when parsing
+// a input sequence of terminals.
+class ParsingContext {
+ public:
+ ParsingContext(ParsingAlgorithmContext* parsing_algorithm_context,
+ std::vector<Terminal*> input);
+
+ CRU_DELETE_COPY(ParsingContext)
+ CRU_DELETE_MOVE(ParsingContext)
+
+ ~ParsingContext();
+
+ private:
+ ParsingAlgorithmContext* parsing_algorithm_context_;
+ std::vector<Terminal*> input_;
+};
+} // namespace cru::parse
diff --git a/src/parse/CMakeLists.txt b/src/parse/CMakeLists.txt
index 5c8a6dfa..7c868f98 100644
--- a/src/parse/CMakeLists.txt
+++ b/src/parse/CMakeLists.txt
@@ -1,6 +1,9 @@
add_library(cru_parse SHARED
Grammar.cpp
Nonterminal.cpp
+ ParsingAlgorithm.cpp
+ ParsingAlgorithmContext.cpp
+ ParsingContext.cpp
Production.cpp
Symbol.cpp
Terminal.cpp
diff --git a/src/parse/ParsingAlgorithm.cpp b/src/parse/ParsingAlgorithm.cpp
new file mode 100644
index 00000000..03059e01
--- /dev/null
+++ b/src/parse/ParsingAlgorithm.cpp
@@ -0,0 +1 @@
+#include "cru/parse/ParsingAlgorithm.hpp"
diff --git a/src/parse/ParsingAlgorithmContext.cpp b/src/parse/ParsingAlgorithmContext.cpp
new file mode 100644
index 00000000..98e2343b
--- /dev/null
+++ b/src/parse/ParsingAlgorithmContext.cpp
@@ -0,0 +1,9 @@
+#include "cru/parse/ParsingAlgorithmContext.hpp"
+
+namespace cru::parse {
+ParsingAlgorithmContext::ParsingAlgorithmContext(Grammar* grammar,
+ ParsingAlgorithm* algorithm)
+ : grammar_(grammar), algorithm_(algorithm) {}
+
+ParsingAlgorithmContext::~ParsingAlgorithmContext() {}
+} // namespace cru::parse
diff --git a/src/parse/ParsingContext.cpp b/src/parse/ParsingContext.cpp
new file mode 100644
index 00000000..aafaa65b
--- /dev/null
+++ b/src/parse/ParsingContext.cpp
@@ -0,0 +1,13 @@
+#include "cru/parse/ParsingContext.hpp"
+#include "cru/parse/ParsingAlgorithmContext.hpp"
+
+namespace cru::parse {
+ParsingContext::ParsingContext(
+ ParsingAlgorithmContext* parsing_algorithm_context,
+ std::vector<Terminal*> input)
+ : parsing_algorithm_context_(parsing_algorithm_context),
+ input_(std::move(input)) {}
+
+ParsingContext::~ParsingContext() {}
+
+} // namespace cru::parse