diff --git a/parser/parser.go b/parser/parser.go index 5173393..dee4e25 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -1063,6 +1063,9 @@ func (p *parser) parseDirective(ctx *context, g *TokenGroup) (*ast.DirectiveNode func (p *parser) parseComment(ctx *context) (ast.Node, error) { cm := p.parseHeadComment(ctx) + if ctx.isTokenNotFound() { + return cm, nil + } node, err := p.parseToken(ctx, ctx.currentToken()) if err != nil { return nil, err diff --git a/parser/parser_test.go b/parser/parser_test.go index 14980b5..8d1db7b 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -1471,6 +1471,14 @@ foo: | # comment yaml: ` foo: > # comment x: 42 +`, + }, + { + name: "unattached comment", + yaml: ` +# This comment is in its own document +--- +a: b `, }, }