Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully fail, instead of a logic error when a custom parser encounters keys that are not in order #14

Open
planetis-m opened this issue Jun 29, 2021 · 0 comments

Comments

@planetis-m
Copy link
Owner

proc initFromJson*[T](dst: var Matrix[T]; p: var JsonParser) =
  eat(p, tkCurlyLe)
  var count = 0
  while p.tok != tkCurlyRi:
    if p.tok != tkString:
      raiseParseErr(p, "string literal as key")
    case p.a
    of "m":
      discard getTok(p)
      eat(p, tkColon)
      if count > 1:
        raiseParseErr(p, "JSON keys in order")
      count = 1
      initFromJson(dst.m, p)
    of "n":
      discard getTok(p)
      eat(p, tkColon)
      if count > 2:
        raiseParseErr(p, "JSON keys in order")
      count = 2
      initFromJson(dst.n, p)
    of "data":
      discard getTok(p)
      eat(p, tkColon)
      if count > 3:
        raiseParseErr(p, "JSON keys in order")
      count = 3
      eat(p, tkBracketLe)
      assert dst.m != 0 and dst.n == 0
      dst.data = createData[T](dst.m * dst.n)
      var i = 0
      while p.tok != tkBracketRi:
        initFromJson(dst.data[i], p)
        inc(i)
        if p.tok != tkComma: break
        discard getTok(p)
      eat(p, tkBracketRi)
    else:
      raiseParseErr(p, "valid object field")
    if p.tok != tkComma: break
    discard getTok(p)
  eat(p, tkCurlyRi)

doesn't even seem enough...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant