Repository: goccy/go-yaml
Pull request #514 (2024-11-09 11:28:12)

Fix parsing of reserved chars
fix https://github.com/goccy/go-yaml/issues/336

codecov-commenter: :warning: Please install the !['codecov app svg image'](https://github.com/codecov/engineering-team/assets/152432831/e90313f4-9d3a-4b63-8b54-cfe14e7ec20d) to ensure uploads and comments are reliably processed by Codecov.

## [Codecov](https://app.codecov.io/gh/goccy/go-yaml/pull/514?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
Attention: Patch coverage is `78.57143%` with `3 lines` in your changes missing coverage. Please review.
> Project coverage is 79.09%. Comparing base [(`6b0c68e`)](https://app.codecov.io/gh/goccy/go-yaml/commit/6b0c68e62a7238ec54faa45f188e165412464277?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) to head [(`2a164e4`)](https://app.codecov.io/gh/goccy/go-yaml/commit/2a164e447d3a55cef68ced1d708ecef8fa621eb8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima).

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #514      +/-   ##
==========================================
- Coverage   79.10%   79.09%   -0.01%     
==========================================
  Files          17       17              
  Lines        6096     6110      +14     
==========================================
+ Hits         4822     4833      +11     
- Misses        963      965       +2     
- Partials      311      312       +1     
```

</details>

Patch:
diff --git a/scanner/scanner.go b/scanner/scanner.go
index 7d7cbbd..746b8c7 100644
--- a/scanner/scanner.go
+++ b/scanner/scanner.go
@@ -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()
@@ -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)


Test patch:
diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go
index 658ff88..9cae3f1 100644
--- a/lexer/lexer_test.go
+++ b/lexer/lexer_test.go
@@ -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) {
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 281cd94..34369c1 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -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) {