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-tokenizer-test.C
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 #include <iostream>
37 
38 #include "stream-tokenizer.H"
39 
40 using namespace reranker;
41 using namespace std;
42 
43 int
44 main(int argc, char **argv) {
45  cerr << "Testing StreamTokenizer with string arg constructor:" << endl;
46  string test_string = "foo(bar, baz(34), bleh(\"fleh with spaces\"))";
47  StreamTokenizer st1(test_string);
48  while (st1.HasNext()) {
50  size_t start = st1.PeekTokenStart();
51  size_t line_number = st1.PeekTokenLineNumber();
52  cout << "token: \"" << st1.Next() << "\""
53  << "; type=" << StreamTokenizer::TypeName(type)
54  << "; start=" << start
55  << "; line=" << line_number
56  << endl;
57  cout << "chars so far: '" << st1.str() << "'" << endl;
58  }
59  cerr << "Done." << endl;
60 
61  cerr << "\nTesting Putback:" << endl;
62  st1.Putback();
63  while (st1.HasNext()) {
65  size_t start = st1.PeekTokenStart();
66  size_t line_number = st1.PeekTokenLineNumber();
67  cout << "token: \"" << st1.Next() << "\""
68  << "; type=" << StreamTokenizer::TypeName(type)
69  << "; start=" << start
70  << "; line=" << line_number
71  << endl;
72  cout << "chars so far: '" << st1.str() << "'" << endl;
73  }
74 
75  cerr << "\nTesting Rewind(3):" << endl;
76  st1.Rewind(3);
77  while (st1.HasNext()) {
79  size_t start = st1.PeekTokenStart();
80  size_t line_number = st1.PeekTokenLineNumber();
81  cout << "token: \"" << st1.Next() << "\""
82  << "; type=" << StreamTokenizer::TypeName(type)
83  << "; start=" << start
84  << "; line=" << line_number
85  << endl;
86  cout << "chars so far: '" << st1.str() << "'" << endl;
87  }
88 
89  cerr << "Testing Rewind():" << endl;
90  st1.Rewind();
91  while (st1.HasNext()) {
93  size_t start = st1.PeekTokenStart();
94  size_t line_number = st1.PeekTokenLineNumber();
95  cout << "token: \"" << st1.Next() << "\""
96  << "; type=" << StreamTokenizer::TypeName(type)
97  << "; start=" << start
98  << "; line=" << line_number
99  << endl;
100  cout << "chars so far: '" << st1.str() << "'" << endl;
101  }
102 
103  cerr << "\nReading from stdin until EOF:" << endl;
104 
105  StreamTokenizer st2(cin);
106  while (st2.HasNext()) {
108  cout << "token: \"" << st2.Next() << "\""
109  << "; type=" << StreamTokenizer::TypeName(type) << endl;
110  }
111 }
A simple class for tokenizing a stream of tokens for the formally specified language used to construc...
string str()
Returns the entire sequence of characters read so far by this stream tokenizer as a newly constructed...
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.
bool HasNext() const
Returns whether there is another token in the token stream.
Provides the StreamTokenizer class.
string Next()
Returns the next token in the token stream.
void Rewind()
Rewinds this token stream to the beginning.
size_t PeekTokenLineNumber() const
Returns the line number of the first byte of the next token, or the current line number of the underl...
TokenType
The set of types of tokens read by this stream tokenizer.
int main(int argc, char **argv)
void Putback()
A synonym for Rewind(1).
TokenType PeekTokenType() const
Returns the type of the next token, or EOF_TYPE if there is no next token.