package scala.dbc2.syntax import DBC2Tokens._ import TokenVal._ import scala.dbc2.statement.AnyValueDef._ import scala.util.parsing.combinator.Parsers /** That trait provide some method used in the SQLParser. */ trait PartialParser extends Parsers { type Elem = Token implicit def tokenValueToParser(tok: TokenValue): Parser[Unit] = new Parser[Unit] { def apply(in: Input) = { if (in.first == token(tok)) Success((), in.rest) else Failure("`" + token(tok) + "' expected but " + in.first + " found", in) } } /** Parser that check that the token is a number. */ def numericLit: Parser[AnyVal] = elem("number", _ match { case DBC2Token(tok, _) => {tok match { case SMALLINT | INTEGER | BIGINT | REAL | DOUBLE => true case _ => false }} case _ => false }) ^^ (n => (n: @unchecked) match { case DBC2Token(_, Some(AnyValue(_, Some(va)))) => va }) /** Parser that check that the token is a string. */ def string: Parser[String] = elem("identifier", _ match {case DBC2Token(STRING, _) => true case _ => false}) ^^ (s => (s: @unchecked) match { case DBC2Token(_, Some(AnyValue(Some(va), _))) => va }) /** Parser that check that the token is a symbol (used to represent indentifier). */ def ident: Parser[String] = elem("identifier", _ match {case DBC2Token(SYMBOL, _) => true case _ => false}) ^^ (s => (s: @unchecked) match { case DBC2Token(_, Some(AnyValue(Some(va), _))) => va }) /** Parser that check that the token is a smallint. */ val smallintDef: Parser[Short] = elem("smallint", _ match {case DBC2Token(SMALLINT, _) => true case _ => false}) ^^ (e => (e: @unchecked) match { case DBC2Token(_, Some(AnyValue(None, Some(i)))) => i.asInstanceOf[Short] }) /** Parser that check that the token is an integer. */ val intDef: Parser[Int] = elem("integer", _ match {case DBC2Token(INTEGER, _) => true case _ => false}) ^^ (e => (e: @unchecked) match { case DBC2Token(_, Some(AnyValue(None, Some(i)))) => i.asInstanceOf[Int] }) /** Parser that check that the token is a bigint. */ val bigintDef: Parser[Long] = elem("bigint", _ match {case DBC2Token(BIGINT, _) => true case _ => false}) ^^ (e => (e: @unchecked) match { case DBC2Token(_, Some(AnyValue(None, Some(i)))) => i.asInstanceOf[Long] }) /** Parser that check that the token is a real. */ val realDef: Parser[Float] = elem("real", _ match {case DBC2Token(REAL, _) => true case _ => false}) ^^ (e => (e: @unchecked) match { case DBC2Token(_, Some(AnyValue(None, Some(i)))) => i.asInstanceOf[Float] }) /** Parser that check that the token is a double precision. */ val doubleDef: Parser[Double] = elem("double", _ match {case DBC2Token(DOUBLE, _) => true case _ => false}) ^^ (e => (e: @unchecked) match { case DBC2Token(_, Some(AnyValue(None, Some(i)))) => i.asInstanceOf[Double] }) }