/scalate-jsp-converter/src/main/scala/org/fusesource/scalate/converter/ExpressionParser.scala

http://github.com/scalate/scalate · Scala · 105 lines · 44 code · 30 blank · 31 comment · 3 complexity · 9b4abfee65e6b62d42c6b576f45f46ec MD5 · raw file

  1. /**
  2. * Copyright (C) 2009-2011 the original author or authors.
  3. * See the notice.md file distributed with this work for additional
  4. * information regarding copyright ownership.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.fusesource.scalate.converter
  19. import util.parsing.input.{ Positional, CharSequenceReader }
  20. import org.fusesource.scalate.support.Text
  21. import org.fusesource.scalate.InvalidSyntaxException
  22. sealed abstract class Expression extends Positional {
  23. /**
  24. * Returns the text of an expression as a numeric method parameter
  25. */
  26. def asUnquotedParam: String
  27. /**
  28. * Returns the text of an expression as a method parameter quoting String values
  29. */
  30. def asParam: String
  31. /**
  32. * Returns the text of an expression as a method parameter
  33. */
  34. def asJsp: String
  35. }
  36. case class TextExpression(text: Text) extends Expression {
  37. def asUnquotedParam = text.toString
  38. def asParam = "\"" + text + "\""
  39. def asJsp = text.toString
  40. }
  41. case class CompositeExpression(list: List[Expression]) extends Expression {
  42. def asUnquotedParam = list.map(_.asUnquotedParam).mkString(" + ")
  43. def asParam = list.map(_.asParam).mkString(" + ")
  44. def asJsp = list.map(_.asJsp).mkString(" + ")
  45. }
  46. case class DollarExpression(code: Text) extends Expression {
  47. val toScala = ExpressionLanguage.asScala(code.toString)
  48. def asUnquotedParam = toScala
  49. def asParam = toScala
  50. def asJsp = "${" + toScala + "}"
  51. }
  52. /**
  53. * Parser for the JSTL EL expressions
  54. */
  55. class ExpressionParser extends MarkupScanner {
  56. override def skipWhitespace = false
  57. def parseExpression(in: String): Expression = toExpression(phraseOrFail(expressionList, in))
  58. private def phraseOrFail[T](p: Parser[T], in: String): T = {
  59. val x = phrase(p)(new CharSequenceReader(in))
  60. x match {
  61. case Success(result, _) => result
  62. case NoSuccess(message, next) => throw new InvalidSyntaxException(message, next.pos);
  63. }
  64. }
  65. def toExpression(list: List[Expression]): Expression = {
  66. if (list.size == 1) {
  67. list(0)
  68. } else { CompositeExpression(list) }
  69. }
  70. // grammar
  71. //-------------------------------------------------------------------------
  72. def expressionList = rep(dollarExpression | staticText)
  73. def staticText = someUpto("${") ^^ { TextExpression(_) }
  74. val dollarExpression = wrapped("${", "}") ^^ { DollarExpression(_) }
  75. }