Skip to content

Commit 09a580e

Browse files
committed
refactor: lexer in indentation.d and wrapping.d
Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
1 parent e160202 commit 09a580e

File tree

2 files changed

+117
-58
lines changed

2 files changed

+117
-58
lines changed

src/dfmt/indentation.d

Lines changed: 109 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,82 @@ module dfmt.indentation;
77

88
import dfmt.config;
99
import dfmt.editorconfig;
10-
import dparse.lexer;
10+
import dmd.tokens;
1111

1212
import std.bitmanip : bitfields;
1313

1414
/**
1515
* Returns: true if the given token type is a wrap indent type
1616
*/
17-
bool isWrapIndent(IdType type) pure nothrow @nogc @safe
17+
bool isWrapIndent(TOK type) pure nothrow @nogc @safe
1818
{
19-
return type != tok!"{" && type != tok!"case" && type != tok!"@"
20-
&& type != tok!"]" && type != tok!"(" && type != tok!")" && isOperator(type);
19+
switch (type)
20+
{
21+
case TOK.leftCurly:
22+
case TOK.case_:
23+
case TOK.at:
24+
case TOK.rightBracket:
25+
case TOK.leftParenthesis:
26+
case TOK.rightParenthesis:
27+
return false;
28+
29+
// Operators
30+
case TOK.lessThan:
31+
case TOK.greaterThan:
32+
case TOK.lessOrEqual:
33+
case TOK.greaterOrEqual:
34+
case TOK.equal:
35+
case TOK.notEqual:
36+
case TOK.identity:
37+
case TOK.notIdentity:
38+
case TOK.is_:
39+
40+
case TOK.leftShift:
41+
case TOK.rightShift:
42+
case TOK.leftShiftAssign:
43+
case TOK.rightShiftAssign:
44+
case TOK.unsignedRightShift:
45+
case TOK.unsignedRightShiftAssign:
46+
case TOK.concatenateAssign:
47+
case TOK.add:
48+
case TOK.min:
49+
case TOK.addAssign:
50+
case TOK.minAssign:
51+
case TOK.mul:
52+
case TOK.div:
53+
case TOK.mod:
54+
case TOK.mulAssign:
55+
case TOK.divAssign:
56+
case TOK.modAssign:
57+
case TOK.and:
58+
case TOK.or:
59+
case TOK.xor:
60+
case TOK.andAssign:
61+
case TOK.orAssign:
62+
case TOK.xorAssign:
63+
case TOK.assign:
64+
case TOK.not:
65+
case TOK.tilde:
66+
case TOK.plusPlus:
67+
case TOK.minusMinus:
68+
case TOK.dot:
69+
case TOK.comma:
70+
case TOK.question:
71+
case TOK.andAnd:
72+
case TOK.orOr:
73+
return true;
74+
default:
75+
return false;
76+
}
2177
}
2278

2379
/**
2480
* Returns: true if the given token type is a temporary indent type
2581
*/
26-
bool isTempIndent(IdType type) pure nothrow @nogc @safe
82+
bool isTempIndent(TOK type) pure nothrow @nogc @safe
2783
{
28-
return type != tok!")" && type != tok!"{" && type != tok!"case" && type != tok!"@";
84+
return type != TOK.rightParenthesis && type != TOK.leftCurly && type != TOK.case_ && type != TOK
85+
.at;
2986
}
3087

3188
/**
@@ -44,24 +101,20 @@ struct IndentStack
44101
static struct Details
45102
{
46103
mixin(bitfields!(
47-
// generally true for all operators except {, case, @, ], (, )
48-
bool, "wrap", 1,
49-
// temporary indentation which get's reverted when a block starts
50-
// generally true for all tokens except ), {, case, @
51-
bool, "temp", 1,
52-
// emit minimal newlines
53-
bool, "mini", 1,
54-
// for associative arrays or arrays containing them, break after every item
55-
bool, "breakEveryItem", 1,
56-
// when an item inside an array would break mid-item, definitely break at the comma first
57-
bool, "preferLongBreaking", 1,
58-
uint, "", 27));
104+
// generally true for all operators except {, case, @, ], (, )
105+
bool, "wrap", 1, // temporary indentation which get's reverted when a block starts
106+
// generally true for all tokens except ), {, case, @
107+
bool, "temp", 1, // emit minimal newlines
108+
bool, "mini", 1, // for associative arrays or arrays containing them, break after every item
109+
bool, "breakEveryItem", 1, // when an item inside an array would break mid-item, definitely break at the comma first
110+
bool, "preferLongBreaking", 1,
111+
uint, "", 27));
59112
}
60113

61114
/**
62115
* Get the indent size at the most recent occurrence of the given indent type
63116
*/
64-
int indentToMostRecent(IdType item) const
117+
int indentToMostRecent(TOK item) const
65118
{
66119
if (index == 0)
67120
return -1;
@@ -84,7 +137,7 @@ struct IndentStack
84137
int tempIndentCount = 0;
85138
for (size_t i = index; i > 0; i--)
86139
{
87-
if (!details[i - 1].wrap && arr[i - 1] != tok!"]")
140+
if (!details[i - 1].wrap && arr[i - 1] != TOK.rightBracket)
88141
break;
89142
tempIndentCount++;
90143
}
@@ -94,7 +147,7 @@ struct IndentStack
94147
/**
95148
* Pushes the given indent type on to the stack.
96149
*/
97-
void push(IdType item) pure nothrow
150+
void push(TOK item) pure nothrow
98151
{
99152
Details detail;
100153
detail.wrap = isWrapIndent(item);
@@ -105,7 +158,7 @@ struct IndentStack
105158
/**
106159
* Pushes the given indent type on to the stack.
107160
*/
108-
void push(IdType item, Details detail) pure nothrow
161+
void push(TOK item, Details detail) pure nothrow
109162
{
110163
arr[index] = item;
111164
details[index] = detail;
@@ -145,7 +198,7 @@ struct IndentStack
145198
index--;
146199
}
147200

148-
bool topAre(IdType[] types...)
201+
bool topAre(TOK[] types...)
149202
{
150203
if (types.length > index)
151204
return false;
@@ -156,7 +209,7 @@ struct IndentStack
156209
/**
157210
* Returns: `true` if the top of the indent stack is the given indent type.
158211
*/
159-
bool topIs(IdType type) const pure nothrow @safe @nogc
212+
bool topIs(TOK type) const pure nothrow @safe @nogc
160213
{
161214
return index > 0 && index <= arr.length && arr[index - 1] == type;
162215
}
@@ -172,9 +225,10 @@ struct IndentStack
172225
/**
173226
* Returns: `true` if the top of the indent stack is a temporary indent with the specified token
174227
*/
175-
bool topIsTemp(IdType item)
228+
bool topIsTemp(TOK item)
176229
{
177-
return index > 0 && index <= arr.length && arr[index - 1] == item && details[index - 1].temp;
230+
return index > 0 && index <= arr.length && arr[index - 1] == item && details[index - 1]
231+
.temp;
178232
}
179233

180234
/**
@@ -188,16 +242,17 @@ struct IndentStack
188242
/**
189243
* Returns: `true` if the top of the indent stack is a temporary indent with the specified token
190244
*/
191-
bool topIsWrap(IdType item)
245+
bool topIsWrap(TOK item)
192246
{
193-
return index > 0 && index <= arr.length && arr[index - 1] == item && details[index - 1].wrap;
247+
return index > 0 && index <= arr.length && arr[index - 1] == item && details[index - 1]
248+
.wrap;
194249
}
195250

196251
/**
197252
* Returns: `true` if the top of the indent stack is one of the given token
198253
* types.
199254
*/
200-
bool topIsOneOf(IdType[] types...) const pure nothrow @safe @nogc
255+
bool topIsOneOf(TOK[] types...) const pure nothrow @safe @nogc
201256
{
202257
if (index == 0)
203258
return false;
@@ -208,7 +263,7 @@ struct IndentStack
208263
return false;
209264
}
210265

211-
IdType top() const pure nothrow @property @safe @nogc
266+
TOK top() const pure nothrow @property @safe @nogc
212267
{
213268
return arr[index - 1];
214269
}
@@ -233,36 +288,38 @@ struct IndentStack
233288
*/
234289
void dump(size_t pos = size_t.max, string file = __FILE__, uint line = __LINE__) const
235290
{
236-
import dparse.lexer : str;
237291
import std.algorithm.iteration : map;
238292
import std.stdio : stderr;
239293

240294
if (pos == size_t.max)
241-
stderr.writefln("\033[31m%s:%d %(%s %)\033[0m", file, line, arr[0 .. index].map!(a => str(a)));
295+
stderr.writefln("\033[31m%s:%d %(%s %)\033[0m", file, line, arr[0 .. index].map!(
296+
a => Token.toString(a)));
242297
else
243-
stderr.writefln("\033[31m%s:%d at %d %(%s %)\033[0m", file, line, pos, arr[0 .. index].map!(a => str(a)));
298+
stderr.writefln("\033[31m%s:%d at %d %(%s %)\033[0m", file, line, pos, arr[0 .. index].map!(
299+
a => Token.toString(a)));
244300
}
245301

246302
private:
247303

248304
size_t index;
249305

250-
IdType[256] arr;
306+
TOK[256] arr;
251307
Details[arr.length] details;
252308

253309
int indentSize(const size_t k = size_t.max) const pure nothrow @safe @nogc
254310
{
255311
import std.algorithm : among;
312+
256313
if (index == 0 || k == 0)
257314
return 0;
258315
immutable size_t j = k == size_t.max ? index : k;
259316
int size = 0;
260317
int parenCount;
261318
foreach (i; 0 .. j)
262319
{
263-
immutable int pc = (arr[i] == tok!"!" || arr[i] == tok!"(" || arr[i] == tok!")") ? parenCount + 1
264-
: parenCount;
265-
if ((details[i].wrap || arr[i] == tok!"(") && parenCount > 1)
320+
immutable int pc = (arr[i] == TOK.not || arr[i] == TOK.leftParenthesis || arr[i] == TOK
321+
.rightParenthesis) ? parenCount + 1 : parenCount;
322+
if ((details[i].wrap || arr[i] == TOK.leftParenthesis) && parenCount > 1)
266323
{
267324
parenCount = pc;
268325
continue;
@@ -277,31 +334,33 @@ private:
277334
}
278335

279336
immutable currentIsNonWrapTemp = !details[i].wrap
280-
&& details[i].temp && arr[i] != tok!")" && arr[i] != tok!"!";
337+
&& details[i].temp && arr[i] != TOK.rightParenthesis && arr[i] != TOK.not;
281338

282-
if (currentIsNonWrapTemp && arr[i + 1] == tok!"]")
339+
if (currentIsNonWrapTemp && arr[i + 1] == TOK.rightBracket)
283340
{
284341
parenCount = pc;
285342
continue;
286343
}
287-
if (arr[i] == tok!"static"
288-
&& arr[i + 1].among!(tok!"if", tok!"else", tok!"foreach", tok!"foreach_reverse")
289-
&& (i + 2 >= index || arr[i + 2] != tok!"{"))
344+
if (arr[i] == TOK.static_
345+
&& arr[i + 1].among!(TOK.if_, TOK.else_, TOK.foreach_, TOK.foreach_reverse_)
346+
&& (i + 2 >= index || arr[i + 2] != TOK.leftCurly))
290347
{
291348
parenCount = pc;
292349
continue;
293350
}
294-
if (currentIsNonWrapTemp && (arr[i + 1] == tok!"switch"
295-
|| arr[i + 1] == tok!"{" || arr[i + 1] == tok!")"))
351+
352+
if (currentIsNonWrapTemp && (arr[i + 1] == TOK.switch_
353+
|| arr[i + 1] == TOK.leftCurly || arr[i + 1] == TOK.rightParenthesis))
296354
{
297355
parenCount = pc;
298356
continue;
299357
}
300358
}
301-
else if (parenCount == 0 && arr[i] == tok!"(" && config.dfmt_single_indent == OptionalBoolean.f)
359+
else if (parenCount == 0 && arr[i] == TOK.leftParenthesis && config.dfmt_single_indent == OptionalBoolean
360+
.f)
302361
size++;
303362

304-
if (arr[i] == tok!"!")
363+
if (arr[i] == TOK.not)
305364
size++;
306365

307366
parenCount = pc;
@@ -312,21 +371,21 @@ private:
312371

313372
bool skipDoubleIndent(size_t i, int parenCount) const pure nothrow @safe @nogc
314373
{
315-
return (details[i + 1].wrap && arr[i] == tok!")")
316-
|| (parenCount == 0 && arr[i + 1] == tok!"," && arr[i] == tok!"(");
374+
return (details[i + 1].wrap && arr[i] == TOK.rightParenthesis)
375+
|| (parenCount == 0 && arr[i + 1] == TOK.comma && arr[i] == TOK.leftParenthesis);
317376
}
318377
}
319378

320379
unittest
321380
{
322381
IndentStack stack;
323-
stack.push(tok!"{");
382+
stack.push(TOK.leftCurly);
324383
assert(stack.length == 1);
325384
assert(stack.indentLevel == 1);
326385
stack.pop();
327386
assert(stack.length == 0);
328387
assert(stack.indentLevel == 0);
329-
stack.push(tok!"if");
388+
stack.push(TOK.if_);
330389
assert(stack.topIsTemp());
331390
stack.popTempIndents();
332391
assert(stack.length == 0);

src/dfmt/wrapping.d

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
module dfmt.wrapping;
77

8-
import dparse.lexer;
8+
import dmd.tokens;
99
import dfmt.tokens;
1010
import dfmt.config;
1111

1212
struct State
1313
{
1414
this(uint breaks, const Token[] tokens, immutable short[] depths,
15-
const Config* config, int currentLineLength, int indentLevel) pure @safe
15+
const Config* config, int currentLineLength, int indentLevel) @safe
1616
{
1717
import std.math : abs;
1818
import core.bitop : popcnt, bsf;
@@ -42,8 +42,8 @@ struct State
4242
{
4343
if (((1 << i) & breaks) == 0)
4444
continue;
45-
immutable prevType = i > 0 ? tokens[i - 1].type : tok!"";
46-
immutable currentType = tokens[i].type;
45+
immutable prevType = i > 0 ? tokens[i - 1].value : TOK.error;
46+
immutable currentType = tokens[i].value;
4747
immutable p = abs(depths[i]);
4848
immutable bc = breakCost(prevType, currentType) * (p == 0 ? 1 : p * 2);
4949
this._cost += bc + newlinePenalty;
@@ -126,7 +126,7 @@ private enum ALGORITHMIC_COMPLEXITY_SUCKS = uint.sizeof * 8;
126126
* continuation indents are reduced. This is used for array literals.
127127
*/
128128
size_t[] chooseLineBreakTokens(size_t index, const Token[] tokens,
129-
immutable short[] depths, const Config* config, int currentLineLength, int indentLevel)
129+
immutable short[] depths, const Config* config, int currentLineLength, int indentLevel)
130130
{
131131
import std.container.rbtree : RedBlackTree;
132132
import std.algorithm : filter, min;
@@ -159,7 +159,7 @@ size_t[] chooseLineBreakTokens(size_t index, const Token[] tokens,
159159
if (current < lowest)
160160
lowest = current;
161161
validMoves!(typeof(open))(open, tokens[0 .. tokensEnd], depths[0 .. tokensEnd],
162-
current.breaks, config, currentLineLength, indentLevel);
162+
current.breaks, config, currentLineLength, indentLevel);
163163
}
164164
foreach (r; open[].filter!(a => a.solved))
165165
return genRetVal(r.breaks, index);
@@ -170,11 +170,11 @@ size_t[] chooseLineBreakTokens(size_t index, const Token[] tokens,
170170
}
171171

172172
void validMoves(OR)(auto ref OR output, const Token[] tokens, immutable short[] depths,
173-
uint current, const Config* config, int currentLineLength, int indentLevel)
173+
uint current, const Config* config, int currentLineLength, int indentLevel)
174174
{
175175
foreach (i, token; tokens)
176176
{
177-
if (!isBreakToken(token.type) || (((1 << i) & current) != 0))
177+
if (!isBreakToken(token.value) || (((1 << i) & current) != 0))
178178
continue;
179179
immutable uint breaks = current | (1 << i);
180180
output.insert(State(breaks, tokens, depths, config, currentLineLength, indentLevel));

0 commit comments

Comments
 (0)