diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-01-21 22:49:30 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-01-21 22:49:30 +0100 |
commit | f4b2027868c9733bbbbcb4c5ec6d5462a8447e5d (patch) | |
tree | 40ce459f1a501d6c88936c78f6dbcbb8aadd04ca /lexer.h | |
parent | 08997620fd617b580c1adbcb03c90cf621aa7069 (diff) |
Separate to cpp files
Diffstat (limited to 'lexer.h')
-rw-r--r-- | lexer.h | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -0,0 +1,56 @@ +#pragma once + +#include "minicc.h" +#include "bnf.h" + +struct TreeNode { + index_t parent{}; + std::vector<index_t> childs; // fill char by char + std::vector<std::string> child_names; // fill always + std::string name; +}; + +class Tree { +private: + std::map<index_t, TreeNode> 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<std::string, std::set<std::string>>& reverseBNF); + std::vector<TreeNode> getParentTreeNode(const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF); + index_t GetLast(); + void AddRootNode(const TreeNode& newRootNode); + void RemoveRootNode(); + std::vector<std::string> GetPath(std::string a, std::string b, const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF); + index_t AddNode(const std::string& name, const std::string& child_name, index_t parent_index, const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF); + void AddPath(const std::vector<std::string>& path, index_t current_index, const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF); + bool Add(char c, const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF); + void Resolve(const BNF& bnf, const std::map<std::string, std::set<std::string>>& reverseBNF); +}; + +class Lexer +{ + +private: + const BNF &bnf; + const std::string& Top; + + Location location{1, 0}; + + std::map<std::string, std::set<std::string>> ReverseBNF; + + // to be called on token end + void FinalizeTree(Tree& tree, std::string& token, std::vector<Token>& result); + +public: + Lexer(const BNF& bnf, const std::string& Top); + std::vector<Token> Lex(const std::string& s); + +}; + + |