shaack / cm-pgn

Parse and create PGNs (Portable Game Notation for chess games)
MIT License
27 stars 21 forks source link

Uncaught peg$SyntaxError #8

Closed ExtremeCoolDude closed 3 years ago

ExtremeCoolDude commented 3 years ago

Hello, I'm getting this error message :

Uncaught peg$SyntaxError {message: "Expected " ", "*", "0-1", "0:1", "1-0", "1/2-1/2",…], [a-h], end of input, or integer but "[" found.", expected: Array(44), found: "[", location: {…}, name: "SyntaxError", …}

Code:

<script type="module">
  import {Pgn} from "/Pgn.js";
  const pgnvv = new Pgn(PGN);
console.log(pgnvv.history());
</script>

PGN:

[Event "?"]
[Site "?"]
[Date "????.??.??"]
[Round "?"]
[White "?"]
[Black "?"]
[Result "*"]
[FEN "r3kb1r/pbq2ppp/2p2n2/1pn1pP2/4P1P1/2NB1Q2/PPPBN2P/R3K2R w KQkq - 0 1"]
[Setup "1"]

1. a4 { [%csl Rh1,Ra8,Gf6,Gc5,Gc3,Gb5,Yb4,Yb7,Yc6,Ra1,Rg2,Rf3,Re4,Rd5][%cal
Gf6e4,Gc5e4,Gc3d1,Gd1f2,Rb7e4,Re4h1,Rg4g5,Rf6d7]}  ( 1. g5)1... b4 2. Nd1 Nfxe4
3. Bxe4 Nxe4 4. Qxe4 c5 5. Qc4 Bxh1 6. Bxb4 Be7 7. Ne3 O-O 8. O-O-O Bf3 9. Nd5
Qb7 10. f6 Bxd5 11. Qxd5 Qxd5 12. Rxd5 gxf6 13. Bxc5 Rfd8 14. Rxd8+ Bxd8 15. b4
h5 16. h3 hxg4 17. hxg4 Kg7 18. Ng3 Bb6 19. Be7 Kg6 20. c4 Bf2 21. Nf5 Kg5 22.
Nd6 Kxg4 23. c5 Kf3 24. c6 e4 25. c7 Bd4 26. Nb5 Be5 27. Bd6 e3 28. Bxe5 fxe5
29. Nd6 Kf2  *

When I strip all the headers manually I get :

IllegalMoveException {fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", notation: "g5", toString: ƒ}

But g5 is a variation, not an illegal move.

shaack commented 3 years ago

The PGN header tags are case sensitiv, you must use [SetUp "1"] not [Setup "1"]. The parser did not start the game with the SetUp FEN, therefore "1. g5" is an invalid move.

I added your PGN to the test suite (last test): https://shaack.com/projekte/cm-pgn/test/tests.html

ExtremeCoolDude commented 3 years ago

Thanks a lot for the reply. test.html shows that pgn should pass. Unfortunately, I can't get it to work. I'm pretty new to coding and was wondering if you have any advice if other factors then the code itself can cause this behaviour. I'm trying to use this pgn handler to handle the pgn in Anki software : https://github.com/ExtremeCoolDude/Anki-Chess-2.0

error:

image

code :

image

PGN :

image

ExtremeCoolDude commented 3 years ago

Update : It shows the same error with the example from github's readme.md :

 import {Pgn} from "./src/cm-pgn/Pgn.js";
    // parse pgn
    const pgn = new Pgn(`[Site "Berlin"]
                         [Date "1989.07.02"]
                         [White "Haack, Stefan"]
                         [Black "Maier, Karsten"]
                         1. e2-e4 e7e5 (e6) 2. Nf3 $1 {Great move!} Nc6`);
       console.log(pgn.header());

So it's not related to pgn. Not sure what's causing this. I'm using a live server on chrome so it shouldn't be affected by chrome's coor policy.

Edit : This is the output from pegjs.org/online showing the same error :

image

ExtremeCoolDude commented 3 years ago

Please forgive me if I'm missing something simple and being stupid but none of the pgn which has at least a single header on test folder pass through the parser, simple san passes though.

image

shaack commented 3 years ago

Oh, sorry, there were failures in the README.md (now fixed with https://github.com/shaack/cm-pgn/commit/b1d7fc0eca14ec58613475457f31e42f66968ae7). Correct is:

[Date "1989.07.02"]
[White "Haack, Stefan"]
[Black "Maier, Karsten"]

1. e4 e5 (e6) 2. Nf3 $1 {Great move!} Nc6 *

Some rules:

and

putting this in https://pegjs.org/online:

1. a4 { [%csl Rh1,Ra8,Gf6,Gc5,Gc3,Gb5,Yb4,Yb7,Yc6,Ra1,Rg2,Rf3,Re4,Rd5][%cal Gf6e4,Gc5e4,Gc3d1,Gd1f2,Rb7e4,Re4h1,Rg4g5,Rf6d7]} ( 1. g5)1... b4 2. Nd1 Nfxe4 3. Bxe4 Nxe4 4. Qxe4 c5 5. Qc4 Bxh1 6. Bxb4 Be7 7. Ne3 O-O 8. O-O-O Bf3 9. Nd5 Qb7 10. f6 Bxd5 11. Qxd5 Qxd5 12. Rxd5 gxf6 13. Bxc5 Rfd8 14. Rxd8+ Bxd8 15. b4 h5 16. h3 hxg4 17. hxg4 Kg7 18. Ng3 Bb6 19. Be7 Kg6 20. c4 Bf2 21. Nf5 Kg5 22. Nd6 Kxg4 23. c5 Kf3 24. c6 e4 25. c7 Bd4 26. Nb5 Be5 27. Bd6 e3 28. Bxe5 fxe5 29. Nd6 Kf2 *

will work: Bildschirmfoto 2021-02-27 um 09 40 04

If you have further questions, just ask.

ExtremeCoolDude commented 3 years ago

Thanks a lot!

A few \n's were driving me crazy! :)

I have another question, can we just do load_pgn() on ChessJs() since chess.js accepts FEN as a parameter, we could just do history() on ChessJs and that wouldn't be illegal if PGN had no FEN included inside it.

Also on Anki since it cannot search for extra module js files, do you have any advice to bundle chess-cm into a single file ? I tried babel+browserify but keeping getting reference error on Chess() is not defined.

shaack commented 3 years ago

You mean "cm-chess"? https://github.com/shaack/cm-chess Yes, I have a problem with cm-chess, because it depends on chess.js https://github.com/jhlywa/chess.js, but chess.js in not shipped as an ES6 module. Therefore chess.js must be included separately via HTML, because it is not (easily) possible, to include non modules with a relative path into a module. One solution could be, to include the source of chess.js into cm-chess, but I have not done this so far.

ExtremeCoolDude commented 3 years ago

Yup, cm-chess! chess.js currently doesn't play well with variations and it's causing problems during pgn rendering. Would this work though : Include minified chess.js inside the same html file as the entire bundled cm-chess ? Thanks a lot for your thoughts on this.

shaack commented 3 years ago

Yes, this should work, but ive never done it

shaack commented 3 years ago

Okay, with https://github.com/shaack/cm-pgn/commit/e31edbc2cd179ec06215b4cded2dc8bd7fda14a2 I created chess.mjs, an ES6 version of chess.js and it should work now, without extra including chess.js.

ExtremeCoolDude commented 3 years ago

Thanks a lot!

I've been tinkering with another library in the meantime : https://github.com/kevinludwig/pgn-parser which has prebuilt js file to be inserted into Githubissues.

  • Githubissues is a development platform for aggregating issues.