Skip to content

Commit a20187a

Browse files
committed
Add support for Scala.js, with cross-compilation.
Scala versions were upgraded to 2.11.7 and 2.12.0-M3. JavaTokenParsers.identifier was rewritten not to use a regexp, but rather primitive parsers and Character.isJavaIdentifier{Start,Part}. The regexp-based implementation relies on Java-specific character ranges. JavaTokenParsers.stringLiteral was slightly adapted with an expansion of `\p{Cntrl}` into `[\x00-\x1F\x7F]`. The former character range is Java-specific. We also removed an invalid (and useless) `+` at the end of the regexp. The test t4929.scala is JVM-only.
1 parent fd09708 commit a20187a

39 files changed

+54
-28
lines changed

build.sbt

+40-23
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
11

2-
scalaModuleSettings
3-
4-
name := "scala-parser-combinators"
5-
6-
version := "1.1.0-SNAPSHOT"
7-
8-
scalaVersion := crossScalaVersions.value.head
9-
10-
crossScalaVersions := Seq("2.11.6", "2.12.0-M1")
11-
12-
// important!! must come here (why?)
13-
scalaModuleOsgiSettings
14-
15-
OsgiKeys.exportPackage := Seq(s"scala.util.parsing.*;version=${version.value}")
16-
17-
// needed to fix classloader issues (see scala-xml#20)
18-
fork in Test := true
19-
20-
libraryDependencies += "junit" % "junit" % "4.11" % "test"
21-
22-
libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test"
23-
24-
mimaPreviousVersion := None
2+
crossScalaVersions in ThisBuild := Seq("2.11.7", "2.12.0-M3")
3+
4+
lazy val `scala-parser-combinators` = crossProject.in(file(".")).
5+
settings(scalaModuleSettings: _*).
6+
jvmSettings(
7+
name := "scala-parser-combinators-jvm"
8+
).
9+
jsSettings(
10+
name := "scala-parser-combinators-js"
11+
).
12+
settings(
13+
moduleName := "scala-parser-combinators",
14+
version := "1.1.0-SNAPSHOT",
15+
scalaVersion := crossScalaVersions.value.head
16+
).
17+
jvmSettings(
18+
// important!! must come here (why?)
19+
scalaModuleOsgiSettings: _*
20+
).
21+
jvmSettings(
22+
OsgiKeys.exportPackage := Seq(s"scala.util.parsing.*;version=${version.value}"),
23+
24+
// needed to fix classloader issues (see scala-xml#20)
25+
fork in Test := true
26+
).
27+
jsSettings(
28+
// Scala.js cannot run forked tests
29+
fork in Test := false
30+
).
31+
jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin)).
32+
jvmSettings(
33+
libraryDependencies += "junit" % "junit" % "4.11" % "test",
34+
libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test"
35+
).
36+
settings(
37+
mimaPreviousVersion := None
38+
)
39+
40+
lazy val `scala-parser-combinatorsJVM` = `scala-parser-combinators`.jvm
41+
lazy val `scala-parser-combinatorsJS` = `scala-parser-combinators`.js

project/plugins.sbt

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
addSbtPlugin("org.scala-lang.modules" % "scala-module-plugin" % "1.0.3")
2+
3+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.6")

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ trait JavaTokenParsers extends RegexParsers {
2626
* <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8">The Java Language Spec</a>.
2727
* Generally, this means a letter, followed by zero or more letters or numbers.
2828
*/
29-
def ident: Parser[String] =
30-
"""\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*""".r
29+
def ident: Parser[String] = (
30+
"" ~> // handle whitespace
31+
rep1(acceptIf(Character.isJavaIdentifierStart)("identifier expected but `" + _ + "' found"),
32+
elem("identifier part", Character.isJavaIdentifierPart(_: Char))) ^^ (_.mkString)
33+
)
34+
3135
/** An integer, without sign or with a negative sign. */
3236
def wholeNumber: Parser[String] =
3337
"""-?\d+""".r
@@ -49,7 +53,7 @@ trait JavaTokenParsers extends RegexParsers {
4953
*/
5054
@migration("`stringLiteral` allows escaping single and double quotes, but not forward slashes any longer.", "2.10.0")
5155
def stringLiteral: Parser[String] =
52-
("\""+"""([^"\p{Cntrl}\\]|\\[\\'"bfnrt]|\\u[a-fA-F0-9]{4})*+"""+"\"").r
56+
("\""+"""([^"\x00-\x1F\x7F\\]|\\[\\'"bfnrt]|\\u[a-fA-F0-9]{4})*"""+"\"").r
5357
/** A number following the rules of `decimalNumber`, with the following
5458
* optional additions:
5559
*

src/test/scala/scala/util/parsing/combinator/JavaTokenParsersTest.scala renamed to shared/src/test/scala/scala/util/parsing/combinator/JavaTokenParsersTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class JavaTokenParsersTest {
9797
case Failure(message, next) =>
9898
assertEquals(next.pos.line, 1)
9999
assertEquals(next.pos.column, 1)
100-
assert(message.endsWith(s"regex `\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*' expected but `-' found"))
100+
assert(message.endsWith(s"identifier expected but `-' found"))
101101
case _ => sys.error(parseResult.toString)
102102
}
103103

src/test/scala/scala/util/parsing/combinator/JsonTest.scala renamed to shared/src/test/scala/scala/util/parsing/combinator/JsonTest.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ class JsonTest {
120120
assertEquals("{\"name\" : {\"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\"}}",
121121
JSONObject(Map("name" -> JSONObject(TreeMap("name1" -> "va1ue1", "name2" -> "va1ue2")))).toString())
122122

123-
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())
123+
val expected =
124+
if (1.0.toString == "1") "[4, 1, 3, 2, 6, 5, 8, 7]"
125+
else "[4.0, 1.0, 3.0, 2.0, 6.0, 5.0, 8.0, 7.0]"
126+
assertEquals(expected, JSONArray(List[Double](4,1,3,2,6,5,8,7)).toString())
124127
}
125128

126129
// A test method that escapes all characters in strings

0 commit comments

Comments
 (0)