Skip to content
This repository was archived by the owner on Mar 11, 2020. It is now read-only.

Commit e4a5db8

Browse files
committed
Support empty configs
Fixes bug reported in #49 by @jedesah.
1 parent daf7c8e commit e4a5db8

4 files changed

Lines changed: 25 additions & 8 deletions

File tree

core/src/main/scala/knobs/Parser.scala

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,18 @@ object ConfigParser {
3939

4040
/** Top-level parser of configuration files */
4141
lazy val topLevel: Parser[List[Directive]] = "configuration" |: {
42-
directives << skipLWS << realEOF
42+
skipLWS >> directives << realEOF
4343
}
4444

4545
/** Parser of configuration files that don't support import directives */
46-
lazy val sansImport: Parser[List[Directive]] = {
47-
val d = (skipLWS >> choice(bindDirective, groupDirective) << skipHWS)
48-
d.map2(attempt(newline >> d).many)(_ :: _)
49-
}
46+
lazy val sansImport: Parser[List[Directive]] =
47+
(choice(bindDirective, groupDirective) << skipHWS).sepEndBy(newline << skipLWS)
5048

5149
lazy val directives: Parser[List[Directive]] =
52-
directive.map2(attempt(newline >> directive).many)(_ :: _)
50+
(directive << skipHWS).sepEndBy(newline << skipLWS)
5351

5452
lazy val directive: Parser[Directive] =
55-
(skipLWS >> choice(importDirective, bindDirective, groupDirective) << skipHWS) scope
56-
"directive"
53+
choice(importDirective, bindDirective, groupDirective) scope "directive"
5754

5855
lazy val importDirective = "import directive" |: {
5956
word("import") >> skipLWS >> stringLiteral.map(Import(_))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
# param1=true
3+
# param2="hello"
4+
5+
# group {
6+
# this.group.is = "commented out"
7+
# }

core/src/test/resources/pathological.cfg

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
3+
4+
# ^^^ Start with some irritating whitespace
5+
16
# Comment
27

38
aa # Comment
@@ -41,3 +46,8 @@ dur = 5 seconds
4146
ca.cb {
4247
cd = "fuf"
4348
}
49+
50+
# And finish with some more irritating whitespace
51+
52+
53+

core/src/test/scala/knobs/Test.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ object Test extends Properties("Knobs") {
164164
}
165165
}
166166

167+
lazy val allCommentsTest: Task[Prop] =
168+
load(List(Required(ClassPathResource("all-comments.cfg")))).attempt.map(_.isRight)
167169

168170
property("load-pathological-config") = loadTest.unsafePerformSync
169171

@@ -203,4 +205,5 @@ object Test extends Properties("Knobs") {
203205

204206
property("mutable-config-value-error") = mutableConfigValueErrorTest.unsafePerformSync
205207

208+
property("all-comments") = allCommentsTest.unsafePerformSync
206209
}

0 commit comments

Comments
 (0)