Skip to content

Commit

Permalink
Include new lines for single quote style (#65)
Browse files Browse the repository at this point in the history
* Include new lines for single quote style
  • Loading branch information
lwronski authored Sep 6, 2021
1 parent 99ef506 commit b01e6bc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ private[yaml] class Scanner(str: String) extends Tokenizer {
Scalar(choompedScalar, ScalarStyle.Folded, pos)

private def parseSingleQuoteValue(): Token = {
val sb = new StringBuilder
val singleQuoteIndent = indent
val sb = new StringBuilder

@tailrec
def readScalar(): String =
in.peek() match
Expand All @@ -217,15 +217,16 @@ private[yaml] class Scanner(str: String) extends Tokenizer {
sb.append('\'')
readScalar()
case Some('\n') =>
in.skipCharacter()
skipUntilNextIndent(singleQuoteIndent)
sb.append(' ')
skipUntilNextChar()
readScalar()
case Some('\'') | None =>
case Some('\'') =>
in.skipCharacter()
sb.result()
case Some(char) =>
sb.append(in.read())
readScalar()
case None => sb.result()

val pos = in.pos()
in.skipCharacter() // skip single quote
Expand Down Expand Up @@ -293,5 +294,8 @@ private[yaml] class Scanner(str: String) extends Tokenizer {
indent += 1
in.skipCharacter()

def skipUntilNextChar() =
while (in.isWhitespace) do in.skipCharacter()

private def skipComment(): Unit = while !in.isNewline do in.skipCharacter()
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ class ScalarSpec extends BaseParseSuite:
assertEventsEquals(events, expectedEvents)
}

test("should parse single quote scalar value with multiline") {
val yaml =
s"""description: 'multiline
| plain
| scalar'
|type: string
|""".stripMargin

val reader = Scanner(yaml)
val events = ParserImpl.getEvents(reader)

val expectedEvents = List(
StreamStart,
DocumentStart(),
MappingStart(),
Scalar("description", ScalarStyle.Plain),
Scalar("multiline plain scalar", ScalarStyle.SingleQuoted),
Scalar("type", ScalarStyle.Plain),
Scalar("string", ScalarStyle.Plain),
MappingEnd(),
DocumentEnd(),
StreamEnd
)

assertEventsEquals(events, expectedEvents)
}

test("should parse value with double quote") {
val yaml =
s"""| "/mnt/ iscsipd"
Expand Down Expand Up @@ -102,7 +129,7 @@ class ScalarSpec extends BaseParseSuite:

test("should parse single quote with multiline") {
val yaml =
s"""|description: 'Quote
s"""|description: 'Quote
| multiline.'
|""".stripMargin

Expand Down

0 comments on commit b01e6bc

Please sign in to comment.