diff --git a/parser/parser.go b/parser/parser.go index bd107ebc..efcd75bf 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -390,7 +390,13 @@ func (p *parser) parseDirective(ctx *context) (ast.Node, error) { } node.Value = value ctx.progress(1) - if ctx.currentToken().Type != token.DocumentHeaderType { + tk := ctx.currentToken() + if tk == nil { + // Since current token is nil, use the previous token to specify + // the syntax error location. + return nil, errors.ErrSyntax("unexpected directive value. document not started", ctx.previousToken()) + } + if tk.Type != token.DocumentHeaderType { return nil, errors.ErrSyntax("unexpected directive value. document not started", ctx.currentToken()) } return node, nil diff --git a/parser/parser_test.go b/parser/parser_test.go index 3303a12a..da4950e8 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -544,6 +544,7 @@ b: c `, }, } + for _, test := range tests { tokens := lexer.Tokenize(test.source) f, err := parser.Parse(tokens, 0) @@ -612,6 +613,14 @@ a > 2 | a 3 | - b: c ^ +`, + }, + { + `%YAML 1.1 {}`, + ` +[1:2] unexpected directive value. document not started +> 1 | %YAML 1.1 {} + ^ `, }, }