Designing a Parsing Library in Scala

How to go fast?

sealed trait Parser[A] {
protected def parseInternal(s: State): A
final def parse(str: String): Either[Error, (String, A)] = {
val state = new State(str)
val res = parseInternal(state)
if (state.isError) Left(state.getError)
else Right((str.substring(state.offset), res))
}
}

Parsing Fast and Safe

Given parser A and parser B, if A returns a success, then if B returns an epsilon failure, then backtrack to before A, else if B has a committed failure return that failure, else if B has a success return the result of both parsers. If A returns a failure return that failure.

Summary and Invitation for Collaboration

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store