Skip to content

Commit

Permalink
returns InvalidToken if scanner encounters error (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy authored Oct 29, 2024
1 parent c7e80a3 commit 87ddfd1
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 121 deletions.
27 changes: 27 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2393,3 +2393,30 @@ b: 1`,
})
}
}

func TestInvalid(t *testing.T) {
tests := []struct {
name string
src string
}{
{
name: "literal opt",
src: `
a: |invalid
foo`,
},
{
name: "literal opt",
src: `
a: |invalid`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := lexer.Tokenize(test.src)
if got.InvalidToken() == nil {
t.Fatal("expected contains invalid token")
}
})
}
}
3 changes: 3 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,9 @@ func ParseBytes(bytes []byte, mode Mode) (*ast.File, error) {

// Parse parse from token instances, and returns ast.File
func Parse(tokens token.Tokens, mode Mode) (*ast.File, error) {
if tk := tokens.InvalidToken(); tk != nil {
return nil, errors.ErrSyntax("found invalid token", tk)
}
var p parser
f, err := p.parse(tokens, mode)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,17 @@ a:
^
`,
},
{
`
a: |invalidopt
foo
`,
`
[2:4] found invalid token
> 2 | a:|invalidopt
^
3 | foo`,
},
}
for _, test := range tests {
t.Run(test.source, func(t *testing.T) {
Expand Down
15 changes: 2 additions & 13 deletions scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type Context struct {
isRawFolded bool
isLiteral bool
isFolded bool
isSingleLine bool
literalOpt string
}

Expand All @@ -35,9 +34,8 @@ var (

func createContext() *Context {
return &Context{
idx: 0,
tokens: token.Tokens{},
isSingleLine: true,
idx: 0,
tokens: token.Tokens{},
}
}

Expand All @@ -58,7 +56,6 @@ func (c *Context) reset(src []rune) {
c.tokens = c.tokens[:0]
c.resetBuffer()
c.isRawFolded = false
c.isSingleLine = true
c.isLiteral = false
c.isFolded = false
c.literalOpt = ""
Expand All @@ -71,10 +68,6 @@ func (c *Context) resetBuffer() {
c.notSpaceOrgCharPos = 0
}

func (c *Context) isSaveIndentMode() bool {
return c.isLiteral || c.isFolded || c.isRawFolded
}

func (c *Context) breakLiteral() {
c.isLiteral = false
c.isRawFolded = false
Expand Down Expand Up @@ -186,10 +179,6 @@ func (c *Context) progress(num int) {
c.idx += num
}

func (c *Context) nextPos() int {
return c.idx + 1
}

func (c *Context) existsBuffer() bool {
return len(c.bufferedSrc()) != 0
}
Expand Down
19 changes: 19 additions & 0 deletions scanner/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package scanner

import "github.com/goccy/go-yaml/token"

type InvalidTokenError struct {
Message string
Token *token.Token
}

func (e *InvalidTokenError) Error() string {
return e.Message
}

func ErrInvalidToken(msg string, tk *token.Token) *InvalidTokenError {
return &InvalidTokenError{
Message: msg,
Token: tk,
}
}
Loading

0 comments on commit 87ddfd1

Please sign in to comment.