
242
CHAPTER 8
■ PARSERS—BECAUSE BNF IS NOT JUST FOR ACADEMICS ANYMORE
In this section, we’ve converted from BNF description of the grammar to a running
application using Scala’s parser combinator library. The example, a four-function calcu-
lator, was very simple. In the next section, we’re going to tackle something more complex.
We’re going to parse JSON.
JSON Parsing
JSON, or JavaScript Object Notation, is a common and lightweight mechanism for exchanging
data over the Internet. JSON is a subset of JavaScript and can be parsed by the fast, stable,
efficient JavaScript parsers that are built into browsers. JSON is as readable as XML, more
compact than XML and corresponds to the types built into most dynamic languages:
strings, numbers, Booleans, arrays, and dictionaries or hash maps.
In this section, we’re going to follow the ECMAScript spec to build our
Parser: we’re
going to copy and paste the spec into our Scala code and literally code our
Parser to the
spec as we go. The only new concept in this
Parser is the regular-expression Parser. The
RegexParsers trait has an implicit conversion from a regular expression to a Parser[String].
If the input matches the regular expression, the matching string will be returned by the
Parser. We use this in the definition of spaces: """\s*""".r.
5
The triple quotes contain a
String that does not support any form of escaping, so \s is the literal \s, rather than escaping
s, and the regular expression \s stands for space. The .r at the end of the String converts
the
String into a regular expression.
The code in Listing 8-2 creates a JSON
Parser.
Listing 8-2. ECMAScript Spec -> JSON Parser
import scala.util.parsing.combinator._
object JSON extends RegexParsers with RunParser {
// translation from ECMAScript spec
// http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
/*
Whitespace
*/
lazy val spaces: Parser[String] = """\s*""".r
5. See the Java RegEx Pattern documentation: http://java.sun.com/javase/6/docs/api/java/util/
regex/Pattern.html.
19897ch08.fm Page 242 Wednesday, April 22, 2009 3:54 PM