Repository: goplus/gop
Pull request #1782 (2024-02-26 03:41:30)

parser: fix string extra check
fix parser panic 
```
run "
```
```
panic: runtime error: slice bounds out of range [1:0] [recovered]
	panic: runtime error: slice bounds out of range [1:0]
goroutine 1 [running]:
github.com/goplus/gop/parser.parseFile.func1()
	/Users/vfc/goplus/gop/parser/interface.go:96 +0x1f2
panic({0x1680080?, 0xc0000c81c8?})
```
fixed to
```
bug.gop:1:5: string literal not terminated
```

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/goplus/gop/pull/1782?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 92.87%. Comparing base [(`ffe6653`)](https://app.codecov.io/gh/goplus/gop/commit/ffe665306b814e8f6a7b7c67d535c929a0660126?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) to head [(`45c69bf`)](https://app.codecov.io/gh/goplus/gop/pull/1782?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).
> Report is 20 commits behind head on main.


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


```diff
@@            Coverage Diff             @@
##             main    #1782      +/-   ##
==========================================
+ Coverage   92.22%   92.87%   +0.64%     
==========================================
  Files          40       38       -2     
  Lines       11021     9051    -1970     
==========================================
- Hits        10164     8406    -1758     
+ Misses        694      483     -211     
+ Partials      163      162       -1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/goplus/gop/pull/1782?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).


Patch:
diff --git a/parser/parser.go b/parser/parser.go
index 3ba5006a..c1246138 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -1705,7 +1705,7 @@ func (p *parser) parseOperand(lhs, allowTuple, allowCmd bool) (x ast.Expr, isTup
 
 	case token.STRING, token.CSTRING, token.INT, token.FLOAT, token.IMAG, token.CHAR, token.RAT:
 		bl := &ast.BasicLit{ValuePos: p.pos, Kind: p.tok, Value: p.lit}
-		if p.tok == token.STRING {
+		if p.tok == token.STRING && len(p.lit) > 1 {
 			bl.Extra = p.stringLit(p.pos, p.lit)
 		}
 		x = bl


Test patch:
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 742b3646..c1edf70c 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -545,4 +545,9 @@ println "$a${b}"
 `, "/foo/bar.gop:2:10: invalid $ expression: neither `${ ... }` nor `$$`", ``)
 }
 
+func TestErrStringLiteral(t *testing.T) {
+	testErrCode(t, `run "
+`, `/foo/bar.gop:1:5: string literal not terminated`, ``)
+}
+
 // -----------------------------------------------------------------------------