forked from asavine/Scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptingParser.cpp
More file actions
67 lines (51 loc) · 1.55 KB
/
scriptingParser.cpp
File metadata and controls
67 lines (51 loc) · 1.55 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
/*
Written by Antoine Savine in 2018
This code is the strict IP of Antoine Savine
License to use and alter this code for personal and commercial applications
is freely granted to any person or company who purchased a copy of the book
Modern Computational Finance: Scripting for Derivatives and XVA
Jesper Andreasen & Antoine Savine
Wiley, 2018
As long as this comment is preserved at the top of the file
*/
#include "scriptingParser.h"
#include <regex>
#include <algorithm>
vector<string> tokenize( const string& str)
{
// Regex matching tokens of interest
static const regex r( "[\\w.]+|[/-]|,|;|:|[\\(\\)\\+\\*\\^]|!=|>=|<=|[<>=]");
// Result, with max possible size reserved
vector<string> v;
v.reserve( str.size());
// Loop over matches
for( sregex_iterator it( str.begin(), str.end(), r), end; it != end; ++it)
{
// Copy match into results
v.push_back( (*it)[0]);
// Uppercase
transform( v.back().begin(), v.back().end(), v.back().begin(), toupper);
}
// C++11 move semantics means no copy
return v;
}
// Event = vector<Statement>
Event parse( const string& eventString)
{
Event e;
auto tokens = tokenize( eventString);
auto it = tokens.begin();
while( it != tokens.end())
{
e.push_back( Parser<decltype(it)>::parseStatement( it, tokens.end()));
}
// C++11 --> vectors are moved, not copied
return e;
}
// Single expression
Expression parseExpression(const string& exprString)
{
auto tokens = tokenize(exprString);
auto it = tokens.begin();
return Parser<decltype(tokens.begin())>::parseStatement(it, tokens.end());
}