diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-01-15 22:44:41 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-01-15 22:44:41 +0100 |
commit | c413fee0cde65e379f82afffd8d701f663aeb0be (patch) | |
tree | 9225f37e595cb43ef8598174364767c5898f80ea | |
parent | 44b2cf820149eb751474d9ccd2746359da73a4e9 (diff) |
Lex (WIP)
-rw-r--r-- | minicc.cpp | 51 |
1 files changed, 46 insertions, 5 deletions
@@ -35,6 +35,20 @@ BNF Reverse(BNF bnf){ return {}; // TODO } +using index_t = size_t; + +struct TreeNode { + index_t parent; + std::vector<index_t> childs; + std::string name; +}; + +using Tree = std::map<index_t, TreeNode>; + +bool ValidTree(const Tree& tree) { + return true; // TODO +} + std::vector<std::string> Lex(std::string s, std::string Top, BNF bnf) { std::vector<std::string> result; @@ -43,13 +57,39 @@ std::vector<std::string> Lex(std::string s, std::string Top, BNF bnf) BNF ReverseBNF{ Reverse(bnf)}; std::string Whitespace{"\t \n\r"}; + std::deque<Tree> candidates; for (size_t pos{0}; pos < s.size(); pos++) { char c{s[pos]}; - if (token.empty() and Whitespace.find(c) != std::string::npos) - continue; // skip whitespace between tokens - - auto Path = GetPath(std::string{1, c}, ReverseBNF, Top); + if (Whitespace.find(c) != std::string::npos) { + if (candidates.empty()) { // skip + if (!token.empty()) + throw std::runtime_error("Expected empty token, got "s + token); + } else { // check candidates + bool valid{false}; + for (const auto& ct : candidates) { + if (ValidTree(ct)) { + result.push_back(token); + token.clear(); + valid = true; + } + } + if (!valid) + throw std::runtime_error("Invalid token: "s + token); + + candidates.clear(); + } + } else { // no whitespace: try to add to tree + for (const auto& ct : candidates) { + if (!TryAdd(cti, c)) { + candidates.erase(i); // no candidate anymore + } + } + if (candidates.empty()) { // no candidates anymore + } else { + } + //token.push_back(c); + } } return result; @@ -95,7 +135,8 @@ TEST_F(Test, BNF) { {"digit", {{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }}}, {"identifier-nondigit", {{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_"}}}, - {"preprocessing-op-or-punc", {{";"}}}, + {"preprocessing-op-or-punc", {{";"}, + {"="}}}, {"pp-number", {{"digit"}, {"pp-number", "digit"}}} }; |