#include #include #include // #include #include #include #include "FRC.h" #include "grammar.h" #include "word.h" TransformatedWord Grammar::applyTransforms(const Word& w) const { TransformatedWord out{w}; for (const auto& c : transform_stages) { const Grammar::channelID cID = c.first; // icu::UnicodeString cline; for (const auto& s : c.second) { auto& cline = out.channels[cID]; cline = (*s)(cline, w); out.history[cID].push_back(cline); // out.channels[cID] = cline; } } return out; } icu::UnicodeString Grammar::Transformer::operator()(const icu::UnicodeString& str, const Word& w) const { return str; } icu::UnicodeString Grammar::assigner::operator()(const icu::UnicodeString& str, const Word& w) const { icu::UnicodeString ret; for (const auto& i : assign) { ret += i.first; if (i.second) { ret += w[g->chI(i.second.getName())]; } } return ret; } icu::UnicodeString Grammar::regexer::operator()(const icu::UnicodeString& str, const Word& w) const { UErrorCode status = U_ZERO_ERROR; icu::UnicodeString ret{str}; for (const auto& r : rules) { std::unique_ptr matcher{r.match->matcher(ret, status)}; if (U_FAILURE(status)) { throw status; } ret = matcher->replaceAll(r.replace, status); if (U_FAILURE(status)) { throw status; } } return ret; } // icu::UnicodeString Grammar::regexer2::operator()(const icu::UnicodeString& str, const Word& w) const // { // std::u32string ret{toUTF32(str)}; // for (const auto& r : rules) { // ret = std::regex_replace(ret, r.match, r.replace); // } // return ret; // } icu::UnicodeString Grammar::state_machine::operator()(const icu::UnicodeString& str, const Word& w) const { icu::UnicodeString state{u"S"}, tmp{str}, ret; if (tapeDirection & Grammar::state_machine::dir::InRev) { tmp.reverse(); } StringCharacterIterator iter{tmp}; for (iter.setToStart(); iter.hasNext(); ) { UChar32 c=iter.next32PostInc(); auto next_computed = states.at(state)(c); ret += next_computed.first; if (!next_computed.second.isEmpty()) { state = next_computed.second; } } ret += states.at(state).end(); if (tapeDirection & Grammar::state_machine::dir::OutRev) { ret.reverse(); } return ret; } std::pair Grammar::state_machine::State::operator()(UChar32 c) const { if (charRules.count(c)) { return charRules.at(c)(c); } else { for (const auto& m : mapRules) { if (m.match(c)) { return m(c); } } for (const auto& m : matchRules) { if (m.match(c)) { return m(c); } } if (hasDefault) { return defaultRule(c); } else { return {c,returnState}; } } } icu::UnicodeString Grammar::state_machine::State::sub(const ParsedString& replace, UChar32 c) { icu::UnicodeString ret; for (const auto& p : replace) { ret += p.first; if (p.second) { ret += c; } } return c; } std::pair Grammar::state_machine::State::MatchRule::operator()(UChar32 c) const { throw 40000+__LINE__; } bool Grammar::state_machine::State::MatchRule::match(UChar32 c) const { throw 40000+__LINE__; } std::pair Grammar::state_machine::State::SetRule::operator()(UChar32 c) const { throw 40000+__LINE__; } std::pair Grammar::state_machine::State::MapRule::operator()(UChar32 c) const { throw 40000+__LINE__; }