-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
42 additions
and
432 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,5 @@ | ||
package arktekk.json | ||
|
||
import cats.data.NonEmptyList | ||
import cats.parse.{Parser, Rfc5234} | ||
import arktekk.ParserSuite | ||
|
||
import scala.util.Try | ||
|
||
class JsonParser extends ParserSuite { | ||
|
||
enum JsonValue { | ||
case JsonNull | ||
case JsonBoolean(value: Boolean) | ||
case JsonNumber(value: BigDecimal) | ||
case JsonString(value: String) | ||
case JsonArray(values: List[JsonValue]) | ||
case JsonObject(pairs: List[(String, JsonValue)]) | ||
} | ||
import JsonValue.* | ||
|
||
val ows = (Rfc5234.wsp | Rfc5234.crlf).rep0.void | ||
val comma = Parser.char(',').surroundedBy(ows).withContext("comma") | ||
val colon = Parser.char(':').surroundedBy(ows) | ||
val openBracket = Parser.char('[').surroundedBy(ows) | ||
val closeBracket = Parser.char(']').surroundedBy(ows).withContext("closeBracket") | ||
val openBrace = Parser.char('{').surroundedBy(ows) | ||
val closeBrace = Parser.char('}').surroundedBy(ows).withContext("closeBrace") | ||
|
||
val jsonNull: Parser[JsonValue] = Parser.string("null").as(JsonNull).withContext("null") | ||
val jsonBoolean: Parser[JsonValue] = | ||
(Parser | ||
.string("false") | ||
.as(JsonBoolean(false)) | Parser.string("true").as(JsonBoolean(true))).withContext("boolean") | ||
val jsonNumber: Parser[JsonValue] = | ||
cats.parse.Numbers.jsonNumber.mapFilter(s => Try(JsonNumber(BigDecimal(s))).toOption) | ||
|
||
val jsonStringParser: Parser[String] = cats.parse.strings.Json.delimited.parser | ||
|
||
val jsonString: Parser[JsonValue] = jsonStringParser.map(JsonString.apply) | ||
|
||
val jsonScalar: Parser[JsonValue] = jsonNull | jsonBoolean | jsonNumber | jsonString | ||
|
||
val jsonRecur: Parser[JsonValue] = Parser.defer { | ||
val jsonValue = (jsonRecur | jsonScalar).surroundedBy(ows) | ||
val jsonArray: Parser[JsonValue] = { | ||
(openBracket ~ closeBracket).as(JsonArray(List.empty)).backtrack | | ||
jsonValue | ||
.repSep(comma) | ||
.between(openBracket, closeBracket) | ||
.map(nel => JsonArray(nel.toList)) | ||
} | ||
|
||
val jsonObject: Parser[JsonValue] = { | ||
(openBrace ~ closeBrace).as(JsonObject(List.empty)).backtrack | | ||
((jsonStringParser <* colon) ~ jsonValue) | ||
.repSep(comma) | ||
.between(openBrace, closeBrace) | ||
.map((pairs: NonEmptyList[(String, JsonValue)]) => JsonObject(pairs.toList)) | ||
} | ||
|
||
jsonArray | jsonObject | ||
} | ||
|
||
val jsonValue: Parser[JsonValue] = jsonScalar | jsonRecur | ||
|
||
test("null") { | ||
assertParses(jsonValue, "null" -> JsonNull) | ||
} | ||
|
||
test("true/false") { | ||
val validInputs = List( | ||
"false" -> JsonBoolean(false), | ||
"true" -> JsonBoolean(true) | ||
) | ||
|
||
assertParses(jsonValue, validInputs*) | ||
} | ||
|
||
test("jsonNumber") { | ||
val validInputs = List( | ||
"123" -> JsonNumber(BigDecimal("123")), | ||
"123.123" -> JsonNumber(123.123) | ||
) | ||
|
||
assertParses(jsonValue, validInputs*) | ||
} | ||
|
||
test("jsonString") { | ||
val validInputs = List( | ||
"\"foo\"" -> JsonString("foo"), | ||
"\"æøå\"" -> JsonString("æøå") | ||
) | ||
assertParses(jsonValue, validInputs*) | ||
} | ||
|
||
test("jsonArray") { | ||
val validInputs = List( | ||
" [ ] " -> JsonArray(List.empty), | ||
"[ 1, 20 ]" -> JsonArray(List(JsonNumber(1), JsonNumber(20))) | ||
) | ||
|
||
assertParses(jsonValue, validInputs*) | ||
} | ||
|
||
test("jsonObject") { | ||
val validInputs = List( | ||
"{ }" -> JsonObject(List.empty), | ||
"""{"test": 1}""" -> JsonObject(List("test" -> JsonNumber(BigDecimal(1)))), | ||
"""{"foo": "bar","fizz":[1,2,3,"fizz"]}""" -> JsonObject( | ||
List( | ||
("foo", JsonString("bar")), | ||
("fizz", JsonArray(List(JsonNumber(1), JsonNumber(2), JsonNumber(3), JsonString("fizz")))) | ||
) | ||
) | ||
) | ||
|
||
assertParses(jsonValue, validInputs*) | ||
} | ||
} | ||
class JsonParser extends ParserSuite {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 6 additions & 28 deletions
34
oppgaver/src/test/scala/arktekk/oppgave2/PointerParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.