2020namespace fdapde {
2121
2222class Formula {
23+ // available tokens
24+ struct efx_token {
25+ private:
26+ std::string cov_;
27+ std::string efx_;
28+ public:
29+ efx_token (const std::string& cov, const std::string& efx) : cov_(cov), efx_(efx) { }
30+ // observers
31+ const std::string& cov () const { return cov_; }
32+ const std::string& efx () const { return efx_; }
33+ friend std::ostream& operator <<(std::ostream& os, const efx_token& m) {
34+ os << " (" << m.cov_ << " , " << m.efx_ << " )" ;
35+ return os;
36+ }
37+ };
38+
2339 std::string lhs_;
24- std::vector<std::string> rhs_;
40+ std::vector<std::string> covs_;
41+ std::vector<efx_token> efxs_;
2542
2643 void throw_parse_error_ (const std::string& msg) {
2744 throw std::runtime_error (std::string (" Formula parse error: " ) + msg);
2845 }
46+ void analyze_token_ (std::string token) {
47+ size_t pos = token.find (' |' );
48+ if (pos != std::string::npos) {
49+ std::string cov_token, efx_token;
50+ cov_token = token.substr (0 , pos);
51+ token.erase (0 , pos + 1 );
52+ efx_token = token;
53+ efxs_.emplace_back (cov_token, efx_token);
54+ } else {
55+ covs_.push_back (token);
56+ }
57+ return ;
58+ }
2959 public:
3060 Formula () noexcept = default ;
3161 Formula (const std::string& formula) {
@@ -37,20 +67,21 @@ class Formula {
3767 // rhs parsing logic
3868 std::string rhs = formula.substr (tilde + 1 , formula.size () - tilde - 1 );
3969 std::erase (rhs, ' ' );
40- if (rhs.empty ()) { throw_parse_error_ (" no rhs found." ); }
70+ if (rhs.empty ()) { throw_parse_error_ (" no rhs found." ); }
4171 size_t pos = rhs.find (' +' );
4272 std::string token;
4373 while (pos != std::string::npos) {
4474 token = rhs.substr (0 , pos);
45- rhs_. push_back (token);
75+ analyze_token_ (token);
4676 rhs.erase (0 , pos + 1 );
47- pos = rhs.find (' +' );
77+ pos = rhs.find (' +' );
4878 }
49- rhs_. push_back (rhs);
79+ analyze_token_ (rhs);
5080 }
5181 // observers
5282 const std::string& lhs () const { return lhs_; }
53- const std::vector<std::string>& rhs () const { return rhs_; }
83+ const std::vector<std::string>& covs () const { return covs_; }
84+ const std::vector<efx_token>& efxs () const { return efxs_; }
5485};
5586
5687} // namespace fdapde
0 commit comments