zevv / npeg

PEGs for Nim, another take
MIT License
330 stars 22 forks source link

javascript support #70

Closed mantielero closed 8 months ago

mantielero commented 8 months ago

First, thanks for this project is great. Trying to compile my code to javascript (just to check if that was possible) I got the following error:

$ nim js test_ex_01.nim
...
asciidoc/src/asciidoc/docheader/docheader.nim(18, 32) template/generic instantiation of `peg` from here
/home/jose/.nimble/pkgs/npeg-0.27.0/npeg/codegen.nim(429, 33) template/generic instantiation of `fn`gensym167` from here
/home/jose/.nimble/pkgs/npeg-0.27.0/npeg/codegen.nim(355, 22) Error: type mismatch
Expression: pop(e`gensym166.trace)
  [1] e`gensym166.trace: string

Expected one of (first mismatch at [position]):
[1] proc pop[A, B](t: OrderedTableRef[A, B]; key: A; val: var B): bool
[1] proc pop[A, B](t: TableRef[A, B]; key: A; val: var B): bool
[1] proc pop[A, B](t: var OrderedTable[A, B]; key: A; val: var B): bool
[1] proc pop[A, B](t: var Table[A, B]; key: A; val: var B): bool
[1] proc pop[A](t: CountTableRef[A]; key: A; val: var int): bool
[1] proc pop[A](t: var CountTable[A]; key: A; val: var int): bool
[1] proc pop[T](s: var seq[T]): T
[1] template pop[T](s: var Stack[T]): T

Is this something that could be fixed or simply npeg won't support javascript? (or maybe is something that I am doing wrong).

zevv commented 8 months ago

JS is supported for NPeg, and part of the regular test suite.

Can you share your code so I can try to reproduce?

mantielero commented 8 months ago

Yes, I have it in github: https://github.com/mantielero/asciidoc

I was compiling this file: https://github.com/mantielero/asciidoc/blob/main/tests/basic/test_ex_01.nim

It is just a proof of concept (in my case, spaguetty code; sorry for that).

zevv commented 8 months ago

I can't seem to reproduce, can you provide clear instructions on how to generate the error you see, and preferably using reduced/minimized code that shows the problematic behavior?

mantielero commented 8 months ago

Yes of course.

  1. Download as .zip the repository: https://github.com/mantielero/asciidoc and unzip it somewhere else.

  2. Go to:

    cd asciidoc-main/tests/basic
  3. You can compile and run as C:

    $ nim c -r test_ex_01
    Hint: used config file '/home/jose/.choosenim/toolchains/nim-2.0.0/config/nim.cfg' [Conf]
    Hint: used config file '/home/jose/.choosenim/toolchains/nim-2.0.0/config/config.nims' [Conf]
    Hint: used config file '/home/jose/Descargas/borrame/asciidoc-main/tests/config.nims' [Conf]
    Hint: used config file '/home/jose/Descargas/borrame/asciidoc-main/tests/basic/config.nims' [Conf]
    Hint: mm: orc; threads: on; opt: none (DEBUG BUILD, `-d:release` generates faster code)
    29480 lines; 0.020s; 30.457MiB peakmem; proj: /home/jose/Descargas/borrame/asciidoc-main/tests/basic/test_ex_01; out: /home/jose/Descargas/borrame/asciidoc-main/tests/basic/test_ex_01 [SuccessX]
    Hint: /home/jose/Descargas/borrame/asciidoc-main/tests/basic/test_ex_01 [Exec]
  4. I cannot generate a JavaScript version from the code:

    
    $ nim js test_ex_01
    Hint: used config file '/home/jose/.choosenim/toolchains/nim-2.0.0/config/nim.cfg' [Conf]
    Hint: used config file '/home/jose/.choosenim/toolchains/nim-2.0.0/config/config.nims' [Conf]
    Hint: used config file '/home/jose/Descargas/borrame/asciidoc-main/tests/config.nims' [Conf]
    Hint: used config file '/home/jose/Descargas/borrame/asciidoc-main/tests/basic/config.nims' [Conf]
    ....................................................................................................
    /home/jose/Descargas/borrame/asciidoc-main/src/asciidoc/parser/docheader/docheader.nim(10, 32) template/generic instantiation of `peg` from here
    /home/jose/.nimble/pkgs/npeg-0.27.0/npeg/codegen.nim(429, 33) template/generic instantiation of `fn`gensym167` from here
    /home/jose/.nimble/pkgs/npeg-0.27.0/npeg/codegen.nim(355, 22) Error: type mismatch
    Expression: pop(e`gensym166.trace)
    [1] e`gensym166.trace: string

Expected one of (first mismatch at [position]): [1] proc pop[A, B](t: OrderedTableRef[A, B]; key: A; val: var B): bool [1] proc pop[A, B](t: TableRef[A, B]; key: A; val: var B): bool [1] proc pop[A, B](t: var OrderedTable[A, B]; key: A; val: var B): bool [1] proc pop[A, B](t: var Table[A, B]; key: A; val: var B): bool [1] proc pop[A](t: CountTableRef[A]; key: A; val: var int): bool [1] proc pop[A](t: var CountTable[A]; key: A; val: var int): bool [1] proc pop[T](s: var seq[T]): T [1] template pop[T](s: var Stack[T]): T

zevv commented 8 months ago

Works for me

ico@ldoos:/tmp/asciidoc/tests/basic(main)$ nim --version
Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2023-12-23
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: c0acf3ce286f92d41e6b280ba9a1b729019bae12
active boot switches: -d:release
ico@ldoos:/tmp/asciidoc/tests/basic(main)$ nim js test_ex_01
Hint: used config file '/home/ico/external/Nim/config/nim.cfg' [Conf]
Hint: used config file '/home/ico/external/Nim/config/config.nims' [Conf]
Hint: used config file '/tmp/asciidoc/tests/config.nims' [Conf]
Hint: used config file '/tmp/asciidoc/tests/basic/config.nims' [Conf]
....................................................................................................
/tmp/asciidoc/src/asciidoc/parser/docheader/docheader.nim(8, 11) Warning: imported and not used: 'strformat' [UnusedImport]
..
/tmp/asciidoc/src/asciidoc/parser/grammar/adocgrammar.nim(3, 1) template/generic instantiation of `grammar` from here
/home/ico/.nimble/pkgs2/npeg-1.2.1-c5451eea5d1f656700f04ad4b234d99ab0b78ed7/npeg/grammar.nim(46, 5) Warning: Redefinition of rule 'comment' [User]
.
/tmp/asciidoc/src/asciidoc/parser/lists/lists.nim(3, 18) Warning: imported and not used: 'adocgrammar' [UnusedImport]
/tmp/asciidoc/src/asciidoc/parser/lists/lists.nim(4, 11) Warning: imported and not used: 'strformat' [UnusedImport]
.
/tmp/asciidoc/src/asciidoc/parser/sections/sections.nim(7, 18) Warning: imported and not used: 'adocgrammar' [UnusedImport]
/tmp/asciidoc/src/asciidoc/parser/sections/sections.nim(8, 11) Warning: imported and not used: 'strformat' [UnusedImport]
.
/tmp/asciidoc/src/asciidoc/parser/paragraph/paragraph.nim(12, 27) template/generic instantiation of `peg` from here
/home/ico/.nimble/pkgs2/npeg-1.2.1-c5451eea5d1f656700f04ad4b234d99ab0b78ed7/npeg/grammar.nim(46, 5) Warning: Redefinition of rule 'comment' [User]
/tmp/asciidoc/src/asciidoc/parser/paragraph/paragraph.nim(12, 27) template/generic instantiation of `peg` from here
/home/ico/.nimble/pkgs2/npeg-1.2.1-c5451eea5d1f656700f04ad4b234d99ab0b78ed7/npeg/grammar.nim(46, 5) Warning: Redefinition of rule 'emptyorcomment' [User]
/tmp/asciidoc/src/asciidoc/parser/paragraph/paragraph.nim(7, 18) Warning: imported and not used: 'adocgrammar' [UnusedImport]
/tmp/asciidoc/src/asciidoc/parser/paragraph/paragraph.nim(8, 11) Warning: imported and not used: 'strformat' [UnusedImport]
.
/tmp/asciidoc/src/asciidoc/parser/breaks/breaks.nim(7, 18) Warning: imported and not used: 'adocgrammar' [UnusedImport]
/tmp/asciidoc/src/asciidoc/parser/breaks/breaks.nim(8, 11) Warning: imported and not used: 'strformat' [UnusedImport]
.
/tmp/asciidoc/src/asciidoc/parser/preprocessor/includes.nim(18, 34) Warning: Number of spaces around '..<' is not consistent [Spacing]
/tmp/asciidoc/src/asciidoc/parser/preprocessor/includes.nim(3, 11) Warning: imported and not used: 'strformat' [UnusedImport]
.
/tmp/asciidoc/src/asciidoc/parser/preprocessor/variables.nim(4, 11) Warning: imported and not used: 'strformat' [UnusedImport]
/tmp/asciidoc/src/asciidoc/parser/preprocessor/variables.nim(2, 8) Warning: imported and not used: 'types' [UnusedImport]
/tmp/asciidoc/src/asciidoc/parser/preprocessor/variables.nim(3, 18) Warning: imported and not used: 'adocgrammar' [UnusedImport]
.............
/tmp/asciidoc/src/asciidoc/exporters/html/header.nim(16, 19) Warning: implicit conversion to 'cstring' from a non-const location: id; this will become a compile time error in the future [CStringConv]
/tmp/asciidoc/src/asciidoc/exporters/html/header.nim(25, 21) Warning: implicit conversion to 'cstring' from a non-const location: id; this will become a compile time error in the future [CStringConv]
/tmp/asciidoc/src/asciidoc/exporters/html/header.nim(27, 22) Warning: implicit conversion to 'cstring' from a non-const location: email; this will become a compile time error in the future [CStringConv]
..
/tmp/asciidoc/src/asciidoc/exporters/html/list.nim(18, 15) Hint: 'lvlValue' is declared but not used [XDeclaredButNotUsed]
/tmp/asciidoc/src/asciidoc/exporters/html/list.nim(11, 7) Hint: 'flag' is declared but not used [XDeclaredButNotUsed]
/tmp/asciidoc/src/asciidoc/exporters/html/list.nim(82, 32) Warning: implicit conversion to 'cstring' from a non-const location: "olist " & levels[item.level].class; this will become a compile time error in the future [CStringConv]
/tmp/asciidoc/src/asciidoc/exporters/html/list.nim(84, 49) Warning: implicit conversion to 'cstring' from a non-const location: levels[item.level].class; this will become a compile time error in the future [CStringConv]
/tmp/asciidoc/src/asciidoc/exporters/html/list.nim(86, 50) Warning: implicit conversion to 'cstring' from a non-const location: levels[item.level].typ; this will become a compile time error in the future [CStringConv]
..
/tmp/asciidoc/src/asciidoc/exporters/html/sections.nim(6, 41) Warning: implicit conversion to 'cstring' from a non-const location: "sect" & $(sect.level - 1); this will become a compile time error in the future [CStringConv]
/tmp/asciidoc/src/asciidoc/exporters/html/sections.nim(27, 24) Warning: implicit conversion to 'cstring' from a non-const location: id; this will become a compile time error in the future [CStringConv]
/tmp/asciidoc/src/asciidoc/exporters/html/sections.nim(1, 14) Warning: imported and not used: 'vstyles' [UnusedImport]
/tmp/asciidoc/src/asciidoc/exporters/html/html.nim(54, 44) Warning: implicit conversion to 'cstring' from a non-const location: description; this will become a compile time error in the future [CStringConv]
/tmp/asciidoc/src/asciidoc/exporters/html/html.nim(56, 39) Warning: implicit conversion to 'cstring' from a non-const location: author; this will become a compile time error in the future [CStringConv]
/tmp/asciidoc/src/asciidoc/exporters/html/html.nim(2, 8) Warning: imported and not used: 'asciidoc' [UnusedImport]
/tmp/asciidoc/src/asciidoc/exporters/html/html.nim(3, 14) Warning: imported and not used: 'vstyles' [UnusedImport]
/tmp/asciidoc/src/asciidoc/exporters/html/html.nim(4, 11) Warning: imported and not used: 'strformat' [UnusedImport]
/tmp/asciidoc/src/asciidoc.nim(64, 13) Hint: 'fileTxt' is declared but not used [XDeclaredButNotUsed]
/tmp/asciidoc/src/asciidoc.nim(98, 9) Hint: 'item' is declared but not used [XDeclaredButNotUsed]
/tmp/asciidoc/src/asciidoc.nim(34, 7) Hint: 'nn' is declared but not used [XDeclaredButNotUsed]
/tmp/asciidoc/src/asciidoc.nim(93, 7) Hint: 'n' is declared but not used [XDeclaredButNotUsed]
/tmp/asciidoc/src/asciidoc.nim(2, 11) Warning: imported and not used: 'strformat' [UnusedImport]
Hint: opt: none (DEBUG BUILD, `-d:release` generates faster code)
73605 lines; 1.234s; 263.32MiB peakmem; proj: /tmp/asciidoc/tests/basic/test_ex_01; out: /tmp/asciidoc/tests/basic/test_ex_01.js [SuccessX]

What is your Nim version?

mantielero commented 8 months ago

I am on Nim v2.0.0 (now I updated into v2.0.2).

It seems the issue had to do with prior npeg installations. After removing the npeg installations for Nim v1.x now it works.

Thanks a lot for your help.