From 360a3bdbc9cf7084d9f1f1af09ea99831edffd34 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 15 Dec 2021 21:11:26 +0800 Subject: ... --- include/cru/parse/ParsingAlgorithm.hpp | 22 ++++++++++++++++++++++ include/cru/parse/ParsingAlgorithmContext.hpp | 23 +++++++++++++++++++++++ include/cru/parse/ParsingContext.hpp | 21 +++++++++++++++++++++ src/parse/CMakeLists.txt | 3 +++ src/parse/ParsingAlgorithm.cpp | 1 + src/parse/ParsingAlgorithmContext.cpp | 9 +++++++++ src/parse/ParsingContext.cpp | 13 +++++++++++++ 7 files changed, 92 insertions(+) create mode 100644 include/cru/parse/ParsingAlgorithm.hpp create mode 100644 include/cru/parse/ParsingAlgorithmContext.hpp create mode 100644 include/cru/parse/ParsingContext.hpp create mode 100644 src/parse/ParsingAlgorithm.cpp create mode 100644 src/parse/ParsingAlgorithmContext.cpp create mode 100644 src/parse/ParsingContext.cpp 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 input); + + CRU_DELETE_COPY(ParsingContext) + CRU_DELETE_MOVE(ParsingContext) + + ~ParsingContext(); + + private: + ParsingAlgorithmContext* parsing_algorithm_context_; + std::vector 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 input) + : parsing_algorithm_context_(parsing_algorithm_context), + input_(std::move(input)) {} + +ParsingContext::~ParsingContext() {} + +} // namespace cru::parse -- cgit v1.2.3