Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stream-init.H
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
35 
36 #ifndef RERANKER_STREAM_INIT_H_
37 #define RERANKER_STREAM_INIT_H_
38 
39 #include <cstdlib>
40 #include <iostream>
41 #include <sstream>
42 #include <memory>
43 #include <unordered_map>
44 #include <unordered_set>
45 #include <vector>
46 #include <stdexcept>
47 
48 #include "stream-tokenizer.H"
49 
50 namespace reranker {
51 
52 using std::ostringstream;
53 using std::shared_ptr;
54 using std::unordered_map;
55 using std::vector;
56 
57 class Environment;
58 
68  public:
70  virtual ~StreamInitializer() { }
71  virtual void Init(StreamTokenizer &st, Environment *env = NULL) = 0;
72 };
73 
74 template <typename T> class Factory;
75 
82 template <typename T>
84  public:
85  Initializer(T *member) : member_(member) { }
86  virtual ~Initializer() { }
87  virtual void Init(StreamTokenizer &st, Environment *env = NULL) {
88  StreamTokenizer::TokenType token_type = st.PeekTokenType();
89  bool is_null =
90  token_type == StreamTokenizer::RESERVED_WORD &&
91  (st.Peek() == "nullptr" || st.Peek() == "NULL");
92  if (!(is_null || token_type == StreamTokenizer::IDENTIFIER)) {
93  ostringstream err_ss;
94  err_ss << "FactoryInitializer: expected \"nullptr\", \"NULL\" or "
95  << "IDENTIFIER token at stream "
96  << "position " << st.PeekTokenStart() << " but found "
97  << StreamTokenizer::TypeName(token_type) << " token: \""
98  << st.Peek() << "\"";
99  throw std::runtime_error(err_ss.str());
100  }
102  (*member_) = factory.CreateOrDie(st, env);
103  }
104  private:
105  T *member_;
106 };
107 
110 template<>
111 class Initializer<int> : public StreamInitializer {
112  public:
113  Initializer(int *member) : member_(member) { }
114  virtual ~Initializer() { }
115  virtual void Init(StreamTokenizer &st, Environment *env = NULL) {
116  StreamTokenizer::TokenType token_type = st.PeekTokenType();
117  if (token_type != StreamTokenizer::NUMBER) {
118  ostringstream err_ss;
119  err_ss << "IntInitializer: expected NUMBER token at stream "
120  << "position " << st.PeekTokenStart() << " but found "
121  << StreamTokenizer::TypeName(token_type) << " token: \""
122  << st.Peek() << "\"";
123  throw std::runtime_error(err_ss.str());
124  }
125  (*member_) = atoi(st.Next().c_str());
126  }
127  private:
128  int *member_;
129 };
130 
132 template<>
133 class Initializer<double> : public StreamInitializer {
134  public:
135  Initializer(double *member) : member_(member) { }
136  virtual ~Initializer() { }
137  virtual void Init(StreamTokenizer &st, Environment *env = NULL) {
138  StreamTokenizer::TokenType token_type = st.PeekTokenType();
139  if (token_type != StreamTokenizer::NUMBER) {
140  ostringstream err_ss;
141  err_ss << "DoubleInitializer: expected NUMBER token at stream "
142  << "position " << st.PeekTokenStart() << " but found "
143  << StreamTokenizer::TypeName(token_type) << " token: \""
144  << st.Peek() << "\"";
145  throw std::runtime_error(err_ss.str());
146  }
147  (*member_) = atof(st.Next().c_str());
148  }
149  private:
150  double *member_;
151 };
152 
154 template<>
155 class Initializer<bool> : public StreamInitializer {
156  public:
157  Initializer(bool *member) : member_(member) { }
158  virtual ~Initializer() { }
159  virtual void Init(StreamTokenizer &st, Environment *env = NULL) {
160  StreamTokenizer::TokenType token_type = st.PeekTokenType();
161  if (token_type != StreamTokenizer::RESERVED_WORD) {
162  ostringstream err_ss;
163  err_ss << "BoolInitializer: expected RESERVED_WORD token at stream "
164  << "position " << st.PeekTokenStart() << " but found "
165  << StreamTokenizer::TypeName(token_type) << " token: \""
166  << st.Peek() << "\"";
167  throw std::runtime_error(err_ss.str());
168  }
169  size_t next_tok_start = st.PeekTokenStart();
170  string next_tok = st.Next();
171  if (next_tok == "false") {
172  (*member_) = false;
173  } else if (next_tok == "true") {
174  (*member_) = true;
175  } else {
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());
181  }
182  }
183  private:
184  bool *member_;
185 };
186 
188 template<>
189 class Initializer<string> : public StreamInitializer {
190  public:
191  Initializer(string *member) : member_(member) { }
192  virtual ~Initializer() { }
193  virtual void Init(StreamTokenizer &st, Environment *env = NULL) {
194  StreamTokenizer::TokenType token_type = st.PeekTokenType();
195  if (token_type != StreamTokenizer::STRING) {
196  ostringstream err_ss;
197  err_ss << "StringInitializer: expected STRING token at stream "
198  << "position " << st.PeekTokenStart() << " but found "
199  << StreamTokenizer::TypeName(token_type) << " token: \""
200  << st.Peek() << "\"";
201  throw std::runtime_error(err_ss.str());
202  }
203  (*member_) = st.Next();
204  }
205  private:
206  string *member_;
207 };
208 
214 template<typename T>
215 class Initializer<vector<T> > : public StreamInitializer {
216  public:
217  Initializer(vector<T> *member) : member_(member) { }
218  virtual ~Initializer() { }
219  virtual void Init(StreamTokenizer &st, Environment *env = NULL) {
220  // Either the next token is an open brace (if reading tokens from
221  // within a Factory-constructible object's member init list), or
222  // else we just read an open brace (if Interpreter is reading tokens).
223  //
224  // TODO(dbikel): Change this "brace optional" business, now that we
225  // we can rewind the token stream.
226  if (st.Peek() == "{") {
227  // Consume open brace.
228  st.Next();
229  } else {
230  if (st.PeekPrev() != "{") {
231  ostringstream err_ss;
232  err_ss << "Initializer<vector<T>>: "
233  << "error: expected '{' at stream position "
234  << st.PeekPrevTokenStart() << " but found \""
235  << st.PeekPrev() << "\"";
236  throw std::runtime_error(err_ss.str());
237  }
238  }
239  while (st.Peek() != "}") {
240  T vector_element;
241  Initializer<T> *element_init = new Initializer<T>(&vector_element);
242  element_init->Init(st, env);
243  member_->push_back(vector_element);
244  delete element_init;
245  // Each vector element initializer must be followed by a comma
246  // or the final closing parenthesis.
247  if (st.Peek() != "," && st.Peek() != "}") {
248  ostringstream err_ss;
249  err_ss << "Initializer<vector<T>>: "
250  << "error: expected ',' or '}' at stream position "
251  << st.PeekTokenStart() << " but found \"" << st.Peek() << "\"";
252  throw std::runtime_error(err_ss.str());
253  }
254  // Read comma, if present.
255  if (st.Peek() == ",") {
256  st.Next();
257  }
258  }
259  // Consume close brace.
260  st.Next();
261  }
262  private:
263  vector<T> *member_;
264 };
265 
266 } // namespace reranker
267 
268 #endif
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)
Definition: stream-init.H:115
size_t PeekTokenStart() const
Returns the next token’s start position, or the byte position of the underlying byte stream if there ...
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.
Definition: factory.H:396
virtual ~Initializer()
Definition: stream-init.H:86
Provides the StreamTokenizer class.
string Next()
Returns the next token in the token stream.
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
Definition: stream-init.H:193
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
Definition: stream-init.H:137
shared_ptr< T > CreateOrDie(StreamTokenizer &st, Environment *env=NULL)
Dynamically creates an object, whose type and initialization are contained in a specification string...
Definition: factory.H:562
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
Definition: stream-init.H:219
An interface for an environment in which variables of various types are mapped to their values...
Definition: environment.H:125
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
Definition: stream-init.H:87
virtual void Init(StreamTokenizer &st, Environment *env=NULL)
Definition: stream-init.H:159
A class to initialize a Factory-constructible object.
Definition: stream-init.H:83
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.
Initializer(T *member)
Definition: stream-init.H:85
Initializer(vector< T > *member)
Definition: stream-init.H:217
An interface that allows for a primitive, Factory-constructible object or vector thereof to be initia...
Definition: stream-init.H:67
TokenType PeekTokenType() const
Returns the type of the next token, or EOF_TYPE if there is no next token.