Skip to content

Commit

Permalink
Support comments in headers (#1168)
Browse files Browse the repository at this point in the history
* Support comments in headers

* share more code
  • Loading branch information
johnynek authored Mar 16, 2024
1 parent 10aeaa3 commit 4689044
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/org/bykn/bosatsu/CommentStatement.scala
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 4689044

Please sign in to comment.