This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
generated from makenowjust-labs/template-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
de0e6c9
commit 72fed25
Showing
10 changed files
with
917 additions
and
3 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
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
64 changes: 64 additions & 0 deletions
64
modules/bench/src/main/scala/codes/quine/labo/parsers/bench/ContparseJSONParser.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package codes.quine.labo.parsers | ||
package bench | ||
|
||
import scala.annotation.switch | ||
|
||
import contparse._ | ||
import JSON._ | ||
|
||
object ContparseJSONParser { | ||
val space: P[Unit] = CharsWhileIn(" \r\n", 0) | ||
|
||
val json: P[JSON] = | ||
P((number | string | `null` | `true` | `false` | array | `object`) ~ space) | ||
|
||
val entry: P[JSON] = space ~ json ~ End | ||
|
||
val digits: P[Unit] = CharsWhileIn("0123456789") | ||
val exponent: P[Unit] = CharIn("eE") ~ CharIn("+-").? ~ digits | ||
val fractional: P[Unit] = "." ~ digits | ||
val integral: P[Unit] = "0" | CharIn("123456789") ~ digits.? | ||
|
||
val number: P[JSON] = | ||
(CharIn("+-").? ~ integral ~ fractional.? ~ exponent.?).!.map(s => JSONNumber(s.toDouble)).named("<number>") | ||
|
||
val `null`: P[JSON] = "null" ~ Pass(JSONNull) | ||
val `true`: P[JSON] = "true" ~ Pass(JSONBoolean(true)) | ||
val `false`: P[JSON] = "false" ~ Pass(JSONBoolean(false)) | ||
|
||
val hexDigit: P[Unit] = CharIn("0123456789abcdefABCDEF") | ||
val unicodeEscape: P[Char] = "u" ~ hexDigit.count(4).!.map(s => Integer.parseInt(s, 16).toChar) | ||
val simpleEscape: P[Char] = | ||
CharIn("\"\\/bfnrt").!.map(s => | ||
(s.charAt(0): @switch) match { | ||
case 'b' => '\b' | ||
case 'f' => '\f' | ||
case 'n' => '\n' | ||
case 'r' => '\r' | ||
case 't' => '\t' | ||
case c => c | ||
} | ||
) | ||
val escape: P[Char] = "\\" ~/ (simpleEscape | unicodeEscape) | ||
val stringContent: P[String] = CharsWhile(c => c != '"' && c != '\\').! | escape.map(String.valueOf(_)) | ||
val key: P[String] = ("\"" ~/ stringContent.rep.map(_.mkString) ~ "\"").named("<string>") | ||
val string: P[JSON] = key.map(JSONString(_)) | ||
|
||
val arrayContent: P[Seq[JSON]] = | ||
(json ~ ("," ~ space ~/ json).rep).?.map { | ||
case None => Seq.empty | ||
case Some((x, xs)) => x +: xs | ||
} | ||
val array: P[JSON] = "[" ~ space ~/ arrayContent.map(JSONArray(_)) ~ "]" | ||
|
||
val pair: P[(String, JSON)] = key ~ space ~ ":" ~ space ~ json | ||
val objectContent: P[Seq[(String, JSON)]] = | ||
(pair ~ ("," ~ space ~/ pair).rep).?.map { | ||
case None => Seq.empty | ||
case Some((k, v, xs)) => (k, v) +: xs | ||
} | ||
val `object`: P[JSON] = "{" ~ space ~/ objectContent.map(JSONObject(_)) ~ "}" | ||
|
||
def parse(input: String): Parsed[JSON] = | ||
contparse.parse(input, entry) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# parsers-contparse | ||
|
||
> A parser combinator library with continuation passing style. |
Oops, something went wrong.