A stream of token */
| 121 | |
| 122 | /** A stream of token */ |
| 123 | class TokenStream |
| 124 | { |
| 125 | // NOTE: _tokens is never empty. The end of token stream is signalled by the End Token |
| 126 | public: |
| 127 | static constexpr size_t max_look_ahead = 10; |
| 128 | |
| 129 | public: |
| 130 | /** Constructor |
| 131 | * |
| 132 | * @param[in] s Input stream |
| 133 | * @param[in] delims Delimiter characters packed in a string. Each char from the string can be used as a delim on its own |
| 134 | */ |
| 135 | TokenStream(std::istream &s, const std::string &delims = ",\n"); |
| 136 | |
| 137 | /** Check if there're more (non-End) Tokens |
| 138 | * @return true If there are more tokens |
| 139 | * @return false If reached end of stream (only End token) |
| 140 | */ |
| 141 | explicit operator bool() const; |
| 142 | |
| 143 | /** Get and pop off the current token |
| 144 | * |
| 145 | * @return Token |
| 146 | */ |
| 147 | Token take(); |
| 148 | |
| 149 | /** Peek the next ith token |
| 150 | * |
| 151 | * @param[in] i The next ith token. i < @ref max_look_ahead. |
| 152 | * |
| 153 | * @return Token |
| 154 | */ |
| 155 | Token peek(size_t i = 0); |
| 156 | |
| 157 | /** Get the position of the current token |
| 158 | * |
| 159 | * @return CharPosition |
| 160 | */ |
| 161 | CharPosition current_pos() const |
| 162 | { |
| 163 | return _tokens.front().pos; |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | void read(); |
| 168 | |
| 169 | Token recognize_tok(char ch); |
| 170 | |
| 171 | Token num_st(std::string value = ""); |
| 172 | |
| 173 | Token float_after_dp_st(std::string value = ""); |
| 174 | |
| 175 | Token text_st(std::string value = ""); |
| 176 | |
| 177 | bool reached_end() const; |
| 178 | |
| 179 | bool is_delim(char ch) const; |
| 180 |