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 +++++++++++++++++++++ 3 files changed, 66 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 (limited to 'include/cru/parse') 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 -- cgit v1.2.3