diff options
author | Johannes Rudolph <johannes_rudolph@gmx.de> | 2012-10-16 14:24:54 +0200 |
---|---|---|
committer | Johannes Rudolph <johannes_rudolph@gmx.de> | 2012-10-16 14:24:54 +0200 |
commit | 3dc171e41676e9043fe0c6d935b97a944c143dad (patch) | |
tree | d4a9a441eef5502567f79270e136b2bde7e99ea1 /src/main/scala/spray/boilerplate/Generator.scala | |
parent | cbbca47a06609e4c523dfbf8498d4f0552f2aa20 (diff) | |
download | sbt-boilerplate-3dc171e41676e9043fe0c6d935b97a944c143dad.tar.gz sbt-boilerplate-3dc171e41676e9043fe0c6d935b97a944c143dad.tar.bz2 sbt-boilerplate-3dc171e41676e9043fe0c6d935b97a944c143dad.zip |
move from "cc.spray" to "io.spray"
Diffstat (limited to 'src/main/scala/spray/boilerplate/Generator.scala')
-rw-r--r-- | src/main/scala/spray/boilerplate/Generator.scala | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/scala/spray/boilerplate/Generator.scala b/src/main/scala/spray/boilerplate/Generator.scala new file mode 100644 index 0000000..f124691 --- /dev/null +++ b/src/main/scala/spray/boilerplate/Generator.scala @@ -0,0 +1,32 @@ +/* + * sbt-boilerplate is distributed under the 2-Clause BSD license. See the LICENSE file in the root + * of the repository. + * + * Copyright (c) 2012 Johannes Rudolph + */ +package spray.boilerplate + +import util.parsing.combinator.RegexParsers + +object Generator { + def generateFromTemplate(template: String, expandTo: Int): String = + generate(TemplateParser.parse(template))(expandTo) + + def formatNum0(num: Int) = num.formatted("%d") + def formatNumSpace(num: Int) = num.formatted("%d") + def replaceInPattern(pattern: String)(idx: Int): String = + // in likely identifiers replace by '04' etc. + pattern.replaceAll("(?<=\\w)(?<!\\d)(?<!##)1(?!\\d)", formatNum0(idx)) + .replaceAll("(?<=\\w)(?<!\\d)(?<!##)0(?!\\d)", formatNum0(idx - 1)) + + // in other places replace by ' 4' etc. + .replaceAll("(?<!\\w)(?<!\\d)(?<!##)1(?!\\d)", formatNumSpace(idx)) + .replaceAll("(?<!\\w)(?<!\\d)(?<!##)0(?!\\d)", formatNumSpace(idx - 1)) + + 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 LiteralString(lit) => replaceInPattern(lit)(idx) + case FixedString(lit) => lit + } +} |