Skip to content

Commit fcb78a6

Browse files
committed
Add support for file-level constants
1 parent f8cf608 commit fcb78a6

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/ASTBuilder.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,23 @@ const transformAST = {
939939
}
940940
},
941941

942+
FileLevelConstant(ctx) {
943+
const type = this.visit(ctx.typeName())
944+
const iden = ctx.identifier()
945+
const name = toText(iden)
946+
947+
let expression = null
948+
if (ctx.expression()) {
949+
expression = this.visit(ctx.expression())
950+
}
951+
952+
return {
953+
typeName: type,
954+
name,
955+
initialValue: expression,
956+
}
957+
},
958+
942959
ForStatement(ctx) {
943960
let conditionExpression = this.visit(ctx.expressionStatement())
944961
if (conditionExpression) {

test/ast.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,25 @@ describe('AST', () => {
719719
})
720720
})
721721

722+
it("file-level constants", function() {
723+
var ast = parser.parse("uint constant EXPONENT = 10;")
724+
var fileLevelConstantNode = ast.children[0];
725+
726+
assert.deepEqual(fileLevelConstantNode, {
727+
type: "FileLevelConstant",
728+
initialValue: {
729+
number: "10",
730+
subdenomination: null,
731+
type: "NumberLiteral"
732+
},
733+
name: "EXPONENT",
734+
typeName: {
735+
name: "uint",
736+
type: "ElementaryTypeName"
737+
}
738+
})
739+
})
740+
722741
it("ModifierInvocation", function() {
723742
var ast = parseNode("function foo(uint a) onlyOwner {}")
724743
assert.deepEqual(ast.modifiers[0], {

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("#parse", function() {
2020
it("supports tolerant mode", function() {
2121
var source = "not good"
2222
var root = parser.parse(source, { tolerant: true })
23-
assert.equal(root.errors.length, 1)
23+
assert.isAbove(root.errors.length, 0)
2424
})
2525

2626
it("supports loc", function() {

test/test.sol

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,3 +708,6 @@ contract AssemblySlotNotation {
708708
function helper(uint x) pure returns (uint) {
709709
return x * 2;
710710
}
711+
712+
// file-level constant
713+
uint constant topLevelConstantVariable = 3;

0 commit comments

Comments
 (0)