diff options
author | Mark Lister <toomuchspam@nowhere> | 2013-06-07 17:29:12 +0300 |
---|---|---|
committer | Mark Lister <toomuchspam@nowhere> | 2013-06-07 17:29:12 +0300 |
commit | bd1a8a0c20996489e4f723a35b640562edf4b5df (patch) | |
tree | 798d15e6ad088a95e0d3ac8f387af837e5f5569f /src/main/scala | |
parent | ed711be7c21e64a5e79d4cb301959d35ef971141 (diff) | |
download | sbt-boilerplate-bd1a8a0c20996489e4f723a35b640562edf4b5df.tar.gz sbt-boilerplate-bd1a8a0c20996489e4f723a35b640562edf4b5df.tar.bz2 sbt-boilerplate-bd1a8a0c20996489e4f723a35b640562edf4b5df.zip |
Modify TemplateParser to reduce parser scope.
Template file to parse regions outside [# and #] as FixedString
not LiteralString
FixedString to match ##nnn... not ##n
Diffstat (limited to 'src/main/scala')
-rw-r--r-- | src/main/scala/spray/boilerplate/TemplateParser.scala | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/src/main/scala/spray/boilerplate/TemplateParser.scala b/src/main/scala/spray/boilerplate/TemplateParser.scala index ca6b329..d42ba98 100644 --- a/src/main/scala/spray/boilerplate/TemplateParser.scala +++ b/src/main/scala/spray/boilerplate/TemplateParser.scala @@ -32,16 +32,20 @@ object TemplateParser extends RegexParsers { def literal: Parser[LiteralString] = literalChars ^^ LiteralString - def fixed: Parser[FixedString] = "##" ~> ".".r ^^ (new String(_)) ^^ FixedString + def fixed: Parser[FixedString] = "##" ~> """\d+""".r ^^ (new String(_)) ^^ FixedString + + def outsideTemplate: Parser[FixedString]= """(?s).*?(?=(\[#)|(\z))""".r ^^ (FixedString(_)) def expand: Parser[Expand] = "[#" ~> elements ~ "#" ~ separatorChars <~ "]" ^^ { case els ~ x ~ sep => Expand(els, sep.getOrElse(", ")) } - + def embeddedTemplate:Parser[TemplateElement] = opt(outsideTemplate)~expand~opt(outsideTemplate) ^^ { + case before~ex~after => Sequence(Seq(before,Some(ex),after).flatten) + } def separatorChars: Parser[Option[String]] = rep("""[^\]]""".r) ^^ (_.reduceLeftOption(_ + _)) def parse(input:String): TemplateElement = - phrase(elements)(new scala.util.parsing.input.CharArrayReader(input.toCharArray)) match { + phrase(embeddedTemplate)(new scala.util.parsing.input.CharArrayReader(input.toCharArray)) match { case Success(res,_) => res case x:NoSuccess => throw new RuntimeException(x.msg) } @@ -50,7 +54,20 @@ object TemplateParser extends RegexParsers { object TestParser extends App { def check(format: String) { println(TemplateParser.parse(format)) + println("Template:\n"+format+"\n") + println("Generated Code:\n"+Generator.generateFromTemplate(format,5)) + println("-----End-----\n") } + check("""This text + |should not be parsed + |Tuple1 + | + [#Tuple1#] + | + |Product 1""".stripMargin) + check("[#abc ##1 # ++ ]") + check("[#abc Tuple##22 #]") + check("[#abc Tuple1 #]") }
\ No newline at end of file |