diff --git a/src/yaml/private/lexer.nim b/src/yaml/private/lexer.nim index cc1a945..8789c2b 100644 --- a/src/yaml/private/lexer.nim +++ b/src/yaml/private/lexer.nim @@ -60,7 +60,9 @@ type # tags ylTagHandle, ylTagSuffix, ylVerbatimTag, # document separation - ylDashes, ylDots + ylDashes, ylDots, + # anchoring + ylAnchor, ylAlias YamlLexer* = object of BaseLexer indentations: seq[int] @@ -549,9 +551,9 @@ iterator tokens*(my: var YamlLexer): YamlLexerEvent = my.content.add(c) state = ylTagHandle of '&': - yieldError("TODO: anchors") + state = ylAnchor of '*': - yieldError("TODO: links") + state = ylAlias of ' ': discard of '-': @@ -795,6 +797,22 @@ iterator tokens*(my: var YamlLexer): YamlLexerEvent = continue else: my.content.add(c) + of ylAnchor: + case c + of EndOfFile, '\r', '\x0A', ' ', '\t', '{', '}', '[', ']': + yieldToken(yamlAnchor) + state = ylInitialInLine + continue + else: + my.content.add(c) + of ylAlias: + case c + of EndOfFile, '\r', '\x0A', ' ', '\t', '{', '}', '[', ']': + yieldToken(yamlAlias) + state = ylInitialInLine + continue + else: + my.content.add(c) my.bufpos += my.charlen inc(position) \ No newline at end of file diff --git a/test/lexing.nim b/test/lexing.nim index f23ae87..58d06bb 100644 --- a/test/lexing.nim +++ b/test/lexing.nim @@ -167,9 +167,19 @@ foo: - baz - biz herp: derp""", - [t(yamlLineStart, ""), t(yamlScalar, "foo"), t(yamlColon, nil), - t(yamlLineStart, " "), t(yamlScalar, "bar"), t(yamlColon, nil), - t(yamlLineStart, " "), t(yamlDash, nil), t(yamlScalar, "baz"), - t(yamlLineStart, " "), t(yamlDash, nil), t(yamlScalar, "biz"), - t(yamlLineStart, " "), t(yamlScalar, "herp"), t(yamlColon, nil), - t(yamlScalar, "derp"), t(yamlStreamEnd, nil)]) \ No newline at end of file + [t(yamlLineStart, ""), t(yamlScalar, "foo"), t(yamlColon, nil), + t(yamlLineStart, " "), t(yamlScalar, "bar"), t(yamlColon, nil), + t(yamlLineStart, " "), t(yamlDash, nil), t(yamlScalar, "baz"), + t(yamlLineStart, " "), t(yamlDash, nil), t(yamlScalar, "biz"), + t(yamlLineStart, " "), t(yamlScalar, "herp"), t(yamlColon, nil), + t(yamlScalar, "derp"), t(yamlStreamEnd, nil)]) + + test "Anchor": + ensure("foo: &bar", [t(yamlLineStart, ""), t(yamlScalar, "foo"), + t(yamlColon, nil), t(yamlAnchor, "bar"), + t(yamlStreamEnd, nil)]) + + test "Alias": + ensure("foo: *bar", [t(yamlLineStart, ""), t(yamlScalar, "foo"), + t(yamlColon, nil), t(yamlAlias, "bar"), + t(yamlStreamEnd, nil)]) \ No newline at end of file