Pass almost all test in yaml test suite

This commit is contained in:
Felix Krause 2016-09-14 13:37:56 +02:00
parent b64e40ec41
commit 345f86c52e
3 changed files with 28 additions and 21 deletions

View File

@ -911,15 +911,18 @@ proc blockScalar[T](lex: YamlLexer): bool =
lex.lineStartState = insideDoc[T] lex.lineStartState = insideDoc[T]
lex.cur = ltBlockScalar lex.cur = ltBlockScalar
result = true result = true
echo "exiting block scalar with indentation=" & $lex.indentation
proc indentationAfterBlockScalar[T](lex: YamlLexer): bool = proc indentationAfterBlockScalar[T](lex: YamlLexer): bool =
if lex.c == '#': if lex.indentation == 0:
lex.nextState = lex.insideDocImpl
elif lex.c == '#':
lex.nextState = expectLineEnd[T] lex.nextState = expectLineEnd[T]
result = false result = false
else: else:
lex.cur = ltIndentation lex.cur = ltIndentation
result = true result = true
lex.nextState = lex.lineStartState lex.nextState = lex.insideLineImpl
proc dirEndAfterBlockScalar[T](lex: YamlLexer): bool = proc dirEndAfterBlockScalar[T](lex: YamlLexer): bool =
lex.cur = ltDirectivesEnd lex.cur = ltDirectivesEnd
@ -1139,6 +1142,7 @@ proc newYamlLexer*(source: string, startAt: int = 0): YamlLexer =
proc next*(lex: YamlLexer) = proc next*(lex: YamlLexer) =
while not lex.nextState(lex): discard while not lex.nextState(lex): discard
debug("lexer -> " & $lex.cur)
proc setFlow*(lex: YamlLexer, value: bool) = proc setFlow*(lex: YamlLexer, value: bool) =
lex.inFlow = value lex.inFlow = value

View File

@ -202,7 +202,7 @@ proc handleMapKeyIndicator(c: ParserContext, e: var YamlStreamEvent): bool =
c.advance() c.advance()
if c.lex.cur != ltIndentation: if c.lex.cur != ltIndentation:
# this enables the parser to properly parse compact structures, like # this enables the parser to properly parse compact structures, like
# a: - a # ? - a
# - b # - b
# and such. At the first `-`, the indentation must equal its level to be # and such. At the first `-`, the indentation must equal its level to be
# parsed properly. # parsed properly.
@ -339,7 +339,7 @@ macro parserState(name: untyped, impl: untyped): typed =
parserStates(initial, blockLineStart, blockObjectStart, blockAfterObject, parserStates(initial, blockLineStart, blockObjectStart, blockAfterObject,
scalarEnd, plainScalarEnd, objectEnd, expectDocEnd, startDoc, scalarEnd, plainScalarEnd, objectEnd, expectDocEnd, startDoc,
endDoc, afterDocument, closeStream, closeMoreIndentedLevels, afterDocument, closeStream, closeMoreIndentedLevels,
emitEmptyScalar, tagHandle, anchor, alias, flow, leaveFlowMap, emitEmptyScalar, tagHandle, anchor, alias, flow, leaveFlowMap,
leaveFlowSeq, flowAfterObject, leaveFlowSinglePairMap) leaveFlowSeq, flowAfterObject, leaveFlowSinglePairMap)
@ -400,6 +400,11 @@ proc handleMapValueIndicator(c: ParserContext, e: var YamlStreamEvent): bool =
of fplSinglePairKey, fplSinglePairValue, fplDocument: of fplSinglePairKey, fplSinglePairValue, fplDocument:
internalError("Unexpected level kind: " & $c.level.kind) internalError("Unexpected level kind: " & $c.level.kind)
c.advance() c.advance()
if c.lex.cur != ltIndentation:
# see comment in handleMapKeyIndicator, this time with structures like
# a: - a
# - b
c.lex.indentation = c.lex.curStartPos.column - 1
template handleObjectEnd(c: ParserContext, mayHaveEmptyValue: bool = false): template handleObjectEnd(c: ParserContext, mayHaveEmptyValue: bool = false):
bool = bool =
@ -427,6 +432,7 @@ proc leaveFlowLevel(c: ParserContext, e: var YamlStreamEvent): bool =
else: else:
c.storedState = stateFlowAfterObject c.storedState = stateFlowAfterObject
c.nextImpl = stateObjectEnd c.nextImpl = stateObjectEnd
c.advance()
parserState initial: parserState initial:
case c.lex.cur case c.lex.cur
@ -466,9 +472,8 @@ parserState initial:
c.advance() c.advance()
state = blockObjectStart state = blockObjectStart
of ltDocumentEnd: of ltDocumentEnd:
e = startDocEvent() c.advance()
result = true state = afterDocument
state = endDoc
else: internalError("Unexpected lexer token: " & $c.lex.cur) else: internalError("Unexpected lexer token: " & $c.lex.cur)
parserState blockLineStart: parserState blockLineStart:
@ -496,6 +501,7 @@ parserState blockObjectStart:
c.closeEverything() c.closeEverything()
stored = startDoc stored = startDoc
of ltDocumentEnd: of ltDocumentEnd:
c.advance()
c.closeEverything() c.closeEverything()
stored = afterDocument stored = afterDocument
of ltMapKeyInd: of ltMapKeyInd:
@ -530,7 +536,7 @@ parserState blockObjectStart:
stored = blockAfterObject stored = blockAfterObject
of ltSeqItemInd: of ltSeqItemInd:
result = c.handleBlockSequenceIndicator(e) result = c.handleBlockSequenceIndicator(e)
of ltTagHandle: of ltTagHandle, ltLiteralTag:
result = c.handleBlockItemStart(e) result = c.handleBlockItemStart(e)
state = tagHandle state = tagHandle
stored = blockObjectStart stored = blockObjectStart
@ -593,7 +599,6 @@ parserState blockAfterObject:
of ltDirectivesEnd: of ltDirectivesEnd:
c.closeEverything() c.closeEverything()
stored = startDoc stored = startDoc
c.advance()
of ltStreamEnd: of ltStreamEnd:
c.closeEverything() c.closeEverything()
stored = afterDocument stored = afterDocument
@ -618,6 +623,7 @@ parserState expectDocEnd:
e = endDocEvent() e = endDocEvent()
result = true result = true
state = afterDocument state = afterDocument
c.advance()
of ltStreamEnd: of ltStreamEnd:
e = endDocEvent() e = endDocEvent()
result = true result = true
@ -630,13 +636,9 @@ parserState startDoc:
c.initDocValues() c.initDocValues()
e = startDocEvent() e = startDocEvent()
result = true result = true
c.advance()
state = blockObjectStart state = blockObjectStart
parserState endDoc:
e = endDocEvent()
result = true
state = initial
parserState afterDocument: parserState afterDocument:
case c.lex.cur case c.lex.cur
of ltStreamEnd: c.isFinished = true of ltStreamEnd: c.isFinished = true
@ -647,7 +649,6 @@ parserState afterDocument:
parserState closeStream: parserState closeStream:
case c.level.kind case c.level.kind
of fplUnknown: discard c.ancestry.pop()
of fplDocument: discard of fplDocument: discard
else: else:
case c.endLevel(e) case c.endLevel(e)
@ -670,6 +671,7 @@ parserState closeMoreIndentedLevels:
(c.lex.indentation == parent.indentation and (c.lex.indentation == parent.indentation and
c.level.kind == fplUnknown and parent.kind != fplSequence): c.level.kind == fplUnknown and parent.kind != fplSequence):
state = stored state = stored
debug("Not closing because sequence indicator")
return false return false
debug("Closing because parent.indentation (" & $parent.indentation & debug("Closing because parent.indentation (" & $parent.indentation &
") >= indentation(" & $c.lex.indentation & ")") ") >= indentation(" & $c.lex.indentation & ")")
@ -679,9 +681,13 @@ parserState closeMoreIndentedLevels:
of lerAdditionalMapEnd: return true of lerAdditionalMapEnd: return true
discard c.handleObjectEnd(false) discard c.handleObjectEnd(false)
return result return result
debug("Not closing level because parent.indentation (" &
$parent.indentation & ") < indentation(" & $c.lex.indentation &
")")
if c.level.kind == fplDocument: state = expectDocEnd if c.level.kind == fplDocument: state = expectDocEnd
else: state = stored else: state = stored
elif c.lex.indentation == c.level.indentation: elif c.lex.indentation == c.level.indentation:
debug("Closing document")
let res = c.endLevel(e) let res = c.endLevel(e)
yAssert(res == lerOne) yAssert(res == lerOne)
result = true result = true
@ -733,12 +739,10 @@ parserState flow:
of ltBraceClose: of ltBraceClose:
yAssert(c.level.kind == fplUnknown) yAssert(c.level.kind == fplUnknown)
c.level = c.ancestry.pop() c.level = c.ancestry.pop()
c.advance()
state = leaveFlowMap state = leaveFlowMap
of ltBracketClose: of ltBracketClose:
yAssert(c.level.kind == fplUnknown) yAssert(c.level.kind == fplUnknown)
c.level = c.ancestry.pop() c.level = c.ancestry.pop()
c.advance()
state = leaveFlowSeq state = leaveFlowSeq
of ltComma: of ltComma:
yAssert(c.level.kind == fplUnknown) yAssert(c.level.kind == fplUnknown)
@ -805,7 +809,7 @@ parserState flow:
state = objectEnd state = objectEnd
stored = flowAfterObject stored = flowAfterObject
c.advance() c.advance()
of ltTagHandle: of ltTagHandle, ltLiteralTag:
if c.handleFlowItemStart(e): return true if c.handleFlowItemStart(e): return true
c.handleTagHandle() c.handleTagHandle()
of ltAnchor: of ltAnchor:
@ -896,7 +900,6 @@ parserState flowAfterObject:
of fplUnknown, fplSinglePairKey, fplDocument: of fplUnknown, fplSinglePairKey, fplDocument:
internalError("Unexpected level kind: " & $c.level.kind) internalError("Unexpected level kind: " & $c.level.kind)
result = c.leaveFlowLevel(e) result = c.leaveFlowLevel(e)
c.advance()
of ltBraceClose: of ltBraceClose:
case c.level.kind case c.level.kind
of fplMapKey, fplMapValue: discard of fplMapKey, fplMapValue: discard
@ -905,7 +908,6 @@ parserState flowAfterObject:
of fplUnknown, fplSinglePairKey, fplDocument: of fplUnknown, fplSinglePairKey, fplDocument:
internalError("Unexpected level kind: " & $c.level.kind) internalError("Unexpected level kind: " & $c.level.kind)
result = c.leaveFlowLevel(e) result = c.leaveFlowLevel(e)
c.advance()
of ltComma: of ltComma:
case c.level.kind case c.level.kind
of fplSequence: discard of fplSequence: discard

View File

@ -49,6 +49,7 @@ proc next*(s: YamlStream): YamlStreamEvent =
if s.peeked: if s.peeked:
s.peeked = false s.peeked = false
shallowCopy(result, s.cached) shallowCopy(result, s.cached)
return
else: else:
yAssert(not s.isFinished) yAssert(not s.isFinished)
try: try: