diff options
author | Johannes Rudolph <johannes.rudolph@gmail.com> | 2014-07-08 15:19:20 +0200 |
---|---|---|
committer | Johannes Rudolph <johannes.rudolph@gmail.com> | 2014-07-08 15:19:20 +0200 |
commit | df2ad7c83b8cc1ef00e1e0fc446121879057d77d (patch) | |
tree | 80d021b86b02f58a4294af5a1c92d3f24ccc27db /src/main/scala | |
parent | 79587ca29b48b9c62dfbcaa6ad3318f623e156a6 (diff) | |
download | sbt-boilerplate-df2ad7c83b8cc1ef00e1e0fc446121879057d77d.tar.gz sbt-boilerplate-df2ad7c83b8cc1ef00e1e0fc446121879057d77d.tar.bz2 sbt-boilerplate-df2ad7c83b8cc1ef00e1e0fc446121879057d77d.zip |
add support for custom ranges
Diffstat (limited to 'src/main/scala')
-rw-r--r-- | src/main/scala/spray/boilerplate/Generator.scala | 8 | ||||
-rw-r--r-- | src/main/scala/spray/boilerplate/TemplateParser.scala | 21 |
2 files changed, 21 insertions, 8 deletions
diff --git a/src/main/scala/spray/boilerplate/Generator.scala b/src/main/scala/spray/boilerplate/Generator.scala index b115002..5576f56 100644 --- a/src/main/scala/spray/boilerplate/Generator.scala +++ b/src/main/scala/spray/boilerplate/Generator.scala @@ -13,9 +13,9 @@ object Generator { generate(TemplateParser.parse(template))(expandTo) def generate(format: TemplateElement)(idx: Int): String = format match { - case Sequence(els @ _*) ⇒ els.map(e ⇒ generate(e)(idx)).mkString - case Expand(inner, sep) ⇒ (1 to idx).map(generate(inner)).mkString(sep) - case Offset(i) ⇒ (idx + i - 1).toString - case LiteralString(lit) ⇒ lit + case Sequence(els @ _*) ⇒ els.map(e ⇒ generate(e)(idx)).mkString + case Expand(inner, sep, range) ⇒ (range.start.getOrElse(1) to range.end.getOrElse(idx)).map(generate(inner)).mkString(sep) + case Offset(i) ⇒ (idx + i - 1).toString + case LiteralString(lit) ⇒ lit } } diff --git a/src/main/scala/spray/boilerplate/TemplateParser.scala b/src/main/scala/spray/boilerplate/TemplateParser.scala index 6d8e22d..8808bc8 100644 --- a/src/main/scala/spray/boilerplate/TemplateParser.scala +++ b/src/main/scala/spray/boilerplate/TemplateParser.scala @@ -19,8 +19,14 @@ case class Sequence(elements: TemplateElement*) extends TemplateElement { case class LiteralString(literal: String) extends TemplateElement /* An offset to be replaced by the current index */ case class Offset(i: Int) extends TemplateElement + +case class Range(start: Option[Int] = None, end: Option[Int] = None) + /** A region in which to apply expansions */ -case class Expand(inner: TemplateElement, separator: String = Expand.defaultSeparator) extends TemplateElement +case class Expand( + inner: TemplateElement, + separator: String = Expand.defaultSeparator, + range: Range = Range(None, None)) extends TemplateElement object Expand { val defaultSeparator = ", " } @@ -36,7 +42,7 @@ object TemplateParser extends RegexParsers { def offset: Parser[Offset] = offsetChars ^^ (s ⇒ Offset(s.toInt)) def literalString: Parser[LiteralString] = rep1(escapedLiteralNumber | literalChar) ^^ (chs ⇒ LiteralString(chs.mkString)) def literalChar: Parser[Char] = - not("[#" | """#[^\]]*\]""".r | offsetChars) ~> elem("Any character", _ ⇒ true) + not(expandStart | """#[^\]]*\]""".r | offsetChars) ~> elem("Any character", _ ⇒ true) def offsetChars = "[012]".r @@ -44,9 +50,16 @@ object TemplateParser extends RegexParsers { def outsideTemplate: Parser[LiteralString] = """(?s).+?(?=(\[#)|(\z))""".r ^^ (LiteralString(_)) - def expand: Parser[Expand] = "[#" ~> elements ~ "#" ~ separatorChars <~ "]" ^^ { - case els ~ x ~ sep ⇒ Expand(els, sep.getOrElse(Expand.defaultSeparator)) + def expand: Parser[Expand] = expandStart ~ elements ~ "#" ~ separatorChars <~ "]" ^^ { + case range ~ els ~ x ~ sep ⇒ Expand(els, sep.getOrElse(Expand.defaultSeparator), range) } + def expandStart: Parser[Range] = "[" ~> range <~ "#" + + def range: Parser[Range] = + (opt("""\d{1,2}""".r) ~ """\s*\.\.\s*""".r ~ opt("""\d{1,2}""".r) ^^ { + case start ~ sep ~ end ⇒ Range(start.map(_.toInt), end.map(_.toInt)) + }) | success(Range()) + def outsideElements: Parser[TemplateElement] = rep1(expand | outsideTemplate) ^^ maybeSequence |