From 849301504280cc353266feab07885ad2be32c5a9 Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Sun, 29 Nov 2015 22:43:10 +0100 Subject: [PATCH] Implemented anchors and aliases --- src/yaml/private/lexer.nim | 24 +++++++++++++++++++++--- test/lexing.nim | 22 ++++++++++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) 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