InFact
Interpreter and factory for easily creating C++ objects at run-time
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
stream-tokenizer-test.cc
Go to the documentation of this file.
1 // Copyright 2014, 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 //
36 
37 #include <iostream>
38 #include <string>
39 
40 #include "stream-tokenizer.h"
41 
42 using namespace std;
43 using namespace infact;
44 
45 int
46 main(int argc, char **argv) {
47  cerr << "Testing StreamTokenizer with string arg constructor:" << endl;
48  std::string test_string = "foo(bar, baz(34), bleh(\"fleh with spaces\"))";
49  StreamTokenizer st1(test_string);
50  while (st1.HasNext()) {
52  size_t start = st1.PeekTokenStart();
53  size_t line_number = st1.PeekTokenLineNumber();
54  cout << "token: \"" << st1.Next() << "\""
55  << "; type=" << StreamTokenizer::TypeName(type)
56  << "; start=" << start
57  << "; line=" << line_number
58  << endl;
59  cout << "chars so far: '" << st1.str() << "'" << endl;
60  }
61  cerr << "Done." << endl;
62 
63  cerr << "\nTesting Putback:" << endl;
64  st1.Putback();
65  while (st1.HasNext()) {
67  size_t start = st1.PeekTokenStart();
68  size_t line_number = st1.PeekTokenLineNumber();
69  cout << "token: \"" << st1.Next() << "\""
70  << "; type=" << StreamTokenizer::TypeName(type)
71  << "; start=" << start
72  << "; line=" << line_number
73  << endl;
74  cout << "chars so far: '" << st1.str() << "'" << endl;
75  }
76 
77  cerr << "\nTesting Rewind(3):" << endl;
78  st1.Rewind(3);
79  while (st1.HasNext()) {
81  size_t start = st1.PeekTokenStart();
82  size_t line_number = st1.PeekTokenLineNumber();
83  cout << "token: \"" << st1.Next() << "\""
84  << "; type=" << StreamTokenizer::TypeName(type)
85  << "; start=" << start
86  << "; line=" << line_number
87  << endl;
88  cout << "chars so far: '" << st1.str() << "'" << endl;
89  }
90 
91  cerr << "Testing Rewind():" << endl;
92  st1.Rewind();
93  while (st1.HasNext()) {
95  size_t start = st1.PeekTokenStart();
96  size_t line_number = st1.PeekTokenLineNumber();
97  cout << "token: \"" << st1.Next() << "\""
98  << "; type=" << StreamTokenizer::TypeName(type)
99  << "; start=" << start
100  << "; line=" << line_number
101  << endl;
102  cout << "chars so far: '" << st1.str() << "'" << endl;
103  }
104 
105  cerr << "\nReading from stdin until EOF:" << endl;
106 
107  StreamTokenizer st2(cin);
108  while (st2.HasNext()) {
110  cout << "token: \"" << st2.Next() << "\""
111  << "; type=" << StreamTokenizer::TypeName(type) << endl;
112  }
113 }
string Next()
Returns the next token in the token stream.
int main(int argc, char **argv)
TokenType PeekTokenType() const
Returns the type of the next token, or EOF_TYPE if there is no next token.
void Putback()
A synonym for Rewind(1).
string str()
Returns the entire sequence of characters read so far by this stream tokenizer as a newly constructed...
A simple class for tokenizing a stream of tokens for the formally specified language used to construc...
bool HasNext() const
Returns whether there is another token in the token stream.
Provides the StreamTokenizer class.
size_t PeekTokenStart() const
Returns the next token’s start position, or the byte position of the underlying byte stream if there ...
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.
void Rewind()
Rewinds this token stream to the beginning.