forked from trufflesuite/truffle-compile
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
176 lines (144 loc) · 5.82 KB
/
parser.js
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
var CompileError = require("./compileerror");
var solc = require("solc");
var fs = require("fs");
var path = require("path");
// Clean up after solc.
var listeners = process.listeners("uncaughtException");
var solc_listener = listeners[listeners.length - 1];
if (solc_listener) {
process.removeListener("uncaughtException", solc_listener);
}
// Warning issued by a pre-release compiler version, ignored by this component.
var preReleaseCompilerWarning = "This is a pre-release compiler version, please do not use it in production.";
var installedContractsDir = "installed_contracts"
module.exports = {
parse: function(body, fileName) {
// Here, we want a valid AST even if imports don't exist. The way to
// get around that is to tell the compiler, as they happen, that we
// have source for them (an empty file).
var build_remappings = function() {
// Maps import paths to paths from EthPM installed contracts, so we can correctly solve imports
// e.g. "my_pkg/=installed_contracts/my_pkg/contracts/"
var remappings = [];
if (fs.existsSync('ethpm.json')) {
ethpm = JSON.parse(fs.readFileSync('ethpm.json'));
for (pkg in ethpm.dependencies) {
remappings.push(pkg + "/=" + path.join(installedContractsDir, pkg, 'contracts', '/'));
}
}
return remappings;
}
var fileName = fileName || "ParsedContract.sol";
var remappings = build_remappings();
var solcStandardInput = {
language: "Solidity",
sources: {
[fileName]: {
content: body
}
},
settings: {
remappings: remappings,
outputSelection: {
"*": {
"": [
"ast"
]
}
}
}
};
var output = solc.compileStandard(JSON.stringify(solcStandardInput), function(file_path) {
// Resolve dependency manually.
if (fs.existsSync(file_path)) {
contents = fs.readFileSync(file_path, {encoding: 'UTF-8'});
}
else {
contents = "pragma solidity ^0.4.0;";
}
return {contents: contents};
});
output = JSON.parse(output);
// Filter out the "pre-release compiler" warning, if present.
var errors = output.errors ? output.errors.filter(function(solidity_error) {
return solidity_error.message.indexOf(preReleaseCompilerWarning) < 0;
}) : [];
// Filter out warnings.
var warnings = output.errors ? output.errors.filter(function(solidity_error) {
return solidity_error.severity == "warning";
}) : [];
var errors = output.errors ? output.errors.filter(function(solidity_error) {
return solidity_error.severity != "warning";
}) : [];
if (errors.length > 0) {
throw new CompileError(errors[0].formattedMessage);
}
return {
contracts: Object.keys(output.contracts[fileName]),
ast: output.sources[fileName].ast
};
},
// This needs to be fast! It is fast (as of this writing). Keep it fast!
parseImports: function(body) {
var self = this;
// WARNING: Kind of a hack (an expedient one).
// So we don't have to maintain a separate parser, we'll get all the imports
// in a file by sending the file to solc and evaluating the error messages
// to see what import statements couldn't be resolved. To prevent full-on
// compilation when a file has no import statements, we inject an import
// statement right on the end; just to ensure it will error and we can parse
// the imports speedily without doing extra work.
// Helper to detect import errors with an easy regex.
var importErrorKey = "TRUFFLE_IMPORT";
// Inject failing import.
var failingImportFileName = "__Truffle__NotFound.sol";
body = body + "\n\nimport '" + failingImportFileName + "';\n";
var solcStandardInput = {
language: "Solidity",
sources: {
"ParsedContract.sol": {
content: body
}
},
settings: {
outputSelection: {
"ParsedContract.sol": {
"*": [] // We don't need any output.
}
}
}
};
var output = solc.compileStandard(JSON.stringify(solcStandardInput), function() {
// The existence of this function ensures we get a parsable error message.
// Without this, we'll get an error message we *can* detect, but the key will make it easier.
// Note: This is not a normal callback. See docs here: https://github.com/ethereum/solc-js#from-version-021
return {error: importErrorKey};
});
output = JSON.parse(output);
// Filter out the "pre-release compiler" warning, if present.
var errors = output.errors.filter(function(solidity_error) {
return solidity_error.message.indexOf(preReleaseCompilerWarning) < 0;
});
var nonImportErrors = errors.filter(function(solidity_error) {
// If the import error key is not found, we must not have an import error.
// This means we have a *different* parsing error which we should show to the user.
// Note: solc can return multiple parsing errors at once.
// We ignore the "pre-release compiler" warning message.
return solidity_error.formattedMessage.indexOf(importErrorKey) < 0;
});
// Should we try to throw more than one? (aside; we didn't before)
if (nonImportErrors.length > 0) {
throw new CompileError(nonImportErrors[0].formattedMessage);
}
// Now, all errors must be import errors.
// Filter out our forced import, then get the import paths of the rest.
var imports = errors.filter(function(solidity_error) {
return solidity_error.message.indexOf(failingImportFileName) < 0;
}).map(function(solidity_error) {
var matches = solidity_error.formattedMessage.match(/import[^'"]+("|')([^'"]+)("|');/);
// Return the item between the quotes.
return matches[2];
});
return imports;
}
}