federicobond / solidity-parser-antlr

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

Tokenize function is not working #94

Open fvictorio opened 4 years ago

fvictorio commented 4 years ago

Using version 0.4.11.

Reproduction steps: install the library and run:

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

const tokens = parser.tokenize('contract Foo {}')

Throws this error:

Error: ENOENT: no such file or directory, open <root>/node_modules/solidity-parser-antlr/lib/Solidity.tokens'

Possible fix

I cloned the repo and made two modifications:

  1. Change the build script to rm -rf dist && babel --out-dir=dist src --copy-files (adds --copy-files)
  2. In src/tokens.js, make this change:
diff --git a/src/tokens.js b/src/tokens.js
index 10836dd..f47b0ac 100644
--- a/src/tokens.js
+++ b/src/tokens.js
@@ -55,7 +55,7 @@ function getTokenType(value) {
 }

 function getTokenTypeMap() {
-  const filePath = path.join(__dirname, '../lib/Solidity.tokens')
+  const filePath = path.join(__dirname, './lib/Solidity.tokens')

   return fs
     .readFileSync(filePath)

This worked for me, but I'm not sure if it may have another implications. If you want, I can create a PR with this change.