Skip to content

Commit

Permalink
fix parsing of tag (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored Nov 12, 2024
1 parent 8808389 commit a2d04d6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
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

0 comments on commit a2d04d6

Please sign in to comment.