Skip to content

Instantly share code, notes, and snippets.

@lmdexpr
Last active December 2, 2020 18:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmdexpr/5219fee25e8d01c2a853b4b4eb1546cb to your computer and use it in GitHub Desktop.
Save lmdexpr/5219fee25e8d01c2a853b4b4eb1546cb to your computer and use it in GitHub Desktop.
ScalaMatsuri2020 Scalaひどいコード選手権用 JSON parser だったもの
type S=Seq[_]
def o(a:Seq[(_,_)],? :S):(Map[_,_],S)= ?match{case'}'+:r=>a.toMap->r
case','+:r=>o(a,r)case? =>val m=""""(.+?)":(.+)""".r
val m(k,t)= ?mkString
val(v,r)=p()(t.toSeq)
o(a:+(k->v),r)}
def y(a:S):S=>(S,S)={case']'+:r=>a->r
case','+:r=>y(a)(r)case s=>val(v,r)=p()(s)
y(a:+v)(r)}
def u(a:S,b:S):(S,S)=b match{case c+:r=>c match{case'}'|']'=>a->b
case','=>a->r
case x=>u(a:+c,r)}case _ =>a->Nil}
def p(a:S=""):S=>(Any,S)={case'{'+:r=>o(Nil,r)case'['+:r=>y("")(r)case','+:r=>a->r
case s=>val (t,r)= u("",s)
v(t.mkString)->r}
def v(? :String):Any= ?match{case"null"|"true"|"false"=> ?case? => ?toSeq match{case'"'+:t:+'"'=>t
case a=>if(?toSet('.'))?toDouble else?toInt}}
print(p()(io.StdIn.readLine.replaceAll("\\s",""))_1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment