diff options
author | Johannes Rudolph <johannes.rudolph@gmail.com> | 2014-09-01 11:51:32 +0200 |
---|---|---|
committer | Johannes Rudolph <johannes.rudolph@gmail.com> | 2014-09-01 11:51:32 +0200 |
commit | 070b0b07170788d78c41cb59e71f624ca6f03266 (patch) | |
tree | 095ca4623f315989de40b4b61d1eccd345bff138 /src/main/scala | |
parent | ead8d3df2e155762fc426b8d3fcd7d97af83a235 (diff) | |
download | sbt-boilerplate-070b0b07170788d78c41cb59e71f624ca6f03266.tar.gz sbt-boilerplate-070b0b07170788d78c41cb59e71f624ca6f03266.tar.bz2 sbt-boilerplate-070b0b07170788d78c41cb59e71f624ca6f03266.zip |
cache parser rules for better performance
Diffstat (limited to 'src/main/scala')
-rw-r--r-- | src/main/scala/spray/boilerplate/TemplateParser.scala | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/src/main/scala/spray/boilerplate/TemplateParser.scala b/src/main/scala/spray/boilerplate/TemplateParser.scala index 3e20fb1..857b1cb 100644 --- a/src/main/scala/spray/boilerplate/TemplateParser.scala +++ b/src/main/scala/spray/boilerplate/TemplateParser.scala @@ -38,35 +38,35 @@ object TemplateParser extends RegexParsers { val EOI = 26.toChar - def elements: Parser[TemplateElement] = rep1(element) ^^ maybeSequence - def element: Parser[TemplateElement] = offset | literalString | expand + lazy val elements: Parser[TemplateElement] = rep1(element) ^^ maybeSequence + lazy val element: Parser[TemplateElement] = offset | literalString | expand - def offset: Parser[Offset] = offsetChars ^^ (s ⇒ Offset(s.toInt)) - def literalString: Parser[LiteralString] = rep1(escapedLiteralNumber | literalChar) ^^ (chs ⇒ LiteralString(chs.mkString)) - def literalChar: Parser[Char] = + lazy val offset: Parser[Offset] = offsetChars ^^ (s ⇒ Offset(s.toInt)) + lazy val literalString: Parser[LiteralString] = rep1(escapedLiteralNumber | literalChar) ^^ (chs ⇒ LiteralString(chs.mkString)) + lazy val literalChar: Parser[Char] = not(expandStart | """#[^\]]*\]""".r | offsetChars) ~> elem("Any character", _ != EOI) - def offsetChars = "[012]".r + lazy val offsetChars = "[012]".r - def escapedLiteralNumber: Parser[Char] = "##" ~> offsetChars ^^ (_.head) + lazy val escapedLiteralNumber: Parser[Char] = "##" ~> offsetChars ^^ (_.head) - def outsideLiteralString: Parser[LiteralString] = rep1(outsideLiteralChar) ^^ (chs ⇒ LiteralString(chs.mkString)) - def outsideLiteralChar: Parser[Char] = not(expandStart | """#[^\]]*\]""".r) ~> elem("Any character", _ != EOI) + lazy val outsideLiteralString: Parser[LiteralString] = rep1(outsideLiteralChar) ^^ (chs ⇒ LiteralString(chs.mkString)) + lazy val outsideLiteralChar: Parser[Char] = not(expandStart | """#[^\]]*\]""".r) ~> elem("Any character", _ != EOI) - def expand: Parser[Expand] = expandStart ~ elements ~ "#" ~ separatorChars <~ "]" ^^ { + lazy val expand: Parser[Expand] = expandStart ~ elements ~ "#" ~ separatorChars <~ "]" ^^ { case range ~ els ~ x ~ sep ⇒ Expand(els, sep.getOrElse(Expand.defaultSeparator), range) } - def expandStart: Parser[Range] = "[" ~> range <~ "#" + lazy val expandStart: Parser[Range] = "[" ~> range <~ "#" - def range: Parser[Range] = + lazy val 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] = + lazy val outsideElements: Parser[TemplateElement] = rep1(expand | outsideLiteralString) ^^ maybeSequence - def separatorChars: Parser[Option[String]] = rep("""[^\]]""".r) ^^ (_.reduceLeftOption(_ + _)) + lazy val separatorChars: Parser[Option[String]] = rep("""[^\]]""".r) ^^ (_.reduceLeftOption(_ + _)) def maybeSequence(els: Seq[TemplateElement]): TemplateElement = els match { case one :: Nil ⇒ one |