Skip to content

Commit 4a0ae9f

Browse files
Recognize Backslash as a token
1 parent cd765fa commit 4a0ae9f

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

src/lpython/parser/parser.tab.hh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
UMINUS = 494,
249249
TK_DOCSTRING = 495,
250250
TK_LBRACE = 496,
251-
TK_RBRACE = 497,
251+
TK_RBRACE = 497,
252252
TK_AMPERSAND = 498,
253253
TK_DOT = 499,
254254
TK_BACKQUOTE = 500,
@@ -269,8 +269,9 @@
269269
TK_RIGHTSHIFT_EQUAL = 515,
270270
TK_POW_EQUAL = 516,
271271
TK_DOUBLESLASH_EQUAL = 517,
272-
KW_FROM = 518,
273-
KW_DEF = 519
272+
TK_BACKSLASH = 518,
273+
KW_FROM = 519,
274+
KW_DEF = 520
274275
};
275276

276277
#endif // LPYTHON_PARSER_PARSER_TAB_H

src/lpython/parser/tokenizer.re

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
419419
'in' { KW(IN) }
420420
'include' { KW(INCLUDE) }
421421
'inout' { KW(INOUT) }
422-
'in' whitespace 'out' { KW(IN_OUT) }
422+
// 'in' whitespace 'out' { KW(IN_OUT) }
423423
'inquire' { KW(INQUIRE) }
424424
'integer' { KW(INTEGER) }
425425
'intent' { KW(INTENT) }
@@ -537,6 +537,7 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
537537
":" { RET(TK_COLON) }
538538
";" { RET(TK_SEMICOLON) }
539539
"/" { RET(TK_SLASH) }
540+
"\\" { RET(TK_BACKSLASH) }
540541
"%" { RET(TK_PERCENT) }
541542
"," { RET(TK_COMMA) }
542543
"*" { RET(TK_STAR) }
@@ -600,7 +601,7 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
600601
601602
'True' { RET(TK_TRUE) }
602603
'False' { RET(TK_FALSE) }
603-
604+
604605
// This is needed to ensure that 2.op.3 gets tokenized as
605606
// TK_INTEGER(2), TK_DEFOP(.op.), TK_INTEGER(3), and not
606607
// TK_REAL(2.), TK_NAME(op), TK_REAL(.3). The `.op.` can be a
@@ -817,6 +818,7 @@ std::string token2text(const int token)
817818
T(TK_MINUS, "-")
818819
T(TK_STAR, "*")
819820
T(TK_SLASH, "/")
821+
T(TK_BACKSLASH, "\\")
820822
T(TK_COLON, ":")
821823
T(TK_SEMICOLON, ";")
822824
T(TK_COMMA, ",")
@@ -1110,8 +1112,8 @@ std::string pickle_token(int token, const LFortran::YYSTYPE &yystype)
11101112
{
11111113
std::string t;
11121114
t += "(";
1113-
if (token >= yytokentype::TK_NAME && token <= TK_FALSE ||
1114-
token >= yytokentype::TK_DOCSTRING && token <= TK_DOUBLESLASH_EQUAL) {
1115+
if ((token >= yytokentype::TK_NAME && token <= TK_FALSE) ||
1116+
(token >= yytokentype::TK_DOCSTRING && token <= TK_BACKSLASH)) {
11151117
t += "TOKEN";
11161118
} else if (token == yytokentype::TK_NEWLINE) {
11171119
t += "NEWLINE";

tests/reference/cpp-symbols1-658c990.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "cpp-symbols1-658c990",
33
"cmd": "lpython --no-color --show-tokens {infile} -o {outfile}",
44
"infile": "tests/tokens/symbols1.py",
5-
"infile_hash": "c1a4fc8a1c2188105740fc26dd5e7b8e0aeab378fec8685ec6465f06",
5+
"infile_hash": "4f8b111c9caf59245efae13d9d9efbf2ce5f2c42d0850079e6313903",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-symbols1-658c990.stdout",
9-
"stdout_hash": "84f6a8d52d509259941bca1eee0b4adb5a2749af89483e64a27228f2",
9+
"stdout_hash": "0c55157185187ddf4454e6129e742698606b70075a4e689d340f770f",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-symbols1-658c990.stdout

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,23 @@
244244
(TOKEN "identifier" x) 518:518
245245
(TOKEN "=") 520:520
246246
(TOKEN "identifier" x) 522:522
247-
(EOF) 523:523
247+
(NEWLINE) 523:523
248+
(NEWLINE) 524:524
249+
(KEYWORD "if") 525:526
250+
(TOKEN "identifier" i) 528:528
251+
(TOKEN ">=") 530:531
252+
(TOKEN "integer" 1) 533:533
253+
(TOKEN "or") 535:536
254+
(TOKEN "\") 538:538
255+
(NEWLINE) 539:539
256+
(TOKEN "identifier" j) 548:548
257+
(TOKEN "<=") 550:551
258+
(TOKEN "integer" 10) 553:554
259+
(TOKEN ":") 555:555
260+
(NEWLINE) 556:556
261+
(KEYWORD "print") 561:565
262+
(TOKEN "(") 566:566
263+
(TOKEN "string" "Backlash") 567:576
264+
(TOKEN ")") 577:577
265+
(NEWLINE) 578:578
266+
(EOF) 579:579

tests/tokens/symbols1.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ def test(x):
4545

4646
class t:
4747
def __init__(self, x):
48-
self.x = x
48+
self.x = x
49+
50+
if i >= 1 or \
51+
j <= 10:
52+
print("Backlash")

0 commit comments

Comments
 (0)