mirror of
https://github.com/status-im/NimYAML.git
synced 2025-01-11 20:14:19 +00:00
JSON tests working again. Other tests TODO
This commit is contained in:
parent
92f5a7a6fd
commit
92504c1893
@ -161,7 +161,9 @@ proc handleMapKeyIndicator(c: ParserContext, e: var YamlStreamEvent): bool =
|
|||||||
result = true
|
result = true
|
||||||
of fplMapValue:
|
of fplMapValue:
|
||||||
if c.level.indentation != c.lex.indentation:
|
if c.level.indentation != c.lex.indentation:
|
||||||
raise c.generateError("Invalid p.indentation of map key indicator")
|
raise c.generateError("Invalid p.indentation of map key indicator " &
|
||||||
|
"(expected" & $c.level.indentation & ", got " & $c.lex.indentation &
|
||||||
|
")")
|
||||||
e = scalarEvent("", yTagQuestionMark, yAnchorNone)
|
e = scalarEvent("", yTagQuestionMark, yAnchorNone)
|
||||||
result = true
|
result = true
|
||||||
c.level.kind = fplMapKey
|
c.level.kind = fplMapKey
|
||||||
@ -176,10 +178,9 @@ proc handleMapKeyIndicator(c: ParserContext, e: var YamlStreamEvent): bool =
|
|||||||
raise c.generateError("Unexpected map key indicator (expected '- ')")
|
raise c.generateError("Unexpected map key indicator (expected '- ')")
|
||||||
of fplSinglePairKey, fplSinglePairValue, fplDocument:
|
of fplSinglePairKey, fplSinglePairValue, fplDocument:
|
||||||
internalError("Unexpected level kind: " & $c.level.kind)
|
internalError("Unexpected level kind: " & $c.level.kind)
|
||||||
# TODO: why was this there?
|
|
||||||
# c.lexer.skipWhitespace()
|
|
||||||
# c.indentation = c.lexer.getColNumber(c.lexer.bufpos)
|
|
||||||
c.lex.next()
|
c.lex.next()
|
||||||
|
if c.lex.cur != ltIndentation:
|
||||||
|
c.lex.indentation = c.lex.curStartPos.column - 1
|
||||||
|
|
||||||
proc handleBlockSequenceIndicator(c: ParserContext, e: var YamlStreamEvent):
|
proc handleBlockSequenceIndicator(c: ParserContext, e: var YamlStreamEvent):
|
||||||
bool =
|
bool =
|
||||||
@ -191,14 +192,15 @@ proc handleBlockSequenceIndicator(c: ParserContext, e: var YamlStreamEvent):
|
|||||||
result = true
|
result = true
|
||||||
of fplSequence:
|
of fplSequence:
|
||||||
if c.level.indentation != c.lex.indentation:
|
if c.level.indentation != c.lex.indentation:
|
||||||
raise c.generateError("Invalid p.indentation of block sequence indicator")
|
raise c.generateError(
|
||||||
|
"Invalid p.indentation of block sequence indicator (expected " &
|
||||||
|
$c.level.indentation & ", got " & $c.lex.indentation & ")")
|
||||||
c.ancestry.add(c.level)
|
c.ancestry.add(c.level)
|
||||||
c.level = initLevel(fplUnknown)
|
c.level = initLevel(fplUnknown)
|
||||||
else: raise c.generateError("Illegal sequence item in map")
|
else: raise c.generateError("Illegal sequence item in map")
|
||||||
# TODO: why was this there?
|
|
||||||
# c.lexer.skipWhitespace()
|
|
||||||
# c.indentation = c.lexer.getColNumber(c.lexer.bufpos)
|
|
||||||
c.lex.next()
|
c.lex.next()
|
||||||
|
if c.lex.cur != ltIndentation:
|
||||||
|
c.lex.indentation = c.lex.curStartPos.column - 1
|
||||||
|
|
||||||
proc handleBlockItemStart(c: ParserContext, e: var YamlStreamEvent): bool =
|
proc handleBlockItemStart(c: ParserContext, e: var YamlStreamEvent): bool =
|
||||||
result = false
|
result = false
|
||||||
@ -321,11 +323,11 @@ macro parserState(name: untyped, impl: untyped): typed =
|
|||||||
|
|
||||||
# --- parser states ---
|
# --- parser states ---
|
||||||
|
|
||||||
parserStates(initial, blockObjectStart, blockAfterObject, scalarEnd,
|
parserStates(initial, blockLineStart, blockObjectStart, blockAfterObject,
|
||||||
plainScalarEnd, objectEnd, expectDocEnd, startDoc, afterDocument,
|
scalarEnd, plainScalarEnd, objectEnd, expectDocEnd, startDoc,
|
||||||
closeStream, closeMoreIndentedLevels, emitEmptyScalar, tagHandle,
|
afterDocument, closeStream, closeMoreIndentedLevels,
|
||||||
anchor, alias, flow, leaveFlowMap, leaveFlowSeq, flowAfterObject,
|
emitEmptyScalar, tagHandle, anchor, alias, flow, leaveFlowMap,
|
||||||
leaveFlowSinglePairMap)
|
leaveFlowSeq, flowAfterObject, leaveFlowSinglePairMap)
|
||||||
|
|
||||||
proc closeEverything(c: ParserContext) =
|
proc closeEverything(c: ParserContext) =
|
||||||
c.lex.indentation = -1
|
c.lex.indentation = -1
|
||||||
@ -420,6 +422,8 @@ parserState initial:
|
|||||||
assert c.lex.cur == ltYamlVersion, $c.lex.cur
|
assert c.lex.cur == ltYamlVersion, $c.lex.cur
|
||||||
if c.lex.buf != "1.2":
|
if c.lex.buf != "1.2":
|
||||||
c.callCallback("Version is not 1.2, but " & c.lex.buf)
|
c.callCallback("Version is not 1.2, but " & c.lex.buf)
|
||||||
|
c.lex.buf.setLen(0)
|
||||||
|
c.lex.next()
|
||||||
of ltTagDirective:
|
of ltTagDirective:
|
||||||
c.lex.next()
|
c.lex.next()
|
||||||
assert c.lex.cur == ltTagShorthand
|
assert c.lex.cur == ltTagShorthand
|
||||||
@ -430,6 +434,7 @@ parserState initial:
|
|||||||
assert c.lex.cur == ltTagUri
|
assert c.lex.cur == ltTagUri
|
||||||
c.shorthands[tagShorthand] = c.lex.buf
|
c.shorthands[tagShorthand] = c.lex.buf
|
||||||
c.lex.buf.setLen(0)
|
c.lex.buf.setLen(0)
|
||||||
|
c.lex.next()
|
||||||
of ltUnknownDirective:
|
of ltUnknownDirective:
|
||||||
c.callCallback("Unknown directive: " & c.lex.buf)
|
c.callCallback("Unknown directive: " & c.lex.buf)
|
||||||
c.lex.buf.setLen(0)
|
c.lex.buf.setLen(0)
|
||||||
@ -447,31 +452,33 @@ parserState initial:
|
|||||||
state = blockObjectStart
|
state = blockObjectStart
|
||||||
else: internalError("Unexpected lexer token: " & $c.lex.cur)
|
else: internalError("Unexpected lexer token: " & $c.lex.cur)
|
||||||
|
|
||||||
parserState blockObjectStart:
|
parserState blockLineStart:
|
||||||
var atLineStart = false
|
case c.lex.cur
|
||||||
while c.lex.cur in {ltEmptyLine, ltIndentation}:
|
of ltIndentation: discard
|
||||||
atLineStart = true
|
of ltEmptyLine: c.lex.next()
|
||||||
c.lex.next()
|
of ltStreamEnd:
|
||||||
if atLineStart and (c.lex.indentation < c.ancestry[^1].indentation or
|
c.closeEverything()
|
||||||
(c.lex.indentation == c.ancestry[^1].indentation and
|
stored = afterDocument
|
||||||
(c.lex.cur != ltSeqItemInd or c.ancestry[^1].kind != fplSequence))):
|
else:
|
||||||
state = closeMoreIndentedLevels
|
if c.lex.indentation <= c.ancestry[^1].indentation:
|
||||||
stored = blockObjectStart
|
state = closeMoreIndentedLevels
|
||||||
return false
|
stored = blockObjectStart
|
||||||
|
else:
|
||||||
|
state = blockObjectStart
|
||||||
|
|
||||||
|
parserState blockObjectStart:
|
||||||
echo "blockObjectStart: ", c.lex.cur
|
echo "blockObjectStart: ", c.lex.cur
|
||||||
case c.lex.cur
|
case c.lex.cur
|
||||||
|
of ltEmptyLine: c.lex.next()
|
||||||
|
of ltIndentation:
|
||||||
|
c.lex.next()
|
||||||
|
state = blockLineStart
|
||||||
of ltDirectivesEnd:
|
of ltDirectivesEnd:
|
||||||
c.closeEverything()
|
c.closeEverything()
|
||||||
stored = startDoc
|
stored = startDoc
|
||||||
return false
|
|
||||||
of ltDocumentEnd:
|
of ltDocumentEnd:
|
||||||
c.closeEverything()
|
c.closeEverything()
|
||||||
stored = afterDocument
|
stored = afterDocument
|
||||||
return false
|
|
||||||
of ltEmptyLine, ltIndentation:
|
|
||||||
c.lex.next()
|
|
||||||
return false
|
|
||||||
of ltMapKeyInd:
|
of ltMapKeyInd:
|
||||||
result = c.handleMapKeyIndicator(e)
|
result = c.handleMapKeyIndicator(e)
|
||||||
of ltMapValInd:
|
of ltMapValInd:
|
||||||
@ -486,7 +493,16 @@ parserState blockObjectStart:
|
|||||||
state = scalarEnd
|
state = scalarEnd
|
||||||
of ltScalarPart:
|
of ltScalarPart:
|
||||||
result = c.handleBlockItemStart(e)
|
result = c.handleBlockItemStart(e)
|
||||||
c.handleFlowPlainScalar()
|
while true:
|
||||||
|
c.lex.next()
|
||||||
|
c.lex.newlines.inc()
|
||||||
|
case c.lex.cur
|
||||||
|
of ltEmptyLine: c.lex.newlines.inc()
|
||||||
|
of ltIndentation:
|
||||||
|
if c.lex.indentation <= c.ancestry[^1].indentation: break
|
||||||
|
of ltScalarPart: discard
|
||||||
|
else: break
|
||||||
|
c.lex.newlines = 0
|
||||||
state = plainScalarEnd
|
state = plainScalarEnd
|
||||||
stored = blockAfterObject
|
stored = blockAfterObject
|
||||||
of ltSeqItemInd:
|
of ltSeqItemInd:
|
||||||
@ -527,7 +543,7 @@ parserState blockAfterObject:
|
|||||||
case c.lex.cur
|
case c.lex.cur
|
||||||
of ltIndentation:
|
of ltIndentation:
|
||||||
c.lex.next()
|
c.lex.next()
|
||||||
state = blockObjectStart
|
state = blockLineStart
|
||||||
of ltMapValInd:
|
of ltMapValInd:
|
||||||
case c.level.kind
|
case c.level.kind
|
||||||
of fplUnknown:
|
of fplUnknown:
|
||||||
|
@ -12,14 +12,21 @@ proc wc(line, column: int, lineContent: string, message: string) =
|
|||||||
echo "Warning (", line, ",", column, "): ", message, "\n", lineContent
|
echo "Warning (", line, ",", column, "): ", message, "\n", lineContent
|
||||||
|
|
||||||
proc ensureEqual(yamlIn, jsonIn: string) =
|
proc ensureEqual(yamlIn, jsonIn: string) =
|
||||||
var
|
try:
|
||||||
parser = newYamlParser(initCoreTagLibrary(), wc)
|
var
|
||||||
s = parser.parse(newStringStream(yamlIn))
|
parser = newYamlParser(initCoreTagLibrary(), wc)
|
||||||
yamlResult = constructJson(s)
|
s = parser.parse(newStringStream(yamlIn))
|
||||||
jsonResult = parseJson(jsonIn)
|
yamlResult = constructJson(s)
|
||||||
assert yamlResult.len == 1
|
jsonResult = parseJson(jsonIn)
|
||||||
assert(jsonResult == yamlResult[0], "Expected: " & $jsonResult & ", got: " &
|
assert yamlResult.len == 1
|
||||||
$yamlResult[0])
|
assert(jsonResult == yamlResult[0], "Expected: " & $jsonResult & ", got: " &
|
||||||
|
$yamlResult[0])
|
||||||
|
except YamlStreamError:
|
||||||
|
let e = (ref YamlParserError)(getCurrentException().parent)
|
||||||
|
echo "error occurred: " & e.msg
|
||||||
|
echo "line: ", e.line, ", column: ", e.column
|
||||||
|
echo e.lineContent
|
||||||
|
raise e
|
||||||
|
|
||||||
suite "Constructing JSON":
|
suite "Constructing JSON":
|
||||||
test "Constructing JSON: Simple Sequence":
|
test "Constructing JSON: Simple Sequence":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user