-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAstHelper.cs
154 lines (135 loc) · 4.57 KB
/
AstHelper.cs
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
using Meadow.Contract;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Meadow.CoverageReport
{
static class AstHelper
{
/// <summary>
/// Walk the entry AST node tree to create flattened list of all AstNodes
/// </summary>
public static IEnumerable<AstNode> Walk(JToken node)
{
if (node is JObject jObj)
{
if (jObj.ContainsKey("src"))
{
yield return AstNode.Create(jObj);
}
}
foreach (var child in node)
{
foreach (var subResult in Walk(child))
{
yield return subResult;
}
}
}
/// <summary>
/// Create a list without nodes that are a subset of another node
/// </summary>
public static AstNode[] RemoveSubsets(AstNode[] nodes)
{
var newList = nodes.ToList();
for (var i = newList.Count - 1; i >= 0; i--)
{
var cur = newList[i];
for (var j = 0; j < newList.Count; j++)
{
var other = newList[j];
if (other != cur
&& other.SourceIndex == cur.SourceIndex
&& other.SourceRange.Contains(cur.SourceRange))
{
newList.RemoveAt(i);
break;
}
}
}
return newList.ToArray();
}
/// <summary>
/// Create a list without nodes that are a superset of another node
/// </summary>
public static AstNode[] RemoveSupersets(AstNode[] nodes)
{
var newList = nodes.ToList();
for (var i = newList.Count - 1; i >= 0; i--)
{
var cur = newList[i];
for (var j = 0; j < newList.Count; j++)
{
var other = newList[j];
if (other != cur
&& other.SourceIndex == cur.SourceIndex
&& cur.SourceRange.Contains(other.SourceRange))
{
newList.RemoveAt(i);
break;
}
}
}
return newList.ToArray();
}
/// <summary>
/// Returns a list of nodes that are of a type in the given type list.
/// </summary>
public static IEnumerable<AstNode> GetNodesOfTypes(
SolcSourceInfo[] sourcesList,
IEnumerable<AstNode> nodes,
AstNodeType[] nodeTypes)
{
foreach (var node in nodes)
{
if (nodeTypes.Contains(node.NodeType))
{
yield return node;
}
}
}
/// <summary>
/// Returns a list of nodes that are not of a type in the given type list.
/// </summary>
public static IEnumerable<AstNode> GetNodesNotOfTypes(
SolcSourceInfo[] sourcesList,
IEnumerable<AstNode> nodes,
AstNodeType[] nodeTypes)
{
foreach (var node in nodes)
{
if (!nodeTypes.Contains(node.NodeType))
{
yield return node;
}
}
}
/// <summary>
/// Group AST nodes by their source file index as the dictionary key.
/// </summary>
public static (Dictionary<int, AstNode[]> Indexed, AstNode[] FullNodeArray) IndexAstNodes(
SolcSourceInfo[] sourcesList)
{
var astDict = new Dictionary<int, List<AstNode>>();
foreach (var sourceItem in sourcesList)
{
var astEntry = sourceItem.AstJson;
var entryID = sourceItem.ID;
foreach (var node in Walk(astEntry))
{
if (!astDict.TryGetValue(node.SourceRange.SourceIndex, out var list))
{
list = new List<AstNode>();
astDict.Add(node.SourceRange.SourceIndex, list);
}
list.Add(node);
}
}
var index = astDict.ToDictionary(e => e.Key, e => e.Value.ToArray());
var entireArray = index.Values.SelectMany(e => e).ToArray();
return (index, entireArray);
}
}
}