loloicci / nimly

Lexer Generator and Parser Generator as a Library in Nim.
MIT License
147 stars 4 forks source link

Parser does not work when compiling to JavaScript #84

Open xigoi opened 3 years ago

xigoi commented 3 years ago

Minimal example:

# A parser that only parses the string "0"

import nimly
import patty

variant ZeroToken:
  zero

niml ZeroLexer[ZeroToken]:
  r"0":
    zero()

nimy ZeroParser[ZeroToken]:
  top[int]:
    zero:
      0

var lexer = ZeroLexer.newWithString("0")
var parser = ZeroParser.newParser
echo parser.parse lexer

When the code is compiled to C, it successfully outputs 0. However, if compiled to JavaScript (with -d:release), it outputs:

/home/xigoi/sandbox/nimlytest.js:878
    throw new Error(cbuf_1420201);
    ^

Error: Error: unhandled exception: Unexpected token(kind: TermS, term: zero)is passed.
token: (kind: zero) [NimyActionError]

    at unhandledException (/home/xigoi/sandbox/nimlytest.js:878:11)
    at raiseException (/home/xigoi/sandbox/nimlytest.js:478:5)
    at parseImpl_14412133 (/home/xigoi/sandbox/nimlytest.js:2596:11)
    at parse_14412000 (/home/xigoi/sandbox/nimlytest.js:2962:25)
    at Object.<anonymous> (/home/xigoi/sandbox/nimlytest.js:2972:23)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Function.Module._load (node:internal/modules/cjs/loader:828:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)

Note that this happens both in Node and in the browser. The error happens on the first token passed to the parser, no matter what the parser looks like.

loloicci commented 3 years ago

Sorry, nimly does not support compiling to JS now.

nimly targets to use as a part of compilers, so it only targets C/C++ compiling. In what situation do you want to use nimly in JS? If it looks like a common situation, I will try to support JS.

Or, you are very welcome to make PRs for supporting JS!

xigoi commented 3 years ago

I'm making a library that my team wants to use both in JavaScript and in Python. But I found out that I can use NPeg instead (which seems to work fine with JavaScript), so it's not a problem in the end. Thanks!