|
1 | | -"use strict"; |
| 1 | +'use strict'; |
2 | 2 |
|
3 | | -const PEG = require("peggy"); |
4 | | -const fs = require("fs"); |
5 | | -const path = require("path"); |
| 3 | +const PEG = require('peggy'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
6 | 6 |
|
7 | 7 | const builtParsers = { |
8 | | - "solidity": require("./build/parser"), |
9 | | - "imports": require("./build/imports_parser") |
| 8 | + solidity: require('./build/parser'), |
| 9 | + imports: require('./build/imports_parser'), |
10 | 10 | }; |
11 | 11 |
|
12 | | - |
13 | 12 | function parseComments(sourceCode) { |
14 | | - // for Line comment regexp, the "." doesn't cover line termination chars so we're good :) |
15 | | - const comments = [], commentParser = /(\/\*(\*(?!\/)|[^*])*\*\/)|(\/\/.*)/g; |
16 | | - let nextComment; |
| 13 | + // for Line comment regexp, the "." doesn't cover line termination chars so we're good :) |
| 14 | + const comments = [], |
| 15 | + commentParser = /(\/\*(\*(?!\/)|[^*])*\*\/)|(\/\/.*)/g; |
| 16 | + let nextComment; |
17 | 17 |
|
18 | | - // eslint-disable-next-line no-cond-assign |
19 | | - while (nextComment = commentParser.exec(sourceCode)) { |
20 | | - const text = nextComment[0], types = { "//": "Line", "/*": "Block" }; |
| 18 | + // eslint-disable-next-line no-cond-assign |
| 19 | + while ((nextComment = commentParser.exec(sourceCode))) { |
| 20 | + const text = nextComment[0], |
| 21 | + types = { '//': 'Line', '/*': 'Block' }; |
21 | 22 |
|
22 | | - comments.push({ |
23 | | - text, |
24 | | - type: types[text.slice(0, 2)], |
25 | | - start: nextComment.index, |
26 | | - end: nextComment.index + text.length |
27 | | - }); |
28 | | - } |
| 23 | + comments.push({ |
| 24 | + text, |
| 25 | + type: types[text.slice(0, 2)], |
| 26 | + start: nextComment.index, |
| 27 | + end: nextComment.index + text.length, |
| 28 | + }); |
| 29 | + } |
29 | 30 |
|
30 | | - return comments; |
| 31 | + return comments; |
31 | 32 | } |
32 | 33 |
|
33 | | - |
34 | 34 | // TODO: Make all this async. |
35 | 35 | module.exports = { |
36 | | - getParser: function(parser_name, rebuild) { |
37 | | - if (rebuild == true) { |
38 | | - let parserfile = fs.readFileSync(path.resolve("./" + parser_name + ".pegjs"), {encoding: "utf8"}); |
39 | | - return PEG.generate(parserfile); |
40 | | - } else { |
41 | | - return builtParsers[parser_name]; |
42 | | - } |
43 | | - }, |
44 | | - parse: function(source, options, parser_name, rebuild) { |
45 | | - if (typeof parser_name == "boolean") { |
46 | | - rebuild = parser_name; |
47 | | - parser_name = null; |
48 | | - } |
| 36 | + getParser: function (parser_name, rebuild) { |
| 37 | + if (rebuild == true) { |
| 38 | + let parserfile = fs.readFileSync( |
| 39 | + path.resolve('./' + parser_name + '.pegjs'), |
| 40 | + { encoding: 'utf8' }, |
| 41 | + ); |
| 42 | + return PEG.generate(parserfile); |
| 43 | + } else { |
| 44 | + return builtParsers[parser_name]; |
| 45 | + } |
| 46 | + }, |
| 47 | + parse: function (source, options, parser_name, rebuild) { |
| 48 | + if (typeof parser_name == 'boolean') { |
| 49 | + rebuild = parser_name; |
| 50 | + parser_name = null; |
| 51 | + } |
49 | 52 |
|
50 | | - if (parser_name == null) { |
51 | | - parser_name = "solidity"; |
52 | | - } |
| 53 | + if (parser_name == null) { |
| 54 | + parser_name = 'solidity'; |
| 55 | + } |
53 | 56 |
|
54 | | - let parser = this.getParser(parser_name, rebuild); |
55 | | - let result; |
| 57 | + let parser = this.getParser(parser_name, rebuild); |
| 58 | + let result; |
56 | 59 |
|
57 | | - try { |
58 | | - result = parser.parse(source); |
59 | | - } catch (e) { |
60 | | - if (e instanceof parser.SyntaxError) { |
61 | | - e.message += " Line: " + e.location.start.line + ", Column: " + e.location.start.column; |
62 | | - } |
63 | | - throw e; |
64 | | - } |
| 60 | + try { |
| 61 | + result = parser.parse(source); |
| 62 | + } catch (e) { |
| 63 | + if (e instanceof parser.SyntaxError) { |
| 64 | + e.message += |
| 65 | + ' Line: ' + |
| 66 | + e.location.start.line + |
| 67 | + ', Column: ' + |
| 68 | + e.location.start.column; |
| 69 | + } |
| 70 | + throw e; |
| 71 | + } |
65 | 72 |
|
66 | | - if (typeof options === "object" && options.comment === true) { |
67 | | - result.comments = parseComments(source); |
68 | | - } |
| 73 | + if (typeof options === 'object' && options.comment === true) { |
| 74 | + result.comments = parseComments(source); |
| 75 | + } |
69 | 76 |
|
70 | | - return result; |
71 | | - }, |
72 | | - parseFile: function(file, parser_name, rebuild) { |
73 | | - return this.parse(fs.readFileSync(path.resolve(file), {encoding: "utf8"}), parser_name, rebuild); |
74 | | - }, |
75 | | - parseComments |
| 77 | + return result; |
| 78 | + }, |
| 79 | + parseFile: function (file, parser_name, rebuild) { |
| 80 | + return this.parse( |
| 81 | + fs.readFileSync(path.resolve(file), { encoding: 'utf8' }), |
| 82 | + parser_name, |
| 83 | + rebuild, |
| 84 | + ); |
| 85 | + }, |
| 86 | + parseComments, |
76 | 87 | }; |
0 commit comments