-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: sam bacha <[email protected]>
- Loading branch information
Showing
11 changed files
with
3,660 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ codecov/ | |
*.xhtml | ||
peggy/ | ||
*.peg | ||
.nyc* | ||
lib/solidity-pegis.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
'use strict'; | ||
|
||
/* eslint no-console: 0 */ | ||
let argv = require("yargs").argv; | ||
let SolidityParser = require("./index.js"); | ||
|
||
let argv = require('yargs').argv; | ||
let SolidityParser = require('./index.js'); | ||
|
||
let result; | ||
|
||
try { | ||
|
||
if (argv.e) { | ||
result = SolidityParser.parse(argv.e || argv.expression); | ||
} else { | ||
result = SolidityParser.parseFile(argv.f || argv.file || argv._[0]); | ||
} | ||
console.log(JSON.stringify(result, null, 2)); | ||
|
||
if (argv.e) { | ||
result = SolidityParser.parse(argv.e || argv.expression); | ||
} else { | ||
result = SolidityParser.parseFile(argv.f || argv.file || argv._[0]); | ||
} | ||
console.log(JSON.stringify(result, null, 2)); | ||
} catch (e) { | ||
console.error(e.message); | ||
process.exit(1); | ||
} | ||
console.error(e.message); | ||
process.exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ codecov: | |
coverage: | ||
precision: 2 | ||
round: down | ||
range: "70...100" | ||
range: '70...100' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,87 @@ | ||
"use strict"; | ||
'use strict'; | ||
|
||
const PEG = require("peggy"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const PEG = require('peggy'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const builtParsers = { | ||
"solidity": require("./build/parser"), | ||
"imports": require("./build/imports_parser") | ||
solidity: require('./build/parser'), | ||
imports: require('./build/imports_parser'), | ||
}; | ||
|
||
|
||
function parseComments(sourceCode) { | ||
// for Line comment regexp, the "." doesn't cover line termination chars so we're good :) | ||
const comments = [], commentParser = /(\/\*(\*(?!\/)|[^*])*\*\/)|(\/\/.*)/g; | ||
let nextComment; | ||
// for Line comment regexp, the "." doesn't cover line termination chars so we're good :) | ||
const comments = [], | ||
commentParser = /(\/\*(\*(?!\/)|[^*])*\*\/)|(\/\/.*)/g; | ||
let nextComment; | ||
|
||
// eslint-disable-next-line no-cond-assign | ||
while (nextComment = commentParser.exec(sourceCode)) { | ||
const text = nextComment[0], types = { "//": "Line", "/*": "Block" }; | ||
// eslint-disable-next-line no-cond-assign | ||
while ((nextComment = commentParser.exec(sourceCode))) { | ||
const text = nextComment[0], | ||
types = { '//': 'Line', '/*': 'Block' }; | ||
|
||
comments.push({ | ||
text, | ||
type: types[text.slice(0, 2)], | ||
start: nextComment.index, | ||
end: nextComment.index + text.length | ||
}); | ||
} | ||
comments.push({ | ||
text, | ||
type: types[text.slice(0, 2)], | ||
start: nextComment.index, | ||
end: nextComment.index + text.length, | ||
}); | ||
} | ||
|
||
return comments; | ||
return comments; | ||
} | ||
|
||
|
||
// TODO: Make all this async. | ||
module.exports = { | ||
getParser: function(parser_name, rebuild) { | ||
if (rebuild == true) { | ||
let parserfile = fs.readFileSync(path.resolve("./" + parser_name + ".pegjs"), {encoding: "utf8"}); | ||
return PEG.generate(parserfile); | ||
} else { | ||
return builtParsers[parser_name]; | ||
} | ||
}, | ||
parse: function(source, options, parser_name, rebuild) { | ||
if (typeof parser_name == "boolean") { | ||
rebuild = parser_name; | ||
parser_name = null; | ||
} | ||
getParser: function (parser_name, rebuild) { | ||
if (rebuild == true) { | ||
let parserfile = fs.readFileSync( | ||
path.resolve('./' + parser_name + '.pegjs'), | ||
{ encoding: 'utf8' }, | ||
); | ||
return PEG.generate(parserfile); | ||
} else { | ||
return builtParsers[parser_name]; | ||
} | ||
}, | ||
parse: function (source, options, parser_name, rebuild) { | ||
if (typeof parser_name == 'boolean') { | ||
rebuild = parser_name; | ||
parser_name = null; | ||
} | ||
|
||
if (parser_name == null) { | ||
parser_name = "solidity"; | ||
} | ||
if (parser_name == null) { | ||
parser_name = 'solidity'; | ||
} | ||
|
||
let parser = this.getParser(parser_name, rebuild); | ||
let result; | ||
let parser = this.getParser(parser_name, rebuild); | ||
let result; | ||
|
||
try { | ||
result = parser.parse(source); | ||
} catch (e) { | ||
if (e instanceof parser.SyntaxError) { | ||
e.message += " Line: " + e.location.start.line + ", Column: " + e.location.start.column; | ||
} | ||
throw e; | ||
} | ||
try { | ||
result = parser.parse(source); | ||
} catch (e) { | ||
if (e instanceof parser.SyntaxError) { | ||
e.message += | ||
' Line: ' + | ||
e.location.start.line + | ||
', Column: ' + | ||
e.location.start.column; | ||
} | ||
throw e; | ||
} | ||
|
||
if (typeof options === "object" && options.comment === true) { | ||
result.comments = parseComments(source); | ||
} | ||
if (typeof options === 'object' && options.comment === true) { | ||
result.comments = parseComments(source); | ||
} | ||
|
||
return result; | ||
}, | ||
parseFile: function(file, parser_name, rebuild) { | ||
return this.parse(fs.readFileSync(path.resolve(file), {encoding: "utf8"}), parser_name, rebuild); | ||
}, | ||
parseComments | ||
return result; | ||
}, | ||
parseFile: function (file, parser_name, rebuild) { | ||
return this.parse( | ||
fs.readFileSync(path.resolve(file), { encoding: 'utf8' }), | ||
parser_name, | ||
rebuild, | ||
); | ||
}, | ||
parseComments, | ||
}; |
Oops, something went wrong.