Skip to content

Commit 1e485c0

Browse files
authored
Merge pull request #141 from Raiden1411/ast
ast: allow parsing of multiple state modifiers
2 parents b1251c3 + 1b3da6e commit 1e485c0

File tree

4 files changed

+65
-26
lines changed

4 files changed

+65
-26
lines changed

src/ast/Ast.zig

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,23 @@ pub fn stateVariableDecl(self: Ast, node: Node.Index) ast.StateVariableDecl {
177177

178178
std.debug.assert(token_tags.len > state);
179179

180-
switch (token_tags[state]) {
181-
.keyword_public => result.public = state,
182-
.keyword_private => result.private = state,
183-
.keyword_internal => result.internal = state,
184-
.keyword_constant => result.constant = state,
185-
.keyword_immutable => result.immutable = state,
186-
.keyword_override => result.override = state,
187-
else => {
188-
if (nodes.len > state and nodes[state] == .override_specifier) {
189-
result.override = self.nodes.items(.main_token)[state];
190-
}
191-
},
180+
const modifier_range = self.extraData(state, Node.Range);
181+
const modifier_node = self.extra_data[modifier_range.start..modifier_range.end];
182+
183+
for (modifier_node) |state_mod| {
184+
switch (token_tags[state_mod]) {
185+
.keyword_public => result.public = state_mod,
186+
.keyword_private => result.private = state_mod,
187+
.keyword_internal => result.internal = state_mod,
188+
.keyword_constant => result.constant = state_mod,
189+
.keyword_immutable => result.immutable = state_mod,
190+
.keyword_override => result.override = state_mod,
191+
else => {
192+
if (nodes.len > state and nodes[state] == .override_specifier) {
193+
result.override = self.nodes.items(.main_token)[state];
194+
}
195+
},
196+
}
192197
}
193198

194199
return result;
@@ -1076,6 +1081,7 @@ pub fn firstToken(self: Ast, node: Node.Index) TokenIndex {
10761081
=> current_node = data[current_node].lhs,
10771082
.modifier_specifiers,
10781083
.specifiers,
1084+
.state_modifiers,
10791085
=> {
10801086
const extra = self.extraData(main_token[current_node], Node.Range);
10811087

@@ -1539,6 +1545,7 @@ pub fn lastToken(self: Ast, node: Node.Index) TokenIndex {
15391545

15401546
.specifiers,
15411547
.modifier_specifiers,
1548+
.state_modifiers,
15421549
=> {
15431550
const extra = self.extraData(main_token[current_node], Node.Range);
15441551

@@ -2375,6 +2382,9 @@ pub const Node = struct {
23752382
/// rhs is the expression or null_node.
23762383
state_variable_decl,
23772384
constant_variable_decl,
2385+
/// main token is the state keywords range.
2386+
/// lhs and rhs are undefined.
2387+
state_modifiers,
23782388
};
23792389

23802390
/// Range used for params and others

src/ast/Parser.zig

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -749,16 +749,7 @@ pub fn parseStateVariableDecl(self: *Parser) ParserErrors!Node.Index {
749749
if (type_decl == 0)
750750
return null_node;
751751

752-
const state = switch (self.token_tags[self.token_index]) {
753-
.keyword_public,
754-
.keyword_private,
755-
.keyword_internal,
756-
.keyword_constant,
757-
.keyword_immutable,
758-
=> self.nextToken(),
759-
.keyword_override => try self.parseOverrideSpecifier(),
760-
else => return null_node,
761-
};
752+
const state = try self.parseStateModifier();
762753

763754
_ = try self.expectToken(.identifier);
764755

@@ -793,6 +784,40 @@ pub fn parseStateVariableDecl(self: *Parser) ParserErrors!Node.Index {
793784
else => return self.fail(.expected_semicolon),
794785
}
795786
}
787+
/// [Grammar](https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.stateVariableDeclaration)
788+
///
789+
/// Parses specifically the state modifiers since solidity can have multiple
790+
pub fn parseStateModifier(self: *Parser) ParserErrors!Node.Index {
791+
const scratch = self.scratch.items.len;
792+
defer self.scratch.shrinkRetainingCapacity(scratch);
793+
794+
while (true) {
795+
switch (self.token_tags[self.token_index]) {
796+
.keyword_public,
797+
.keyword_private,
798+
.keyword_internal,
799+
.keyword_constant,
800+
.keyword_immutable,
801+
=> try self.scratch.append(self.allocator, self.nextToken()),
802+
.keyword_override => try self.scratch.append(
803+
self.allocator,
804+
try self.parseOverrideSpecifier(),
805+
),
806+
else => break,
807+
}
808+
}
809+
810+
const slice = self.scratch.items[scratch..];
811+
812+
return self.addNode(.{
813+
.tag = .state_modifiers,
814+
.main_token = try self.addExtraData(try self.listToSpan(slice)),
815+
.data = .{
816+
.lhs = undefined,
817+
.rhs = undefined,
818+
},
819+
});
820+
}
796821
/// [Grammar](https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.functionDefinition)
797822
pub fn parseFunctionSpecifiers(self: *Parser) ParserErrors!Node.Index {
798823
const scratch = self.scratch.items.len;
@@ -876,7 +901,8 @@ pub fn parseModifierSpecifiers(self: *Parser) ParserErrors!Node.Index {
876901
}
877902
/// [Grammar](https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.overrideSpecifier)
878903
pub fn parseOverrideSpecifier(self: *Parser) ParserErrors!Node.Index {
879-
const override = self.consumeToken(.keyword_override) orelse null_node;
904+
const override = self.consumeToken(.keyword_override) orelse
905+
return null_node;
880906

881907
const scratch = self.scratch.items.len;
882908
defer self.scratch.shrinkRetainingCapacity(scratch);

src/clients/optimism/clients/L1PubClient.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ pub fn L1Client(comptime client_type: Clients) type {
203203
return list.toOwnedSlice();
204204
}
205205
/// Returns if a withdrawal has finalized or not.
206-
pub fn getFinalizedWithdrawals(self: *L1, withdrawal_hash: Hash) (EncodeErrors || ClientType.BasicRequestErrors || error{ExpectOpStackContracts})!bool {
206+
pub fn getFinalizedWithdrawals(
207+
self: *L1,
208+
withdrawal_hash: Hash,
209+
) (EncodeErrors || ClientType.BasicRequestErrors || error{ExpectOpStackContracts})!bool {
207210
const contracts = self.rpc_client.network_config.op_stack_contracts orelse return error.ExpectOpStackContracts;
208211

209212
const encoded = try abi_items.get_finalized_withdrawal.encode(self.allocator, .{withdrawal_hash});

tests/ast/parser.test.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,8 @@ test "Uniswap V3" {
873873
\\}
874874
\\
875875
\\contract UniswapV3Liquidity is IERC721Receiver {
876-
\\ IERC20 private dai = IERC20(DAI);
877-
\\ IWETH private weth = IWETH(WETH);
876+
\\ IERC20 private immutable dai = IERC20(DAI);
877+
\\ IWETH private constant weth = IWETH(WETH);
878878
\\
879879
\\ int24 private MIN_TICK = -887272;
880880
\\ int24 private MAX_TICK = -MIN_TICK;

0 commit comments

Comments
 (0)