-
Notifications
You must be signed in to change notification settings - Fork 223
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
improvement: Try and remove more macros from parsers #3359
Conversation
63433bc
to
7f153d5
Compare
private def hspaces0[_: P] = hspacesMin(0) | ||
private def hspaces1[_: P] = hspacesMin(1) | ||
private def hspacesMinWithLen[_: P](min: Int): P[Int] = | ||
private def hspace[$: P] = CharIn("\t\r ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the previous breaks in Scala 3 and it's also changed in the examples of fastparse https://com-lihaoyi.github.io/fastparse/
do { | ||
|
||
builder += parseRuleAfterBOF(parseSourceImpl()) | ||
while (in.token match { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no do ... while()
construct for Scala 3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we have it in many places, and since in at least one you define a method, why not define our own:
// in companion object ScalametaParser, or perhaps even in scala.meta.common somewhere
def doWhile(body: => Unit)(cond: => Boolean): Unit = {
body
while (cond)
body
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, done!
@@ -196,6 +198,10 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser => | |||
@inline private def tryAheadNot[T: ClassTag]: Boolean = | |||
nextIf(!peekToken.is[T]) | |||
|
|||
private def unreachable(debugges: Map[String, Any]): Nothing = UnreachableError.raise(debugges) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another set of macros that were useful, but were possible to replace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debugges? typo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
Outdated
Show resolved
Hide resolved
@@ -583,7 +594,7 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser => | |||
// Unquote's pt may not be directly equal unwrapped ellipsis's pt, but be its refinement instead. | |||
// For example, in `new { ..$stats }`, ellipsis's pt is List[Stat], but quasi's pt is Term. | |||
// This is an artifact of the current implementation, so we just need to keep it mind and work around it. | |||
require(classTag[T].runtimeClass.isAssignableFrom(tree.pt) && debug(ell, tree, tree.structure)) | |||
assert(classTag[T].runtimeClass.isAssignableFrom(tree.pt) && debug(ell, tree, tree.structure)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require
is another macro
@@ -6,7 +6,7 @@ import org.scalameta.data._ | |||
import scala.meta.inputs._ | |||
import scala.meta.internal.inputs._ | |||
|
|||
@root trait Parsed[+T] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why this needed to be marked as ast since this are perfectly fine as case classes.
@@ -196,6 +198,10 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser => | |||
@inline private def tryAheadNot[T: ClassTag]: Boolean = | |||
nextIf(!peekToken.is[T]) | |||
|
|||
private def unreachable(debugges: Map[String, Any]): Nothing = UnreachableError.raise(debugges) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debugges? typo?
scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala
Outdated
Show resolved
Hide resolved
@@ -1875,7 +1889,7 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser => | |||
}).map(_.reduceWith(toParamClause(None))) | |||
|
|||
def implicitClosure(location: Location): Term.Function = { | |||
require(token.isNot[KwImplicit] && debug(token)) | |||
assert(token.isNot[KwImplicit], s"Expected implicit keyword but found ${token.name}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, it's the opposite. expected to be after the implicit keyword... why not make implicitClosure private and remove the assert?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! That's much better, I don't love putting asserts anywhere in the code aside from tests.
do { | ||
|
||
builder += parseRuleAfterBOF(parseSourceImpl()) | ||
while (in.token match { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we have it in many places, and since in at least one you define a method, why not define our own:
// in companion object ScalametaParser, or perhaps even in scala.meta.common somewhere
def doWhile(body: => Unit)(cond: => Boolean): Unit = {
body
while (cond)
body
}
}) | ||
|
||
doWhile { | ||
parseRuleAfterBOF(parseSourceImpl()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
builder +
?
}) | ||
|
||
doWhile { | ||
builder += parseRuleAfterBOF(parseSourceImpl()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
builder += parseRuleAfterBOF(parseSourceImpl()) | |
builder += parseRuleAfterBOF(parseSourceImpl()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was too quick with pushing 😅
Ok, it's good now 😅 |
Also some changes to make it cross compile