From f4b2027868c9733bbbbcb4c5ec6d5462a8447e5d Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Tue, 21 Jan 2020 22:49:30 +0100 Subject: Separate to cpp files --- lexer.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lexer.h (limited to 'lexer.h') diff --git a/lexer.h b/lexer.h new file mode 100644 index 0000000..c571294 --- /dev/null +++ b/lexer.h @@ -0,0 +1,56 @@ +#pragma once + +#include "minicc.h" +#include "bnf.h" + +struct TreeNode { + index_t parent{}; + std::vector childs; // fill char by char + std::vector child_names; // fill always + std::string name; +}; + +class Tree { +private: + std::map nodes; // index 0 = non existing; index starting at 1 + index_t node_num{}; + index_t root{}; + index_t last{}; + +public: + void clear(); + std::string GetType(); + bool Valid(const std::string& Top) const; + bool AddFirstNode(char c, const BNF& bnf, const std::map>& reverseBNF); + std::vector getParentTreeNode(const BNF& bnf, const std::map>& reverseBNF); + index_t GetLast(); + void AddRootNode(const TreeNode& newRootNode); + void RemoveRootNode(); + std::vector GetPath(std::string a, std::string b, const BNF& bnf, const std::map>& reverseBNF); + index_t AddNode(const std::string& name, const std::string& child_name, index_t parent_index, const BNF& bnf, const std::map>& reverseBNF); + void AddPath(const std::vector& path, index_t current_index, const BNF& bnf, const std::map>& reverseBNF); + bool Add(char c, const BNF& bnf, const std::map>& reverseBNF); + void Resolve(const BNF& bnf, const std::map>& reverseBNF); +}; + +class Lexer +{ + +private: + const BNF &bnf; + const std::string& Top; + + Location location{1, 0}; + + std::map> ReverseBNF; + + // to be called on token end + void FinalizeTree(Tree& tree, std::string& token, std::vector& result); + +public: + Lexer(const BNF& bnf, const std::string& Top); + std::vector Lex(const std::string& s); + +}; + + -- cgit v1.2.3