Skip to content

[backport] Scala.js support for 1.0.x #83

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 1 commit into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import com.typesafe.tools.mima.plugin.{MimaPlugin, MimaKeys}

scalaModuleSettings

name := "scala-parser-combinators"

version := "1.0.5-SNAPSHOT"
scalaVersion in ThisBuild := crossScalaVersions.value.head

crossScalaVersions in ThisBuild := {
val javaVersion = System.getProperty("java.version")
Expand All @@ -16,16 +10,45 @@ crossScalaVersions in ThisBuild := {
Seq("2.11.7", "2.12.0-M3")
}

// important!! must come here (why?)
scalaModuleOsgiSettings

OsgiKeys.exportPackage := Seq(s"scala.util.parsing.*;version=${version.value}")

// needed to fix classloader issues (see scala-xml#20)
fork in Test := true

libraryDependencies += "junit" % "junit" % "4.11" % "test"

libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test"

mimaPreviousVersion := Some("1.0.2")
lazy val `scala-parser-combinators` = crossProject.in(file(".")).
settings(scalaModuleSettings: _*).
settings(
name := "scala-parser-combinators-root"
).
jvmSettings(
// Mima uses the name of the jvm project in the artifactId
// when resolving previous versions (so no "-jvm" project)
name := "scala-parser-combinators"
).
jsSettings(
name := "scala-parser-combinators-js"
).
settings(
moduleName := "scala-parser-combinators",
version := "1.0.5-SNAPSHOT"
).
jvmSettings(
// important!! must come here (why?)
scalaModuleOsgiSettings: _*
).
jvmSettings(
OsgiKeys.exportPackage := Seq(s"scala.util.parsing.*;version=${version.value}"),

// needed to fix classloader issues (see scala-xml#20)
fork in Test := true
).
jsSettings(
// Scala.js cannot run forked tests
fork in Test := false
).
jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin)).
jvmSettings(
libraryDependencies += "junit" % "junit" % "4.11" % "test",
libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test"
).
jvmSettings(
mimaPreviousVersion := Some("1.0.4")
)

lazy val `scala-parser-combinatorsJVM` = `scala-parser-combinators`.jvm
lazy val `scala-parser-combinatorsJS` = `scala-parser-combinators`.js
14 changes: 14 additions & 0 deletions js/src/main/scala/scala/util/parsing/input/PositionCache.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package scala.util.parsing.input

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

private[input] trait PositionCache {
private[input] lazy val indexCache: java.util.Map[CharSequence,Array[Int]] = new AbstractMap[CharSequence, Array[Int]] {

override def entrySet() = Collections.emptySet()

// the /dev/null of Maps
override def put(ch: CharSequence, a: Array[Int]) = null
}
}
17 changes: 17 additions & 0 deletions jvm/src/main/scala/scala/util/parsing/input/PositionCache.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package scala.util.parsing.input

import java.lang.{CharSequence, ThreadLocal}
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]]
}

private[input] def indexCache = indexCacheTL.get
}
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
addSbtPlugin("org.scala-lang.modules" % "scala-module-plugin" % "1.0.4")

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.6")
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ trait JavaTokenParsers extends RegexParsers {
* <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8">The Java Language Spec</a>.
* Generally, this means a letter, followed by zero or more letters or numbers.
*/
def ident: Parser[String] =
"""\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*""".r
def ident: Parser[String] = (
"" ~> // handle whitespace
rep1(acceptIf(Character.isJavaIdentifierStart)("identifier expected but `" + _ + "' found"),
elem("identifier part", Character.isJavaIdentifierPart(_: Char))) ^^ (_.mkString)
)

/** An integer, without sign or with a negative sign. */
def wholeNumber: Parser[String] =
"""-?\d+""".r
Expand All @@ -49,7 +53,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] =
("\""+"""([^"\p{Cntrl}\\]|\\[\\'"bfnrt]|\\u[a-fA-F0-9]{4})*+"""+"\"").r
("\""+"""([^"\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 @@ -91,15 +91,5 @@ case class OffsetPosition(source: CharSequence, offset: Int) extends Position {
}

/** An object holding the index cache.
*
* @author Tomáš Janoušek
*/
object OffsetPosition extends scala.runtime.AbstractFunction2[CharSequence,Int,OffsetPosition] {
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]]
}

private def indexCache = indexCacheTL.get
}
object OffsetPosition extends scala.runtime.AbstractFunction2[CharSequence,Int,OffsetPosition] with PositionCache
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class JavaTokenParsersTest {
case Failure(message, next) =>
assertEquals(next.pos.line, 1)
assertEquals(next.pos.column, 1)
assert(message.endsWith(s"regex `\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*' expected but `-' found"))
assert(message.endsWith(s"identifier expected but `-' found"))
case _ => sys.error(parseResult.toString)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ class JsonTest {
assertEquals("{\"name\" : {\"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\"}}",
JSONObject(Map("name" -> JSONObject(TreeMap("name1" -> "va1ue1", "name2" -> "va1ue2")))).toString())

assertEquals("[4.0, 1.0, 3.0, 2.0, 6.0, 5.0, 8.0, 7.0]", JSONArray(List[Double](4,1,3,2,6,5,8,7)).toString())
val expected =
if (1.0.toString == "1") "[4, 1, 3, 2, 6, 5, 8, 7]"
else "[4.0, 1.0, 3.0, 2.0, 6.0, 5.0, 8.0, 7.0]"
assertEquals(expected, JSONArray(List[Double](4,1,3,2,6,5,8,7)).toString())
}

// A test method that escapes all characters in strings
Expand Down