Skip to content

Commit

Permalink
do a little more compile-time work to avoid eof1 and trailing1
Browse files Browse the repository at this point in the history
  • Loading branch information
fhackett committed Oct 24, 2024
1 parent f9b793a commit 12673b7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
44 changes: 18 additions & 26 deletions SeqPattern.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,16 @@ final class SeqPattern[+T](val manip: Manip[(Manip.Handle, Boolean, T)])
Fields(flds1.fields ++ flds2.fields)

@scala.annotation.targetName("fieldsEnd")
def ~[Tpl <: Tuple](using T <:< Fields[Tpl])(using
scala.util.NotGiven[Tpl <:< Tuple1[?]]
)(using DebugInfo)(other: eof.type): SeqPattern[Tpl] =
this.productAtRightSibling(atEnd).map(_._1.fields)
def ~[Tpl <: Tuple, T2](using T <:< Fields[Tpl])(using maybeStrip: Fields.MaybeStripTuple1[Tpl, T2])(using DebugInfo)(other: eof.type): SeqPattern[T2] =
this.productAtRightSibling(atEnd).map(flds => maybeStrip(flds._1.fields))

@scala.annotation.targetName("fieldsTrailing")
def ~[Tpl <: Tuple](using
def ~[Tpl <: Tuple, T2](using
T <:< Fields[Tpl]
)(using scala.util.NotGiven[Tpl <:< Tuple1[?]])(
)(using maybeStrip: Fields.MaybeStripTuple1[Tpl, T2])(
other: trailing.type
): SeqPattern[Tpl] =
this.map(_.fields)

@scala.annotation.targetName("fieldsEndTuple1")
def ~[TT](using T <:< Fields[Tuple1[TT]])(using DebugInfo)(
other: eof1.type
): SeqPattern[TT] =
this.productAtRightSibling(atEnd).map(_._1.fields._1)

@scala.annotation.targetName("fieldsTrailingTuple1")
def ~[TT](using T <:< Fields[Tuple1[TT]])(
other: trailing1.type
): SeqPattern[TT] =
this.map(_.fields._1)
): SeqPattern[T2] =
this.map(flds => maybeStrip(flds.fields))

def stripFields[TT](using T <:< Fields[TT]): SeqPattern[TT] =
this.map(t => t.fields)
Expand Down Expand Up @@ -147,10 +133,18 @@ object SeqPattern:
tok(token)

final class Fields[T](val fields: T) extends AnyVal
object Fields:
sealed trait MaybeStripTuple1[Tpl <: Tuple, T]:
def apply(tpl: Tpl): T

given stripTuple1[T]: MaybeStripTuple1[Tuple1[T], T] with
def apply(tpl: Tuple1[T]): T = tpl._1

given notTuple1[Tpl <: Tuple](using scala.util.NotGiven[Tpl <:< Tuple1[?]]): MaybeStripTuple1[Tpl, Tpl] with
def apply(tpl: Tpl): Tpl = tpl

case object FieldsEndMarker
case object FieldsTrailingMarker
case object FieldsEndTuple1Marker
case object FieldsTrailingTuple1Marker

object ops:
def refine[T](manip: Manip[T]): SeqPattern[T] =
Expand Down Expand Up @@ -203,9 +197,7 @@ object SeqPattern:

export SeqPattern.{
FieldsEndMarker as eof,
FieldsEndTuple1Marker as eof1,
FieldsTrailingMarker as trailing,
FieldsTrailingTuple1Marker as trailing1
FieldsTrailingMarker as trailing
}

def not[T](using DebugInfo)(pattern: SeqPattern[T]): SeqPattern[Unit] =
Expand Down Expand Up @@ -234,7 +226,7 @@ object SeqPattern:
DebugInfo
)(sep: SeqPattern[?])(pattern: SeqPattern[T]): SeqPattern[List[T]] =
(field(pattern) ~ field(
repeated(skip(sep) ~ field(pattern) ~ trailing1)
repeated(skip(sep) ~ field(pattern) ~ trailing)
) ~ trailing)
.map(_ :: _)

Expand Down
4 changes: 2 additions & 2 deletions SeqPattern.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SeqPatternTests extends munit.FunSuite:

on(
field(repeated(tok(tok1)))
~ eof1
~ eof
| SeqPattern.pure("no")
)
.onChildren("fields repeated: 0")()(Nil)
Expand All @@ -60,7 +60,7 @@ class SeqPatternTests extends munit.FunSuite:
skip(tok(tok1))
~ field(tok(tok2))
~ skip(tok(tok3))
~ eof1
~ eof
| SeqPattern.pure("no")
)
.onChildren("fields skips: exact")(tok1(), tok2(), tok3())(tok2())
Expand Down
2 changes: 1 addition & 1 deletion Wellformed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ final class Wellformed private (
tok(SList).withChildren:
skip(tok(Atom).src(txt))
~ field(tok(Atom).map(_.sourceRange))
~ eof1
~ eof
~ field:
optional:
tok(SList).withChildren:
Expand Down
10 changes: 5 additions & 5 deletions tla/TLAParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object TLAParser extends PassSeq:
~ field:
tok(ParenthesesGroup).withChildren:
field(repeatedSepBy(`,`)(Alpha.src("_")).map(_.size))
~ eof1
~ eof
~ trailing

val unitDefns = passDef:
Expand Down Expand Up @@ -77,8 +77,8 @@ object TLAParser extends PassSeq:
repeatedSepBy1(skip(optional(Comment)) ~ skip(`,`)):
skip(optional(Comment))
~ field(Alpha)
~ trailing1
) ~ trailing1
~ trailing
) ~ trailing
) ~ field(optional(anyChild))
~ trailing
).rewrite: (m, name, extendsOpt, endOpt) =>
Expand Down Expand Up @@ -166,7 +166,7 @@ object TLAParser extends PassSeq:
| on(
skip(Alpha.src("VARIABLE") | Alpha.src("VARIABLES"))
~ field(repeatedSepBy1(`,`)(Alpha))
~ trailing1
~ trailing
).rewrite: vars =>
splice(
vars.map: v =>
Expand All @@ -179,7 +179,7 @@ object TLAParser extends PassSeq:
repeatedSepBy1(`,`):
opDeclPattern
| tok(Alpha)
) ~ trailing1
) ~ trailing
).rewrite: decls =>
splice(
decls.iterator
Expand Down

0 comments on commit 12673b7

Please sign in to comment.