Skip to content

Commit 9e3d33c

Browse files
Merge pull request #619 from akshanshbhatt/pr_comments
2 parents ae8ce28 + 1679365 commit 9e3d33c

11 files changed

+228
-15
lines changed

src/lpython/parser/parser.yy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ statements1
290290
;
291291

292292
statement
293-
: single_line_statement sep
293+
: single_line_statement sep { $$ = $1; }
294294
| multi_line_statement
295+
| multi_line_statement sep { $$ = $1; }
295296
;
296297

297298
single_line_statement

src/lpython/parser/tokenizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Tokenizer
2323

2424
bool indent = false; // Next line is expected to be indented
2525
int dedent = 0; // Allowed values: 0, 1, 2, see the code below the meaning of this state variable
26+
bool colon_actual_last_token = false; // If the actual last token was a colon
2627
long int last_indent_length = 0;
2728
std::vector<uint64_t> indent_length;
2829

src/lpython/parser/tokenizer.re

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,11 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
356356
357357
// Tokens
358358
newline {
359+
if(cur[0] == '#') { RET(TK_NEWLINE); }
359360
if(parenlevel) { continue; }
360-
if (last_token == yytokentype::TK_COLON) {
361+
if (last_token == yytokentype::TK_COLON
362+
|| colon_actual_last_token) {
363+
colon_actual_last_token = false;
361364
indent = true;
362365
} else if (cur[0] != ' ' && cur[0] != '\n'
363366
&& last_indent_length > cur-tok) {
@@ -379,7 +382,12 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
379382
"+" { RET(TK_PLUS) }
380383
"-" { RET(TK_MINUS) }
381384
"=" { RET(TK_EQUAL) }
382-
":" { RET(TK_COLON) }
385+
":" {
386+
if(cur[0] == '\n'){
387+
colon_actual_last_token = true;
388+
}
389+
RET(TK_COLON);
390+
}
383391
";" { RET(TK_SEMICOLON) }
384392
"/" { RET(TK_SLASH) }
385393
"%" { RET(TK_PERCENT) }
@@ -449,7 +457,8 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
449457
RET(TK_IMAG_NUM)
450458
}
451459
452-
comment newline {
460+
comment {
461+
if(last_token == -1) { RET(TK_COMMENT); }
453462
if(parenlevel) { continue; }
454463
line_num++; cur_line=cur;
455464
token(yylval.string);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "ast_new-comment2-f0984d5",
3+
"cmd": "lpython --show-ast --new-parser --no-color {infile} -o {outfile}",
4+
"infile": "tests/tokens/comment2.py",
5+
"infile_hash": "8bbf936b113be965ec25f99d010959c3d6551e0271fa70c76cff1693",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "ast_new-comment2-f0984d5.stdout",
9+
"stdout_hash": "1668ba80cbd288c4926c8e48593f374a1e49963dff66747738f82fd8",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(Module [(FunctionDef main ([] [] [] [] [] [] []) [(If (Name foo Load) [(If (Name bar Load) [(Pass)] [])] [(Pass)]) (Return ())] [] () ()) (FunctionDef test ([] [] [] [] [] [] []) [(Expr (Call (Name print Load) [] []))] [] () ()) (FunctionDef main ([] [] [] [] [] [] []) [(Expr (Call (Name print Load) [] []))] [] () ()) (FunctionDef main ([] [] [] [] [] [] []) [(Expr (Call (Name print Load) [] [])) (Pass)] [] () ()) (FunctionDef main ([] [] [] [] [] [] []) [(If (Name foo Load) [(Pass) (Pass) (If (Name bar Load) [(Pass)] []) (Pass)] []) (Pass)] [] () ())] [])

tests/reference/tokens-comment1-2f8ab90.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "tokens-comment1-2f8ab90.stdout",
9-
"stdout_hash": "1dd4691dd42cfd3ed3e6c7bfb783aa2b0977cf977bd68729f5264224",
9+
"stdout_hash": "9e9a900b941381aadb0d8f29662fa337e918a769daba0c019d7ccdee",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
(TOKEN "eolcomment") 0:10
1+
(TOKEN "comment") 0:9
2+
(NEWLINE) 10:10
23
(KEYWORD "def") 11:13
34
(TOKEN "identifier" test) 15:18
45
(TOKEN "(") 19:19
56
(TOKEN ")") 20:20
67
(TOKEN ":") 21:21
7-
(TOKEN "eolcomment") 23:34
8-
(TOKEN "comment") 39:50
8+
(TOKEN "eolcomment") 23:33
9+
(NEWLINE) 34:34
10+
(TOKEN "comment") 39:49
11+
(NEWLINE) 50:50
912
(TOKEN "indent") 51:54
1013
(TOKEN "identifier" print) 55:59
1114
(TOKEN "(") 60:60
1215
(TOKEN ")") 61:61
1316
(NEWLINE) 62:62
14-
(TOKEN "dedent") 62:62
15-
(TOKEN "comment") 63:74
17+
(TOKEN "comment") 63:73
18+
(NEWLINE) 74:74
19+
(TOKEN "dedent") 74:74
1620
(TOKEN "identifier" test) 75:78
1721
(TOKEN "(") 79:79
1822
(TOKEN ")") 80:80
19-
(TOKEN "eolcomment") 82:93
20-
(TOKEN "comment") 94:105
23+
(TOKEN "eolcomment") 82:92
24+
(NEWLINE) 93:93
25+
(TOKEN "comment") 94:104
26+
(NEWLINE) 105:105
2127
(NEWLINE) 106:106
22-
(TOKEN "comment") 107:110
23-
(TOKEN "comment") 111:123
24-
(TOKEN "comment") 124:134
28+
(TOKEN "comment") 107:109
29+
(NEWLINE) 110:110
30+
(TOKEN "comment") 111:122
31+
(NEWLINE) 123:123
32+
(TOKEN "comment") 124:133
33+
(NEWLINE) 134:134
2534
(EOF) 135:135
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "tokens-comment2-b289dad",
3+
"cmd": "lpython --no-color --show-tokens {infile} -o {outfile}",
4+
"infile": "tests/tokens/comment2.py",
5+
"infile_hash": "8bbf936b113be965ec25f99d010959c3d6551e0271fa70c76cff1693",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "tokens-comment2-b289dad.stdout",
9+
"stdout_hash": "69a0edcb42300fd719c704671b6aa3f815691c7af05048cba629b84c",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
(KEYWORD "def") 0:2
2+
(TOKEN "identifier" main) 4:7
3+
(TOKEN "(") 8:8
4+
(TOKEN ")") 9:9
5+
(TOKEN ":") 10:10
6+
(NEWLINE) 11:11
7+
(TOKEN "comment") 12:14
8+
(NEWLINE) 15:15
9+
(TOKEN "comment") 20:22
10+
(NEWLINE) 23:23
11+
(TOKEN "indent") 24:27
12+
(KEYWORD "if") 28:29
13+
(TOKEN "identifier" foo) 31:33
14+
(TOKEN ":") 34:34
15+
(TOKEN "eolcomment") 36:38
16+
(NEWLINE) 39:39
17+
(TOKEN "comment") 48:50
18+
(NEWLINE) 51:51
19+
(TOKEN "comment") 56:58
20+
(NEWLINE) 59:59
21+
(TOKEN "comment") 60:61
22+
(NEWLINE) 62:62
23+
(TOKEN "indent") 63:70
24+
(KEYWORD "if") 71:72
25+
(TOKEN "identifier" bar) 74:76
26+
(TOKEN ":") 77:77
27+
(TOKEN "eolcomment") 79:81
28+
(NEWLINE) 82:82
29+
(TOKEN "indent") 83:94
30+
(KEYWORD "pass") 95:98
31+
(NEWLINE) 99:99
32+
(TOKEN "dedent") 100:103
33+
(TOKEN "dedent") 103:103
34+
(KEYWORD "else") 104:107
35+
(TOKEN ":") 108:108
36+
(NEWLINE) 109:109
37+
(TOKEN "indent") 110:117
38+
(KEYWORD "pass") 118:121
39+
(NEWLINE) 122:122
40+
(TOKEN "dedent") 123:126
41+
(KEYWORD "return") 127:132
42+
(NEWLINE) 133:133
43+
(NEWLINE) 134:134
44+
(TOKEN "dedent") 134:134
45+
(KEYWORD "def") 135:137
46+
(TOKEN "identifier" test) 139:142
47+
(TOKEN "(") 143:143
48+
(TOKEN ")") 144:144
49+
(TOKEN ":") 145:145
50+
(NEWLINE) 146:146
51+
(TOKEN "indent") 147:150
52+
(TOKEN "identifier" print) 151:155
53+
(TOKEN "(") 156:156
54+
(TOKEN ")") 157:157
55+
(NEWLINE) 158:158
56+
(NEWLINE) 159:159
57+
(TOKEN "comment") 160:168
58+
(NEWLINE) 169:169
59+
(NEWLINE) 170:170
60+
(TOKEN "dedent") 170:170
61+
(KEYWORD "def") 171:173
62+
(TOKEN "identifier" main) 175:178
63+
(TOKEN "(") 179:179
64+
(TOKEN ")") 180:180
65+
(TOKEN ":") 181:181
66+
(NEWLINE) 182:182
67+
(TOKEN "indent") 183:186
68+
(TOKEN "identifier" print) 187:191
69+
(TOKEN "(") 192:192
70+
(TOKEN ")") 193:193
71+
(NEWLINE) 194:194
72+
(TOKEN "comment") 199:207
73+
(NEWLINE) 208:208
74+
(NEWLINE) 209:209
75+
(TOKEN "dedent") 209:209
76+
(KEYWORD "def") 210:212
77+
(TOKEN "identifier" main) 214:217
78+
(TOKEN "(") 218:218
79+
(TOKEN ")") 219:219
80+
(TOKEN ":") 220:220
81+
(NEWLINE) 221:221
82+
(TOKEN "indent") 222:225
83+
(TOKEN "identifier" print) 226:230
84+
(TOKEN "(") 231:231
85+
(TOKEN ")") 232:232
86+
(NEWLINE) 233:233
87+
(TOKEN "comment") 234:242
88+
(NEWLINE) 243:243
89+
(KEYWORD "pass") 248:251
90+
(NEWLINE) 252:252
91+
(NEWLINE) 253:253
92+
(TOKEN "dedent") 253:253
93+
(KEYWORD "def") 254:256
94+
(TOKEN "identifier" main) 258:261
95+
(TOKEN "(") 262:262
96+
(TOKEN ")") 263:263
97+
(TOKEN ":") 264:264
98+
(NEWLINE) 265:265
99+
(TOKEN "indent") 266:269
100+
(KEYWORD "if") 270:271
101+
(TOKEN "identifier" foo) 273:275
102+
(TOKEN ":") 276:276
103+
(NEWLINE) 277:277
104+
(TOKEN "indent") 278:285
105+
(KEYWORD "pass") 286:289
106+
(NEWLINE) 290:290
107+
(TOKEN "comment") 291:299
108+
(NEWLINE) 300:300
109+
(KEYWORD "pass") 309:312
110+
(NEWLINE) 313:313
111+
(KEYWORD "if") 322:323
112+
(TOKEN "identifier" bar) 325:327
113+
(TOKEN ":") 328:328
114+
(NEWLINE) 329:329
115+
(TOKEN "indent") 330:341
116+
(KEYWORD "pass") 342:345
117+
(NEWLINE) 346:346
118+
(TOKEN "dedent") 347:354
119+
(KEYWORD "pass") 355:358
120+
(NEWLINE) 359:359
121+
(TOKEN "dedent") 360:363
122+
(KEYWORD "pass") 364:367
123+
(NEWLINE) 368:368
124+
(TOKEN "dedent") 368:368
125+
(EOF) 369:369

tests/tests.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ tokens = true
293293
filename = "tokens/comment1.py"
294294
tokens = true
295295

296+
[[test]]
297+
filename = "tokens/comment2.py"
298+
tokens = true
299+
ast_new = true
300+
296301
[[test]]
297302
filename = "tokens/symbols1.py"
298303
tokens = true

tests/tokens/comment2.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def main():
2+
# a
3+
# b
4+
if foo: # c
5+
# d
6+
# e
7+
#f
8+
if bar: # g
9+
pass
10+
else:
11+
pass
12+
return
13+
14+
def test():
15+
print()
16+
17+
# Comment
18+
19+
def main():
20+
print()
21+
# Comment
22+
23+
def main():
24+
print()
25+
# Comment
26+
pass
27+
28+
def main():
29+
if foo:
30+
pass
31+
# Comment
32+
pass
33+
if bar:
34+
pass
35+
pass
36+
pass

0 commit comments

Comments
 (0)