scala.util.parsing.input.Positional Scala Examples

The following examples show how to use scala.util.parsing.input.Positional. 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: ConcordanceParser.scala    From CSYE7200_Old   with MIT License 5 votes vote down vote up
package edu.neu.coe.csye7200.concordance

import scala.util.parsing.combinator._
import scala.util.parsing.input.Positional
import scala.io.Source
import scala.collection.immutable.Map


class ConcordanceParser extends RegexParsers {
  private val rWord = """[\w’]+[,;\.\-\?\!\—]?""".r
  def word: Parser[PositionalString] = positioned(regex(rWord) ^^ {w => PositionalString(w)})
  def sentence: Parser[Seq[PositionalString]] = rep(word)
}

case class PositionalString(s: String) extends Positional

object ConcordanceParser {
 
  def main(args: Array[String]): Unit = {
    val docs = for (f <- args) yield Source.fromFile(f).mkString
    val concordance = for (i <- docs.indices) yield (args(i),parseDoc(docs(i)))
    println(concordance)
    // an alternative way of looking at the data (gives doc, page, line and char numbers with each string)
    val q = for {(d,xxxx) <- concordance; (p,xxx) <- xxxx; (l,xx) <- xxx; (_,c,x) <- xx} yield (d, p,l,c,x)
    println(q)
    // yet another way to look at the data
    val concordanceMap = concordance.toMap
    println(concordanceMap)
  }
  
  private def parseDoc(content: String) = {
    val pages = for (p <- content.split("/p")) yield p
    for (i <- pages.indices) yield (i+1,parsePage(pages(i)))
  }

  private def parsePage(content: String) = {
    val lines = for (l <- content.split("\n")) yield l
    for (i <- lines.indices) yield (i+1,parseLine(lines(i)))
  }

  def parseLine(line: String): Seq[(Int,Int,String)] = {
    def tidy(s: String) = s.replaceAll("""[,;\.\-\?\!\—]""", "")
    val p = new ConcordanceParser
    val r = p.parseAll(p.sentence,line) match {
      case p.Success(ws,_) => ws
      case p.Failure(e,_) => println(e); List()
      case _ => println("PositionalParser: logic error"); List()
    }
    r map {case p @ PositionalString(s) => (p.pos.line,p.pos.column,tidy(s).toLowerCase)}
  }
} 
Example 2
Source File: ConcordanceParser.scala    From CSYE7200   with MIT License 5 votes vote down vote up
package edu.neu.coe.csye7200.concordance

import scala.util.parsing.combinator._
import scala.util.parsing.input.Positional
import scala.io.Source
import scala.collection.immutable.Map


class ConcordanceParser extends RegexParsers {
  private val rWord = """[\w’]+[,;\.\-\?\!\—]?""".r
  def word: Parser[PositionalString] = positioned(regex(rWord) ^^ {w => PositionalString(w)})
  def sentence: Parser[Seq[PositionalString]] = rep(word)
}

case class PositionalString(s: String) extends Positional

object ConcordanceParser {
 
  def main(args: Array[String]): Unit = {
    val docs = for (f <- args) yield Source.fromFile(f).mkString
    val concordance = for (i <- docs.indices) yield (args(i),parseDoc(docs(i)))
    println(concordance)
    // an alternative way of looking at the data (gives doc, page, line and char numbers with each string)
    val q = for {(d,xxxx) <- concordance; (p,xxx) <- xxxx; (l,xx) <- xxx; (_,c,x) <- xx} yield (d, p,l,c,x)
    println(q)
    // yet another way to look at the data
    val concordanceMap = concordance.toMap
    println(concordanceMap)
  }
  
  private def parseDoc(content: String) = {
    val pages = for (p <- content.split("/p")) yield p
    for (i <- pages.indices) yield (i+1,parsePage(pages(i)))
  }

  private def parsePage(content: String) = {
    val lines = for (l <- content.split("\n")) yield l
    for (i <- lines.indices) yield (i+1,parseLine(lines(i)))
  }

  def parseLine(line: String): Seq[(Int,Int,String)] = {
    def tidy(s: String) = s.replaceAll("""[,;\.\-\?\!\—]""", "")
    val p = new ConcordanceParser
    val r = p.parseAll(p.sentence,line) match {
      case p.Success(ws,_) => ws
      case p.Failure(e,_) => println(e); List()
      case _ => println("PositionalParser: logic error"); List()
    }
    r map {case p @ PositionalString(s) => (p.pos.line,p.pos.column,tidy(s).toLowerCase)}
  }
} 
Example 3
Source File: IR.scala    From inox   with Apache License 2.0 5 votes vote down vote up
package inox
package parsing

import scala.util.parsing.input.Positional

trait IRs extends BuiltIns with ExprIRs with TypeIRs with DefinitionIRs {
  protected val trees: ast.Trees
}

 
trait IR {

  type Identifier  // Identifier of the language.
  type Type        // Types.
  type Operator    // Primitive operators.
  type Value       // Literal values.
  type Field       // Fields.
  type Quantifier  // Quantifiers.

  abstract class Expression(pre: String) extends Positional with Product {
    override def productPrefix = pos + "@" + pre
  }
  case class Variable(identifier: Identifier) extends Expression("Variable")
  case class Application(callee: Expression, args: Seq[Expression]) extends Expression("Application")
  case class Abstraction(quantifier: Quantifier, bindings: Seq[(Identifier, Option[Type])], body: Expression) extends Expression("Abstraction")
  case class Operation(operator: Operator, args: Seq[Expression]) extends Expression("Operation")
  case class Selection(structure: Expression, field: Field) extends Expression("Selection")
  case class Literal(value: Value) extends Expression("Literal")
  case class TypeApplication(callee: Expression, args: Seq[Type]) extends Expression("TypeApplication")
  case class Let(bindings: Seq[(Identifier, Option[Type], Expression)], body: Expression) extends Expression("Let")
} 
Example 4
Source File: DefinitionIR.scala    From inox   with Apache License 2.0 5 votes vote down vote up
package inox
package parsing

import scala.util.parsing.input.Positional

trait DefinitionIRs { self: IRs =>

  
  object DefinitionIR {

    import ExprIR.{Identifier, Expression}
    import TypeIR.{Expression => Type}

    sealed abstract class Definition(pre: String) extends Positional with Product {
      override def productPrefix = pos + "@" + pre
    }

    case class FunDef(
      id: Identifier,
      tparams: Seq[Identifier],
      params: Seq[(Identifier, Type)],
      returnType: Type,
      body: Expression
    ) extends Definition("Function")

    case class TypeDef(
      id: Identifier,
      tparams: Seq[Identifier],
      constructors: Seq[(Identifier, Seq[(Identifier, Type)])]
    ) extends Definition("Type")
  }
} 
Example 5
Source File: ConcordanceParser.scala    From Scalaprof   with GNU General Public License v2.0 5 votes vote down vote up
package com.phasmid.concordance

import scala.util.parsing.combinator._
import scala.util.parsing.input.Positional
import scala.io.Source
import scala.collection.immutable.Map


class ConcordanceParser extends RegexParsers {
  val rWord = """[\w’]+[,;\.\-\?\!\—]?""".r
  def word: Parser[PositionalString] = positioned(regex(rWord) ^^ {w => PositionalString(w)})
  def sentence: Parser[Seq[PositionalString]] = rep(word)
}

case class PositionalString(s: String) extends Positional

object ConcordanceParser {
 
  def main(args: Array[String]): Unit = {
    val docs = for (f <- args) yield Source.fromFile(f).mkString
    val concordance = for (i <- 0 to docs.length-1) yield (args(i),parseDoc(docs(i)))
    println(concordance)
    // an alternative way of looking at the data (gives doc, page, line and char numbers with each string)
    val q = for {(d,xxxx) <- concordance; (p,xxx) <- xxxx; (l,xx) <- xxx; (_,c,x) <- xx} yield (d, p,l,c,x)
    println(q)
    // yet another way to look at the data
    val concordanceMap = concordance.toMap
    println(concordanceMap)
  }
  
  def parseDoc(content: String) = {
    val pages = for (p <- content.split("/p")) yield p
    for (i <- 0 to pages.length-1) yield (i+1,parsePage(pages(i)))
  }

  def parsePage(content: String) = {
    val lines = for (l <- content.split("\n")) yield l
    for (i <- 0 to lines.length-1) yield (i+1,parseLine(lines(i)))
  }

  def parseLine(line: String): Seq[(Int,Int,String)] = {
    def tidy(s: String) = s.replaceAll("""[,;\.\-\?\!\—]""", "")
    val p = new ConcordanceParser
    val r = p.parseAll(p.sentence,line) match {
      case p.Success(ws,_) => ws
      case p.Failure(e,_) => println(e); List()
      case _ => println("PositionalParser: logic error"); List()
    }
    r map {case p @ PositionalString(s) => (p.pos.line,p.pos.column,tidy(s).toLowerCase)}
  }
} 
Example 6
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
  }
}