parsecomb.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. discard """
  2. matrix: "--mm:arc; --mm:refc"
  3. """
  4. type Input[T] = object
  5. toks: seq[T]
  6. index: int
  7. type
  8. ResultKind* = enum rkSuccess, rkFailure
  9. Result*[T, O] = object
  10. case kind*: ResultKind
  11. of rkSuccess:
  12. output*: O
  13. input: Input[T]
  14. of rkFailure:
  15. nil
  16. type
  17. Parser*[T, O] = proc (input: Input[T]): Result[T, O]
  18. proc unit*[T, O](v: O): Parser[T, O] =
  19. result = proc (inp: Input[T]): Result[T, O] =
  20. Result[T, O](kind: rkSuccess, output: v, input: inp)
  21. proc fail*[T, O](): Parser[T, O] =
  22. result = proc (inp: Input[T]): Result[T, O] =
  23. Result(kind: rkFailure)
  24. method runInput[T, O](self: Parser[T, O], inp: Input[T]): Result[T, O] =
  25. # hmmm ..
  26. type tmp = proc (input: Input[T]): Result[T, O]
  27. # XXX: above needed for now, as without the `tmp` bit below, it compiles to invalid C.
  28. tmp(self)(inp)
  29. proc run*[T, O](self: Parser[T, O], toks: seq[T]): Result[T, O] =
  30. self.runInput(Input[T](toks: toks, index: 0))
  31. proc chain*[T, O1, O2](self: Parser[T, O1], nextp: proc (v: O1): Parser[T, O2]): Parser[T, O2] =
  32. result = proc (inp: Input[T]): Result[T, O2] =
  33. let r = self.runInput(inp)
  34. case r.kind:
  35. of rkSuccess:
  36. nextp(r.output).runInput(r.input)
  37. of rkFailure:
  38. Result[T, O2](kind: rkFailure)
  39. method skip[T](self: Input[T], n: int): Input[T] {.base.} =
  40. Input[T](toks: self.toks, index: self.index + n)
  41. proc pskip*[T](n: int): Parser[T, tuple[]] =
  42. result = proc (inp: Input[T]): Result[T, tuple[]] =
  43. if inp.index + n <= inp.toks.len:
  44. Result[T, tuple[]](kind: rkSuccess, output: (), input: inp.skip(n))
  45. else:
  46. Result[T, tuple[]](kind: rkFailure)
  47. proc tok*[T](t: T): Parser[T, T] =
  48. result = proc (inp: Input[T]): Result[T, T] =
  49. if inp.index < inp.toks.len and inp.toks[inp.index] == t:
  50. pskip[T](1).then(unit[T, T](t)).runInput(inp)
  51. else:
  52. Result[T, T](kind: rkFailure)
  53. proc `+`*[T, O](first: Parser[T, O], second: Parser[T, O]): Parser[T, O] =
  54. result = proc (inp: Input[T]): Result[T, O] =
  55. let r = first.runInput(inp)
  56. case r.kind
  57. of rkSuccess:
  58. r
  59. else:
  60. second.runInput(inp)
  61. # end of primitives (definitions involving Parser(..))
  62. proc map*[T, O1, O2](self: Parser[T, O1], p: proc (v: O1): O2): Parser[T, O2] =
  63. self.chain(proc (v: O1): Parser[T, O2] =
  64. unit[T, O2](p(v)))
  65. proc then*[T, O1, O2](self: Parser[T, O1], next: Parser[T, O2]): Parser[T, O2] =
  66. self.chain(proc (v: O1): Parser[T, O2] =
  67. next)
  68. proc `*`*[T, O1, O2](first: Parser[T, O1], second: Parser[T, O2]): Parser[T, (O1, O2)] =
  69. first.chain(proc (v1: O1): Parser[T, (O1, O2)] =
  70. second.map(proc (v2: O2): (O1, O2) =
  71. (v1, v2)))
  72. proc repeat0*[T, O](inner: Parser[T, O]): Parser[T, seq[O]] =
  73. var nothing = unit[T, seq[O]](@[])
  74. inner.chain(proc(v: O): Parser[T, seq[O]] =
  75. repeat0(inner).map(proc(vs: seq[O]): seq[O] =
  76. @[v] & vs)) + nothing
  77. proc repeat1*[T, O](inner: Parser[T, O]): Parser[T, seq[O]] =
  78. inner.chain(proc(v: O): Parser[T, seq[O]] =
  79. repeat0(inner).map(proc(vs: seq[O]): seq[O] =
  80. @[v] & vs))
  81. proc leftRec*[T, O, A](inner: Parser[T, O], after: Parser[T, A], fold: proc(i: O, a: A): O): Parser[T, O] =
  82. (inner*repeat0(after)).map(proc(ias: (O, seq[A])): O =
  83. var (i, asx) = ias
  84. for a in asx:
  85. i = fold(i, a)
  86. i)
  87. proc lazy*[T, O](inner: proc(): Parser[T, O]): Parser[T, O] =
  88. unit[T, tuple[]](()).chain(proc(v: tuple[]): Parser[T, O] =
  89. inner())