Skip to content

Commit

Permalink
Merge pull request #160 from quasilyte/quasilyte/fix/parser_panic_on_…
Browse files Browse the repository at this point in the history
…invalid_directive

parser: fix parser panic for the invalid directive
  • Loading branch information
goccy authored Sep 10, 2020
2 parents 890f9ae + 3831509 commit d2d6e68
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ b: c
`,
},
}

for _, test := range tests {
tokens := lexer.Tokenize(test.source)
f, err := parser.Parse(tokens, 0)
Expand Down Expand Up @@ -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 {}
^
`,
},
}
Expand Down

0 comments on commit d2d6e68

Please sign in to comment.