/* * Copyright (C) 2007-2019 Artima, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Automatically generated Scala interpreter transcript from: * * Programming in Scala (Fourth Edition) * by Martin Odersky, Lex Spoon, Bill Venners * * http://booksites.artima.com/programming_in_scala_4ed */ scala> import scala.util.parsing.combinator._ class Arith extends JavaTokenParsers { def expr: Parser[Any] = term~rep("+"~term | "-"~term) def term: Parser[Any] = factor~rep("*"~factor | "/"~factor) def factor: Parser[Any] = floatingPointNumber | "("~expr~")" } ^ error: object parsing is not a member of package util class Arith extends JavaTokenParsers { ^ On line 3: error: not found: type JavaTokenParsers def expr: Parser[Any] = term~rep("+"~term | "-"~term) ^ On line 4: error: not found: type Parser def term: Parser[Any] = factor~rep("*"~factor | "/"~factor) ^ On line 5: error: not found: type Parser def factor: Parser[Any] = floatingPointNumber | "("~expr~")" ^ On line 6: error: not found: type Parser def factor: Parser[Any] = floatingPointNumber | "("~expr~")" ^ On line 6: error: not found: value floatingPointNumber def factor: Parser[Any] = floatingPointNumber | "("~expr~")" ^ On line 6: error: value ~ is not a member of String scala> object ParseExpr extends Arith { def main(args: Array[String]) = { println("input : " + args(0)) println(parseAll(expr, args(0))) } } ^ error: not found: type Arith println(parseAll(expr, args(0))) ^ On line 4: error: not found: value parseAll println(parseAll(expr, args(0))) ^ On line 4: error: not found: value expr scala> ParseExpr.main(Array("2 * (3 + 7)")) ^ error: not found: value ParseExpr scala> ParseExpr.main(Array("2 * (3 + 7))")) ^ error: not found: value ParseExpr scala> import scala.util.parsing.combinator._ class JSON extends JavaTokenParsers { def value : Parser[Any] = obj | arr | stringLiteral | floatingPointNumber | "null" | "true" | "false" def obj : Parser[Any] = "{"~repsep(member, ",")~"}" def arr : Parser[Any] = "["~repsep(value, ",")~"]" def member: Parser[Any] = stringLiteral~":"~value } import java.io.FileReader object ParseJSON extends JSON { def main(args: Array[String]) = { val reader = new FileReader(args(0)) println(parseAll(value, reader)) } } ^ error: object parsing is not a member of package util class JSON extends JavaTokenParsers { ^ On line 3: error: not found: type JavaTokenParsers def value : Parser[Any] = obj | arr | ^ On line 5: error: not found: type Parser def obj : Parser[Any] = "{"~repsep(member, ",")~"}" ^ On line 10: error: not found: type Parser def member: Parser[Any] = stringLiteral~":"~value ^ On line 14: error: not found: type Parser def obj : Parser[Any] = "{"~repsep(member, ",")~"}" ^ On line 10: error: value ~ is not a member of String def obj : Parser[Any] = "{"~repsep(member, ",")~"}" ^ On line 10: error: not found: value repsep def arr : Parser[Any] = "["~repsep(value, ",")~"]" ^ On line 12: error: not found: type Parser def arr : Parser[Any] = "["~repsep(value, ",")~"]" ^ On line 12: error: value ~ is not a member of String def arr : Parser[Any] = "["~repsep(value, ",")~"]" ^ On line 12: error: not found: value repsep def member: Parser[Any] = stringLiteral~":"~value ^ On line 14: error: not found: value stringLiteral println(parseAll(value, reader)) ^ On line 22: error: not found: value parseAll scala> val addressbook = """ { "address book": { "name": "John Smith", "address": { "street": "10 Market Street", "city" : "San Francisco, CA", "zip" : 94111 }, "phone numbers": [ "408 338-4238", "408 111-6892" ] } } """.trim addressbook: String = { "address book": { "name": "John Smith", "address": { "street": "10 Market Street", "city" : "San Francisco, CA", "zip" : 94111 }, "phone numbers": [ "408 338-4238", "408 111-6892" ] } } scala> object ParseJSONShell extends JSON { def main(str: String) = { val reader = new java.io.StringReader(str) println(parseAll(value, reader)) } } ^ error: not found: type JSON println(parseAll(value, reader)) ^ On line 4: error: not found: value parseAll println(parseAll(value, reader)) ^ On line 4: error: not found: value value scala> ParseJSONShell.main(addressbook) ^ error: not found: value ParseJSONShell scala> import scala.util.parsing.combinator._ class JSON1 extends JavaTokenParsers { def obj: Parser[Map[String, Any]] = "{"~> repsep(member, ",") <~"}" ^^ (Map() ++ _) def arr: Parser[List[Any]] = "["~> repsep(value, ",") <~"]" def member: Parser[(String, Any)] = stringLiteral~":"~value ^^ { case name~":"~value => (name, value) } def value: Parser[Any] = ( obj | arr | stringLiteral | floatingPointNumber ^^ (_.toDouble) | "null" ^^ (x => null) | "true" ^^ (x => true) | "false" ^^ (x => false) ) } ^ error: object parsing is not a member of package util class JSON1 extends JavaTokenParsers { ^ On line 3: error: not found: type JavaTokenParsers def obj: Parser[Map[String, Any]] = ^ On line 5: error: not found: type Parser def member: Parser[(String, Any)] = ^ On line 11: error: not found: type Parser "{"~> repsep(member, ",") <~"}" ^^ (Map() ++ _) ^ On line 6: error: value ~> is not a member of String "{"~> repsep(member, ",") <~"}" ^^ (Map() ++ _) ^ On line 6: error: not found: value repsep def arr: Parser[List[Any]] = ^ On line 8: error: not found: type Parser def value: Parser[Any] = ( ^ On line 15: error: not found: type Parser "["~> repsep(value, ",") <~"]" ^ On line 9: error: value ~> is not a member of String "["~> repsep(value, ",") <~"]" ^ On line 9: error: not found: value repsep stringLiteral~":"~value ^^ ^ On line 12: error: not found: value stringLiteral { case name~":"~value => (name, value) } ^ On line 13: error: not found: value ~ { case name~":"~value => (name, value) } ^ On line 13: error: not found: value ~ scala> val addressbook = """ { "address book": { "name": "John Smith", "address": { "street": "10 Market Street", "city" : "San Francisco, CA", "zip" : 94111 }, "phone numbers": [ "408 338-4238", "408 111-6892" ] } } """.trim addressbook: String = { "address book": { "name": "John Smith", "address": { "street": "10 Market Street", "city" : "San Francisco, CA", "zip" : 94111 }, "phone numbers": [ "408 338-4238", "408 111-6892" ] } } scala> object JSON1TestShell extends JSON1 { def main(str: String) = { val reader = new java.io.StringReader(str) println(parseAll(value, reader)) } } ^ error: not found: type JSON1 println(parseAll(value, reader)) ^ On line 4: error: not found: value parseAll println(parseAll(value, reader)) ^ On line 4: error: not found: value value scala> JSON1TestShell.main(addressbook) ^ error: not found: value JSON1TestShell