Skip to content

Commit

Permalink
fix parsing of reserved chars (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored Nov 9, 2024
1 parent 6b0c68e commit 8f36c1b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3133,6 +3133,14 @@ a: |invalid`,
name: "invalid document header option number",
src: "a: >3\n 1",
},
{
name: "use reserved character @",
src: "key: [@val]",
},
{
name: "use reserved character `",
src: "key: [`val]",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,18 @@ a:
^
6 | d: e`,
},
{
"key: [@val]",
`
[1:7] found invalid token
> 1 | key: [@val]
^
`,
},
{
"key: [`val]",
"\n[1:7] found invalid token\n> 1 | key: [`val]\n ^\n",
},
}
for _, test := range tests {
t.Run(test.source, func(t *testing.T) {
Expand Down
17 changes: 17 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,19 @@ func (s *Scanner) scanAlias(ctx *Context) bool {
return true
}

func (s *Scanner) scanReservedChar(ctx *Context, c rune) error {
if ctx.existsBuffer() {
return nil
}

ctx.addBuf(c)
ctx.addOriginBuf(c)
err := ErrInvalidToken("%q is a reserved character", token.Invalid(string(ctx.obuf), s.pos()))
s.progressColumn(ctx, 1)
ctx.clear()
return err
}

func (s *Scanner) scan(ctx *Context) error {
for ctx.next() {
c := ctx.currentChar()
Expand Down Expand Up @@ -1103,6 +1116,10 @@ func (s *Scanner) scan(ctx *Context) error {
if s.scanWhiteSpace(ctx) {
continue
}
case '@', '`':
if err := s.scanReservedChar(ctx, c); err != nil {
return err
}
}
ctx.addBuf(c)
ctx.addOriginBuf(c)
Expand Down

0 comments on commit 8f36c1b

Please sign in to comment.