parser.scala
来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 40 行
SCALA
40 行
/* __ *\** ________ ___ / / ___ Scala API **** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL **** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **** /____/\___/_/ |_/____/_/ | | **** |/ **\* */// $Id: Parser.scala 13911 2008-02-07 12:39:51Z moors $package scala.util.parsing.jsonimport scala.util.parsing.combinator._import scala.util.parsing.combinator.syntactical._import scala.util.parsing.combinator.lexical._/** * @author Derek Chen-Becker <"java"+@+"chen-becker"+"."+"org"> */class Parser extends StdTokenParsers with ImplicitConversions { // Fill in abstract defs type Tokens = Lexer val lexical = new Tokens // Configure lexical parsing lexical.reserved ++= List("true", "false", "null") lexical.delimiters ++= List("{", "}", "[", "]", ":", ",") // Define the grammar def root = jsonObj | jsonArray def jsonObj = "{" ~> repsep(objEntry, ",") <~ "}" def jsonArray = "[" ~> repsep(value, ",") <~ "]" def objEntry = stringVal ~ (":" ~> value) ^^ { case x ~ y => (x, y) } def value: Parser[Any] = (jsonObj | jsonArray | number | "true" ^^^ true | "false" ^^^ false | "null" ^^^ null | stringVal) def stringVal = accept("string", { case lexical.StringLit(n) => n} ) def number = accept("number", { case lexical.NumericLit(n) => n.toDouble} )}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?