nimly icon indicating copy to clipboard operation
nimly copied to clipboard

Parser does not work when compiling to JavaScript

Open xigoi opened this issue 3 years ago • 2 comments

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.

xigoi avatar Jun 09 '21 14:06 xigoi

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!

loloicci avatar Jun 10 '21 01:06 loloicci

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!

xigoi avatar Jun 10 '21 08:06 xigoi