diff options
author | crupest <crupest@outlook.com> | 2021-12-15 21:11:26 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-12-15 21:11:26 +0800 |
commit | 360a3bdbc9cf7084d9f1f1af09ea99831edffd34 (patch) | |
tree | 616f684c1602b1aba55f9427106b34cbc727f5e2 | |
parent | fa3794469430b4ddfe0b0ca62850e9911c99b558 (diff) | |
download | cru-360a3bdbc9cf7084d9f1f1af09ea99831edffd34.tar.gz cru-360a3bdbc9cf7084d9f1f1af09ea99831edffd34.tar.bz2 cru-360a3bdbc9cf7084d9f1f1af09ea99831edffd34.zip |
...
-rw-r--r-- | include/cru/parse/ParsingAlgorithm.hpp | 22 | ||||
-rw-r--r-- | include/cru/parse/ParsingAlgorithmContext.hpp | 23 | ||||
-rw-r--r-- | include/cru/parse/ParsingContext.hpp | 21 | ||||
-rw-r--r-- | src/parse/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/parse/ParsingAlgorithm.cpp | 1 | ||||
-rw-r--r-- | src/parse/ParsingAlgorithmContext.cpp | 9 | ||||
-rw-r--r-- | src/parse/ParsingContext.cpp | 13 |
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 |