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 reserved chars #514

Merged
merged 1 commit into from
Nov 9, 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
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
Loading