<commit_before>/*
 *  @author <NAME>
 *  
 *  Copyright 2014 University of Zurich
 *      
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *         http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 */

package com.signalcollect.triplerush.sparql

import scala.util.parsing.combinator.ImplicitConversions
import com.signalcollect.triplerush.TriplePattern

case class ParsedSparqlQuery(prefixes: List[PrefixDeclaration], select: Select)

sealed trait VariableOrBound

case class Variable(name: String) extends VariableOrBound

case class Iri(url: String) extends VariableOrBound

//case class IntLiteral(i: Int) extends VariableOrBound
//
//case class StringLiteral(string: String) extends VariableOrBound

case class ParsedPattern(s: VariableOrBound, p: VariableOrBound, o: VariableOrBound)

case class PrefixDeclaration(prefix: String, expanded: String)

case class Select(
  selectVariables: List[Variable],
  patternUnions: List[List[ParsedPattern]],
  isDistinct: Boolean = false,
  orderBy: Option[Variable] = None,
  limit: Option[Int] = None)

object SparqlParser extends ParseHelper[ParsedSparqlQuery] with ImplicitConversions {

  lexical.delimiters ++= List(
    "(", ")", ",", ":", "<", ">")
  protected override val whiteSpace = """(\s|//.*|#.*|(?m)/\*(\*(?!/)|[^*])*\*/)+""".r

  def defaultParser = sparqlQuery

  val prefix = "PREFIX" // | "prefix"
  val select = "SELECT" // | "select"
  val where = "WHERE" // | "where"
  val distinct = "DISTINCT" // | "distinct"
  val union = "UNION" // | "union"
  val orderBy = "ORDER" ~> "BY"
  val limit = "LIMIT"

  val url: Parser[String] = "[-a-zA-Z0-9:/\\.]*".r

  val iri: Parser[Iri] = {
    (("<" ~> url <~ ">") | url) ^^ {
      case url => Iri(url)
    }
  }

  //  val string = """.*""".r
  //
  //  val stringLiteral: Parser[StringLiteral] = {
  //    "\"" ~> string <~ "\"" ^^ {
  //      case l => StringLiteral(l)
  //    }
  //  }

  val variable: Parser[Variable] = {<commit_msg>Allowing for # in urls.<commit_after>/*
 *  @author <NAME>
 *  
 *  Copyright 2014 University of Zurich
 *      
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *         http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 */

package com.signalcollect.triplerush.sparql

import scala.util.parsing.combinator.ImplicitConversions
import com.signalcollect.triplerush.TriplePattern

case class ParsedSparqlQuery(prefixes: List[PrefixDeclaration], select: Select)

sealed trait VariableOrBound

case class Variable(name: String) extends VariableOrBound

case class Iri(url: String) extends VariableOrBound

//case class IntLiteral(i: Int) extends VariableOrBound
//
//case class StringLiteral(string: String) extends VariableOrBound

case class ParsedPattern(s: VariableOrBound, p: VariableOrBound, o: VariableOrBound)

case class PrefixDeclaration(prefix: String, expanded: String)

case class Select(
  selectVariables: List[Variable],
  patternUnions: List[List[ParsedPattern]],
  isDistinct: Boolean = false,
  orderBy: Option[Variable] = None,
  limit: Option[Int] = None)

object SparqlParser extends ParseHelper[ParsedSparqlQuery] with ImplicitConversions {

  lexical.delimiters ++= List(
    "(", ")", ",", ":", "<", ">")
  protected override val whiteSpace = """(\s|//.*|(?m)/\*(\*(?!/)|[^*])*\*/)+""".r

  def defaultParser = sparqlQuery

  val prefix = "PREFIX" // | "prefix"
  val select = "SELECT" // | "select"
  val where = "WHERE" // | "where"
  val distinct = "DISTINCT" // | "distinct"
  val union = "UNION" // | "union"
  val orderBy = "ORDER" ~> "BY"
  val limit = "LIMIT"

  val url: Parser[String] = "[-a-zA-Z0-9:/\\.#]*".r

  val iri: Parser[Iri] = {
    (("<" ~> url <~ ">") | url) ^^ {
      case url => Iri(url)
    }
  }

  //  val string = """.*""".r
  //
  //  val stringLiteral: Parser[StringLiteral] = {
  //    "\"" ~> string <~ "\"" ^^ {
  //      case l => StringLiteral(l)
  //    }
  //  }

  val variable: Parser[Variable] = {
{'fraction_non_alphanumeric': 0.14545079430575614, 'fraction_numerical': 0.004126263668248401, 'mean_word_length': 3.501392757660167, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6961993', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1540, 'n_words': 449}