PageRenderTime 65ms CodeModel.GetById 60ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 18package org.fusesource.scalate.converter
 19
 20import util.parsing.input.{ Positional, CharSequenceReader }
 21import org.fusesource.scalate.support.Text
 22import org.fusesource.scalate.InvalidSyntaxException
 23
 24sealed abstract class Expression extends Positional {
 25
 26  /**
 27   * Returns the text of an expression as a numeric method parameter
 28   */
 29  def asUnquotedParam: String
 30
 31  /**
 32   * Returns the text of an expression as a method parameter quoting String values
 33   */
 34  def asParam: String
 35
 36  /**
 37   * Returns the text of an expression as a method parameter
 38   */
 39  def asJsp: String
 40
 41}
 42
 43case class TextExpression(text: Text) extends Expression {
 44
 45  def asUnquotedParam = text.toString
 46
 47  def asParam = "\"" + text + "\""
 48
 49  def asJsp = text.toString
 50
 51}
 52
 53case class CompositeExpression(list: List[Expression]) extends Expression {
 54
 55  def asUnquotedParam = list.map(_.asUnquotedParam).mkString(" + ")
 56
 57  def asParam = list.map(_.asParam).mkString(" + ")
 58
 59  def asJsp = list.map(_.asJsp).mkString(" + ")
 60}
 61
 62case class DollarExpression(code: Text) extends Expression {
 63
 64  val toScala = ExpressionLanguage.asScala(code.toString)
 65
 66  def asUnquotedParam = toScala
 67
 68  def asParam = toScala
 69
 70  def asJsp = "${" + toScala + "}"
 71}
 72
 73/**
 74 * Parser for the JSTL EL expressions
 75 */
 76class ExpressionParser extends MarkupScanner {
 77
 78  override def skipWhitespace = false
 79
 80  def parseExpression(in: String): Expression = toExpression(phraseOrFail(expressionList, in))
 81
 82  private def phraseOrFail[T](p: Parser[T], in: String): T = {
 83    val x = phrase(p)(new CharSequenceReader(in))
 84    x match {
 85      case Success(result, _) => result
 86      case NoSuccess(message, next) => throw new InvalidSyntaxException(message, next.pos);
 87    }
 88  }
 89
 90  def toExpression(list: List[Expression]): Expression = {
 91    if (list.size == 1) {
 92      list(0)
 93    } else { CompositeExpression(list) }
 94  }
 95
 96  // grammar
 97  //-------------------------------------------------------------------------
 98
 99  def expressionList = rep(dollarExpression | staticText)
100
101  def staticText = someUpto("${") ^^ { TextExpression(_) }
102
103  val dollarExpression = wrapped("${", "}") ^^ { DollarExpression(_) }
104
105}