Skip to content

Commit

Permalink
Accept //using directive without a space
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed Nov 8, 2021
1 parent a1a7c43 commit cb9c30e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@ import scala.build.preprocessing.directives.Directive
object TemporaryDirectivesParser {

private def ws[_: P] = P(" ".rep(1))
private def optWs[_: P] = P(" ".rep(0))
private def nl[_: P] = P(("\r".? ~ "\n").rep(1))
private def emptyLine[_: P] = P(ws.rep() ~ nl)

private def singleLineComment[_: P] =
P(ws.rep() ~ !("//" ~ ws ~ "require") ~ !("//" ~ ws ~ "using") ~ "//" ~ P(CharPred(c =>
P(ws.rep() ~ !("//" ~ optWs ~ "require") ~ !("//" ~ optWs ~ "using") ~ "//" ~ P(CharPred(c =>
c != '\n'
)).rep() ~ nl)
.map(_ => ())

private def directive[_: P] = {
def sc = P(";")
def tpe = {
def commentedUsingTpe = P("//" ~ ws ~ Index ~ "using")
def commentedUsingTpe = P("//" ~ optWs ~ Index ~ "using")
.map(actualStartIdx => (Directive.Using: Directive.Type, Some(actualStartIdx)))
def usingKeywordTpe = P("using")
.map(_ => (Directive.Using: Directive.Type, None))
def usingTpe = P(ws.? ~ (commentedUsingTpe | usingKeywordTpe) ~ !(ws ~ "target"))
def commentedRequireTpe = P("//" ~ ws ~ Index ~ "require")
def commentedRequireTpe = P("//" ~ optWs ~ Index ~ "require")
.map(actualStartIdx => (Directive.Require: Directive.Type, Some(actualStartIdx)))
def requireKeywordTpe = P("require")
.map(_ => (Directive.Require: Directive.Type, None))
def commentedUsingTargetTpe = P("//" ~ ws ~ Index ~ "using" ~ ws ~ "target")
def commentedUsingTargetTpe = P("//" ~ optWs ~ Index ~ "using" ~ ws ~ "target")
.map(actualStartIdx => (Directive.Require: Directive.Type, Some(actualStartIdx)))
def usingTargetKeywordTpe = P("using" ~ ws ~ "target")
.map(_ => (Directive.Require: Directive.Type, None))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ class TemporaryDirectivesParserTests extends munit.FunSuite {
expect(res == expectedRes)
}

test("no spaces after //") {
val res = TemporaryDirectivesParser.parseDirectives(
Left(""),
"""//using foo
|//require foo
|""".stripMargin
).map(_._1)
val expectedRes = Some(
Seq(
Directive(
Directive.Using,
Seq("foo"),
None,
isComment = true,
Position.File(Left(""), (0, 2), (0, 11))
),
Directive(
Directive.Require,
Seq("foo"),
None,
isComment = true,
Position.File(Left(""), (1, 2), (1, 14))
)
)
)
expect(res == expectedRes)
}

test("using target as require") {
val res = TemporaryDirectivesParser.parseDirectives(
Left(""),
Expand Down

0 comments on commit cb9c30e

Please sign in to comment.