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

Support comments in headers #1168

Merged
merged 2 commits into from Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/main/scala/org/bykn/bosatsu/CommentStatement.scala
Expand Up @@ -31,12 +31,12 @@ object CommentStatement {
val commentBlock: P[NonEmptyList[String]] =
// if the next line is part of the comment until we see the # or not
(Parser.maybeSpace.with1.soft *> commentPart)
.repSep(sep = sep) <* Parser.newline.orElse(P.end)
.repSep(sep = sep) <* Parser.termination

(commentBlock ~ onP(indent))
.map { case (m, on) => CommentStatement(m, on) }
}

val commentPart: P[String] =
P.char('#') *> P.until0(P.char('\n'))
P.char('#') *> P.until0(Parser.termination)
}
14 changes: 10 additions & 4 deletions core/src/main/scala/org/bykn/bosatsu/Package.scala
Expand Up @@ -190,12 +190,17 @@ object Package {
Doc.intercalate(Doc.empty, p :: i :: e :: b)
}


def headerParser(defaultPack: Option[PackageName]): P0[Header] = {
// TODO: support comments before the Statement
val spaceComment: P0[Unit] =
(Parser.spaces.? ~ CommentStatement.commentPart.?).void

val eol = spaceComment <* Parser.termination
val parsePack = Padding
.parser(
(P.string("package")
.soft ~ spaces) *> PackageName.parser <* Parser.toEOL
.soft ~ spaces) *> PackageName.parser <* eol,
spaceComment
)
.map(_.padded)
val pname: P0[PackageName] =
Expand All @@ -204,12 +209,13 @@ object Package {
case Some(p) => parsePack.?.map(_.getOrElse(p))
}

val im = Padding.parser(Import.parser <* Parser.toEOL).map(_.padded).rep0
val im = Padding.parser(Import.parser <* eol, spaceComment).map(_.padded).rep0
val ex = Padding
.parser(
(P.string("export")
.soft ~ spaces) *> ExportedName.parser.itemsMaybeParens
.map(_._2) <* Parser.toEOL
.map(_._2) <* eol,
spaceComment
)
.map(_.padded)

Expand Down
6 changes: 4 additions & 2 deletions core/src/main/scala/org/bykn/bosatsu/Padding.scala
Expand Up @@ -22,13 +22,15 @@ object Padding {

/** This allows an empty padding
*/
def parser[T](p: P[T]): P[Padding[T]] = {
val spacing = (maybeSpace.with1.soft ~ Parser.newline).void.rep0
def parser[T](p: P[T], space: P0[Unit]): P[Padding[T]] = {
val spacing = (space.with1.soft ~ Parser.newline).void.rep0

(spacing.with1.soft ~ p)
.map { case (vec, t) => Padding(vec.size, t) }
}

def parser[T](p: P[T]): P[Padding[T]] = parser(p, maybeSpace)

/** Parses a padding of length 1 or more, then p
*/
def parser1[T](p: P0[T]): P[Padding[T]] =
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/scala/org/bykn/bosatsu/Parser.scala
Expand Up @@ -448,7 +448,8 @@ object Parser {
(P.char('(') ~ ws) *> pa <* (ws ~ P.char(')'))

val newline: P[Unit] = P.char('\n')
val toEOL: P0[Unit] = maybeSpace *> newline.orElse(P.end)
val termination: P0[Unit] = newline.orElse(P.end)
val toEOL: P0[Unit] = maybeSpace *> termination
val toEOL1: P[Unit] = maybeSpace.with1 *> newline

def optionParse[A](pa: P0[A], str: String): Option[A] =
Expand Down
5 changes: 4 additions & 1 deletion core/src/test/scala/org/bykn/bosatsu/ParserTest.scala
Expand Up @@ -1722,9 +1722,12 @@ external def foo2(i: Integer, b: a) -> String
roundTrip(
Package.parser(None),
"""
# we can comment the package
package Foo/Bar
# comments are allowed
from Baz import Bippy
export foo
# even here
export foo # or here

foo = 1
"""
Expand Down