Skip to content

Commit 652daef

Browse files
wetorncw
authored andcommitted
parser: fix CRLF(\r\n) file parsing error, SyntaxError: 'invalid syntax'
1 parent 337df2a commit 652daef

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

parser/lexer.go

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func (x *yyLex) dequeue() int {
110110
func (x *yyLex) refill() {
111111
var err error
112112
x.line, err = x.reader.ReadString('\n')
113+
if strings.HasSuffix(x.line, "\r\n") {
114+
x.line = x.line[:len(x.line)-2] + "\n"
115+
}
113116
if yyDebug >= 2 {
114117
fmt.Printf("line = %q, err = %v\n", x.line, err)
115118
}

parser/lexer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func TestLex(t *testing.T) {
262262
{"01", "illegal decimal with leading zero 1:0", "exec", LexTokens{
263263
{FILE_INPUT, nil, ast.Pos{0, 0}},
264264
}},
265-
{"1\n 2\n 3\n4\n", "", "exec", LexTokens{
265+
{"1\n 2\r\n 3\r\n4\n", "", "exec", LexTokens{
266266
{FILE_INPUT, nil, ast.Pos{0, 0}},
267267
{NUMBER, py.Int(1), ast.Pos{1, 0}},
268268
{NEWLINE, nil, ast.Pos{1, 1}},

0 commit comments

Comments
 (0)