aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md5
-rw-r--r--src/main/scala/spray/boilerplate/TemplateParser.scala23
2 files changed, 25 insertions, 3 deletions
diff --git a/README.md b/README.md
index 8c94aaa..d3fdccb 100644
--- a/README.md
+++ b/README.md
@@ -91,6 +91,11 @@ appear in `src/main/boilerplate` with the `.template` extension stripped off.
* The maximum number of arguments, 22, is hard-coded.
* Instances for 0 arguments have to be supplied manually.
+## Projects using sbt-boilerplate
+
+ * [spray-routing](http://github.com/spray/spray) uses sbt-boilerplate to provide conversions to/from HLists
+ * [product-collections](https://github.com/marklister/product-collections)
+
## License
Copyright (c) 2012 Johannes Rudolph
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