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 1 commit
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
12 changes: 8 additions & 4 deletions core/src/main/scala/org/bykn/bosatsu/Package.scala
Expand Up @@ -191,11 +191,14 @@ object Package {
}

def headerParser(defaultPack: Option[PackageName]): P0[Header] = {
// TODO: support comments before the Statement
val spaceComment: P0[Unit] =
(Parser.spaces | (P.char('#') <* P.charsWhile0(_ != '\n'))).?.void
Copy link
Owner Author

Choose a reason for hiding this comment

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

Parser should be moved to a private val above.

val eol = Parser.commentToEOL.void | Parser.toEOL
Copy link
Owner Author

Choose a reason for hiding this comment

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

This should be spaceComment *> 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 +207,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
6 changes: 5 additions & 1 deletion core/src/main/scala/org/bykn/bosatsu/Parser.scala
Expand Up @@ -448,7 +448,11 @@ 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)
// The comment excluding the leading # up to the EOL or EOF
val termination: P0[Unit] = newline.orElse(P.end)
val commentToEOL: P[String] =
P.char('#') *> P.charsWhile0(_ != '\n') <* termination
Copy link
Owner Author

Choose a reason for hiding this comment

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

This only parses a single comment to EOL. We could have several. This isn't a problem for the code as it is now due to the way it's used but it's dangerous to leave it around since it isn't clear if it parses one or many lines.

Also it doesn't allow space then a comment which makes it rarely useful.

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