-
Notifications
You must be signed in to change notification settings - Fork 107
/
rewriteutils.h
84 lines (65 loc) · 2.08 KB
/
rewriteutils.h
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
#ifndef ETHSERP_REWRITEUTILS
#define ETHSERP_REWRITEUTILS
#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include "util.h"
// Valid functions and their min and max argument counts
extern std::string validFunctions[][3];
extern std::map<std::string, bool> vfMap;
bool isValidFunctionName(std::string f);
// Invalid variable names
extern std::map<std::string, bool> reservedWords;
// Converts deep array access into ordered list of the arguments
// along the descent
std::vector<Node> listfyStorageAccess(Node node);
// Cool function for debug purposes (named cerrStringList to make
// all prints searchable via 'cerr')
void cerrStringList(std::vector<std::string> s, std::string suffix="");
// Is the given node something of the form
// self.cow
// self.horse[0]
// self.a[6][7][self.storage[3]].chicken[9]
bool isNodeStorageVariable(Node node);
// Applies rewrite rules adding without wrapper
Node rewriteChunk(Node inp);
// Match result storing object
struct matchResult {
bool success;
std::map<std::string, Node> map;
};
// Match node to pattern
matchResult match(Node p, Node n);
// Substitute node using pattern
Node subst(Node pattern,
std::map<std::string, Node> dict,
std::string varflag,
Metadata m);
Node withTransform(Node source);
class rewriteRule {
public:
rewriteRule(Node p, Node s) {
pattern = p;
substitution = s;
}
Node pattern;
Node substitution;
};
class rewriteRuleSet {
public:
rewriteRuleSet() {
ruleLists = std::map<std::string, std::vector<rewriteRule> >();
}
void addRule(rewriteRule r) {
if (!ruleLists.count(r.pattern.val))
ruleLists[r.pattern.val] = std::vector<rewriteRule>();
ruleLists[r.pattern.val].push_back(r);
}
std::map<std::string, std::vector<rewriteRule> > ruleLists;
};
// Flatten nested sequence into flat sequence
Node flattenSeq(Node inp);
// Insert metadata into an AST built without metadata
Node insertMetadata(Node inp, Metadata m);
#endif