From 31850d3f29a37ca7bd0a4edfe3b06a06c21b2110 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 10 Aug 2023 01:49:37 +0800 Subject: [PATCH] support line continuation character --- src/lexer.cpp | 7 +++++++ tests/04_line_continue.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/04_line_continue.py diff --git a/src/lexer.cpp b/src/lexer.cpp index 78a7f5c9..718731d3 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -315,6 +315,13 @@ static bool is_unicode_Lo_char(uint32_t c) { case '[': add_token(TK("[")); return true; case ']': add_token(TK("]")); return true; case '@': add_token(TK("@")); return true; + case '\\': { + // line continuation character + char c = eatchar_include_newline(); + if (c != '\n') SyntaxError("expected newline after line continuation character"); + eat_spaces(); + return true; + } case '$': { for(int i=TK("$goto"); i<=TK("$label"); i++){ // +1 to skip the '$' diff --git a/tests/04_line_continue.py b/tests/04_line_continue.py new file mode 100644 index 00000000..464a2dee --- /dev/null +++ b/tests/04_line_continue.py @@ -0,0 +1,31 @@ +a = 1 + 2 \ + + 3 + +assert a == 6 + +assert 1 + 2 \ + + 3 == 6 + +assert 1 + 2 + \ + 3 + \ + 4 == 10 + +assert 1 + 2 + \ + 3 + \ + 4 + 5 + 6 \ + == 21 + +if 1 and 2 \ + and 3 \ + and 4 \ + and 5: + assert True +else: + assert False + +1 and 2 \ + and 3 \ + and 4 + +a = 1 +assert a == 1 \ No newline at end of file