#include "FRC.h" #include "loader.h" #include #include #include class BNFGrammar {}; static std::vector> extractTransform(const YAML::Node& n); Grammar loadGrammar(YAML::Node node) { Grammar g; // extract: // channelnames // transformations // nodes if (node["channels"]) { for (const auto& c : node["channels"]) { g.nameChannel( fromUTF8(c.first.template as()), fromUTF8(c.second.template as()) ); } node.remove("channels"); } if (node["replace"]) { for (const auto& rch : node["replace"]) { g.transform_stages[ g.chI(fromUTF8(rch.first.template as())) ] = extractTransform(rch.second); } node.remove("replace"); } else { if (node["replacement"]) { g.transform_stages[g.chI(u"val")] = extractTransform(node["replacement"]); node.remove("replacement"); } if (node["replaceIPA"]) { g.transform_stages[g.chI(u"ipa")] = extractTransform(node["replaceIPA"]); node.remove("replaceIPA"); } } for (const auto& n : node) { Grammar::nodeData branches; for (const auto& b : n.second) { // std::cerr<<':'; Grammar::nodeData::value_type m; for (const auto& c : b) { // std::cerr<<'.'; m[g.chI(fromUTF8(c.first.as()))] = fromUTF8(c.second.as()); } branches.push_back(std::move(m)); } // std::cerr<()); int c = std::count( name.getBuffer(), name.getBuffer()+name.length(), u'|' ); #ifdef _DEBUG std::cerr<<"Node "<() <<" has "< extractStates(const YAML::Node& n) { auto tmp = std::make_unique(); for (const auto& s : n) { if (s.first.as() == "reversed") { auto r = s.second.as(); if (r > 3 || r < 0) { throw std::runtime_error("\"reversed\" must be 0..3"); } tmp->tapeDirection = static_cast(r); } else { Grammar::state_machine::State state; for (const auto& r : s) { const auto& i = r.first.as(); const auto& d = r.second.as>(); if (i == "default") { auto v = r.second.as>(); icu::UnicodeString o{fromUTF8(v.at(0))}, s{v.size()>1?fromUTF8(v[1]):u""}; state.defaultRule = {o,s}; state.hasDefault = true; } else if (i == "return") { state.returnState = fromUTF8(r.second.as()); } else if (i == "set") { auto v = r.second.as>(); icu::UnicodeString m{fromUTF8(v.at(0))}, r{fromUTF8(v.at(1))}, s{v.size()>2?fromUTF8(v[2]):u""}; state.setRules.emplace_back(m,r,s); } else if (i == "match") { auto v = r.second.as>(); icu::UnicodeString m{fromUTF8(v.at(0))}, s{fromUTF8(v.at(1))}; state.matchRules.emplace_back(m,s); } else if (i == "map") { auto v = r.second.as>(); icu::UnicodeString m{fromUTF8(v.at(0))}, r{fromUTF8(v.at(1))}, s{v.size()>2?fromUTF8(v[2]):u""}; state.mapRules.emplace_back(m,r,s); } else if (i == "end") { state.endRule = fromUTF8(r.second.as()); } else { auto v = r.second.as>(); icu::UnicodeString o{fromUTF8(v.at(0))}, s{v.size()>1?fromUTF8(v[1]):u""}; if (o.countChar32() == 1) { state.charRules[o.char32At(0)] = {o,s}; } else { //Invalid rule } } } tmp->states[fromUTF8(s.first.as())] = state; } } return tmp; } static std::vector> extractTransform(const YAML::Node& n) { std::vector> ret; for (auto& stage : n) { if (stage.IsSequence()) { // std::vector REs; // for (auto& RE : stage) { // REs.emplace_back( // fromUTF8(RE["m"].as()), // fromUTF8(RE["r"].as()) // ); // std::clog<<__LINE__<<" m: "<()<()<(std::move(REs))); std::vector REs; for (auto& RE : stage) { UErrorCode status{U_ZERO_ERROR}; UParseError pe; REs.emplace_back( fromUTF8(RE["m"].as()), pe, status, fromUTF8(RE["r"].as()) ); std::clog<<__LINE__<<" m: "<()<()<(std::move(REs))); } else if (stage["S"]) { ret.push_back(extractStates(stage)); } } return ret; } YAML::Node dumpGrammar(const Grammar&) { throw 10000+__LINE__; } Grammar loadBNF(const BNFGrammar&) { throw 10000+__LINE__; } BNFGrammar dumpBNF(const Grammar&) { throw 10000+__LINE__; }