diff options
author | Johannes Rudolph <johannes.rudolph@gmail.com> | 2014-07-08 14:51:25 +0200 |
---|---|---|
committer | Johannes Rudolph <johannes.rudolph@gmail.com> | 2014-07-08 14:51:25 +0200 |
commit | 5f2a8a060191f839a7e632d23344a4337e80e877 (patch) | |
tree | deb55ff5176312da51c55089390f9816c8978c4f /src/main/scala/spray/boilerplate/TemplateParser.scala | |
parent | 6a0844a9be28ab845272cf194773bf6a2b99c552 (diff) | |
download | sbt-boilerplate-5f2a8a060191f839a7e632d23344a4337e80e877.tar.gz sbt-boilerplate-5f2a8a060191f839a7e632d23344a4337e80e877.tar.bz2 sbt-boilerplate-5f2a8a060191f839a7e632d23344a4337e80e877.zip |
saner parser and generation model
Diffstat (limited to 'src/main/scala/spray/boilerplate/TemplateParser.scala')
-rw-r--r-- | src/main/scala/spray/boilerplate/TemplateParser.scala | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/src/main/scala/spray/boilerplate/TemplateParser.scala b/src/main/scala/spray/boilerplate/TemplateParser.scala index 41d971b..f97a352 100644 --- a/src/main/scala/spray/boilerplate/TemplateParser.scala +++ b/src/main/scala/spray/boilerplate/TemplateParser.scala @@ -11,10 +11,10 @@ import java.lang.RuntimeException sealed trait TemplateElement case class Sequence(elements: Seq[TemplateElement]) extends TemplateElement -/** A string possibly containing patterns to replace */ +/** A literal string that shouldn't be changed */ case class LiteralString(literal: String) extends TemplateElement -/** A fixed string that shouldn't be changed */ -case class FixedString(literal: String) extends TemplateElement +/* An offset to be replaced by the current index */ +case class Offset(i: Int) extends TemplateElement /** A region in which to apply expansions */ case class Expand(inner: TemplateElement, separator: String) extends TemplateElement @@ -28,16 +28,16 @@ object TemplateParser extends RegexParsers { case several => Sequence(several) } - def element: Parser[TemplateElement] = literal | fixed | expand + def element: Parser[TemplateElement] = offset| literalString | expand - def literalChar: Parser[String] = """(?s:(?!\[#)(?!#[^\]]*\]).)""".r - def literalChars: Parser[String] = rep1(literalChar) ^^ { _.reduceLeft(_ + _) } + def offset: Parser[Offset] = "[012]".r ^^ (s => Offset(s.toInt)) + def literalString: Parser[LiteralString] = rep1(escapedLiteralNumber | literalChar) ^^ (chs => LiteralString(chs.mkString)) + def literalChar: Parser[Char] = + not("[#" | """#[^\]]*\]""".r | "[012]".r) ~> elem("Any character", _ => true) - def literal: Parser[LiteralString] = literalChars ^^ LiteralString + def escapedLiteralNumber: Parser[Char] = "##" ~> """[012]""".r ^^ (_.head) - def fixed: Parser[FixedString] = "##" ~> """\d+""".r ^^ (new String(_)) ^^ FixedString - - def outsideTemplate: Parser[FixedString]= """(?s).+?(?=(\[#)|(\z))""".r ^^ (FixedString(_)) + def outsideTemplate: Parser[LiteralString]= """(?s).+?(?=(\[#)|(\z))""".r ^^ (LiteralString(_)) def expand: Parser[Expand] = "[#" ~> elements ~ "#" ~ separatorChars <~ "]" ^^ { case els ~ x ~ sep => Expand(els, sep.getOrElse(", ")) |