-
Notifications
You must be signed in to change notification settings - Fork 71
/
cli_gp.cpp
303 lines (266 loc) · 11.1 KB
/
cli_gp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/////////////////////////////////////////////////////////////////
// gp command file.
//
// Author: Jonathan Voigt, voigtjr@gmail.com, Bob Marinier
// Date : 2008
//
/////////////////////////////////////////////////////////////////
#include <portability.h>
#include "cli_CommandLineInterface.h"
#include "cli_Commands.h"
#include <iostream>
#include <sstream>
using namespace cli;
typedef std::list< std::string > valueCollection;
struct iterTriple
{
valueCollection::iterator current;
valueCollection::iterator begin;
valueCollection::iterator end;
};
bool CommandLineInterface::DoGP(const std::string& productionString)
{
// set up collection of collections of strings segments:
std::list< valueCollection > topLevel;
bool pipe = false;
bool inValues = false;
std::string::size_type pos = 0; // result of current search
std::string::size_type searchpos = 0; // result of previous search
const char* targets = "\\|[] \n\r\t#";
valueCollection currentValueCollection;
std::string currentValueToken;
size_t total = 0;
for ( pos = productionString.find_first_of(targets, searchpos); pos != std::string::npos; pos = productionString.find_first_of(targets, searchpos) ) {
switch ( productionString[ pos ] ) {
case '\\': // skip backslashes
// if the escaped char is a space, consume the space and drop the backslash
if(productionString[ pos+1 ] == ' ')
{
currentValueToken += productionString.substr( searchpos, (pos - searchpos) );
currentValueToken += productionString.substr( pos + 1, 1 ); // we could just append a space here, but this form lets us easily add other characters we might want to append like this
}
else // otherwise consume it and the escaped char
{
currentValueToken += productionString.substr( searchpos, (pos - searchpos) + 2 );
}
searchpos = pos + 2;
break;
case '|': // note pipe
pipe = !pipe;
// consume it
currentValueToken += productionString.substr( searchpos, (pos - searchpos) + 1 );
searchpos = pos + 1;
if(!pipe && inValues)
{
if ( currentValueToken.size() ) // I don't think this is necessary since it should always contain at least the pipes
{
// tokenize
currentValueCollection.push_back( currentValueToken );
currentValueToken.clear();
}
}
break;
case '[': // start of value list
if ( !pipe )
{
// make sure there is not a square bracket immediately following this one
if ( productionString.size() < pos + 2 )
return SetError( "gp production ends with [" );
if ( productionString[ pos + 1 ] == ']' )
return SetError( "gp can't have empty value collections" );
// we've started a values list, finish and save the previous segment
currentValueToken += productionString.substr( searchpos, pos - searchpos );
currentValueCollection.push_back( currentValueToken );
topLevel.push_back( currentValueCollection );
inValues = true;
currentValueCollection.clear();
currentValueToken.clear();
}
else
{
// in a pipe, append it
currentValueToken += productionString.substr( searchpos, (pos - searchpos) + 1 );
}
// consume it
searchpos = pos + 1;
break;
case ']': // end of values list
if ( !pipe )
{
// make sure there is not a square bracket immediately following this one
if ( productionString.size() < pos + 2 )
return SetError( "gp production ends with ]" );
if ( productionString[ pos + 1 ] == '[' )
return SetError( "gp production requires space between value lists" );
// end of values list
currentValueToken += productionString.substr( searchpos, pos - searchpos );
if ( currentValueToken.size() )
{
// tokenize
currentValueCollection.push_back( currentValueToken );
currentValueToken.clear();
}
if ( currentValueCollection.size() )
{
if (total == 0) {
total += currentValueCollection.size();
} else {
total *= currentValueCollection.size();
}
//std::cout << "total: " << total << std::endl;
topLevel.push_back( currentValueCollection );
}
inValues = false;
currentValueCollection.clear();
}
else
{
// in a pipe, append it
currentValueToken += productionString.substr( searchpos, (pos - searchpos) + 1 );
}
// consume it
searchpos = pos + 1;
break;
case ' ': // whitespace
case '\n':
case '\r':
case '\t':
if ( inValues )
{
if ( pipe )
{
currentValueToken += productionString.substr( searchpos, pos - searchpos + 1 );
}
else
{
currentValueToken += productionString.substr( searchpos, pos - searchpos );
if ( currentValueToken.size() )
{
// tokenize
currentValueCollection.push_back( currentValueToken );
currentValueToken.clear();
}
}
}
else
{
// not in values list, append it
currentValueToken += productionString.substr( searchpos, (pos - searchpos) + 1 );
}
// consume it
searchpos = pos + 1;
break;
case '#':
if (!pipe)
{
// if not in a pipe string, ignore to end of line.
searchpos = productionString.find_first_of("\n", pos + 1);
}
else
{
// in a pipe, append it
currentValueToken += productionString.substr( searchpos, (pos - searchpos) + 1 );
}
break;
default:
assert( false );
break;
}
}
// grab end of production
currentValueToken += productionString.substr( searchpos, std::string::npos );
if ( currentValueToken.size() )
{
// tokenize
currentValueCollection.push_back( currentValueToken );
currentValueToken.clear();
}
topLevel.push_back( currentValueCollection );
if (pipe || inValues) {
std::ostringstream message;
message << "gp syntax error, unmatched ";
if (pipe) {
message << "pipe";
}
if (pipe && inValues) {
message << ", ";
}
if (inValues) {
message << "values bracket";
}
message << ".";
return SetError( message.str() );;
}
if (m_GPMax != 0) {
if (total > m_GPMax) {
std::ostringstream message;
message << "gp-max exceeded, current production produces " << total << " productions.";
return SetError( message.str() );
}
}
// final output
//std::cout << "****DoGP collections:" << std::endl;
//for ( std::list< valueCollection >::iterator topIter = topLevel.begin(), endIter = topLevel.end(); topIter != endIter; ++topIter )
//{
// std::cout << "~~~" << std::endl;
// for ( valueCollection::iterator valueIter = topIter->begin(); valueIter != topIter->end(); ++valueIter )
// {
// std::cout << "%" << *valueIter << "% ";
// }
// std::cout << std::endl;
//}
// create a collection of iterators
// two for each value collection in the topLevel collection
// one that points to the current value, and one that represents the end of the value collection
std::list< iterTriple > valueIters;
for ( std::list< valueCollection >::iterator topIter = topLevel.begin(), endIter = topLevel.end(); topIter != endIter; ++topIter )
{
iterTriple it;
it.begin = topIter->begin(); it.current = topIter->begin(); it.end = topIter->end();
valueIters.push_back( it );
}
bool finished = false;
uint64_t prodnum = 1;
while(!finished)
{
// generate production corresponding to current values
std::string generatedProduction = "";
for ( std::list< iterTriple >::iterator valueItersIter = valueIters.begin(), endIter = valueIters.end(); valueItersIter != endIter; ++valueItersIter )
{
generatedProduction += *(valueItersIter->current);
// if this is the first part of the production,
// find the end of the production name and add a number to differentiate it
if(valueItersIter == valueIters.begin())
{
pos = generatedProduction.find_first_of("\n \t\r");
std::stringstream numsstr; numsstr << "*" << prodnum;
generatedProduction.insert(pos, numsstr.str());
++prodnum;
}
}
//std::cout << std::endl << "++++++" << std::endl << generatedProduction << std::endl << "++++++" << std::endl;
if(!DoSP(generatedProduction))
{
return false;
}
// update value iterators
finished = true;
for ( std::list< iterTriple >::iterator valueItersIter = valueIters.begin(), endIter = valueIters.end(); valueItersIter != endIter; ++valueItersIter )
{
// increment the value iterator
++(valueItersIter->current);
// If it is at the end, set it back to the beginning.
// Otherwise we're at a new value, so break out and generate a new production.
if(valueItersIter->current == valueItersIter->end)
{
valueItersIter->current = valueItersIter->begin;
}
else
{
finished = false;
break;
}
}
}
return true;
}