aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-12-21 15:13:55 +0800
committercrupest <crupest@outlook.com>2021-12-21 15:13:55 +0800
commit061f9ad04131febcc66975fd3d95b332789da52f (patch)
treeecd33d0eefdd4fc124f081512906adb9c235dd50
parent360a3bdbc9cf7084d9f1f1af09ea99831edffd34 (diff)
downloadcru-061f9ad04131febcc66975fd3d95b332789da52f.tar.gz
cru-061f9ad04131febcc66975fd3d95b332789da52f.tar.bz2
cru-061f9ad04131febcc66975fd3d95b332789da52f.zip
...
-rw-r--r--include/cru/parse/ParsingTreeNode.hpp33
-rw-r--r--src/parse/CMakeLists.txt1
-rw-r--r--src/parse/ParsingTreeNode.cpp27
3 files changed, 61 insertions, 0 deletions
diff --git a/include/cru/parse/ParsingTreeNode.hpp b/include/cru/parse/ParsingTreeNode.hpp
new file mode 100644
index 00000000..d1c3d058
--- /dev/null
+++ b/include/cru/parse/ParsingTreeNode.hpp
@@ -0,0 +1,33 @@
+#pragma once
+#include "Grammar.hpp"
+
+#include <vector>
+
+namespace cru::parse {
+class ParsingTreeNode {
+ public:
+ ParsingTreeNode(Symbol* symbol, Production* production);
+
+ CRU_DELETE_COPY(ParsingTreeNode)
+ CRU_DELETE_MOVE(ParsingTreeNode)
+
+ // In destructor, it will delete all children.
+ ~ParsingTreeNode();
+
+ public:
+ Symbol* GetSymbol() const { return symbol_; }
+ Production* GetProduction() const { return production_; }
+ Grammar* GetGrammar() const;
+
+ const std::vector<ParsingTreeNode*>& GetChildren() const { return children_; }
+
+ void AddChild(ParsingTreeNode* child);
+ void AddChild(ParsingTreeNode* child, Index index);
+ void RemoveChild(Index index);
+
+ private:
+ Symbol* symbol_;
+ Production* production_;
+ std::vector<ParsingTreeNode*> children_;
+};
+} // namespace cru::parse
diff --git a/src/parse/CMakeLists.txt b/src/parse/CMakeLists.txt
index 7c868f98..631c9881 100644
--- a/src/parse/CMakeLists.txt
+++ b/src/parse/CMakeLists.txt
@@ -4,6 +4,7 @@ add_library(cru_parse SHARED
ParsingAlgorithm.cpp
ParsingAlgorithmContext.cpp
ParsingContext.cpp
+ ParsingTreeNode.cpp
Production.cpp
Symbol.cpp
Terminal.cpp
diff --git a/src/parse/ParsingTreeNode.cpp b/src/parse/ParsingTreeNode.cpp
new file mode 100644
index 00000000..23bbf803
--- /dev/null
+++ b/src/parse/ParsingTreeNode.cpp
@@ -0,0 +1,27 @@
+#include "cru/parse/ParsingTreeNode.hpp"
+
+namespace cru::parse {
+ParsingTreeNode::ParsingTreeNode(Symbol* symbol, Production* production)
+ : symbol_(symbol), production_(production) {}
+
+ParsingTreeNode::~ParsingTreeNode() {
+ for (auto child : children_) {
+ delete child;
+ }
+}
+
+Grammar* ParsingTreeNode::GetGrammar() const { return symbol_->GetGrammar(); }
+
+void ParsingTreeNode::AddChild(ParsingTreeNode* child) {
+ children_.push_back(child);
+}
+
+void ParsingTreeNode::AddChild(ParsingTreeNode* child, Index index) {
+ children_.insert(children_.begin() + index, child);
+}
+
+void ParsingTreeNode::RemoveChild(Index index) {
+ delete children_[index];
+ children_.erase(children_.begin() + index);
+}
+} // namespace cru::parse