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

improvement: Try and remove more macros from parsers #3359

Merged
merged 2 commits into from Oct 4, 2023

Conversation

tgodzik
Copy link
Collaborator

@tgodzik tgodzik commented Oct 3, 2023

Also some changes to make it cross compile

@tgodzik tgodzik force-pushed the remove-macro2 branch 2 times, most recently from 63433bc to 7f153d5 Compare October 3, 2023 17:26
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 ")
Copy link
Collaborator Author

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 {
Copy link
Collaborator Author

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

Copy link
Contributor

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
}

Copy link
Collaborator Author

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)
Copy link
Collaborator Author

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

Copy link
Contributor

Choose a reason for hiding this comment

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

debugges? typo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

@@ -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))
Copy link
Collaborator Author

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] {
Copy link
Collaborator Author

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.

@tgodzik tgodzik marked this pull request as ready for review October 3, 2023 17:41
@tgodzik tgodzik requested a review from kitbellew October 3, 2023 17:41
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

debugges? typo?

@@ -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}")
Copy link
Contributor

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?

Copy link
Collaborator Author

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 {
Copy link
Contributor

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
}

@tgodzik tgodzik requested a review from kitbellew October 4, 2023 12:19
})

doWhile {
parseRuleAfterBOF(parseSourceImpl())
Copy link
Contributor

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())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
builder += parseRuleAfterBOF(parseSourceImpl())
builder += parseRuleAfterBOF(parseSourceImpl())

Copy link
Collaborator Author

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 😅

@tgodzik
Copy link
Collaborator Author

tgodzik commented Oct 4, 2023

Ok, it's good now 😅

@tgodzik tgodzik requested a review from kitbellew October 4, 2023 15:10
@kitbellew kitbellew merged commit 69f5292 into scalameta:main Oct 4, 2023
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants