Skip to content

Commit 30f051b

Browse files
committed
2 parents 7804916 + bb2463d commit 30f051b

File tree

5 files changed

+143
-10
lines changed

5 files changed

+143
-10
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
### 0.3.0 (unreleased)
1+
### 0.4.0 (unreleased)
2+
3+
* Build parser on package install
4+
* Parse array constructors correctly
5+
* Parse named arguments correctly
6+
* Parse assembly local bindings correctly
7+
* Parse missing values when destructuring tuples correctly
8+
* Add support for Solidity interfaces
9+
10+
### 0.3.0
211

312
* Improve parsing of visibility and storage specifiers
413
* Add separate `StateVariableDeclaration` node type

imports.pegjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ ContractToken = "contract" !IdentifierPart
127127
FromToken = "from" !IdentifierPart
128128
ForToken = "for" !IdentifierPart
129129
ImportToken = "import" !IdentifierPart
130+
InterfaceToken = "interface" !IdentifierPart
130131
IsToken = "is" !IdentifierPart
131132
LibraryToken = "library" !IdentifierPart
132133
MappingToken = "mapping" !IdentifierPart
@@ -210,6 +211,7 @@ Statement
210211
= PragmaStatement
211212
/ ImportStatement
212213
/ ContractStatement
214+
/ InterfaceStatement
213215
/ LibraryStatement
214216
/ UsingStatement
215217

@@ -274,6 +276,13 @@ ContractStatement
274276
return undefined
275277
}
276278

279+
InterfaceStatement
280+
= InterfaceToken __ id:Identifier __ BlockList
281+
{
282+
return undefined
283+
}
284+
285+
277286
LibraryStatement
278287
= LibraryToken __ id:Identifier __ is:IsStatement? __ BlockList
279288
{

package.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,26 @@
1212
"bin": {
1313
"solidity-parser": "./cli.js"
1414
},
15-
"author": "Tim Coulter <[email protected]> (http://timothyjcoulter.com), Raghav Dua <[email protected]> (http://raghavdua.com)",
15+
"contributors": [
16+
{
17+
"name": "Tim Coulter",
18+
"email": "[email protected]",
19+
"url": "http://timothyjcoulter.com"
20+
},
21+
{
22+
"name": "Raghav Dua",
23+
"email": "[email protected]",
24+
"url": "http://raghavdua.com"
25+
},
26+
{
27+
"name": "Federico Bond",
28+
"email": "[email protected]"
29+
},
30+
{
31+
"name": "Christopher Gewecke",
32+
"email": "[email protected]"
33+
}
34+
],
1635
"license": "MIT",
1736
"dependencies": {
1837
"mocha": "^2.4.5",

solidity.pegjs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ Keyword
208208
= BreakToken
209209
/ ContinueToken
210210
/ ContractToken
211+
/ InterfaceToken
211212
/ DeleteToken
212213
/ DoToken
213214
/ ElseToken
@@ -479,6 +480,7 @@ IfToken = "if" !IdentifierPart
479480
IsToken = "is" !IdentifierPart
480481
IndexedToken = "indexed" !IdentifierPart
481482
ImportToken = "import" !IdentifierPart
483+
InterfaceToken = "interface" !IdentifierPart
482484
InternalToken = "internal" !IdentifierPart
483485
LibraryToken = "library" !IdentifierPart
484486
MappingToken = "mapping" !IdentifierPart
@@ -522,6 +524,9 @@ EOS = __ ";"
522524
EOF
523525
= !.
524526

527+
Comma
528+
= __ "," __
529+
525530
/* ----- A.2 Number Conversions ----- */
526531

527532
/* Irrelevant. */
@@ -652,12 +657,31 @@ Arguments
652657
= "(" __ args:(ArgumentList __)? ")" {
653658
return optionalList(extractOptional(args, 0));
654659
}
660+
/ "(" __ "{" __ args:(NameValueList (__ ",")? )? __ "}" __ ")" {
661+
return optionalList(extractOptional(args, 0));
662+
}
655663

656664
ArgumentList
657665
= head:AssignmentExpression tail:(__ "," __ AssignmentExpression)* {
658666
return buildList(head, tail, 3);
659667
}
660668

669+
NameValueList
670+
= head:NameValueAssignment tail:(__ "," __ NameValueAssignment)* {
671+
return buildList(head, tail, 3);
672+
}
673+
674+
NameValueAssignment
675+
= name:Identifier __ ":" __ value:AssignmentExpression __ {
676+
return {
677+
type: "NameValueAssignment",
678+
name: name,
679+
value: value,
680+
start: location().start.offset,
681+
end: location().end.offset
682+
};
683+
}
684+
661685
LeftHandSideExpression
662686
= DeclarativeExpression
663687
/ CallExpression
@@ -995,9 +1019,9 @@ VariableStatement
9951019
}
9961020

9971021
VariableDeclarationTuple
998-
= "(" head:VariableDeclarationNoInit tail:(__ "," __ VariableDeclarationNoInit)* ")" init:(__ Initialiser) {
1022+
= "("Comma* __ head:VariableDeclarationNoInit tail:(Comma+ VariableDeclarationNoInit)* __ Comma* ")" init:(__ Initialiser) {
9991023
return {
1000-
declarations: buildList(head, tail, 3),
1024+
declarations: buildList(head, tail, 1),
10011025
init: extractOptional(init, 1)
10021026
}
10031027
}
@@ -1037,7 +1061,7 @@ EmptyStatement
10371061
= ";" { return { type: "EmptyStatement", start: location().start.offset, end: location().end.offset }; }
10381062

10391063
ExpressionStatement
1040-
= !("{" / FunctionToken / ContractToken / LibraryToken / StructToken / EnumToken) expression:Expression EOS {
1064+
= !("{" / FunctionToken / ContractToken / InterfaceToken / LibraryToken / StructToken / EnumToken) expression:Expression EOS {
10411065
return {
10421066
type: "ExpressionStatement",
10431067
expression: expression,
@@ -1270,6 +1294,21 @@ ContractStatement
12701294
}
12711295
}
12721296

1297+
InterfaceStatement
1298+
= InterfaceToken __ id:Identifier __
1299+
"{" __ body:SourceElements? __ "}"
1300+
{
1301+
return {
1302+
type: "InterfaceStatement",
1303+
name: id.name,
1304+
is: [],
1305+
body: body,
1306+
start: location().start.offset,
1307+
end: location().end.offset
1308+
}
1309+
}
1310+
1311+
12731312
LibraryStatement
12741313
= LibraryToken __ id:Identifier __ is:IsStatement? __
12751314
"{" __ body:SourceElements? __ "}"
@@ -1495,6 +1534,7 @@ SourceUnit
14951534
= PragmaStatement
14961535
/ ImportStatement
14971536
/ ContractStatement
1537+
/ InterfaceStatement
14981538
/ LibraryStatement
14991539

15001540
SourceElements
@@ -1522,7 +1562,7 @@ InlineAssemblyBlock
15221562
}
15231563

15241564
AssemblyItem
1525-
= FunctionalAssemblyExpression
1565+
= FunctionalAssemblyInstruction
15261566
/ InlineAssemblyBlock
15271567
/ AssemblyLocalBinding
15281568
/ AssemblyAssignment
@@ -1531,8 +1571,17 @@ AssemblyItem
15311571
/ HexStringLiteral
15321572
/ Identifier
15331573

1574+
AssemblyExpression
1575+
= FunctionalAssemblyInstruction
1576+
/ ElementaryAssemblyOperation
1577+
1578+
ElementaryAssemblyOperation
1579+
= NumericLiteral
1580+
/ StringLiteral
1581+
/ Identifier
1582+
15341583
AssemblyLocalBinding
1535-
= 'let' __ name:Identifier __ ':=' __ expression:FunctionalAssemblyExpression {
1584+
= 'let' __ name:Identifier __ ':=' __ expression:AssemblyExpression {
15361585
return {
15371586
type: "AssemblyLocalBinding",
15381587
name: name,
@@ -1543,7 +1592,7 @@ AssemblyLocalBinding
15431592
}
15441593

15451594
AssemblyAssignment
1546-
= name:Identifier __ ':=' __ expression:FunctionalAssemblyExpression {
1595+
= name:Identifier __ ':=' __ expression:FunctionalAssemblyInstruction {
15471596
return {
15481597
type: "AssemblyAssignment",
15491598
name: name,
@@ -1561,10 +1610,10 @@ AssemblyAssignment
15611610
}
15621611
}
15631612

1564-
FunctionalAssemblyExpression
1613+
FunctionalAssemblyInstruction
15651614
= name:Identifier __ '(' __ head:AssemblyItem? __ tail:( ',' __ AssemblyItem )* __ ')' {
15661615
return {
1567-
type: "FunctionalAssemblyExpression",
1616+
type: "FunctionalAssemblyInstruction",
15681617
name: name,
15691618
arguments: buildList(head, tail, 2),
15701619
start: location().start.offset,

test/doc_examples.sol

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import "SomeFile.sol" as SomeOtherFile;
1616
import * as SomeSymbol from "AnotherFile.sol";
1717
import {symbol1 as alias, symbol2} from "File.sol";
1818

19+
interface i {
20+
function f();
21+
}
22+
1923
contract c {
2024
function c()
2125
{
@@ -297,6 +301,17 @@ library GetCode {
297301
}
298302
}
299303

304+
contract assemblyLocalBinding {
305+
function test(){
306+
assembly {
307+
let v := 1
308+
let x := 0x00
309+
let y := x
310+
let z := "hello"
311+
}
312+
}
313+
}
314+
300315
contract usesConst {
301316
uint const = 0;
302317
}
@@ -341,10 +356,42 @@ contract VariableDeclarationTuple {
341356
function ham (){
342357
var (x, y) = (10, 20);
343358
var (a, b) = getMyTuple();
359+
var (,c) = (10, 20);
360+
var (d,,) = (10, 20, 30);
361+
var (,e,,f,) = (10, 20, 30, 40, 50);
362+
363+
var (
364+
num1, num2,
365+
num3, ,num5
366+
) = (10, 20, 30, 40, 50);
344367
}
345368
}
346369

347370
contract TypeIndexSpacing {
348371
uint [ 7 ] x;
349372
uint [] y;
350373
}
374+
375+
contract Ballot {
376+
377+
struct Voter {
378+
uint weight;
379+
bool voted;
380+
}
381+
382+
function abstain() returns (bool) {
383+
return false;
384+
}
385+
386+
Voter you = Voter(1, true);
387+
388+
Voter me = Voter({
389+
weight: 2,
390+
voted: abstain()
391+
});
392+
393+
Voter airbnb = Voter({
394+
weight: 2,
395+
voted: true,
396+
});
397+
}

0 commit comments

Comments
 (0)