Skip to content

Commit 2da6c24

Browse files
committed
feat(esm): esm library fixes
1 parent c53934e commit 2da6c24

File tree

7 files changed

+3543
-8264
lines changed

7 files changed

+3543
-8264
lines changed

imports.pegjs

Lines changed: 455 additions & 0 deletions
Large diffs are not rendered by default.

index.mjs

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

lib/solidity.pegjs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ WeeksToken = "weeks" !IdentifierPart
559559
WeiToken = "wei" !IdentifierPart
560560
WhileToken = "while" !IdentifierPart
561561
YearsToken = "years" !IdentifierPart
562+
UncheckedToken = "unchecked" !IdentifierPart
562563

563564
/* Skipped */
564565

@@ -1151,8 +1152,8 @@ Statements
11511152
}
11521153

11531154
Statement
1154-
=
1155-
Block
1155+
= Block
1156+
/ UncheckedBlock
11561157
/ VariableStatement
11571158
/ EmptyStatement
11581159
/ ExpressionStatement
@@ -1231,6 +1232,16 @@ Block
12311232
};
12321233
}
12331234

1235+
UncheckedBlock
1236+
= UncheckedToken __ "{" __ body:(StatementList __)? "}" {
1237+
return {
1238+
type: "BlockStatement",
1239+
body: optionalList(extractOptional(body, 0)),
1240+
start: location().start.offset,
1241+
end: location().end.offset
1242+
};
1243+
}
1244+
12341245
StatementList
12351246
= head:Statement tail:(__ Statement)* { return buildList(head, tail, 1); }
12361247

@@ -1717,7 +1728,8 @@ ModifierDeclaration
17171728
}
17181729

17191730
FunctionDeclaration
1720-
= FunctionToken __ fnname:FunctionName __ args:ModifierArgumentList? __ returns:ReturnsDeclarations __ body:FunctionBody
1731+
= FunctionToken __ fnname:FunctionName __ args:ModifierArgumentList? __
1732+
OverrideToken? __ returns:ReturnsDeclarations __ body:FunctionBody
17211733
{
17221734
return {
17231735
type: "FunctionDeclaration",
@@ -1731,7 +1743,7 @@ FunctionDeclaration
17311743
end: location().end.offset
17321744
};
17331745
}
1734-
/ FunctionToken __ fnname:FunctionName __ args:ModifierArgumentList? __ returns:ReturnsDeclarations __ EOS
1746+
/ FunctionToken __ fnname:FunctionName __ args:ModifierArgumentList?__ OverrideToken? __ returns:ReturnsDeclarations __ EOS
17351747
{
17361748
return {
17371749
type: "FunctionDeclaration",
@@ -2130,4 +2142,4 @@ AssemblyFor
21302142
start: location().start.offset,
21312143
end: location().end.offset
21322144
};
2133-
}
2145+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "pegjs-solidity",
33
"version": "1.2.0",
4+
"type": "module",
45
"description": "pegjs grammar for solidity parsing with esm/commonjs and browser umd support",
56
"main": "index.js",
67
"files": [

0 commit comments

Comments
 (0)