From 7c9569bf01fe16a100b5c0007cad98a1ac066811 Mon Sep 17 00:00:00 2001 From: Daniel Spiewak Date: Wed, 1 Jul 2009 10:45:46 -0500 Subject: [PATCH] Config example parser --- examples/buildfile | 6 +++++- examples/src/{conf => config}/ConfigParser.scala | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) rename examples/src/{conf => config}/ConfigParser.scala (89%) diff --git a/examples/buildfile b/examples/buildfile index 22e75db..c8814ae 100644 --- a/examples/buildfile +++ b/examples/buildfile @@ -28,7 +28,7 @@ define 'examples', :layout => layout do trace cp start = Time.now - system JAVA_HOME + '/bin/java', '-cp', cp, classname, *Dir.glob("input/#{name.downcase}/*") + sh JAVA_HOME + '/bin/java', '-cp', cp, classname, *Dir.glob("input/#{name.downcase}/*") puts "Total run time: #{Time.now - start} seconds" puts '---------------------------------------' @@ -51,5 +51,9 @@ define 'examples', :layout => layout do run_eg.call 'Paren' end + task :conf => :compile do + run_eg.call 'Config' + end + task :run => [:arithmetic, :miniml, :lambdacalc, :paren] end diff --git a/examples/src/conf/ConfigParser.scala b/examples/src/config/ConfigParser.scala similarity index 89% rename from examples/src/conf/ConfigParser.scala rename to examples/src/config/ConfigParser.scala index bc2d872..26e34f1 100644 --- a/examples/src/conf/ConfigParser.scala +++ b/examples/src/config/ConfigParser.scala @@ -1,4 +1,4 @@ -package conf +package config import scala.io.Source import edu.uwm.cs.gll._ @@ -10,7 +10,7 @@ import edu.uwm.cs.gll._ * possible results into a single map. This illustrates a practical * advantage to ambiguous parsing. */ -object ConfigParser extends RegexParsers { +object ConfigParser extends common.Example[Map[String, String]] with RegexParsers { class ConfigException(failures: List[Failure]) extends RuntimeException("Failed to parse config file") @@ -43,6 +43,12 @@ object ConfigParser extends RegexParsers { } } + def handleSuccesses(forest: List[Map[String, String]]) { + for ((key, value) <- forest.foldLeft(Map[String, String]()) { _ ++ _ }) { + println(" " + key + " -> " + value) + } + } + // %% override val whitespace = """[ \t]+"""r // process newlines separately