All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xgi.ut.dsl.ConversionConfigParsers.scala Maven / Gradle / Ivy

/*
 *   ScenarLang a domain specific language that attempts to make it even easier
 *   to write unit tests with mockito
 *    
 *   Copyright (C) 2012  X. Gillard 
 *   
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *   
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *   
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package xgi.ut.dsl

import scala.util.parsing.combinator.JavaTokenParsers

/**
 * This contains the grammar of an internal DSL meant to simplify our lives 
 * when it comes to configuring the parsing/conversion of some given types
 * these types are represented as strings in the "program" text
 */
trait ConversionConfigParsers extends CommentsSkippingParser {
  import ConversionConfigParsers._
  /* parses a conversion configuration */
  def conversion:Parser[ConversionConfig]="parse"~>stringLiteral~"with"~stringLiteral ^^ {
    case typeName~"with"~conversionClass => ConversionConfig(typeName.drop(1).dropRight(1), conversionClass.drop(1).dropRight(1))
  }
  /* parses an aliasing */
  def alias:Parser[AliasConfig]="alias"~stringLiteral~"with"~stringLiteral ^^ {
    case "alias"~typeName~"with"~aliasType => AliasConfig(typeName.drop(1).dropRight(1), aliasType.drop(1).dropRight(1))
  }
  
  def config:Parser[TypesConfig]= alias | conversion
  
  /* returns a conversion config that is maybe a syntax error*/
  def parseConfig(text:String):TypesConfig = parseAll(config, text) match {
    case Success(x, _) => x
    case x:NoSuccess => ConversionConfigSyntaxError(errorString(x))
  }
}

/* the companion object */
object ConversionConfigParsers {
  sealed abstract case class TypesConfig
  sealed case class ConversionConfig(typeName:String, parserClass:String) extends TypesConfig {
    override def toString = """parse "%s" with "%s"""".format(typeName, parserClass)
  }
  sealed case class AliasConfig(typeName:String, aliasType:String)        extends TypesConfig {
    override def toString = """alias "%s" with "%s"""".format(typeName, aliasType)
  }
  sealed case class ConversionConfigSyntaxError(mesg:String) 			  extends TypesConfig {
    override def toString = mesg
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy