From 2bb32139c48cecd696c7f1b10fed42378d22878f Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Tue, 14 Feb 2017 22:06:48 +0100 Subject: [PATCH] Added `+DOC ---` event output. Fixed parserTests --- test/tparser.nim | 9 +++++---- yaml/parser.nim | 12 +++++++++--- yaml/stream.nim | 24 +++++++++++++++++++----- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/test/tparser.nim b/test/tparser.nim index 7c44be5..cebf8cd 100644 --- a/test/tparser.nim +++ b/test/tparser.nim @@ -21,10 +21,10 @@ proc ensureTestSuiteCloneCorrect(pwd: string) {.compileTime.} = var isCorrectClone = true if dirExists(absolutePath / ".git"): let remoteUrl = - staticExec("cd \"" & absolutePath & "\" && git remote get-url origin") + staticExec("cd \"" & absolutePath & "\" && git remote get-url origin").strip if remoteUrl != testSuiteUrl: isCorrectClone = false - let branches = staticExec("cd \"" & absolutePath & "\" && git branch") + let branches = staticExec("cd \"" & absolutePath & "\" && git branch").strip if "* data" notin branches.splitLines(): isCorrectClone = false if isCorrectClone: @@ -96,7 +96,7 @@ proc parserTest(path: string): bool = macro genTests(): untyped = let - pwd = staticExec("pwd") + pwd = staticExec("pwd").strip absolutePath = '"' & (pwd / testSuiteFolder) & '"' echo "[tparser] Generating tests from " & absolutePath ensureTestSuiteCloneCorrect(pwd) @@ -104,6 +104,7 @@ macro genTests(): untyped = # walkDir for some crude reason does not work with travis build let dirItems = staticExec("ls -1d " & absolutePath / "*") for dirPath in dirItems.splitLines(): + if dirPath.strip.len == 0: continue if dirPath[^4..^1] in [".git", "name", "tags", "meta"]: continue let title = slurp(dirPath / "===") result.add(newCall("test", @@ -112,4 +113,4 @@ macro genTests(): untyped = newLit(dirPath))))) result = newCall("suite", newLit("Parser Tests (from yaml-test-suite)"), result) -genTests() \ No newline at end of file +genTests() diff --git a/yaml/parser.nim b/yaml/parser.nim index 64d4e08..faaabdb 100644 --- a/yaml/parser.nim +++ b/yaml/parser.nim @@ -547,7 +547,8 @@ parserState initial: state = blockObjectStart of ltStreamEnd: c.isFinished = true of ltDirectivesEnd: - e = startDocEvent() + when defined(yamlScalarRepInd): e = startDocEvent(true) + else: e = startDocEvent() result = true c.advance() state = blockObjectStart @@ -738,7 +739,9 @@ parserState expectDocEnd: parserState startDoc: c.initDocValues() - e = startDocEvent() + when defined(yamlScalarRepInd): + e = startDocEvent(c.lex.cur == ltDirectivesEnd) + else: e = startDocEvent() result = true c.advance() state = blockObjectStart @@ -1110,7 +1113,10 @@ proc display*(p: YamlParser, event: YamlStreamEvent): string case event.kind of yamlEndMap: result = "-MAP" of yamlEndSeq: result = "-SEQ" - of yamlStartDoc: result = "+DOC" + of yamlStartDoc: + result = "+DOC" + when defined(yamlScalarRepInd): + if event.explicitDirectivesEnd: result &= " ---" of yamlEndDoc: result = "-DOC" of yamlStartMap: result = "+MAP" & p.renderAttrs(event.mapTag, event.mapAnchor, true) diff --git a/yaml/stream.nim b/yaml/stream.nim index 393e33c..1ac4e65 100644 --- a/yaml/stream.nim +++ b/yaml/stream.nim @@ -58,7 +58,11 @@ type scalarContent*: string # may not be nil (but empty) when defined(yamlScalarRepInd): scalarRep* : ScalarRepresentationIndicator - of yamlEndMap, yamlEndSeq, yamlStartDoc, yamlEndDoc: discard + of yamlStartDoc: + when defined(yamlScalarRepInd): + explicitDirectivesEnd*: bool + else: discard + of yamlEndMap, yamlEndSeq, yamlEndDoc: discard of yamlAlias: aliasTarget* : AnchorId @@ -259,7 +263,10 @@ proc `$`*(event: YamlStreamEvent): string {.raises: [].} = case event.kind of yamlEndMap: result = "-MAP" of yamlEndSeq: result = "-SEQ" - of yamlStartDoc: result = "+DOC" + of yamlStartDoc: + result = "+DOC" + when defined(yamlScalarRepInd): + if event.explicitDirectivesEnd: result &= " ---" of yamlEndDoc: result = "-DOC" of yamlStartMap: result = "+MAP" & renderAttrs(event.mapTag, event.mapAnchor) of yamlStartSeq: result = "+SEQ" & renderAttrs(event.seqTag, event.seqAnchor) @@ -284,9 +291,16 @@ proc tag*(event: YamlStreamEvent): TagId {.raises: [FieldError].} = of yamlScalar: result = event.scalarTag else: raise newException(FieldError, "Event " & $event.kind & " has no tag") -proc startDocEvent*(): YamlStreamEvent {.inline, raises: [].} = - ## creates a new event that marks the start of a YAML document - result = YamlStreamEvent(kind: yamlStartDoc) +when defined(yamlScalarRepInd): + proc startDocEvent*(explicit: bool = false): YamlStreamEvent + {.inline, raises: [].} = + ## creates a new event that marks the start of a YAML document + result = YamlStreamEvent(kind: yamlStartDoc, + explicitDirectivesEnd: explicit) +else: + proc startDocEvent*(): YamlStreamEvent {.inline, raises: [].} = + ## creates a new event that marks the start of a YAML document + result = YamlStreamEvent(kind: yamlStartDoc) proc endDocEvent*(): YamlStreamEvent {.inline, raises: [].} = ## creates a new event that marks the end of a YAML document