Skip to content

Commit dcd584b

Browse files
Merge pull request #1014 from Thirumalai-Shaktivel/indent
2 parents 8a016cd + 1928d67 commit dcd584b

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

src/lpython/parser/tokenizer.re

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
323323
RET(TK_INDENT);
324324
} else {
325325
if(last_token == yytokentype::TK_NEWLINE && cur[0] != ' '
326-
&& last_indent_length > cur-tok) {
326+
&& cur[0] != '\t' && last_indent_length > cur-tok) {
327327
last_indent_length = cur-tok;
328328
dedent = 2;
329329
if (!indent_length.empty()) {
@@ -376,8 +376,8 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost
376376
|| colon_actual_last_token) {
377377
colon_actual_last_token = false;
378378
indent = true;
379-
} else if (cur[0] != ' ' && cur[0] != '\n'
380-
&& last_indent_length > cur-tok) {
379+
} else if (cur[0] != ' ' && cur[0] != '\t' && cur[0] != '\n'
380+
&& last_indent_length >= cur-tok) {
381381
last_indent_length = 0;
382382
dedent = 1;
383383
}

tests/reference/tokens-indent2-e702789.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "tokens-indent2-e702789",
33
"cmd": "lpython --no-color --show-tokens {infile} -o {outfile}",
44
"infile": "tests/tokens/indent2.py",
5-
"infile_hash": "0bf5680f2c0bd9241c0622181be765f900672a0d54ca6366fd5842ff",
5+
"infile_hash": "6ee8bbeb2e48631f07b13012f569904545ce5c14e714003d030fc385",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "tokens-indent2-e702789.stdout",
9-
"stdout_hash": "d21868282c9ca047305e18e15d2961166902b5eecba2f8d905d7d219",
9+
"stdout_hash": "e259b5fcc4c1d83a04924c51a6a6f6d9be631970d3663e5be065ed7d",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/tokens-indent2-e702789.stdout

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,64 @@
2525
(KEYWORD "return") 84:89
2626
(TOKEN "string" "bar") 91:95
2727
(NEWLINE) 96:96
28-
(TOKEN "dedent") 96:96
29-
(TOKEN "dedent") 96:96
30-
(EOF) 97:97
28+
(NEWLINE) 97:97
29+
(TOKEN "dedent") 97:97
30+
(TOKEN "dedent") 97:97
31+
(KEYWORD "def") 98:100
32+
(TOKEN "identifier" test) 102:105
33+
(TOKEN "(") 106:106
34+
(TOKEN ")") 107:107
35+
(TOKEN ":") 108:108
36+
(NEWLINE) 109:109
37+
(TOKEN "indent") 110:110
38+
(KEYWORD "if") 111:112
39+
(TOKEN "True") 114:117
40+
(TOKEN ":") 118:118
41+
(NEWLINE) 119:119
42+
(TOKEN "indent") 120:121
43+
(KEYWORD "pass") 122:125
44+
(NEWLINE) 126:126
45+
(TOKEN "dedent") 127:127
46+
(KEYWORD "else") 128:131
47+
(TOKEN ":") 132:132
48+
(NEWLINE) 133:133
49+
(TOKEN "indent") 134:135
50+
(KEYWORD "pass") 136:139
51+
(NEWLINE) 140:140
52+
(NEWLINE) 141:141
53+
(TOKEN "dedent") 141:141
54+
(TOKEN "dedent") 141:141
55+
(KEYWORD "def") 142:144
56+
(TOKEN "identifier" test) 146:149
57+
(TOKEN "(") 150:150
58+
(TOKEN "identifier" x) 151:151
59+
(TOKEN ":") 152:152
60+
(TOKEN "identifier" i32) 154:156
61+
(TOKEN ")") 157:157
62+
(TOKEN ":") 158:158
63+
(NEWLINE) 159:159
64+
(TOKEN "indent") 160:160
65+
(KEYWORD "if") 161:162
66+
(TOKEN "identifier" x) 164:164
67+
(TOKEN "<") 166:166
68+
(TOKEN "integer" 0) 168:168
69+
(TOKEN ":") 169:169
70+
(NEWLINE) 170:170
71+
(TOKEN "indent") 171:172
72+
(KEYWORD "pass") 173:176
73+
(NEWLINE) 177:177
74+
(TOKEN "dedent") 178:178
75+
(KEYWORD "else") 179:182
76+
(TOKEN ":") 183:183
77+
(NEWLINE) 184:184
78+
(TOKEN "indent") 185:186
79+
(TOKEN "identifier" Test) 187:190
80+
(TOKEN ".") 191:191
81+
(TOKEN "identifier" foo) 192:194
82+
(TOKEN "(") 195:195
83+
(TOKEN "identifier" x) 196:196
84+
(TOKEN ")") 197:197
85+
(NEWLINE) 198:198
86+
(TOKEN "dedent") 198:198
87+
(TOKEN "dedent") 198:198
88+
(EOF) 199:199

tests/tokens/indent2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@ def foo(a):
33
if a == 'foo':
44
return 'foo'
55
return 'bar'
6+
7+
def test():
8+
if True:
9+
pass
10+
else:
11+
pass
12+
13+
def test(x: i32):
14+
if x < 0:
15+
pass
16+
else:
17+
Test.foo(x)

0 commit comments

Comments
 (0)