Skip to content

Commit d234223

Browse files
authored
Merge pull request scala#206 from Philippus/clean-up-more
Clean up code
2 parents d3ebdf1 + 4cbf039 commit d234223

File tree

14 files changed

+56
-57
lines changed

14 files changed

+56
-57
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
# 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)
1+
# scala-parser-combinators
2+
3+
[<img src="https://img.shields.io/travis/scala/scala-parser-combinators.svg"/>](https://travis-ci.org/scala/scala-parser-combinators)
4+
[<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)
5+
[<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)
6+
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/scala/scala-parser-combinators)
27

38
### Scala Standard Parser Combinator Library
49

5-
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).
10+
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).
611

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

build.sbt

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import ScalaModulePlugin._
2-
import sbtcrossproject.crossProject
2+
import sbtcrossproject.CrossPlugin.autoImport.crossProject
33

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

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

10-
lazy val `scala-parser-combinators` = crossProject(JSPlatform, JVMPlatform, NativePlatform).
11-
withoutSuffixFor(JVMPlatform).in(file(".")).
12-
settings(scalaModuleSettings: _*).
13-
jvmSettings(scalaModuleSettingsJVM).
14-
settings(
10+
lazy val `scala-parser-combinators` = crossProject(JSPlatform, JVMPlatform, NativePlatform)
11+
.withoutSuffixFor(JVMPlatform).in(file("."))
12+
.settings(scalaModuleSettings: _*)
13+
.jvmSettings(scalaModuleSettingsJVM)
14+
.settings(
1515
name := "scala-parser-combinators",
1616
version := "1.2.0-SNAPSHOT",
1717
mimaPreviousVersion := None,
@@ -38,18 +38,18 @@ lazy val `scala-parser-combinators` = crossProject(JSPlatform, JVMPlatform, Nati
3838
}
3939
}
4040
}
41-
).
42-
jvmSettings(
41+
)
42+
.jvmSettings(
4343
OsgiKeys.exportPackage := Seq(s"scala.util.parsing.*;version=${version.value}"),
44-
libraryDependencies += "junit" % "junit" % "4.12" % "test",
45-
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
46-
).
47-
jsSettings(
44+
libraryDependencies += "junit" % "junit" % "4.12" % Test,
45+
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
46+
)
47+
.jsSettings(
4848
// Scala.js cannot run forked tests
4949
fork in Test := false
50-
).
51-
jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin)).
52-
nativeSettings(
50+
)
51+
.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))
52+
.nativeSettings(
5353
skip in compile := System.getProperty("java.version").startsWith("1.6") || !scalaVersion.value.startsWith("2.11"),
5454
test := {},
5555
libraryDependencies := {

js/src/main/scala/scala/util/parsing/input/PositionCache.scala

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212

1313
package scala.util.parsing.input
1414

15-
import java.util.{AbstractMap, Collections}
15+
import java.util.Collections
1616

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

20-
override def entrySet() = Collections.emptySet()
21-
22-
// the /dev/null of Maps
23-
override def put(ch: CharSequence, a: Array[Int]) = null
24-
}
22+
// the /dev/null of Maps
23+
override def put(ch: CharSequence, a: Array[Int]) = null
24+
}
2525
}

jvm/src/main/scala/scala/util/parsing/input/PositionCache.scala

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212

1313
package scala.util.parsing.input
1414

15-
import java.util.WeakHashMap
16-
1715
/**
1816
* @author Tomáš Janoušek
1917
*/
2018
private[input] trait PositionCache {
2119
private lazy val indexCacheTL =
2220
// not DynamicVariable as that would share the map from parent to child :-(
2321
new ThreadLocal[java.util.Map[CharSequence, Array[Int]]] {
24-
override def initialValue = new WeakHashMap[CharSequence, Array[Int]]
22+
override def initialValue = new java.util.WeakHashMap[CharSequence, Array[Int]]
2523
}
2624

2725
private[input] def indexCache = indexCacheTL.get

native/src/main/scala/scala/util/parsing/input/PositionCache.scala

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
package scala.util.parsing.input
1414

1515
import java.lang.CharSequence
16-
import java.util.{AbstractMap, Collections}
16+
import java.util.Collections
1717

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

21-
override def entrySet() = Collections.emptySet()
22-
23-
// the /dev/null of Maps
24-
override def put(ch: CharSequence, a: Array[Int]) = null
25-
}
23+
// the /dev/null of Maps
24+
override def put(ch: CharSequence, a: Array[Int]) = null
25+
}
2626
}

shared/src/main/scala/scala/util/parsing/combinator/JavaTokenParsers.scala

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ trait JavaTokenParsers extends RegexParsers {
3737
/** An integer, without sign or with a negative sign. */
3838
def wholeNumber: Parser[String] =
3939
"""-?\d+""".r
40+
4041
/** Number following one of these rules:
4142
*
4243
* - An integer. For example: `13`
@@ -46,6 +47,7 @@ trait JavaTokenParsers extends RegexParsers {
4647
*/
4748
def decimalNumber: Parser[String] =
4849
"""(\d+(\.\d*)?|\d*\.\d+)""".r
50+
4951
/** Double quotes (`"`) enclosing a sequence of:
5052
*
5153
* - Any character except double quotes, control characters or backslash (`\`)
@@ -56,6 +58,7 @@ trait JavaTokenParsers extends RegexParsers {
5658
@migration("`stringLiteral` allows escaping single and double quotes, but not forward slashes any longer.", "2.10.0")
5759
def stringLiteral: Parser[String] =
5860
("\""+"""([^"\x00-\x1F\x7F\\]|\\[\\'"bfnrt]|\\u[a-fA-F0-9]{4})*"""+"\"").r
61+
5962
/** A number following the rules of `decimalNumber`, with the following
6063
* optional additions:
6164
*

shared/src/main/scala/scala/util/parsing/combinator/PackratParsers.scala

+2-5
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ import scala.language.implicitConversions
5656
*/
5757

5858
trait PackratParsers extends Parsers {
59-
60-
//type Input = PackratReader[Elem]
61-
6259
/**
6360
* A specialized `Reader` class that wraps an underlying `Reader`
6461
* and provides memoization of parse results.
@@ -165,7 +162,7 @@ trait PackratParsers extends Parsers {
165162
case None => /*no heads*/ cached
166163
case Some(h@Head(hp, involved, evalSet)) => {
167164
//heads found
168-
if(cached == None && !(hp::involved contains p)) {
165+
if(cached.isEmpty && !(hp::involved contains p)) {
169166
//Nothing in the cache, and p is not involved
170167
return Some(MemoEntry(Right(Failure("dummy ",in))))
171168
}
@@ -190,7 +187,7 @@ trait PackratParsers extends Parsers {
190187
* the current parser again
191188
*/
192189
private def setupLR(p: Parser[_], in: PackratReader[_], recDetect: LR): Unit = {
193-
if(recDetect.head == None) recDetect.head = Some(Head(p, Nil, Nil))
190+
if(recDetect.head.isEmpty) recDetect.head = Some(Head(p, Nil, Nil))
194191

195192
in.lrStack.takeWhile(_.rule != p).foreach {x =>
196193
x.head = recDetect.head

shared/src/main/scala/scala/util/parsing/combinator/Parsers.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ trait Parsers {
154154
def get: T = result
155155

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

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

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

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

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

946946
/** A parser whose `~` combinator disallows back-tracking.

shared/src/main/scala/scala/util/parsing/combinator/lexical/Lexical.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class Lexical extends Scanners with Tokens {
3535
def digit = elem("digit", _.isDigit)
3636

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

4040
/** A character-parser that matches a white-space character (and returns it).*/
4141
def whitespaceChar = elem("space char", ch => ch <= ' ' && ch != EofCh)

shared/src/main/scala/scala/util/parsing/combinator/lexical/Scanners.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ trait Scanners extends Parsers {
4545
*/
4646
class Scanner(in: Reader[Char]) extends Reader[Token] {
4747
/** Convenience constructor (makes a character reader out of the given string) */
48-
def this(in: String) = this(new CharArrayReader(in.toCharArray()))
48+
def this(in: String) = this(new CharArrayReader(in.toCharArray))
4949
private val (tok, rest1, rest2) = whitespace(in) match {
5050
case Success(_, in1) =>
5151
token(in1) match {
@@ -64,4 +64,3 @@ trait Scanners extends Parsers {
6464
def atEnd = in.atEnd || (whitespace(in) match { case Success(_, in1) => in1.atEnd case _ => false })
6565
}
6666
}
67-

shared/src/main/scala/scala/util/parsing/combinator/syntactical/StdTokenParsers.scala

+2-5
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ trait StdTokenParsers extends TokenParsers {
3434
* @param chars The character string making up the matched keyword.
3535
* @return a `Parser` that matches the given string
3636
*/
37-
// implicit def keyword(chars: String): Parser[String] = accept(Keyword(chars)) ^^ (_.chars)
38-
implicit def keyword(chars: String): Parser[String] =
39-
keywordCache.getOrElseUpdate(chars, accept(Keyword(chars)) ^^ (_.chars))
37+
implicit def keyword(chars: String): Parser[String] =
38+
keywordCache.getOrElseUpdate(chars, accept(Keyword(chars)) ^^ (_.chars))
4039

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

shared/src/main/scala/scala/util/parsing/combinator/token/StdTokens.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ package token
2323
trait StdTokens extends Tokens {
2424
/** The class of keyword tokens */
2525
case class Keyword(chars: String) extends Token {
26-
override def toString = "'"+chars+"'"
26+
override def toString = s"'$chars'"
2727
}
2828

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

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

3939
/** The class of identifier tokens */
4040
case class Identifier(chars: String) extends Token {
41-
override def toString = "identifier "+chars
41+
override def toString = s"identifier $chars"
4242
}
4343
}

shared/src/main/scala/scala/util/parsing/combinator/token/Tokens.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ trait Tokens {
3434
* errors detected during lexical analysis
3535
*/
3636
case class ErrorToken(msg: String) extends Token {
37-
def chars = "*** error: "+msg
37+
def chars = s"*** error: $msg"
3838
}
3939

4040
/** A class for end-of-file tokens */

shared/src/main/scala/scala/util/parsing/input/Position.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ trait Position {
3737
protected def lineContents: String
3838

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

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

0 commit comments

Comments
 (0)