36 #ifndef RERANKER_STREAM_INIT_H_
37 #define RERANKER_STREAM_INIT_H_
43 #include <unordered_map>
44 #include <unordered_set>
52 using std::ostringstream;
53 using std::shared_ptr;
54 using std::unordered_map;
74 template <
typename T>
class Factory;
91 (st.
Peek() ==
"nullptr" || st.
Peek() ==
"NULL");
94 err_ss <<
"FactoryInitializer: expected \"nullptr\", \"NULL\" or "
95 <<
"IDENTIFIER token at stream "
99 throw std::runtime_error(err_ss.str());
118 ostringstream err_ss;
119 err_ss <<
"IntInitializer: expected NUMBER token at stream "
122 << st.
Peek() <<
"\"";
123 throw std::runtime_error(err_ss.str());
125 (*member_) = atoi(st.
Next().c_str());
140 ostringstream err_ss;
141 err_ss <<
"DoubleInitializer: expected NUMBER token at stream "
144 << st.
Peek() <<
"\"";
145 throw std::runtime_error(err_ss.str());
147 (*member_) = atof(st.
Next().c_str());
162 ostringstream err_ss;
163 err_ss <<
"BoolInitializer: expected RESERVED_WORD token at stream "
166 << st.
Peek() <<
"\"";
167 throw std::runtime_error(err_ss.str());
170 string next_tok = st.
Next();
171 if (next_tok ==
"false") {
173 }
else if (next_tok ==
"true") {
176 ostringstream err_ss;
177 err_ss <<
"Initializer<bool>: expected either \"true\" or \"false\" "
178 <<
"token at stream position " << next_tok_start <<
" but found "
179 <<
"token: \"" << next_tok <<
"\"";
180 throw new std::runtime_error(err_ss.str());
196 ostringstream err_ss;
197 err_ss <<
"StringInitializer: expected STRING token at stream "
200 << st.
Peek() <<
"\"";
201 throw std::runtime_error(err_ss.str());
203 (*member_) = st.
Next();
226 if (st.
Peek() ==
"{") {
231 ostringstream err_ss;
232 err_ss <<
"Initializer<vector<T>>: "
233 <<
"error: expected '{' at stream position "
236 throw std::runtime_error(err_ss.str());
239 while (st.
Peek() !=
"}") {
242 element_init->
Init(st, env);
243 member_->push_back(vector_element);
247 if (st.
Peek() !=
"," && st.
Peek() !=
"}") {
248 ostringstream err_ss;
249 err_ss <<
"Initializer<vector<T>>: "
250 <<
"error: expected ',' or '}' at stream position "
252 throw std::runtime_error(err_ss.str());
255 if (st.
Peek() ==
",") {
Initializer(bool *member)
Initializer(double *member)
A simple class for tokenizing a stream of tokens for the formally specified language used to construc...
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
size_t PeekTokenStart() const
Returns the next token’s start position, or the byte position of the underlying byte stream if there ...
Initializer(string *member)
static const char * TypeName(TokenType token_type)
Returns a string type name for the specified TokenType constant.
virtual void Init(StreamTokenizer &st, Environment *env=NULL)=0
size_t PeekPrevTokenStart() const
Factory for dynamically created instance of the specified type.
Provides the StreamTokenizer class.
string Next()
Returns the next token in the token stream.
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
shared_ptr< T > CreateOrDie(StreamTokenizer &st, Environment *env=NULL)
Dynamically creates an object, whose type and initialization are contained in a specification string...
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
An interface for an environment in which variables of various types are mapped to their values...
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
A class to initialize a Factory-constructible object.
TokenType
The set of types of tokens read by this stream tokenizer.
string Peek() const
Returns the next token that would be returned by the Next method.
virtual ~StreamInitializer()
Initializer(vector< T > *member)
An interface that allows for a primitive, Factory-constructible object or vector thereof to be initia...
TokenType PeekTokenType() const
Returns the type of the next token, or EOF_TYPE if there is no next token.