Repository: go-python/gpython
Pull request #219 (2023-03-12 06:54:08)

parser: fix CRLF(\r\n) file parsing error, SyntaxError: 'invalid syntax'
The python file on the Windows platform may be in CRLF format. For example, git may convert the file to CRLF under Windows, resulting in parsing failure

ncw: The fix looks reasonable to me.

Can you link some docs showing .py files can end CR LF please?
codecov[bot]: ## [Codecov](https://codecov.io/gh/go-python/gpython/pull/219?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-python) Report
Patch coverage: **`100.00`**% and no project coverage change.
> Comparison is base [(`337df2a`)](https://codecov.io/gh/go-python/gpython/commit/337df2ad1ec2f2a13ef65dc8f56ce1ce8c8cec77?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-python) 74.41% compared to head [(`42f20b7`)](https://codecov.io/gh/go-python/gpython/pull/219?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-python) 74.42%.

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


```diff
@@           Coverage Diff           @@
##             main     #219   +/-   ##
=======================================
  Coverage   74.41%   74.42%           
=======================================
  Files          76       76           
  Lines       12673    12675    +2     
=======================================
+ Hits         9431     9433    +2     
  Misses       2567     2567           
  Partials      675      675           
```


| [Impacted Files](https://codecov.io/gh/go-python/gpython/pull/219?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-python) | Coverage Δ | |
|---|---|---|
| [parser/lexer.go](https://codecov.io/gh/go-python/gpython/pull/219?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-python#diff-cGFyc2VyL2xleGVyLmdv) | `89.07% <100.00%> (+0.04%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-python). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-python)

</details>

[:umbrella: View full report in Codecov by Sentry](https://codecov.io/gh/go-python/gpython/pull/219?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-python).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-python).

wetor: here https://docs.python.org/3.5/reference/lexical_analysis.html
of course, we can also use "build tag" to do special processing on the Windows platform

ncw: The docs you linked are quite clear

> A physical line is a sequence of characters terminated by an end-of-line sequence. In source files, any of the standard platform line termination sequences can be used - the Unix form using ASCII LF (linefeed), the Windows form using the ASCII sequence CR LF (return followed by linefeed), or the old Macintosh form using the ASCII CR (return) character. All of these forms can be used equally, regardless of platform.

So this should be done on any platform, so we don't need build tags.

Also, theoretically, we should be looking at CR terminated lines. However that is a bigger change to the lexer, so let's not do that until someone requests it as I don't think macs have used CR terminated files for a very long time.

I'll merge this now - thank you :-)

Patch:
diff --git a/parser/lexer.go b/parser/lexer.go
index 801215b..119a741 100644
--- a/parser/lexer.go
+++ b/parser/lexer.go
@@ -110,6 +110,9 @@ func (x *yyLex) dequeue() int {
 func (x *yyLex) refill() {
 	var err error
 	x.line, err = x.reader.ReadString('\n')
+	if strings.HasSuffix(x.line, "\r\n") {
+		x.line = x.line[:len(x.line)-2] + "\n"
+	}
 	if yyDebug >= 2 {
 		fmt.Printf("line = %q, err = %v\n", x.line, err)
 	}


Test patch:
diff --git a/parser/lexer_test.go b/parser/lexer_test.go
index 9971c2d..5e044d3 100644
--- a/parser/lexer_test.go
+++ b/parser/lexer_test.go
@@ -262,7 +262,7 @@ func TestLex(t *testing.T) {
 		{"01", "illegal decimal with leading zero 1:0", "exec", LexTokens{
 			{FILE_INPUT, nil, ast.Pos{0, 0}},
 		}},
-		{"1\n 2\n  3\n4\n", "", "exec", LexTokens{
+		{"1\n 2\r\n  3\r\n4\n", "", "exec", LexTokens{
 			{FILE_INPUT, nil, ast.Pos{0, 0}},
 			{NUMBER, py.Int(1), ast.Pos{1, 0}},
 			{NEWLINE, nil, ast.Pos{1, 1}},