solidity-parser / parser

A Solidity parser for JS built on top of a robust ANTLR4 grammar
MIT License
157 stars 44 forks source link

Error interpreting assembly assignment with whitespace between `:` and `=` #110

Open echodrift opened 9 months ago

echodrift commented 9 months ago

When I try to parse a solidity file with assembly code, parser throw a ParseError:

ParserError: extraneous input '=' expecting {'from', '{', '}', '(', 'error', 'for', 'function', 'address', 'calldata', 'if', 'return', 'revert', 'byte', 'let', 'switch', 'callback', BooleanLiteral, DecimalNumber, HexNumber, HexLiteralFragment, 'break', 'continue', 'leave', 'payable', 'global', 'constructor', 'receive', Identifier, StringLiteralFragment}

But when I use solidity compiler to compile file, it is still compilable. Here is my source code example.

Janther commented 9 months ago

Hello @lvdthieu,

Our parser doesn't understand size: = extcodesize(investor) because the operator we consider as assignment in assembly is := (no space between : and =) which would make your code look like this size := extcodesize(investor).

Just out of curiosity, does : = compile without errors using the solidity compiler? If that's the case we need to update our grammar.

echodrift commented 9 months ago

Hello @Janther, Thank for give me your time, I have tried to compile the file using hardhat framework, in which I config solidity compiler version as 0.4.24 and it's compilable. image image

Janther commented 9 months ago

for now just change name: = value to name := value in your code since fixing the grammar might take some time.

echodrift commented 9 months ago

Actually, I am doing a survey about solidity smart contracts and I need to parse multiple files (from verified Smart Contracts from Etherscan.io). I encounter some weird cases that a file has some "syntax error" (I'm not sure) but it's still compilable. I have recognized some patterns in these files and I want to let you know. Example files

  1. "pragma - ;" (pragma_error_sample.sol)
  2. "uint256 indexed indexed round" (two "indexed" keywords) (indexed_error_sample.sol)
    errors: [
    {
      message: "extraneous input 'indexed' expecting {',', ')'}",
      line: 516,
      column: 91
    }
    ]
  3. "TKN internal fallback" (fallback_error_sample.sol)
    errors: [
    {
      message: "extraneous input 'fallback' expecting {'from', 'error', 'address', 'calldata', 'revert', 'callback', 'override', 'constant', 'immutable', 'leave', 'internal', 'payable', 'private', 'public', 'global', 'constructor', 'receive', Identifier}",
      line: 35,
      column: 17
    }
    ]
  4. Multiple lines string (string_error_sample.sol)
    errors: [
    {
      message: `token recognition error at: '"The number of recipients differs from the number of amounts \\\r\n` +
        "'",
      line: 6,
      column: 8
    },
    {
      message: `token recognition error at: '";\r'`,
      line: 7,
      column: 19
    },
    { message: "missing ';' at 'string'", line: 8, column: 4 },
    {
      message: `token recognition error at: '"The ether value sent is less than the total intended amount to send \\\r\n` +
        "'",
      line: 9,
      column: 8
    },
    {
      message: `token recognition error at: '";\r'`,
      line: 10,
      column: 21
    },
    {
      message: "extraneous input 'event' expecting {'from', 'error', 'address', 'calldata', 'revert', 'callback', 'leave', 'payable', 'global', 'constructor', 'receive', Identifier}",
      line: 12,
      column: 4
    },
    {
      message: "mismatched input '(' expecting ';'",
      line: 12,
      column: 14
    },
    {
      message: "mismatched input ',' expecting {';', '='}",
      line: 12,
      column: 32
    },
    {
      message: "extraneous input ')' expecting {';', '='}",
      line: 12,
      column: 45
    }
    ]