Implemented anchors and aliases

This commit is contained in:
Felix Krause 2015-11-29 22:43:10 +01:00
parent f876c845b7
commit 8493015042
2 changed files with 37 additions and 9 deletions

View File

@ -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)

View File

@ -173,3 +173,13 @@ foo:
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)])