Solidity邮件正则合约编译报错:Expected ',' but got identifier求助
解决Solidity 0.5.x中函数类型结构体成员的编译错误
错误原因分析
编译报错ParserError: Expected ',' but got identifier出现在cur = state(cur).func(uint c);行,核心问题有两个:
- 结构体函数类型声明不匹配且语法错误:结构体
State中func的类型声明为function (byte) internal pure returns (uint),但实际所有状态处理函数(s1-s10)接收的是uint8参数,且Solidity 0.5.x要求函数类型的参数列表必须严格匹配,不能有语法冗余。 - 函数调用时不必要的类型转换:调用
func时对uint8类型的c做uint转换,既多余又触发语法解析错误。
修正步骤
1. 修正结构体函数类型
将State结构体中的func类型改为与实际函数匹配的uint8参数类型:
struct State { bool accepts; function(uint8) internal pure returns (uint) func; }
2. 修正函数调用语句
在matches函数中,直接传递uint8类型的c参数,去掉多余的类型转换:
cur = state(cur).func(c);
完整修正后的合约代码
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.4.22 <0.9.0; library EmailRegex { struct State { bool accepts; function(uint8) internal pure returns (uint) func; } function state(uint id) internal pure returns (State memory) { if (id == 1) { return State(false, s1); } if (id == 2) { return State(false, s2); } if (id == 3) { return State(false, s3); } if (id == 4) { return State(false, s4); } if (id == 5) { return State(false, s5); } if (id == 6) { return State(false, s6); } if (id == 7) { return State(false, s7); } if (id == 8) { return State(false, s8); } if (id == 9) { return State(true, s9); } if (id == 10) { return State(true, s10); } // 添加默认返回,避免无匹配时的编译警告 return State(false, s1); } function matches(string memory input) public pure returns (bool) { uint cur = 1; for (uint i = 0; i < bytes(input).length; i++) { uint8 c = uint8(bytes(input)[i]); cur = state(cur).func(c); if (cur == 0) { return false; } } return state(cur).accepts; } function s1(uint8 c) internal pure returns (uint) { if (c == 37 || c == 43 || c == 45 || c == 46 || c == 57 || c >= 65 && c <= 90 || c == 95 || c >= 97 && c <= 122) { return 2; } return 0; } function s2(uint8 c) internal pure returns (uint) { if (c == 37 || c == 43 || c == 45 || c == 46 || c >= 48 && c <= 57 || c >= 65 && c <= 90 || c == 95 || c >= 97 && c <= 122) { return 3; } if (c == 64) { return 4; } return 0; } function s3(uint8 c) internal pure returns (uint) { if (c == 37 || c == 43 || c == 45 || c == 46 || c >= 48 && c <= 57 || c >= 65 && c <= 90 || c == 95 || c >= 97 && c <= 122) { return 3; } if (c == 64) { return 4; } return 0; } function s4(uint8 c) internal pure returns (uint) { if (c >= 46 && c <= 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 65 && c <= 90 || c >= 91 && c <= 95 || c >= 97 && c <= 122) { return 5; } return 0; } function s5(uint8 c) internal pure returns (uint) { if (c == 46) { return 6; } if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 65 && c <= 90 || c >= 91 && c <= 95 || c >= 97 && c <= 122) { return 7; } return 0; } function s6(uint8 c) internal pure returns (uint) { if (c == 46) { return 6; } if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 91 && c <= 95) { return 7; } if (c >= 65 && c <= 90 || c >= 97 && c <= 122) { return 8; } return 0; } function s7(uint8 c) internal pure returns (uint) { if (c == 46) { return 6; } if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 65 && c <= 90 || c >= 91 && c <= 95 || c >= 97 && c <= 122) { return 7; } return 0; } function s8(uint8 c) internal pure returns (uint) { if (c == 46) { return 6; } if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 91 && c <= 95) { return 7; } if (c >= 65 && c <= 90 || c >= 97 && c <= 122) { return 9; } return 0; } function s9(uint8 c) internal pure returns (uint) { if (c == 46) { return 6; } if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 91 && c <= 95) { return 7; } if (c >= 65 && c <= 90 || c >= 97 && c <= 122) { return 10; } return 0; } function s10(uint8 c) internal pure returns (uint) { if (c == 46) { return 6; } if (c == 47 || c >= 48 && c <= 57 || c >= 58 && c <= 64 || c >= 91 && c <= 95) { return 7; } if (c >= 65 && c <= 90 || c >= 97 && c <= 122) { return 10; } return 0; } }
验证
将修正后的代码替换原合约,使用你当前的环境(Truffle v5.5.21、Solidity v0.5.16)编译,即可通过编译。
内容的提问来源于stack exchange,提问作者user14487732
相关产品推荐
相关产品推荐

