Skip to content

Commit e354f91

Browse files
committed
fix tokens slices for some special parse methods
fixes FunctionCallExpression to include its type, FunctionDeclaration to include its type, IndexExpression to include its unary expression, fish operators, etc are fixed
1 parent 86c9bf4 commit e354f91

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

src/dparse/parser.d

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,7 @@ class Parser
13821382
auto startIndex = index;
13831383
auto node = allocator.make!AttributeDeclaration;
13841384
node.line = current.line;
1385+
moveStartIndexBefore(startIndex, attribute);
13851386
node.attribute = attribute is null ? parseAttribute() : attribute;
13861387
mixin(tokenCheck!":");
13871388
node.tokens = tokens[startIndex .. index];
@@ -1593,10 +1594,11 @@ class Parser
15931594
* $(LITERAL 'case') $(RULE assignExpression) $(LITERAL ':') $(LITERAL '...') $(LITERAL 'case') $(RULE assignExpression) $(LITERAL ':') $(RULE declarationsAndStatements)
15941595
* ;)
15951596
*/
1596-
CaseRangeStatement parseCaseRangeStatement(ExpressionNode low)
1597+
CaseRangeStatement parseCaseRangeStatement(ExpressionNode low, size_t startIndex = -1)
15971598
{
15981599
mixin(traceEnterAndExit!(__FUNCTION__));
1599-
auto startIndex = index;
1600+
if (startIndex == -1)
1601+
startIndex = index;
16001602
auto node = allocator.make!CaseRangeStatement;
16011603
assert (low !is null);
16021604
node.low = low;
@@ -1620,10 +1622,11 @@ class Parser
16201622
* $(LITERAL 'case') $(RULE _argumentList) $(LITERAL ':') $(RULE declarationsAndStatements)
16211623
* ;)
16221624
*/
1623-
CaseStatement parseCaseStatement(ArgumentList argumentList = null)
1625+
CaseStatement parseCaseStatement(ArgumentList argumentList = null, size_t startIndex = -1)
16241626
{
16251627
mixin(traceEnterAndExit!(__FUNCTION__));
1626-
auto startIndex = index;
1628+
if (startIndex == -1)
1629+
startIndex = index;
16271630
auto node = allocator.make!CaseStatement;
16281631
node.argumentList = argumentList;
16291632
const colon = expect(tok!":");
@@ -3148,6 +3151,7 @@ class Parser
31483151
mixin(traceEnterAndExit!(__FUNCTION__));
31493152
auto startIndex = index;
31503153
auto node = allocator.make!EqualExpression;
3154+
moveStartIndexBefore(startIndex, shift);
31513155
node.left = shift is null ? parseShiftExpression() : shift;
31523156
mixin (nullCheck!`node.left`);
31533157
if (currentIsOneOf(tok!"==", tok!"!="))
@@ -3190,6 +3194,7 @@ class Parser
31903194
mixin(traceEnterAndExit!(__FUNCTION__));
31913195
auto startIndex = index;
31923196
auto node = allocator.make!ExpressionStatement;
3197+
moveStartIndexBefore(startIndex, expression);
31933198
node.expression = expression is null ? parseExpression() : expression;
31943199
if (node.expression is null || expect(tok!";") is null)
31953200
return null;
@@ -3575,7 +3580,10 @@ class Parser
35753580
break;
35763581
default:
35773582
if (unary !is null)
3583+
{
3584+
moveStartIndexBefore(startIndex, unary);
35783585
node.unaryExpression = unary;
3586+
}
35793587
else
35803588
mixin(parseNodeQ!(`node.unaryExpression`, `UnaryExpression`));
35813589
if (currentIs(tok!"!"))
@@ -3633,6 +3641,8 @@ class Parser
36333641
comment = null;
36343642
StackBuffer memberFunctionAttributes;
36353643
node.attributes = attributes;
3644+
foreach (attr; attributes)
3645+
moveStartIndexBefore(startIndex, attr);
36363646

36373647
if (isAuto)
36383648
{
@@ -3660,7 +3670,10 @@ class Parser
36603670
if (type is null)
36613671
mixin(parseNodeQ!(`node.returnType`, `Type`));
36623672
else
3673+
{
3674+
moveStartIndexBefore(startIndex, type);
36633675
node.returnType = type;
3676+
}
36643677
}
36653678

36663679
mixin(tokenCheck!(`node.name`, "identifier"));
@@ -4117,6 +4130,7 @@ class Parser
41174130
mixin(traceEnterAndExit!(__FUNCTION__));
41184131
auto startIndex = index;
41194132
auto node = allocator.make!IdentityExpression;
4133+
moveStartIndexBefore(startIndex, shift);
41204134
mixin(nullCheck!`node.left = shift is null ? parseShiftExpression() : shift`);
41214135
if (currentIs(tok!"!"))
41224136
{
@@ -4291,6 +4305,7 @@ class Parser
42914305
{
42924306
auto startIndex = index;
42934307
auto node = allocator.make!ImportBindings;
4308+
moveStartIndexBefore(startIndex, singleImport);
42944309
mixin(nullCheck!`node.singleImport = singleImport is null ? parseSingleImport() : singleImport`);
42954310
mixin(tokenCheck!":");
42964311
StackBuffer importBinds;
@@ -4425,6 +4440,7 @@ class Parser
44254440
mixin(traceEnterAndExit!(__FUNCTION__));
44264441
auto startIndex = index;
44274442
auto node = allocator.make!IndexExpression;
4443+
moveStartIndexBefore(startIndex, unaryExpression);
44284444
mixin(nullCheck!`node.unaryExpression = unaryExpression is null ? parseUnaryExpression() : unaryExpression`);
44294445
mixin(tokenCheck!"[");
44304446
StackBuffer indexes;
@@ -4484,6 +4500,7 @@ class Parser
44844500
mixin(traceEnterAndExit!(__FUNCTION__));
44854501
auto startIndex = index;
44864502
auto node = allocator.make!InExpression;
4503+
moveStartIndexBefore(startIndex, shift);
44874504
mixin(nullCheck!`node.left = shift is null ? parseShiftExpression() : shift`);
44884505
if (currentIs(tok!"!"))
44894506
{
@@ -6214,9 +6231,9 @@ class Parser
62146231
if (argumentList is null)
62156232
return null;
62166233
if (argumentList.items.length == 1 && startsWith(tok!":", tok!".."))
6217-
node.caseRangeStatement = parseCaseRangeStatement(argumentList.items[0]);
6234+
node.caseRangeStatement = parseCaseRangeStatement(argumentList.items[0], startIndex);
62186235
else
6219-
node.caseStatement = parseCaseStatement(argumentList);
6236+
node.caseStatement = parseCaseStatement(argumentList, startIndex);
62206237
break;
62216238
case tok!"default":
62226239
mixin(parseNodeQ!(`node.defaultStatement`, `DefaultStatement`));
@@ -7954,6 +7971,8 @@ class Parser
79547971
if (newUnary) newUnary.tokens = tokens[startIndex .. index];
79557972
return newUnary;
79567973
}
7974+
if (node !is null)
7975+
node.tokens = tokens[startIndex .. index]; // for parseFunctionCallExpression to inherit
79577976
mixin (nullCheck!`newUnary.functionCallExpression = parseFunctionCallExpression(node)`);
79587977
node = newUnary;
79597978
break;
@@ -7966,6 +7985,8 @@ class Parser
79667985
break;
79677986
case tok!"[":
79687987
auto n = allocator.make!UnaryExpression;
7988+
if (node !is null)
7989+
node.tokens = tokens[startIndex .. index]; // for parseIndexExpression to inherit
79697990
n.indexExpression = parseIndexExpression(node);
79707991
node = n;
79717992
break;
@@ -8076,6 +8097,7 @@ class Parser
80768097
return null;
80778098
ownArray(node.storageClasses, storageClasses);
80788099

8100+
moveStartIndexBefore(startIndex, type);
80798101
node.type = type is null ? parseType() : type;
80808102
node.comment = comment;
80818103
comment = null;
@@ -8733,6 +8755,27 @@ protected: final:
87338755
}
87348756
}
87358757

8758+
void moveStartIndexBefore(ref size_t startIndex, const BaseNode child, string func = __FUNCTION__)
8759+
{
8760+
if (child !is null)
8761+
{
8762+
debug
8763+
{
8764+
assert(child.tokens.length,
8765+
"AST parameter must be finalized or receive early tokens slice before calling "
8766+
~ func);
8767+
}
8768+
else
8769+
{
8770+
if (!child.tokens.length)
8771+
return;
8772+
}
8773+
8774+
startIndex = min(startIndex, &child.tokens[0] - &tokens[0]);
8775+
assert(startIndex >= 0 && startIndex < tokens.length);
8776+
}
8777+
}
8778+
87368779
bool currentIsMemberFunctionAttribute() const
87378780
{
87388781
return moreTokens && isMemberFunctionAttribute(current.type);
@@ -8743,6 +8786,7 @@ protected: final:
87438786
{
87448787
ExpressionNode node;
87458788
auto startIndex = index;
8789+
moveStartIndexBefore(startIndex, part);
87468790
mixin ("node = part is null ? parse" ~ ExpressionPartType.stringof ~ "() : part;");
87478791
if (node is null)
87488792
return null;

0 commit comments

Comments
 (0)