Skip to content

Clean up code #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 20, 2019
Merged
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# scala-parser-combinators [<img src="https://img.shields.io/travis/scala/scala-parser-combinators.svg"/>](https://travis-ci.org/scala/scala-parser-combinators) [<img src="https://img.shields.io/maven-central/v/org.scala-lang.modules/scala-parser-combinators_2.11.svg?label=latest%20release%20for%202.11"/>](http://search.maven.org/#search%7Cga%7C1%7Cg%3Aorg.scala-lang.modules%20a%3Ascala-parser-combinators_2.11) [<img src="https://img.shields.io/maven-central/v/org.scala-lang.modules/scala-parser-combinators_2.12.svg?label=latest%20release%20for%202.12"/>](http://search.maven.org/#search%7Cga%7C1%7Cg%3Aorg.scala-lang.modules%20a%3Ascala-parser-combinators_2.12) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/scala/scala-parser-combinators)
# scala-parser-combinators

[<img src="https://img.shields.io/travis/scala/scala-parser-combinators.svg"/>](https://travis-ci.org/scala/scala-parser-combinators)
[<img src="https://img.shields.io/maven-central/v/org.scala-lang.modules/scala-parser-combinators_2.11.svg?label=latest%20release%20for%202.11"/>](http://search.maven.org/#search%7Cga%7C1%7Cg%3Aorg.scala-lang.modules%20a%3Ascala-parser-combinators_2.11)
[<img src="https://img.shields.io/maven-central/v/org.scala-lang.modules/scala-parser-combinators_2.12.svg?label=latest%20release%20for%202.12"/>](http://search.maven.org/#search%7Cga%7C1%7Cg%3Aorg.scala-lang.modules%20a%3Ascala-parser-combinators_2.12)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/scala/scala-parser-combinators)

### Scala Standard Parser Combinator Library

This library is now community-maintained. If you are interested in helping please contact @gourlaysama or mention it [on Gitter](https://gitter.im/scala/scala-parser-combinators).
This library is now community-maintained. If you are interested in helping please contact [@gourlaysama](https://github.com/gourlaysama) or [@Philippus](https://github.com/philippus) or mention it on [Gitter](https://gitter.im/scala/scala-parser-combinators).

As of Scala 2.11, this library is a separate jar that can be omitted from Scala projects that do not use Parser Combinators.

Expand Down
30 changes: 15 additions & 15 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import ScalaModulePlugin._
import sbtcrossproject.crossProject
import sbtcrossproject.CrossPlugin.autoImport.crossProject

crossScalaVersions in ThisBuild := List("2.12.8", "2.11.12", "2.13.0-RC1")

lazy val root = project.in(file("."))
.aggregate(`scala-parser-combinatorsJS`, `scala-parser-combinatorsJVM`, `scala-parser-combinatorsNative`)
.settings(disablePublishing)

lazy val `scala-parser-combinators` = crossProject(JSPlatform, JVMPlatform, NativePlatform).
withoutSuffixFor(JVMPlatform).in(file(".")).
settings(scalaModuleSettings: _*).
jvmSettings(scalaModuleSettingsJVM).
settings(
lazy val `scala-parser-combinators` = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.withoutSuffixFor(JVMPlatform).in(file("."))
.settings(scalaModuleSettings: _*)
.jvmSettings(scalaModuleSettingsJVM)
.settings(
name := "scala-parser-combinators",
version := "1.2.0-SNAPSHOT",
mimaPreviousVersion := None,
Expand All @@ -38,18 +38,18 @@ lazy val `scala-parser-combinators` = crossProject(JSPlatform, JVMPlatform, Nati
}
}
}
).
jvmSettings(
)
.jvmSettings(
OsgiKeys.exportPackage := Seq(s"scala.util.parsing.*;version=${version.value}"),
libraryDependencies += "junit" % "junit" % "4.12" % "test",
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
).
jsSettings(
libraryDependencies += "junit" % "junit" % "4.12" % Test,
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
)
.jsSettings(
// Scala.js cannot run forked tests
fork in Test := false
).
jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin)).
nativeSettings(
)
.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))
.nativeSettings(
skip in compile := System.getProperty("java.version").startsWith("1.6") || !scalaVersion.value.startsWith("2.11"),
test := {},
libraryDependencies := {
Expand Down
14 changes: 7 additions & 7 deletions js/src/main/scala/scala/util/parsing/input/PositionCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

package scala.util.parsing.input

import java.util.{AbstractMap, Collections}
import java.util.Collections

private[input] trait PositionCache {
private[input] lazy val indexCache: java.util.Map[CharSequence,Array[Int]] = new AbstractMap[CharSequence, Array[Int]] {
private[input] lazy val indexCache: java.util.Map[CharSequence,Array[Int]] =
new java.util.AbstractMap[CharSequence, Array[Int]] {
override def entrySet() = Collections.emptySet()

override def entrySet() = Collections.emptySet()

// the /dev/null of Maps
override def put(ch: CharSequence, a: Array[Int]) = null
}
// the /dev/null of Maps
override def put(ch: CharSequence, a: Array[Int]) = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@

package scala.util.parsing.input

import java.util.WeakHashMap

/**
* @author Tomáš Janoušek
*/
private[input] trait PositionCache {
private lazy val indexCacheTL =
// not DynamicVariable as that would share the map from parent to child :-(
new ThreadLocal[java.util.Map[CharSequence, Array[Int]]] {
override def initialValue = new WeakHashMap[CharSequence, Array[Int]]
override def initialValue = new java.util.WeakHashMap[CharSequence, Array[Int]]
}

private[input] def indexCache = indexCacheTL.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
package scala.util.parsing.input

import java.lang.CharSequence
import java.util.{AbstractMap, Collections}
import java.util.Collections

private[input] trait PositionCache {
private[input] lazy val indexCache: java.util.Map[CharSequence,Array[Int]] = new AbstractMap[CharSequence, Array[Int]] {
private[input] lazy val indexCache: java.util.Map[CharSequence,Array[Int]] =
new java.util.AbstractMap[CharSequence, Array[Int]] {
override def entrySet() = Collections.emptySet()

override def entrySet() = Collections.emptySet()

// the /dev/null of Maps
override def put(ch: CharSequence, a: Array[Int]) = null
}
// the /dev/null of Maps
override def put(ch: CharSequence, a: Array[Int]) = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ trait JavaTokenParsers extends RegexParsers {
/** An integer, without sign or with a negative sign. */
def wholeNumber: Parser[String] =
"""-?\d+""".r

/** Number following one of these rules:
*
* - An integer. For example: `13`
Expand All @@ -46,6 +47,7 @@ trait JavaTokenParsers extends RegexParsers {
*/
def decimalNumber: Parser[String] =
"""(\d+(\.\d*)?|\d*\.\d+)""".r

/** Double quotes (`"`) enclosing a sequence of:
*
* - Any character except double quotes, control characters or backslash (`\`)
Expand All @@ -56,6 +58,7 @@ trait JavaTokenParsers extends RegexParsers {
@migration("`stringLiteral` allows escaping single and double quotes, but not forward slashes any longer.", "2.10.0")
def stringLiteral: Parser[String] =
("\""+"""([^"\x00-\x1F\x7F\\]|\\[\\'"bfnrt]|\\u[a-fA-F0-9]{4})*"""+"\"").r

/** A number following the rules of `decimalNumber`, with the following
* optional additions:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ import scala.language.implicitConversions
*/

trait PackratParsers extends Parsers {

//type Input = PackratReader[Elem]

/**
* A specialized `Reader` class that wraps an underlying `Reader`
* and provides memoization of parse results.
Expand Down Expand Up @@ -165,7 +162,7 @@ trait PackratParsers extends Parsers {
case None => /*no heads*/ cached
case Some(h@Head(hp, involved, evalSet)) => {
//heads found
if(cached == None && !(hp::involved contains p)) {
if(cached.isEmpty && !(hp::involved contains p)) {
//Nothing in the cache, and p is not involved
return Some(MemoEntry(Right(Failure("dummy ",in))))
}
Expand All @@ -190,7 +187,7 @@ trait PackratParsers extends Parsers {
* the current parser again
*/
private def setupLR(p: Parser[_], in: PackratReader[_], recDetect: LR): Unit = {
if(recDetect.head == None) recDetect.head = Some(Head(p, Nil, Nil))
if(recDetect.head.isEmpty) recDetect.head = Some(Head(p, Nil, Nil))

in.lrStack.takeWhile(_.rule != p).foreach {x =>
x.head = recDetect.head
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ trait Parsers {
def get: T = result

/** The toString method of a Success. */
override def toString = "["+next.pos+"] parsed: "+result
override def toString = s"[${next.pos}] parsed: $result"

val successful = true
}
Expand Down Expand Up @@ -190,7 +190,7 @@ trait Parsers {
*/
case class Failure(override val msg: String, override val next: Input) extends NoSuccess(msg, next) {
/** The toString method of a Failure yields an error message. */
override def toString = "["+next.pos+"] failure: "+msg+"\n\n"+next.pos.longString
override def toString = s"[${next.pos}] failure: $msg\n\n${next.pos.longString}"

def append[U >: Nothing](a: => ParseResult[U]): ParseResult[U] = { val alt = a; alt match {
case Success(_, _) => alt
Expand All @@ -207,7 +207,7 @@ trait Parsers {
*/
case class Error(override val msg: String, override val next: Input) extends NoSuccess(msg, next) {
/** The toString method of an Error yields an error message. */
override def toString = "["+next.pos+"] error: "+msg+"\n\n"+next.pos.longString
override def toString = s"[${next.pos}] error: $msg\n\n${next.pos.longString}"
def append[U >: Nothing](a: => ParseResult[U]): ParseResult[U] = this
}

Expand All @@ -223,7 +223,7 @@ trait Parsers {
abstract class Parser[+T] extends (Input => ParseResult[T]) {
private var name: String = ""
def named(n: String): this.type = {name=n; this}
override def toString() = "Parser ("+ name +")"
override def toString = s"Parser ($name)"

/** An unspecified method that defines the behaviour of this parser. */
def apply(in: Input): ParseResult[T]
Expand Down Expand Up @@ -940,7 +940,7 @@ trait Parsers {
* }}}
*/
case class ~[+a, +b](_1: a, _2: b) {
override def toString = "("+ _1 +"~"+ _2 +")"
override def toString = s"(${_1}~${_2})"
}

/** A parser whose `~` combinator disallows back-tracking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class Lexical extends Scanners with Tokens {
def digit = elem("digit", _.isDigit)

/** A character-parser that matches any character except the ones given in `cs` (and returns it).*/
def chrExcept(cs: Char*) = elem("", ch => (cs forall (ch != _)))
def chrExcept(cs: Char*) = elem("", ch => !cs.contains(ch))

/** A character-parser that matches a white-space character (and returns it).*/
def whitespaceChar = elem("space char", ch => ch <= ' ' && ch != EofCh)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ trait Scanners extends Parsers {
*/
class Scanner(in: Reader[Char]) extends Reader[Token] {
/** Convenience constructor (makes a character reader out of the given string) */
def this(in: String) = this(new CharArrayReader(in.toCharArray()))
def this(in: String) = this(new CharArrayReader(in.toCharArray))
private val (tok, rest1, rest2) = whitespace(in) match {
case Success(_, in1) =>
token(in1) match {
Expand All @@ -64,4 +64,3 @@ trait Scanners extends Parsers {
def atEnd = in.atEnd || (whitespace(in) match { case Success(_, in1) => in1.atEnd case _ => false })
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ trait StdTokenParsers extends TokenParsers {
* @param chars The character string making up the matched keyword.
* @return a `Parser` that matches the given string
*/
// implicit def keyword(chars: String): Parser[String] = accept(Keyword(chars)) ^^ (_.chars)
implicit def keyword(chars: String): Parser[String] =
keywordCache.getOrElseUpdate(chars, accept(Keyword(chars)) ^^ (_.chars))
implicit def keyword(chars: String): Parser[String] =
keywordCache.getOrElseUpdate(chars, accept(Keyword(chars)) ^^ (_.chars))

/** A parser which matches a numeric literal */
def numericLit: Parser[String] =
Expand All @@ -50,5 +49,3 @@ trait StdTokenParsers extends TokenParsers {
def ident: Parser[String] =
elem("identifier", _.isInstanceOf[Identifier]) ^^ (_.chars)
}


Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package token
trait StdTokens extends Tokens {
/** The class of keyword tokens */
case class Keyword(chars: String) extends Token {
override def toString = "'"+chars+"'"
override def toString = s"'$chars'"
}

/** The class of numeric literal tokens */
Expand All @@ -33,11 +33,11 @@ trait StdTokens extends Tokens {

/** The class of string literal tokens */
case class StringLit(chars: String) extends Token {
override def toString = "\""+chars+"\""
override def toString = s""""$chars""""
}

/** The class of identifier tokens */
case class Identifier(chars: String) extends Token {
override def toString = "identifier "+chars
override def toString = s"identifier $chars"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait Tokens {
* errors detected during lexical analysis
*/
case class ErrorToken(msg: String) extends Token {
def chars = "*** error: "+msg
def chars = s"*** error: $msg"
}

/** A class for end-of-file tokens */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait Position {
protected def lineContents: String

/** Returns a string representation of the `Position`, of the form `line.column`. */
override def toString = ""+line+"."+column
override def toString = s"$line.$column"

/** Returns a more ``visual`` representation of this position.
* More precisely, the resulting string consists of two lines:
Expand Down