#pragma once #include "minicc.h" #include "bnf.h" namespace Lex { 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); static void PreprocessorTokensToTokens(std::vector& tokens); }; } // namespace Lex