Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of tag #526

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,16 @@ func (p *parser) parseTag(ctx *context) (*ast.TagNode, error) {
err error
)
switch token.ReservedTagKeyword(tagToken.Value) {
case token.MappingTag,
token.OrderedMapTag:
value, err = p.parseMapping(ctx)
case token.MappingTag, token.OrderedMapTag:
tk := p.currentToken()
if tk.Type == token.CommentType {
tk = p.nextNotCommentToken()
}
if tk != nil && tk.Type == token.MappingStartType {
value, err = p.parseMapping(ctx)
} else {
value, err = p.parseMappingValue(ctx)
}
case token.IntegerTag,
token.FloatTag,
token.StringTag,
Expand All @@ -245,8 +252,7 @@ func (p *parser) parseTag(ctx *context) (*ast.TagNode, error) {
token.SetTag:
err = errors.ErrSyntax(fmt.Sprintf("sorry, currently not supported %s tag", tagToken.Value), tagToken)
default:
// custom tag
value, err = p.parseToken(ctx, p.currentToken())
err = errors.ErrSyntax(fmt.Sprintf("unknown tag name %q specified", tagToken.Value), tagToken)
}
if err != nil {
return nil, err
Expand Down
38 changes: 37 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func TestParser(t *testing.T) {
"%YAML 1.2\n---\n",
"a: !!binary gIGC\n",
"a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
"- !tag\n a: b\n c: d\n",
"v:\n- A\n- |-\n B\n C\n",
"v:\n- A\n- >-\n B\n C\n",
"v: |-\n 0\n",
Expand Down Expand Up @@ -960,6 +959,43 @@ func TestSyntaxError(t *testing.T) {
}{
{
`
- !tag
a: b
c: d
`,
`
[2:3] unknown tag name "!tag" specified
> 2 | - !tag
^
3 | a: b
4 | c: d`,
},
{
`
a: !tag
b: c
`,
`
[2:4] unknown tag name "!tag" specified
> 2 | a: !tag
^
3 | b: c`,
},
{
`
a: !tag
b: c
d: e
`,
`
[2:4] unknown tag name "!tag" specified
> 2 | a: !tag
^
3 | b: c
4 | d: e`,
},
{
`
a:
- b
c: d
Expand Down
14 changes: 10 additions & 4 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,20 @@ func (s *Scanner) scanTag(ctx *Context) bool {
progress = idx + 1
ctx.addOriginBuf(c)
switch c {
case ' ', '\n', '\r':
case ' ':
value := ctx.source(ctx.idx-1, ctx.idx+idx)
ctx.addToken(token.Tag(value, string(ctx.obuf), s.pos()))
s.progressColumn(ctx, len([]rune(value)))
ctx.clear()
return true
case '\n', '\r':
value := ctx.source(ctx.idx-1, ctx.idx+idx)
ctx.addToken(token.Tag(value, string(ctx.obuf), s.pos()))
progress = len([]rune(value))
goto END
s.progressColumn(ctx, len([]rune(value))-1) // progress column before new-line-char for scanning new-line-char at scanNewLine function.
ctx.clear()
return true
}
}
END:
s.progressColumn(ctx, progress)
ctx.clear()
return true
Expand Down
Loading