Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions java/arcs/core/util/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,19 @@ class ManyOfParser<T>(val parser: Parser<T>) : Parser<List<T>>() {
}
}

/**
* A parser providing a name for debugging purposes (traceback) but delegates parsing.
*/
class NamedParser<T>(name: String, val parser: Parser<T>) : Parser<T>() {

init {
this.name = name
}

override fun invoke(string: String, pos: SourcePosition) = parser.invoke(string, pos)
override fun leftTokens() = parser.leftTokens()
}

class ParserException(msg: String, cause: Exception) : Exception(msg, cause)

/** A parser which converts the return value of a parser into another value. */
Expand Down Expand Up @@ -587,6 +600,9 @@ fun <T> many(parser: Parser<T>) = ManyOfParser(parser)
/** Helper for [AnyOfParser]. */
fun <T> any(parsers: List<Parser<T>>) = AnyOfParser(parsers)

/** Helper for [NamedParser] */
fun <T> Parser<T>.named(name: String) = NamedParser(name, this)

/** Helper for [TransformParser]. */
fun <T, R> Parser<T>.map(f: (T) -> R) = TransformParser(this, f)

Expand Down