-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathparser.cpp
More file actions
558 lines (398 loc) · 16.5 KB
/
Copy pathparser.cpp
File metadata and controls
558 lines (398 loc) · 16.5 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/** @file
* Internal functions which parse strings given by users.
*
* Performance here is not critical, so we opt to use
* sub-optimal but clear, defensively-designed functions.
* We also use quite stringent internal error checking
* due to the many risks and uncertainties caused by
* parsing user-given strings with regex.
*
* @author Tyson Jones
*/
#include "quest/include/config.h"
#include "quest/include/precision.h"
#include "quest/include/types.h"
#include "quest/include/paulis.h"
#include "quest/src/core/parser.hpp"
#include "quest/src/core/errors.hpp"
#include "quest/src/core/validation.hpp"
#include <regex>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <stdexcept>
#include <algorithm>
using std::regex;
using std::vector;
using std::string;
using std::smatch;
using std::ifstream;
using std::out_of_range;
using std::stringstream;
using std::sregex_iterator;
using std::invalid_argument;
/*
* REGULAR EXPRESSIONS
*
* which capture our supported PauliStrSum string syntax
*/
namespace patterns {
// utilities
string group(string in) { return "(?:" + in + ")"; } // groups sub-patterns to control eval order
string capt (string in) { return "(" + in + ")"; } // captures sub-patterns for later extraction
string opt (string in) { return group(in) + "?"; } // groups and makes optional a sub-pattern
// sub-numbers
string mantissa = "[0-9]+" + opt("[.][0-9]*") + "|[.][0-9]+"; // e.g. 0 0. .0 0.0
string exponent = "[eE][+-]?[0-9]+"; // e.g. e5 e-5 E5 E-5 e-0
string imagsymb = "[ijIJ]";
// unsigned
string ureal = group(mantissa) + opt(exponent); // e.g. 4 5E0 .1E-10
// constants we may wish to generalise
string space = "[ \\t]"; // single horizontal whitespace char
string sign = "[+-]";
// optional
string optSpace = space + "*";
string optSign = optSpace + opt(sign) + optSpace; // optional +- with any spacing
// component delimiter
string delim = optSpace + sign + optSpace; // mandatory +- with any spacing
// full complex; only one component given, which is captured with sign (excluding imag symb)
string real = capt(optSign + ureal) + optSpace;
string imag = capt(optSign + ureal) + imagsymb + optSpace;
// full complex; both components given, real always before imag, individually captured with signs (but without imag symb)
string comp = capt(optSign + ureal) + capt(delim + ureal) + imagsymb + optSpace;
// full complex; any format, importantly in order of decreasing specificity. do not consult for captured groups
string num = group(comp) + "|" + group(imag) + "|" + group(real);
// no capturing because 'num' pollutes captured groups, and pauli syntax overlaps real integers
string pauli = "[" + parser_RECOGNISED_PAULI_CHARS + "]";
string paulis = group(optSpace + pauli + optSpace) + "+";
string weightedPaulis = "^" + group(num) + space + optSpace + paulis + "$";
}
namespace regexes {
// instantiation here negates risk of later runtime error due to invalid regex
regex real(patterns::real);
regex imag(patterns::imag);
regex comp(patterns::comp);
regex num(patterns::num);
regex paulis(patterns::paulis);
regex weightedPaulis(patterns::weightedPaulis);
}
/*
* HANDLING WHITESPACE
*
* in a verbose, centralised way to ensure we never tolerate space characters
* in our regex which we do not later permit in our parsing. As such,
* isWhiteSpace() must return 'true' for every character in patterns:space
*/
bool isWhiteSpace(char ch) {
// matches patterns:space and more (additionally; newlines)
return isspace(ch);
}
bool isNotWhiteSpace(char ch) {
return !isWhiteSpace(ch);
}
bool isOnlyWhiteSpace(string str) {
return all_of(str.begin(), str.end(), isWhiteSpace);
}
void removeWhiteSpace(string &line) {
// modifies line var, including removing newlines. we don't need to do
// this for pattern matching (our regex patterns include all permitted,
// frivolous whitespace), but we do need to perform it before passing
// matched strings to functions like std::stold()
line.erase(remove_if(line.begin(), line.end(), isWhiteSpace), line.end());
}
/*
* STRING PARTITIONING
*/
void separateStringIntoCoeffAndPaulis(string line, string &coeff, string &paulis) {
// absolutely gauranteed to match due to prior matching of regexes::line
smatch match;
// locate the coefficient
regex_search(line, match, regexes::num);
coeff = match.str(0);
// the remainder of the line must be the paulis, but we explicitly match to
// regex just in case we later permit additional substrings like comments.
// we must match only the substring AFTER the coeff, since valid coeffs (e.g.
// 1) can be mistaken for a Pauli code.
line = line.substr(match.position(0) + match.length(0));
regex_search(line, match, regexes::paulis);
paulis = match.str(0);
}
int getNumPaulisInLine(string line) {
// simply count the non-whitespace chars in the paulis substring
string coeff, paulis;
separateStringIntoCoeffAndPaulis(line, coeff, paulis);
return count_if(paulis.begin(), paulis.end(), isNotWhiteSpace);
}
/*
* REAL NUMBER PARSING
*/
qreal precisionAgnosticStringToFloat(string str) {
// remove whitespace which stold() et al cannot handle after the sign.
// beware this means that e.g. "1 0" (invalid number) would become "10"
// (valid) so this function cannot be used for duck-typing, though that
// is anyway the case since stold() et al permit "10abc"
removeWhiteSpace(str);
// below throws exception when the (prefix) of str cannot be/fit into a qreal
if (FLOAT_PRECISION == 1) return static_cast<qreal>(std::stof (str));
if (FLOAT_PRECISION == 2) return static_cast<qreal>(std::stod (str));
if (FLOAT_PRECISION == 4) return static_cast<qreal>(std::stold(str));
// unreachable
return -1;
}
bool parser_isAnySizedReal(string str) {
// we assume that all strings which match the regex can be parsed by
// precisionAgnosticStringToFloat() above (once whitespace is removed)
// EXCEPT strings which contain a number too large to store in the qreal
// type (as is separately checked below). Note it is insufficient to merely
// duck-type using stold() et al because such functions permit non-numerical
// characters to follow the contained number which are silently removed (grr!)
smatch match;
return regex_match(str, match, regexes::real);
}
bool parser_isValidReal(string str) {
// reject str if it doesn't match regex
if (!parser_isAnySizedReal(str))
return false;
// check number is in-range of qreal via duck-typing
try {
precisionAgnosticStringToFloat(str);
} catch (const out_of_range&) {
return false;
// error if our regex permitted an unparsable string
} catch (const invalid_argument&) {
error_attemptedToParseRealFromInvalidString();
}
return true;
}
qreal parser_parseReal(string str) {
try {
return precisionAgnosticStringToFloat(str);
} catch (const invalid_argument&) {
error_attemptedToParseRealFromInvalidString();
} catch (const out_of_range&) {
error_attemptedToParseOutOfRangeReal();
}
// unreachable
return -1;
}
/*
* COMPLEX NUMBER PARSING
*/
bool parser_isAnySizedComplex(string str) {
// we assume that all strings which match the regex can be parsed to
// a qcomp (once whitespace is removed) EXCEPT strings which contain a
// number too large to store in the qcomp type (as is separately checked
// below). Note it is insufficient to merely duck-type each component using
// using stold() et al because such functions permit non-numerical chars to
// follow the contained number (grr!)
smatch match;
// must match real, imaginary or complex number regex
if (regex_match(str, match, regexes::real)) return true;
if (regex_match(str, match, regexes::imag)) return true;
if (regex_match(str, match, regexes::comp)) return true;
return false;
}
bool parser_isValidComplex(string str) {
// reject str if it doesn't match complex regex
if (!parser_isAnySizedComplex(str))
return false;
// we've so far gauranteed str has a valid form, but we must now check
// each included complex component (which we enumerate) is in range of a qreal
sregex_iterator it(str.begin(), str.end(), regexes::real);
sregex_iterator end;
// valid coeffs contain 1 or 2 reals, never 0, which regex should have caught
if (it == end)
error_attemptedToParseComplexFromInvalidString();
// for each of the 1 or 2 components...
for (; it != end; it++) {
// check component is in-range of qreal via duck-typing
try {
precisionAgnosticStringToFloat(it->str(0));
} catch (const out_of_range&) {
return false;
// error if our regex permitted an unparsable component
} catch (const invalid_argument&) {
error_attemptedToParseComplexFromInvalidString();
}
}
// report that each/all detected components of str can form a valid qcomp
return true;
}
qcomp parser_parseComplex(string str) {
if (!parser_isValidComplex(str))
error_attemptedToParseComplexFromInvalidString();
// we are gauranteed to fully match real, imag or comp after prior validation
smatch match;
// extract and parse components and their signs (excluding imaginary symbol)
if (regex_match(str, match, regexes::real))
return qcomp(parser_parseReal(match.str(1)), 0);
if (regex_match(str, match, regexes::imag))
return qcomp(0, parser_parseReal(match.str(1)));
if (regex_match(str, match, regexes::comp))
return qcomp(
parser_parseReal(match.str(1)),
parser_parseReal(match.str(2)));
// should be unreachable
error_attemptedToParseComplexFromInvalidString();
return qcomp(0,0);
}
/*
* VALIDATION
*
* which checks user-given strings are correctly formatted, which are
* defined here (in lieu of inside validation.cpp) because they make
* extensive use of regex and parsing.
*/
bool isInterpretablePauliStrSumLine(string line) {
// checks whether line has the expected format; a real, imaginary or
// complex number (expressed as an integer, decimal, or in scientific
// notation) followed by 1 or more space characters, then one or
// more pauli codes/chars. It does NOT determine whether the coeff
// can actually be instantiated as a qcomp
return regex_match(line, regexes::weightedPaulis);
}
bool isPauliStrSumCoeffWithinQcompRange(string line) {
// it is gauranteed that line is interpretable and contains a regex-matching
// coefficient, but we must additionally verify it is within range of qreal.
// So we duck type each of the 1 or 2 matches with the real regex (i.e. one or
// both of the real and imaginary components of a complex coeff).
// process only the coeff, since pauli codes may resemble coeffs (e.g. 1)
string coeff, _;
separateStringIntoCoeffAndPaulis(line, coeff, _); // discard pauli substr
// beautiful iterator boilerplate
sregex_iterator it(coeff.begin(), coeff.end(), regexes::real);
sregex_iterator end;
// valid coeffs contain 1 or 2 reals, never 0
if (it == end)
return false;
// enumerate all matches of 'real' regex in line
for (; it != end; it++) {
// remove whitespace (stold cannot handle space between sign and number)
string match = it->str(0);
removeWhiteSpace(match);
// return false if number cannot become a qreal
try {
precisionAgnosticStringToFloat(match);
} catch (const out_of_range&) {
return false;
} catch (const invalid_argument&) { // should be impossible (indicates bad regex)
return false;
}
}
// report that each double in coeff substr was parsable
return true;
}
void assertStringIsValidPauliStrSum(string lines, const char* caller) {
int numPaulis = 0;
bool nonEmptyLine = false;
qindex lineIndex = 0;
// parse each line in-turn
stringstream stream(lines);
for (string line; getline(stream, line); lineIndex++) {
// permit and skip empty lines
if (isOnlyWhiteSpace(line))
continue;
else
nonEmptyLine = true;
// assert the line is interpretable
bool validLine = isInterpretablePauliStrSumLine(line);
validate_parsedPauliStrSumLineIsInterpretable(validLine, line, lineIndex, caller);
// assert the coeff is parsable (e.g. doesn't exceed valid number range)
bool validCoeff = isPauliStrSumCoeffWithinQcompRange(line);
validate_parsedPauliStrSumCoeffWithinQcompRange(validCoeff, line, lineIndex, caller);
// assert the line has a consistent number of Paulis as previous
int numLinePaulis = getNumPaulisInLine(line);
if (!numPaulis)
numPaulis = numLinePaulis;
validate_parsedPauliStrSumLineHasConsistentNumPaulis(numPaulis, numLinePaulis, line, lineIndex, caller);
}
// ensure we parsed at least 1 Pauli string
validate_parsedStringIsNotEmpty(nonEmptyLine, caller);
}
/*
* PAULI STRING PARSING
*/
int parser_getPauliIntFromChar(char ch) {
// must cover every char in parser_RECOGNISED_PAULI_CHARS
switch (ch) {
case '0': case 'i': case 'I': return 0;
case '1': case 'x': case 'X': return 1;
case '2': case 'y': case 'Y': return 2;
case '3': case 'z': case 'Z': return 3;
}
// should be unreachable after validation
error_attemptedToParseUnrecognisedPauliChar();
return -1;
}
/*
* PAULI STRING SUM PARSING
*/
PauliStr parsePaulis(string paulis, bool rightIsLeastSignificant) {
// remove whitespace to make string compatible with getPauliStr()
removeWhiteSpace(paulis);
// default creator treats rightmost pauli as least significant
if (rightIsLeastSignificant)
return getPauliStr(paulis);
// otherwise pass qubits {0, 1, ... }
vector<int> qubits(paulis.size());
for (size_t i=0; i<paulis.size(); i++)
qubits[i] = i;
return getPauliStr(paulis, qubits);
}
void parseWeightedPaulis(string line, qcomp &coeff, PauliStr &pauli, bool rightIsLeastSignificant) {
// separate line into substrings
string coeffStr, pauliStr;
separateStringIntoCoeffAndPaulis(line, coeffStr, pauliStr);
// parse each, overwriting calller primitives
coeff = parser_parseComplex(coeffStr);
pauli = parsePaulis(pauliStr, rightIsLeastSignificant);
}
qindex getNumLines(string lines) {
/// @todo is this platform agnostic?
char newline = '\n';
return 1 + count(lines.begin(), lines.end(), newline);
}
PauliStrSum parser_validateAndParsePauliStrSum(string lines, bool rightIsLeastSignificant, const char* caller) {
assertStringIsValidPauliStrSum(lines, caller);
// allocate space for as many strings as there are lines, though we might collect fewer (we skip empty lines)
qindex numLines = getNumLines(lines);
vector<qcomp> coeffs; coeffs.reserve(numLines);
vector<PauliStr> strings; strings.reserve(numLines);
// parse each line in-turn
stringstream stream(lines);
for (string line; getline(stream, line); ) {
// skip empty lines
if (isOnlyWhiteSpace(line))
continue;
qcomp coeff;
PauliStr string;
parseWeightedPaulis(line, coeff, string, rightIsLeastSignificant); // validates
coeffs.push_back(coeff);
strings.push_back(string);
}
// invoke other API function, which will run additional validation (e.g. checking
// memory allocations succeeded) and may fail, reporting that 'createPauliStrSum()'
// failed, rather than caller. That's a minor, acceptable evil. Furthermore, this
// call creates new memory alongside our existing vectors, meaning total memory
// is temporarily double than that which is strictly necessary; also acceptable.
PauliStrSum out = createPauliStrSum(strings, coeffs);
return out;
}
/*
* FILE IO
*/
bool parser_canReadFile(string fn) {
ifstream file(fn);
return file.good();
}
string parser_loadFile(string fn) {
// ensure file is still readable since validation
ifstream file(fn);
if (!file.good())
error_couldNotReadFile();
// load entire file into string
stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}