Skip to content

Commit

Permalink
javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
konnik committed Sep 18, 2023
1 parent 818a8fe commit 305aca7
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions lib/src/main/kotlin/konnik/json/JsonParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ sealed interface JsonValue {
*/
fun parseJson(input: String): JsonValue? =
when (val result = json(input)) {
null -> null
else -> when (result.second) {
"" -> result.first
else -> null // it's an error if not all input is consumed
}
null -> null
else -> when (result.second) {
"" -> result.first
else -> null // it's an error if not all input is consumed
}
}


Expand All @@ -42,21 +42,19 @@ fun parseJson(input: String): JsonValue? =
*/

/**
* Data type representing a parser that parses a some input string and produces
* a value of type A.
*
* If the parser succeeds the parsed value is returned together with the remainder of the input
* that has not been consumed.
* Type that defines what a parser is.
*
* If the parser fails null is returned.
* A parser is a function that takes some input (String) as it's argument and
* if it succeeds returns a value of type A (together with de remaining input),
* otherwise it returns null.
*
*/
private typealias Parser<A> = (String) -> Pair<A, String>?


/* ----------------------------------------------------------------------------
*
* COMBINATORS
* COMBINATOR FUNCTIONS
*
* ----------------------------------------------------------------------------
*/
Expand Down Expand Up @@ -129,7 +127,7 @@ private fun <A : Any> lazy(parser: () -> Parser<A>): Parser<A> = { input ->
* Combine to parsers into one that concatenates the results of the
* individual parsers into one string.
*/
private operator fun <A: Any, B: Any>Parser<A>.plus(parserB: Parser<B>): Parser<String> =
private operator fun <A : Any, B : Any> Parser<A>.plus(parserB: Parser<B>): Parser<String> =
this.andThen { valueA -> parserB.map { valueB -> valueA.toString() + valueB.toString() } }


Expand Down Expand Up @@ -163,7 +161,6 @@ private fun match(test: (Char) -> Boolean): Parser<Char> = { input ->
}



/* ------------------------------------------------------------------------------------------
* GRAMMAR
*
Expand Down

0 comments on commit 305aca7

Please sign in to comment.