Skip to content
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

[not for merge] Update to fastparse 2 (#510) #541

Closed
wants to merge 6 commits into from
Closed
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
22 changes: 7 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ stages:
if: (branch = master AND type = push AND repo = "scalapb/ScalaPB") OR (tag IS present)

scala:
- 2.10.7
- 2.11.12
- 2.12.8

Expand All @@ -19,22 +18,15 @@ env:
- TEST_SCRIPT=mima.sh
- TEST_SCRIPT=proptest.sh

matrix:
exclude:
- scala: 2.10.7
env: TEST_SCRIPT=e2e.sh E2E_SHADED=0
- scala: 2.10.7
env: TEST_SCRIPT=e2e.sh E2E_SHADED=1

jobs:
include:
- env: SCALA_NATIVE
scala: 2.11.12
before_install:
- sudo apt-get update
- curl https://raw.githubusercontent.com/scala-native/scala-native/master/scripts/travis_setup.sh | bash -x
script:
- sbt ++2.11.12 runtimeNative/test lensesNative/test
#- env: SCALA_NATIVE
# scala: 2.11.12
# before_install:
# - sudo apt-get update
# - curl https://raw.githubusercontent.com/scala-native/scala-native/master/scripts/travis_setup.sh | bash -x
# script:
# - sbt ++2.11.12 runtimeNative/test lensesNative/test
- env: EXAMPLES_AND_FORMATTING
scala: 2.11.12
script:
Expand Down
28 changes: 18 additions & 10 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import ReleaseTransformations._
import sbtcrossproject.CrossPlugin.autoImport.{CrossType, crossProject}

val Scala210 = "2.10.7"

val Scala211 = "2.11.12"

val Scala212 = "2.12.8"
Expand All @@ -18,7 +16,7 @@ val grpcVersion = "1.18.0"

scalaVersion in ThisBuild := Scala212

crossScalaVersions in ThisBuild := Seq(Scala210, Scala211, Scala212)
crossScalaVersions in ThisBuild := Seq(Scala211, Scala212)

scalacOptions in ThisBuild ++= {
CrossVersion.partialVersion(scalaVersion.value) match {
Expand Down Expand Up @@ -85,12 +83,14 @@ lazy val root =
proptest,
scalapbc)

lazy val runtime = crossProject(JSPlatform, JVMPlatform, NativePlatform)
// fastparse 2 is not available for Scala Native yet
// https://github.com/lihaoyi/fastparse/issues/215
lazy val runtime = crossProject(JSPlatform, JVMPlatform/*, NativePlatform*/)
.crossType(CrossType.Full).in(file("scalapb-runtime"))
.settings(
name := "scalapb-runtime",
libraryDependencies ++= Seq(
"com.lihaoyi" %%% "fastparse" % "1.0.0",
"com.lihaoyi" %%% "fastparse" % "2.1.0",
"com.lihaoyi" %%% "utest" % "0.6.6" % "test",
"commons-codec" % "commons-codec" % "1.11" % "test",
"com.google.protobuf" % "protobuf-java-util" % protobufVersion % "test",
Expand Down Expand Up @@ -124,12 +124,16 @@ lazy val runtime = crossProject(JSPlatform, JVMPlatform, NativePlatform)
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.descriptors.FieldDescriptor.buildFieldDescriptor"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.descriptors.FieldDescriptor.this"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.descriptors.Descriptor.this"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.descriptors.EnumDescriptor.this")
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.descriptors.EnumDescriptor.this"),

// Parsing
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.textformat.Basics.*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.textformat.ProtoAsciiParser.*"),
)
}
)
.dependsOn(lenses)
.platformsSettings(JSPlatform, NativePlatform)(
.platformsSettings(JSPlatform/*, NativePlatform*/)(
libraryDependencies ++= Seq(
"com.thesamet.scalapb" %%% "protobuf-runtime-scala" % "0.7.1"
),
Expand All @@ -152,14 +156,16 @@ lazy val runtime = crossProject(JSPlatform, JVMPlatform, NativePlatform)
},
unmanagedResourceDirectories in Compile += baseDirectory.value / "../../third_party"
)
/*
.nativeSettings(
sharedNativeSettings
)
*/


lazy val runtimeJVM = runtime.jvm
lazy val runtimeJS = runtime.js
lazy val runtimeNative = runtime.native
//lazy val runtimeNative = runtime.native

lazy val grpcRuntime = project.in(file("scalapb-runtime-grpc"))
.dependsOn(runtimeJVM)
Expand Down Expand Up @@ -307,7 +313,7 @@ createVersionFile := {
log.info(s"Created $f2")
}

lazy val lenses = crossProject(JSPlatform, JVMPlatform, NativePlatform).in(file("lenses"))
lazy val lenses = crossProject(JSPlatform, JVMPlatform/*, NativePlatform*/).in(file("lenses"))
.settings(
name := "lenses",
sources in Test := {
Expand Down Expand Up @@ -340,13 +346,15 @@ lazy val lenses = crossProject(JSPlatform, JVMPlatform, NativePlatform).in(file(
s"-P:scalajs:mapSourceURI:$a->$g/"
}
)
/*
.nativeSettings(
sharedNativeSettings
)
*/

lazy val lensesJVM = lenses.jvm
lazy val lensesJS = lenses.js
lazy val lensesNative = lenses.native
//lazy val lensesNative = lenses.native

lazy val docs = project.in(file("docs"))
.enablePlugins(MicrositesPlugin, ScalaUnidocPlugin)
Expand Down
12 changes: 6 additions & 6 deletions e2e/src/test/scala/scalapb/TextFormatSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ class TextFormatSpec extends FlatSpec with GeneratorDrivenPropertyChecks with Mu
"optional_string: \"ueoauaoe\noptional_int32: 123" must failParsingWith(
EXPECTED_FIELD)
"[nosuchext]: 123" must failParsingWith(
"expected identifier")
"Expected Message")
"[protobuf_unittest.optional_int32_extension]: 123" must failParsingWith(
"expected identifier")
"Expected Message")
"nosuchfield: 123" must failParsingWith(
"Unknown field name 'nosuchfield' (line 1, column 1)")
"optional_nested_enum: NO_SUCH_VALUE" must failParsingWith(
Expand Down Expand Up @@ -324,16 +324,16 @@ class TextFormatSpec extends FlatSpec with GeneratorDrivenPropertyChecks with Mu
}

def parseInt32[T](input: String): Int =
AstUtils.parseInt32(ProtoAsciiParser.PrimitiveValue.parse(input).get.value).right.get.value
AstUtils.parseInt32(fastparse.parse(input, ProtoAsciiParser.PrimitiveValue(_)).get.value).right.get.value

def parseInt64[T](input: String): Long =
AstUtils.parseInt64(ProtoAsciiParser.PrimitiveValue.parse(input).get.value).right.get.value
AstUtils.parseInt64(fastparse.parse(input, ProtoAsciiParser.PrimitiveValue(_)).get.value).right.get.value

def parseUInt32[T](input: String): Int =
AstUtils.parseUint32(ProtoAsciiParser.PrimitiveValue.parse(input).get.value).right.get.value
AstUtils.parseUint32(fastparse.parse(input, ProtoAsciiParser.PrimitiveValue(_)).get.value).right.get.value

def parseUInt64[T](input: String): Long =
AstUtils.parseUint64(ProtoAsciiParser.PrimitiveValue.parse(input).get.value).right.get.value
AstUtils.parseUint64(fastparse.parse(input, ProtoAsciiParser.PrimitiveValue(_)).get.value).right.get.value

"testParseInteger" should "pass" in {
0 must be(parseInt32( "0"))
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.16")

addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.6.0")

addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "0.6.0")
//addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "0.6.0")

addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)

Expand Down
2 changes: 1 addition & 1 deletion proptest/src/test/scala/SchemaGenerators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ object SchemaGenerators {
jarForClass[com.google.common.util.concurrent.ListenableFuture[_]],
jarForClass[javax.annotation.Nullable],
jarForClass[scalapb.lenses.Lens[_, _]].getPath,
jarForClass[fastparse.core.Parsed[_, _, _]].getPath,
jarForClass[fastparse.Parsed[_]].getPath,
rootDir
)
val annotationJar =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scalapb

import scalapb.textformat.{AstUtils, Printer, ProtoAsciiParser}
import fastparse.core.{ParseError, Parsed}
import fastparse._

case class TextFormatError(msg: String)

Expand All @@ -20,13 +20,13 @@ object TextFormat {
d: GeneratedMessageCompanion[T],
s: String
): Either[TextFormatError, T] = {
ProtoAsciiParser.Message.parse(s) match {
parse(s, ProtoAsciiParser.Message(_)) match {
case Parsed.Success(msg, _) =>
AstUtils.parseMessage(d, msg).left.map { a =>
TextFormatError(s"${a.error} (${indexToLineCol(s, a.index)})")
}
case f: Parsed.Failure[Char, String] =>
Left(TextFormatError(ParseError(f).getMessage))
case f: Parsed.Failure =>
Left(TextFormatError(f.trace(true).longMsg))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package scalapb.textformat

import fastparse.core.ParserApi
import fastparse.NoWhitespace._

import scala.language.implicitConversions

object Basics extends ParserCompat {

import fastparse.all._
import fastparse._

val Newline = P(StringIn("\r\n", "\n"))
def Newline[_: P] = P(StringIn("\r\n", "\n"))

case class NamedFunction[T, V](f: T => V, name: String) extends (T => V) {
def apply(t: T) = f(t)
Expand All @@ -24,49 +24,52 @@ object Basics extends ParserCompat {
val CharChunk = NamedFunction((c: Char) => !"\n\r".contains(c), "CharChunk")

// TODO(nadavsr): figure out this
val sameLineCharChunks = P(CharsWhile(CharChunk) | (!Newline) ~ AnyChar)
def sameLineCharChunks[_: P] = P(CharsWhile(CharChunk) | (!Newline) ~ AnyChar)

val lineComment = P("#" ~ sameLineCharChunks.rep ~ &(Newline | End))
def lineComment[_: P] = P("#" ~ sameLineCharChunks.rep ~ &(Newline | End))

val whiteSpace = (CharIn(" \n\r\t\f") | lineComment).opaque("whitespace").rep
def whiteSpace[_: P] = (CharIn(" \n\r\t\f") | lineComment).opaque("whitespace").rep

val identifier = P(CharIn('a' to 'z', 'A' to 'Z', '0' to '9', "_").rep(1).!).opaque("identifier")
def identifier[_: P] = P(CharIn("a-z", "A-Z", "0-9", "_").rep(1).!).opaque("identifier")

val literal = P(CharIn('a' to 'z', 'A' to 'Z', '0' to '9', "_-.").rep(1).!).opaque("literal")
def literal[_: P] = P(CharIn("a-z", "A-Z", "0-9", "_\\-.").rep(1).!).opaque("literal")

val digits = P(CharsWhile(Digits))
val hexDigits = P(CharsWhile(HexDigits))
val octDigits = P(CharsWhile(OctDigits))
def digits[_: P] = P(CharsWhile(Digits))
def hexDigits[_: P] = P(CharsWhile(HexDigits))
def octDigits[_: P] = P(CharsWhile(OctDigits))

val exponent = P(CharIn("eE") ~ CharIn("+-").? ~ digits)
val fractional =
(CharIn("+-").? ~ (digits ~ "." ~ digits.? | "." ~ digits) ~ exponent.? ~ CharIn("fF").?).!
def exponent[_: P] = P(CharIn("eE") ~ CharIn("+\\-").? ~ digits)
def fractional[_: P] =
(CharIn("+\\-").? ~ (digits ~ "." ~ digits.? | "." ~ digits) ~ exponent.? ~ CharIn("fF").?).!

val decIntegral = P("0" | CharIn('1' to '9') ~ digits.?).!.map(p => BigInt(p))
val hexIntegral = P("0x" ~/ hexDigits.!).map(p => BigInt(p, 16))
val octIntegral = P("0" ~ octDigits.!).map(p => BigInt(p, 8))
def decIntegral[_: P] = P("0" | CharIn("1-9") ~ digits.?).!.map(p => BigInt(p))
def hexIntegral[_: P] = P("0x" ~/ hexDigits.!).map(p => BigInt(p, 16))
def octIntegral[_: P] = P("0" ~ octDigits.!).map(p => BigInt(p, 8))

val integral: P[BigInt] = P(hexIntegral | octIntegral | decIntegral)
def integral[_: P]: P[BigInt] = P(hexIntegral | octIntegral | decIntegral)

val bigInt: P[BigInt] = P(CharIn("+-").!.? ~ integral).map({
case (Some("-"), number) => -number
case (_, number) => number
})
def bigInt[_: P]: P[BigInt] =
P(CharIn("+\\-").!.? ~ integral).map({
case (Some("-"), number) => -number
case (_, number) => number
})

val strNoDQChars = P(CharsWhile(!"\"\n\\".contains(_: Char)))
val strNoQChars = P(CharsWhile(!"'\n\\".contains(_: Char)))
val escape = P("\\" ~ AnyChar)
val singleBytesLiteral = P(
"\"" ~/ (strNoDQChars | escape).rep.! ~ "\"" |
"'" ~/ (strNoQChars | escape).rep.! ~ "'"
).opaque("string")
def strNoDQChars[_: P] = P(CharsWhile(!"\"\n\\".contains(_: Char)))
def strNoQChars[_: P] = P(CharsWhile(!"'\n\\".contains(_: Char)))
def escape[_: P] = P("\\" ~ AnyChar)
def singleBytesLiteral[_: P] =
P(
"\"" ~/ (strNoDQChars | escape).rep.! ~ "\"" |
"'" ~/ (strNoQChars | escape).rep.! ~ "'"
).opaque("string")

val bytesLiteral = P(singleBytesLiteral.rep(1, whiteSpace)).map(_.mkString)
def bytesLiteral[_: P] = P(singleBytesLiteral.rep(1, whiteSpace)).map(_.mkString)

val boolean: P[Boolean] = P(
("true" | "t" | "1").map(_ => true) |
("false" | "f" | "0").map(_ => false)
).opaque("'true' or 'false'")
def boolean[_: P]: P[Boolean] =
P(
("true" | "t" | "1").map(_ => true) |
("false" | "f" | "0").map(_ => false)
).opaque("'true' or 'false'")

def ws(s: String): P[Unit] = P(s ~ &(whiteSpace))
def ws[_: P](s: String): P[Unit] = P(s ~ &(whiteSpace))
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
package scalapb.textformat

import fastparse.WhitespaceApi
import fastparse._
import fastparse.ScriptWhitespace._
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure this correctly corresponds to Basics.whiteSpace


import scala.language.implicitConversions

private[scalapb] object ProtoAsciiParser {
val White = WhitespaceApi.Wrapper {
import fastparse.all._

NoTrace(Basics.whiteSpace)
}

import fastparse.noApi._

// This is needed due to https://github.com/lihaoyi/fastparse/issues/72
protected implicit def strToParserApi(s: String): WhitespaceApi[Unit] = White.parserApi(s)
protected implicit def parserToParserApi[T](s: Parser[T]): WhitespaceApi[T] = White.parserApi(s)

val PrimitiveValue: P[TPrimitive] = P(
def PrimitiveValue[_: P]: P[TPrimitive] = P(
(Index ~ Basics.fractional).map(TLiteral.tupled) |
(Index ~ Basics.bigInt).map(TIntLiteral.tupled) |
(Index ~ Basics.bytesLiteral).map(TBytes.tupled) |
(Index ~ Basics.literal).map(TLiteral.tupled)
)

val MessageValue: P[TMessage] = P(
Index ~ "{" ~/ KeyValue.rep ~/ "}" |
Index ~ "<" ~/ KeyValue.rep ~/ ">"
).map(TMessage.tupled)
def MessageValue[_: P]: P[TMessage] =
P(
Index ~ "{" ~/ KeyValue.rep ~/ "}" |
Index ~ "<" ~/ KeyValue.rep ~/ ">"
).map(TMessage.tupled)

val ValueArray: P[TValue] =
P((Index ~ "[" ~/ (PrimitiveValue | MessageValue).rep(0, ",".~/) ~/ "]")).map(TArray.tupled)
def ValueArray[_: P]: P[TValue] =
P((Index ~ "[" ~/ (PrimitiveValue | MessageValue).rep(0, ",") ~/ "]")).map(TArray.tupled)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the .~/ was meaningful here?


val MessageArray: P[TValue] =
def MessageArray[_: P]: P[TValue] =
P((Index ~ "[" ~/ MessageValue.rep(0, ",") ~/ "]")).map(TArray.tupled)

val Value: P[TValue] = P(
MessageValue | MessageArray |
":" ~/ (MessageValue | ValueArray | PrimitiveValue)
).opaque("':', '{', '<', or '['")
def Value[_: P]: P[TValue] =
P(
MessageValue | MessageArray |
":" ~/ (MessageValue | ValueArray | PrimitiveValue)
).opaque("':', '{', '<', or '['")

val KeyValue: P[TField] = P(
Index ~ Basics.identifier ~/ Value
).map(TField.tupled)
def KeyValue[_: P]: P[TField] =
P(
Index ~ Basics.identifier ~/ Value
).map(TField.tupled)

val Message: P[TMessage] = P(Index ~ KeyValue.rep ~ End).map(TMessage.tupled)
def Message[_: P]: P[TMessage] = P(Index ~ KeyValue.rep ~ End).map(TMessage.tupled)
}
Loading