scala.util.parsing.combinator.JavaTokenParsers Scala Examples

The following examples show how to use scala.util.parsing.combinator.JavaTokenParsers. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: RDataSource.scala    From ScalaStan   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.cibo.scalastan.data

import java.io.FileReader
import scala.util.parsing.combinator.JavaTokenParsers

object RDataSource {

  private object RDataParser extends JavaTokenParsers {

    override protected val whiteSpace = """(\s|#.*)+""".r

    private def value: Parser[String] = floatingPointNumber | (stringLiteral ^^ { s => s.tail.dropRight(1) })

    private def valueList: Parser[Vector[String]] = repsep(value, ",") ^^ { _.toVector }

    private def label: Parser[String] = (stringLiteral ^^ { s => s.tail.dropRight(1) }) | ident

    private def vector: Parser[Vector[String]] = "c" ~> "(" ~ valueList ~ ")" ^^ { case _ ~ ns ~ _ => ns }

    private def structure: Parser[(Vector[Int], Vector[String])] =
      "structure" ~> "(" ~ vector ~ "," ~ ".Dim" ~ "=" ~ vector ~ ")" ^^ { case _ ~ vs ~ _ ~ _ ~ _ ~ ds ~ _ =>
        (ds.map(_.toInt), vs)
      }

    private def assignment: Parser[String] = "<-" | "="

    private def scalarValue: Parser[DataValue] = label ~ assignment ~ value ^^ { case name ~ _ ~ v =>
      DataValue(name, Vector.empty, Vector(v))
    }

    private def vectorValue: Parser[DataValue] = label ~ assignment ~ vector ^^ { case name ~ _ ~ vs =>
      DataValue(name, Vector(vs.length), vs)
    }

    private def structureValue: Parser[DataValue] = label ~ assignment ~ structure ^^ { case name ~ _ ~ v =>
      DataValue(name, v._1, v._2)
    }

    private def statement: Parser[DataValue] = (scalarValue | vectorValue | structureValue) <~ opt(";")

    private def statements: Parser[Seq[DataValue]] = statement.*

    def parse(s: String): ParseResult[Seq[DataValue]] = parseAll(statements, s)

    def parseFile(fileName: String): ParseResult[Seq[DataValue]] = parseAll(statements, new FileReader(fileName))
  }

  def fromString(content: String): DataSource = {
    val values = RDataParser.parse(content) match {
      case RDataParser.Success(vs, _)  => vs
      case RDataParser.Error(msg, _)   => throw new IllegalArgumentException(s"error parsing string: $msg")
      case RDataParser.Failure(msg, _) => throw new IllegalArgumentException(s"error parsing string: $msg")
    }
    DataSource(values)
  }

  def fromFile(fileName: String): DataSource = {
    val values = RDataParser.parseFile(fileName) match {
      case RDataParser.Success(vs, _)  => vs
      case RDataParser.Error(msg, _)   => throw new IllegalArgumentException(s"error parsing $fileName: $msg")
      case RDataParser.Failure(msg, _) => throw new IllegalArgumentException(s"error parsing $fileName: $msg")
    }
    DataSource(values)
  }
} 
Example 2
Source File: GroupParser.scala    From seqspark   with Apache License 2.0 5 votes vote down vote up
package org.dizhang.seqspark.util

import org.dizhang.seqspark.util.GroupParser._

import scala.util.parsing.combinator.JavaTokenParsers

class GroupParser extends JavaTokenParsers {

  def expr: Parser[GroupExpr] = term ~ rep("and" ~ term) ^^ {
    case t ~ ts => ts.foldLeft(t){
      case (a, _ ~ b) => And(a, b)
    }
  }

  def term: Parser[GroupExpr] = """\w+""".r~"."~"""\w+""".r ^^ {
    case "samples" ~ _ ~ key => SampleGroup(key)
    case "variants" ~ _ ~ key => VariantGroup(key)
    case _ => Empty
  } | samples

  def samples: Parser[GroupExpr] = "samples" ^^ {_ => Samples}

  def parse(input: String): ParseResult[GroupExpr] = this.parseAll(expr, input)

}

object GroupParser {

  def parse(input: String): GroupExpr = {
    if (input.isEmpty) Empty else new GroupParser().parse(input).get
  }

  

  def toMap(grpExpr: GroupExpr): Map[String, Set[String]] = {
    grpExpr match {
      case Empty => Map[String, Set[String]]()
      case Samples => Map("bySamples" -> Set("true"))
      case SampleGroup(k) => Map("samples" -> Set(k))
      case VariantGroup(k) => Map("variants" -> Set(k))
      case And(g1, g2) =>
        val m1 = toMap(g1)
        val m2 = toMap(g2)
        m1 ++ (for ((k, v) <- m2) yield if (m1.contains(k)) k -> (m1(k) ++ v) else k -> v)
    }
  }

  sealed trait GroupExpr
  case object Empty extends GroupExpr
  case object Samples extends GroupExpr
  case class SampleGroup(name: String) extends GroupExpr
  case class VariantGroup(name: String) extends GroupExpr
  case class And(grp1: GroupExpr, grp2: GroupExpr) extends GroupExpr

} 
Example 3
Source File: GremlinParser.scala    From vm   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.mmadt.language.gremlin
import org.mmadt.VmException
import org.mmadt.language.LanguageException
import org.mmadt.language.obj._
import org.mmadt.language.obj.`type`.__
import org.mmadt.storage.StorageFactory._

import scala.util.matching.Regex
import scala.util.parsing.combinator.JavaTokenParsers

class GremlinParser extends JavaTokenParsers {

  override val whiteSpace: Regex = """[\s\n]+""".r
  override def decimalNumber: Parser[String] = """-?\d+\.\d+""".r

  // all mm-ADT languages must be able to accept a string representation of an expression in the language and return an Obj
  private def parse[O <: Obj](input: String): O = {
    this.parseAll(expr, input.trim) match {
      case Success(result, _) => (result `,`).asInstanceOf[O]
      case NoSuccess(y) => throw LanguageException.parseError(
        y._1,
        y._2.source.toString,
        y._2.pos.line.asInstanceOf[java.lang.Integer],
        y._2.pos.column.asInstanceOf[java.lang.Integer])
    }
  }

  lazy val expr: Parser[Obj] = rep1sep(step, opt(".")) ^^ (x => {
    x.flatten.foldLeft[Obj](new __())((a, b) => b.exec(a))
  })

  lazy val aobj: Parser[Obj] = astr | abool | aint | astr
  lazy val abool: Parser[Bool] = ("true" | "false") ^^ (x => bool(x.equals("true")))
  lazy val aint: Parser[Int] = wholeNumber ^^ (x => int(x.toLong))
  lazy val astr: Parser[Str] = """'([^'\x00-\x1F\x7F\\]|\\[\\'"bfnrt]|\\u[a-fA-F0-9]{4})*'""".r ^^ (x => str(x.subSequence(1, x.length - 1).toString))


  lazy val step: Parser[List[Inst[Obj, Obj]]] = "[a-zA-Z]+".r ~ ("(" ~> repsep(aobj, ",") <~ ")") ^^ (x => TraversalMonoid.resolve(x._1, x._2))
}
object GremlinParser {
  def parse[O <: Obj](script: String): O = try {
    new GremlinParser().parse[O](script)
  } catch {
    case e: VmException => throw e
    case e: Exception => {
      e.printStackTrace()
      throw new LanguageException(e.getMessage)
    }
  }
} 
Example 4
Source File: ExpressionFilterParser.scala    From NGSI-LD_Experimental   with MIT License 5 votes vote down vote up
package utils

import scala.util.parsing.combinator.JavaTokenParsers

class ExpressionFilterParser(ldContext:Map[String,String]) extends JavaTokenParsers {

  def value: Parser[String] =
    decimalNumber ^^ (x => { x.toString }) |
      stringLiteral ^^ (x => { x.toString }) |
      floatingPointNumber ^^ (x => { x.toString }) |
      wholeNumber ^^ (x => { x.toString })

  def attrName: Parser[String] =
    """[[A-Z][a-z][0-9]]+""".r ^^ (x => {
      ldContext.getOrElse(x,x)
    })

  def operator: Parser[String] =
    ("<=" | ">=" | ">" | "<" | "==" | "!=") ^^ {
      x=> x
    }

  def expr: Parser[String] =
    attrName ~ operator ~ value ^^ {
      case attrName ~ operator ~ value => attrName + operator + value
    }

  def fullExpr: Parser[String] =
    repsep(expr, ";") ^^ (x => {
      x.mkString(";")
    })
} 
Example 5
Source File: JSONParser.scala    From NGSI-LD_Experimental   with MIT License 5 votes vote down vote up
package json

import scala.util.parsing.combinator.JavaTokenParsers



class JSONParser extends JavaTokenParsers {

  def obj: Parser[Map[String, Any]] =
    "{"~> repsep(member, ",") <~"}" ^^ (Map() ++ _)

  def arr: Parser[List[Any]] =
    "["~> repsep(value, ",") <~"]"

  def member: Parser[(String, Any)] =
    stringLiteral~":"~value ^^ {
      case name~":"~value => {
        (name.substring(1,name.length -1), value)
      }
    }

  def value: Parser[Any] = (
    obj
      | arr
      | stringLiteral ^^ (x => {
        // Remove quotes
        x.substring(1, x.length - 1)
      })
      | decimalNumber ^^ (_.toDouble)
      | floatingPointNumber ^^ (_.toDouble)
      | wholeNumber ^^ (_.toLong)
      | "null"  ^^ (x => null)
      | "true"  ^^ (x => true)
      | "false" ^^ (x => false)
    )
} 
Example 6
Source File: BadParser.scala    From CSYE7200_Old   with MIT License 5 votes vote down vote up
package edu.neu.coe.csye7200.parse

import scala.util.parsing.combinator.JavaTokenParsers


class BadParser extends JavaTokenParsers {
  def name: Parser[(String, Option[String], String)] = ident ~ opt(ident) ~ ident ^^ { case f ~ mo ~ l => (f, mo, l) }
}

object BadParser extends App {
  val p = new BadParser
  val r = p.parseAll(p.name, "Martin Scala Odersky")
  val first = r match {
    case p.Success((f, _, _), _) => f
    case _ => throw new RuntimeException("problem")
  }
  println(first)
} 
Example 7
Source File: StatusTrackingFormat.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.common.formats

import cmwell.common.StatusTracking
import scala.util.{Try, Success => USuccess, Failure => UFailure}

import scala.util.parsing.combinator.JavaTokenParsers

object StatusTrackingFormat extends JavaTokenParsers {

  val restOfTheInputAsString = Parser { in =>
    val source = in.source
    val offset = in.offset
    val length = source.length()
    Success(source.subSequence(offset, length).toString, in.drop(length - offset))
  }

  val maxIntString = Int.MaxValue.toString
  val nonNegativeUnpaddedIntNumber: Parser[Int] =
    """\d+""".r ^? ({
      case s if s.length < maxIntString.length || s <= maxIntString => s.toInt
    }, { s =>
      s"'$s' is not a positive valid int (it is greater than the max int value [$maxIntString])"
    })

  val statusTracking = opt(nonNegativeUnpaddedIntNumber <~ ',') ~ restOfTheInputAsString ^^ {
    case Some(n) ~ t => StatusTracking(t, n)
    case None ~ t    => StatusTracking(t, 1)
  }

  def parseTrackingStatus(input: String): Try[StatusTracking] = parseAll(statusTracking, input) match {
    case Success(x, _)     => USuccess(x)
    case NoSuccess(msg, _) => UFailure(new ParsingException(msg))
  }
}

class ParsingException(msg: String) extends IllegalArgumentException(msg) 
Example 8
Source File: ComposedMetricConnection.scala    From DataQuality   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package models.metrics

import models.AppDB
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.annotations.Column

import scala.collection.immutable.Seq
import scala.util.matching.Regex
import scala.util.parsing.combinator.JavaTokenParsers
^]".r ~ factor) ^^ {
    case t ~ ts => ts.foldLeft(t) {
      case (t1, "*" ~ t2) => Mul(t1, t2)
      case (t1, "/" ~ t2) => Div(t1, t2)
      case (t1, "^" ~ t2) => Pow(t1, t2)
    }
  }

  lazy val factor = "(" ~> expr <~ ")" | num | met
  lazy val num = floatingPointNumber ^^ { t => Num(t.toDouble) }
  lazy val met = rgx ^^ { t => Met(t.toString)}

} 
Example 9
Source File: ComposedMetricConnection.scala    From DataQuality   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package dbmodel.metrics

import dbmodel.AppDB
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.annotations.Column

import scala.collection.immutable.Seq
import scala.util.matching.Regex
import scala.util.parsing.combinator.JavaTokenParsers
^]".r ~ factor) ^^ {
    case t ~ ts => ts.foldLeft(t) {
      case (t1, "*" ~ t2) => Mul(t1, t2)
      case (t1, "/" ~ t2) => Div(t1, t2)
      case (t1, "^" ~ t2) => Pow(t1, t2)
    }
  }

  lazy val factor = "(" ~> expr <~ ")" | num | met
  lazy val num = floatingPointNumber ^^ { t => Num(t.toDouble) }
  lazy val met = rgx ^^ { t => Met(t.toString)}

} 
Example 10
Source File: BadParser.scala    From CSYE7200   with MIT License 5 votes vote down vote up
package edu.neu.coe.csye7200.parse

import scala.util.parsing.combinator.JavaTokenParsers


class BadParser extends JavaTokenParsers {
  def name: Parser[(String, Option[String], String)] = ident ~ opt(ident) ~ ident ^^ { case f ~ mo ~ l => (f, mo, l) }
}

object BadParser extends App {
  val p = new BadParser
  val r = p.parseAll(p.name, "Martin Scala Odersky")
  val first = r match {
    case p.Success((f, _, _), _) => f
    case _ => throw new RuntimeException("problem")
  }
  println(first)
} 
Example 11
Source File: ScalaUDFParser.scala    From piglet   with Apache License 2.0 5 votes vote down vote up
package dbis.piglet.udf

import dbis.piglet.op.PigOperator
import dbis.piglet.schema._

import scala.util.parsing.combinator.JavaTokenParsers
import scala.util.parsing.input.CharSequenceReader
import scala.language.postfixOps


class ScalaUDFParser extends JavaTokenParsers {
  def scalaType: Parser[PigType] = (
      "Int" ^^{ t => Types.IntType }
        | "Double" ^^{ t => Types.DoubleType }
        | "String" ^^{ t => Types.CharArrayType }
        | "Any" ^^{ t => Types.AnyType }
        | "Boolean" ^^{ t => Types.BooleanType }
        | "Long" ^^{  t => Types.LongType }
        | scalaTupleType
    )
  def scalaTupleType: Parser[PigType] = "(" ~ rep1sep(scalaType, ",") ~ ")" ^^{
    case _ ~ tlist ~ _ => val fields = tlist.zipWithIndex.map{case (t, p) => Field(s"_${p+1}", t)}.toArray
                          TupleType(fields)
  }

  def paramDecl: Parser[PigType] = ident ~ ":" ~ scalaType ^^{ case _ ~ _ ~ t => t }
  def paramList: Parser[List[PigType]] = "(" ~ repsep(paramDecl, ",") ~ ")" ^^{ case _ ~ t ~ _ => t }
  def resultTypeDecl: Parser[PigType] = scalaType ^^{ t => t }

  def udfDef: Parser[UDF] = "def" ~ ident ~ (paramList?) ~ ":" ~ resultTypeDecl ~ "=" ^^{
    case _ ~ id ~ params ~ _ ~ res ~ _  => UDF(id.toUpperCase, id, params.getOrElse(List()), res, false)
  }

  def parseDef(input: CharSequenceReader): UDF = {
    parsePhrase(input) match {
      case Success(t, _) => t
      case NoSuccess(msg, next) =>
        throw new IllegalArgumentException(s"Could not parse input string:\n${next.pos.longString} => $msg")
    }
  }

  def parsePhrase(input: CharSequenceReader): ParseResult[UDF] = phrase(udfDef)(input)
} 
Example 12
Source File: Helpers.scala    From troy   with Apache License 2.0 5 votes vote down vote up
package troy
package cql.parser

import scala.util.parsing.combinator.JavaTokenParsers

trait Helpers {
  this: JavaTokenParsers =>

  def parenthesis[A](p: Parser[A]) =
    around(p, "(", ")")

  def curlyBraces[A](p: Parser[A]) =
    around(p, "{", "}")

  def squareBrackets[A](p: Parser[A]) =
    around(p, "[", "]")

  def around[A](p: Parser[A], prefix: Parser[_], postfix: Parser[_]) =
    prefix ~> p <~ postfix

  def getOrElse[A](p: Parser[A], a: A) =
    p.?.map(_.getOrElse(a))

  implicit class orElse[A](p: Parser[A]) {
    def orElse[B >: A](default: => B): Parser[B] = p.? ^^ (_ getOrElse default)
  }

  implicit class orEmpty[A](p: Parser[Seq[A]]) {
    def orEmpty: Parser[Seq[A]] = p orElse Seq.empty
  }

  implicit class asSeq[A](p: Parser[A]) {
    def asSeq: Parser[Seq[A]] = p ^^ { case x => Seq(x) }
  }

  implicit class as2[A, B](t: Parser[A ~ B]) {
    def ^^^^[T](co: (A, B) => T) = t map { tt => val (a ~ b) = tt; co(a, b) }
  }

  implicit class as3[A, B, C](t: Parser[A ~ B ~ C]) {
    def ^^^^[T](co: (A, B, C) => T) = t map { tt => val (a ~ b ~ c) = tt; co(a, b, c) }
  }

  implicit class as4[A, B, C, D](t: Parser[A ~ B ~ C ~ D]) {
    def ^^^^[T](co: (A, B, C, D) => T) = t map { tt => val (a ~ b ~ c ~ d) = tt; co(a, b, c, d) }
  }

  implicit class as5[A, B, C, D, E](t: Parser[A ~ B ~ C ~ D ~ E]) {
    def ^^^^[T](co: (A, B, C, D, E) => T) = t map { tt => val (a ~ b ~ c ~ d ~ e) = tt; co(a, b, c, d, e) }
  }

  implicit class as6[A, B, C, D, E, F](t: Parser[A ~ B ~ C ~ D ~ E ~ F]) {
    def ^^^^[T](co: (A, B, C, D, E, F) => T) = t map { tt => val (a ~ b ~ c ~ d ~ e ~ f) = tt; co(a, b, c, d, e, f) }
  }

  implicit class as7[A, B, C, D, E, F, G](t: Parser[A ~ B ~ C ~ D ~ E ~ F ~ G]) {
    def ^^^^[T](co: (A, B, C, D, E, F, G) => T) = t map { tt => val (a ~ b ~ c ~ d ~ e ~ f ~ g) = tt; co(a, b, c, d, e, f, g) }
  }

  implicit class as8[A, B, C, D, E, F, G, H](t: Parser[A ~ B ~ C ~ D ~ E ~ F ~ G ~ H]) {
    def ^^^^[T](co: (A, B, C, D, E, F, G, H) => T) = t map { tt => val (a ~ b ~ c ~ d ~ e ~ f ~ g ~ h) = tt; co(a, b, c, d, e, f, g, h) }
  }

} 
Example 13
Source File: MathExpScanner.scala    From math-expression-parser   with MIT License 5 votes vote down vote up
package io.github.facaiy.math.expression.compiler.scanner

import scala.util.parsing.combinator.JavaTokenParsers

import io.github.facaiy.math.expression._


object MathExpScanner extends JavaTokenParsers {
  def add: Parser[Operator] = ADD.toString ^^ (_ => ADD)
  def minus: Parser[Operator] = MINUS.toString ^^ (_ => MINUS)
  def multiply: Parser[Operator] = MULTIPLY.toString ^^ (_ => MULTIPLY)
  def divide: Parser[Operator] = DIVIDE.toString ^^ (_ => DIVIDE)
  def power: Parser[Operator] = POWER.toString ^^ (_ => POWER)

  def comma: Parser[Delimiter] = COMMA.toString ^^ (_ => COMMA)
  def leftParenthesis: Parser[Delimiter] = LEFT_PARENTHESIS.toString ^^ (_ => LEFT_PARENTHESIS)
  def rightParenthesis: Parser[Delimiter] = RIGHT_PARENTHESIS.toString ^^ (_ => RIGHT_PARENTHESIS)

  def number: Parser[NUMBER] = floatingPointNumber ^^ (x => NUMBER(x.toDouble))

  def variable: Parser[VAR_NAME] = "$" ~ ident ^^ {
    case _ ~ n => VAR_NAME(n)
  }

  def function: Parser[FUNC_NAME] = ident ^^ (n => FUNC_NAME(n))

  def tokens: Parser[List[MathExpToken]] = {
    phrase(rep1(add | power | multiply | divide |
                comma | leftParenthesis | rightParenthesis |
                number |
                minus |                   // 负号与减号冲突
                variable | function))
  }

  def apply(expression: String): Either[MathExpScannerError, List[MathExpToken]] =
    parse(tokens, expression) match {
      case NoSuccess(msg, next) => Left(MathExpScannerError(msg))
      case Success(result, next) => Right(result)
    }
} 
Example 14
Source File: JsonExpressionParser.scala    From coral   with Apache License 2.0 5 votes vote down vote up
package io.coral.lib

import org.json4s.{JArray, JValue, JObject}
import org.json4s.JsonAST.JNothing
import scala.util.parsing.combinator.{PackratParsers, JavaTokenParsers}
import scala.util.parsing.input.CharSequenceReader

object JsonExpressionParser extends JavaTokenParsers with PackratParsers {
	abstract class FieldElement
	// Represents the complete list of identifiers ("field.array[0].reference['elem']")
	// A FieldReference is a concatenation of FieldElements.
	// A FieldElement is either a simple identifier, an array
	// access element or a dictionary access element.
	case class FieldReference(items: List[FieldElement])
	// Represents a simple identifier between dots
	case class JsonIdentifier(id: String) extends FieldElement
	// Represents an array access identifier ("field[0]")
	case class ArrayAccess(id: JsonIdentifier, index: Int) extends FieldElement
	// Represents a dictionary access identifier ("field['inner']")
	case class DictionaryAccess(id: JsonIdentifier, field: String) extends FieldElement
	object ReferenceAll extends FieldElement

	
	def getFieldValue(json: JObject, id: FieldReference): JValue = {
		// tempJson holds the result we want to return
		var tempJson: JValue = json

		id.items.foreach({
			case ReferenceAll => tempJson
			case i: JsonIdentifier =>
				tempJson = tempJson \ i.id
			case a: ArrayAccess =>
				val obj = tempJson \ a.id.id
				obj match {
					case array: JArray =>
						if (a.index < array.arr.length)
							tempJson = array(a.index)
						else return JNothing
					case _ => return JNothing
				}
			case d: DictionaryAccess =>
				tempJson = tempJson \ d.id.id \ d.field
			case _ =>
		})

		tempJson
	}

	type P[+T] = PackratParser[T]

	lazy val local_field_reference: P[FieldReference] =
		repsep(field_element, ".") ^^ { case i => FieldReference(i) }
	lazy val field_element: P[FieldElement] =
		reference_all | array_access | dictionary_access | json_identifier
	lazy val json_identifier: P[JsonIdentifier] =
		ident ^^ { case i => JsonIdentifier(i) }
	lazy val array_access: P[ArrayAccess] =
		json_identifier ~ "[" ~ wholeNumber ~ "]" ^^ {
			case id ~ "[" ~ index ~ "]" =>
				ArrayAccess(id, index.toInt)
		}
	lazy val dictionary_access: P[DictionaryAccess] =
		json_identifier ~ "[" ~ "'" ~ ident ~ "'" ~ "]" ^^ {
			case id ~ "[" ~ "'" ~ field ~ "'" ~ "]" =>
				DictionaryAccess(id, field)
		}
	lazy val reference_all: P[FieldElement] = "*" ^^ { case _ => ReferenceAll }
} 
Example 15
Source File: CommonTypesParser.scala    From rug   with GNU General Public License v3.0 4 votes vote down vote up
package com.atomist.util.scalaparsing

import com.atomist.rug.{BadRugException, BadRugSyntaxException, RugRuntimeException}
import com.atomist.source.FileArtifact
import com.atomist.tree.content.text._
import com.typesafe.scalalogging.LazyLogging

import scala.util.matching.Regex
import scala.util.parsing.combinator.JavaTokenParsers
import scala.util.parsing.input.{CharSequenceReader, OffsetPosition, Positional}


  protected def identifierRef(reservedWords: Set[String], underlying: Parser[String] = ident) = new Parser[IdentifierRef] {
    def apply(in: Input): ParseResult[IdentifierRef] = {
      val pr = underlying.apply(in)
      pr match {
        case succ: Success[String @unchecked] =>
          if (reservedWords.contains(succ.get))
            Failure(s"Cannot use reserved word '${succ.get}' as function name", succ.next)
          else
            Success[IdentifierRef](IdentifierRef(succ.get), succ.next)
        case f: Failure => f
        case _ => ???
      }
    }
  }

  protected def identifierRefString(reservedWords: Set[String], underlying: Parser[String] = ident): Parser[String] =
    identifierRef(reservedWords, underlying) ^^ (ir => ir.name)

  protected def parseTo[T](f: FileArtifact, parser: Parser[T]): T = {
    logger.debug(s"Rug input is\n------\n${f.path}\n${f.content}\n------\n")
    // We need a source that gives us positions
    val source = new CharSequenceReader(f.content)
    val parsed = parse(parser, source) match {
      case Success(matched, _) => matched
      case Failure(msg, rest) =>
        throw new BadRugSyntaxException(ErrorInfo(s"Failure: $msg", badInput = f.content, line = rest.pos.line, col = rest.pos.column, filePath = f.path))
      case Error(msg, rest) =>
        throw new BadRugSyntaxException(ErrorInfo(s"Error: $msg", badInput = f.content, line = rest.pos.line, col = rest.pos.column, filePath = f.path))
    }
    logger.debug(s"Parse result=$parsed")
    parsed
  }
}