loloicci / nimly

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

Confusing error message on ambiguous parse #69

Closed wyattjsmith1 closed 3 years ago

wyattjsmith1 commented 3 years ago

I am not an expert in lexing/parsing, but I think this can be improved. In the example below, there are two parsing matchers, and they both essentially do the same thing. word1 matches a sequence of word2s, and word2 matches a sequence of characters. In this example, it is impossible to tell where the words should be broken up; any sequence of characters could be divided into quite a few sequences of words. This fails to compile (message below) with an unhelpful message that could probably be improved.

import patty
import nimly
import unittest

variantp FluentToken:
  Character(character: char)

niml fluentLexer[FluentToken]:
  r"[A..Za..z1..9\-]":
    Character(token.token[0])

nimy fluentParser[FluentToken]:
  top[seq[string]]:
    word1:
      return $1

  word1[seq[string]]:
    word2{}:
      return $1

  word2[string]:
    Character{}:
      var str = ""
      for character in $1:
        str &= character.character
      return str

test "test":
  var testLexer = fluentLexer.newWithString("testing")
  var parser = fluentParser.newParser()
  discard parser.parse(testLexer)

Outputs:

➜  nim-fluent nim c -r src/nim_fluent/test.nim
Hint: used config file '/Users/wys/.choosenim/toolchains/nim-1.4.4/config/nim.cfg' [Conf]
Hint: used config file '/Users/wys/.choosenim/toolchains/nim-1.4.4/config/config.nims' [Conf]
.........................................
stack trace: (most recent call last)
/Users/wys/my-workspace/nim-fluent/src/nim_fluent/test.nim(22, 5)
/Users/wys/my-workspace/nim-fluent/src/nim_fluent/test.nim(22, 5) :tmp
/Users/wys/.nimble/pkgs/nimly-0.7.0/nimly/lalr.nim(159, 18) makeTableLALR
/Users/wys/.nimble/pkgs/nimly-0.7.0/nimly/lalr.nim(119, 19) toLALRKernel
/Users/wys/.nimble/pkgs/nimly-0.7.0/nimly/lalr.nim(74, 13) closure
/Users/wys/.nimble/pkgs/nimly-0.7.0/nimly/lalr.nim(65, 25) closure
/Users/wys/.nimble/pkgs/nimly-0.7.0/nimly/parsetypes.nim(291, 11) calFirsts
/Users/wys/.choosenim/toolchains/nim-1.4.4/lib/system/assertions.nim(30, 26) failedAssertImpl
/Users/wys/.choosenim/toolchains/nim-1.4.4/lib/system/assertions.nim(23, 11) raiseAssert
/Users/wys/.choosenim/toolchains/nim-1.4.4/lib/system/fatal.nim(49, 5) sysFatal
/Users/wys/my-workspace/nim-fluent/src/nim_fluent/test.nim(12, 6) template/generic instantiation of `nimy` from here
/Users/wys/.choosenim/toolchains/nim-1.4.4/lib/system/fatal.nim(49, 5) Error: unhandled exception: /Users/wys/.nimble/pkgs/nimly-0.7.0/nimly/parsetypes.nim(291, 18) `false`  [AssertionDefect]
loloicci commented 3 years ago

Thank you for your reporting!

This is caused by an unexpected error and I check it now.