Skip to content

Instantly share code, notes, and snippets.

@bshepherdson
Created March 21, 2016 00:02
Show Gist options
  • Select an option

  • Save bshepherdson/f79e0a3521e0206a7fcf to your computer and use it in GitHub Desktop.

Select an option

Save bshepherdson/f79e0a3521e0206a7fcf to your computer and use it in GitHub Desktop.
Generics trouble in Pony
interface ParseBuffer
fun peek(): U8 ?
fun ref head(): U8 ?
fun eof(): Bool
fun clone(): ParseBuffer
class _StringBuffer is ParseBuffer
let _str: String val
var _pos: USize
new create(s: String val) =>
_str = s
_pos = 0
new _positioned(s: String val, p: USize) =>
_str = s
_pos = p
fun peek(): U8 ? =>
_str(_pos)
fun ref head(): U8 ? =>
let c = _str(_pos)
_pos = _pos + 1
c
fun eof(): Bool => _pos >= _str.size()
fun clone(): ParseBuffer => _StringBuffer._positioned(_str, _pos)
interface Parser[A]
fun op_or(p: Parser[A]): Parser[A] =>
PTry[A](this, p)
fun parse(buf: ParseBuffer): A ?
class PTry[A] is Parser[A]
"""Tries the left-hand parser. If it succeeds, returns that value. If it
fails, tries the right-hand parser."""
let _lhs: Parser[A] box
let _rhs: Parser[A] box
new create(l: Parser[A] box, r: Parser[A] box) =>
_lhs = l
_rhs = r
fun parse(buf: ParseBuffer): A ? =>
let copy = buf.clone()
try
_lhs.parse(buf)
else
_rhs.parse(copy)
end
class PSeq[A] is Parser[Array[A]]
"""Performs each parser in sequence, returning an Array."""
let _parsers: Array[Parser[A] box] box
new create(parsers: Array[Parser[A] box] box) =>
_parsers = parsers
fun parse(buf: ParseBuffer): Array[A] ref ? =>
var ret: Array[A] ref = Array[A]()
for p in _parsers.values() do
let res: A = p.parse(buf)
ret.push(res)
end
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment