Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

parse fails to parse empty files with { loc: true } option #95

Open
fernandomg opened this issue Dec 6, 2019 · 0 comments
Open

parse fails to parse empty files with { loc: true } option #95

fernandomg opened this issue Dec 6, 2019 · 0 comments

Comments

@fernandomg
Copy link

fernandomg commented Dec 6, 2019

Hello Federico,

First of all, thank you so much for this fantastic parser!

The issue I'm facing has to do with the parse function and the loc option set to true, if I pass a file without parseable solidity code in it parse throws an error.

I tested the same scenario with tokenize and it worked fine.

Here is the code to reproduce the scenarios:

const parser = require('solidity-parser-antlr')

const emptyFile = ''

const commentedFile = `/*
pragma solidity ^0.5.3;

contract A {
  uint a;
  function b() {}
}
*/
`

const contracts = [{ emptyFile }, { commentedFile }]

const processFile = (parserModule = 'parse') => (loc = false) => contract => {
  const contractName = Object.keys(contract)
  const label = `\n\n=== ${parserModule.toUpperCase()} ${contractName} with ${JSON.stringify({ loc })} ===\n`

  try {
    const result = parser[parserModule](contract[contractName], { loc })
    console.log(label, JSON.stringify(result, null, 2))
  } catch (e) {
    console.log(label, e)
  }
}

const parse = processFile()
contracts.forEach(parse(true))
contracts.forEach(parse())

const tokenize = processFile('tokenize')
contracts.forEach(tokenize(true))
contracts.forEach(tokenize())

And this is the output of running it:

=== PARSE emptyFile with {"loc": true} ===
 TypeError: Cannot read property 'line' of null
    at ASTBuilder._loc (/**/solidity-parser-antlr/dist/ASTBuilder.js:1135:22)
    at ASTBuilder.meta (/**/solidity-parser-antlr/dist/ASTBuilder.js:1149:29)
    at ASTBuilder.createNode (/**/solidity-parser-antlr/dist/ASTBuilder.js:1158:34)
    at ASTBuilder.visit (/**/solidity-parser-antlr/dist/ASTBuilder.js:1187:15)
    at Object.parse (/**/solidity-parser-antlr/dist/index.js:83:21)
    at contract (/**/parse_fails.js:24:40)
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/**/parse_fails.js:33:11)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)


=== PARSE commentedFile with {"loc": true} ===
 TypeError: Cannot read property 'line' of null
    at ASTBuilder._loc (/**/solidity-parser-antlr/dist/ASTBuilder.js:1135:22)
    at ASTBuilder.meta (/**/solidity-parser-antlr/dist/ASTBuilder.js:1149:29)
    at ASTBuilder.createNode (/**/solidity-parser-antlr/dist/ASTBuilder.js:1158:34)
    at ASTBuilder.visit (/**/solidity-parser-antlr/dist/ASTBuilder.js:1187:15)
    at Object.parse (/**/solidity-parser-antlr/dist/index.js:83:21)
    at contract (/**/parse_fails.js:24:40)
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/**/parse_fails.js:33:11)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)


=== PARSE emptyFile with {"loc": false} ===
 {
  "type": "SourceUnit",
  "children": []
}


=== PARSE commentedFile with {"loc": false} ===
 {
  "type": "SourceUnit",
  "children": []
}


=== TOKENIZE emptyFile with {"loc": true} ===
 []


=== TOKENIZE commentedFile with {"loc": true} ===
 [
  {
    "type": "Keyword",
    "value": "/*\npragma solidity ^0.5.3;\n\ncontract A {\n  uint a;\n  function b() {}\n}\n*/",
    "loc": {
      "start": {
        "line": 1,
        "column": 0
      },
      "end": {
        "line": 1,
        "column": 73
      }
    }
  }
]


=== TOKENIZE emptyFile with {"loc": false} ===
 []


=== TOKENIZE commentedFile with {"loc": false} ===
 [
  {
    "type": "Keyword",
    "value": "/*\npragma solidity ^0.5.3;\n\ncontract A {\n  uint a;\n  function b() {}\n}\n*/"
  }
]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant