Skip to content

Commit fcffa4b

Browse files
authored
Merge pull request #4008 from b4n/js/implicit-repr
jscript: Fix representation of held tokens
2 parents c7f0806 + 33b90f1 commit fcffa4b

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--sort=no
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test1 input.js /^test1 = ({}) => {}$/;" f
2+
test2 input.js /^test2 = ({}$/;" f
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test1 = ({}) => {}
2+
3+
test2 = ({}
4+
) => {}

parsers/jscript.c

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ typedef struct sTokenInfo {
135135
MIOPos filePosition;
136136
int nestLevel;
137137
bool dynamicProp;
138+
int c;
138139
} tokenInfo;
139140

140141
/*
@@ -367,6 +368,7 @@ static void copyToken (tokenInfo *const dest, const tokenInfo *const src,
367368
dest->type = src->type;
368369
dest->keyword = src->keyword;
369370
dest->dynamicProp = src->dynamicProp;
371+
dest->c = src->c;
370372
vStringCopy(dest->string, src->string);
371373
if (include_non_read_info)
372374
{
@@ -996,6 +998,32 @@ static void parseTemplateString (vString *const string)
996998
while (c != EOF);
997999
}
9981000

1001+
static void reprToken (const tokenInfo *const token, vString *const repr)
1002+
{
1003+
switch (token->type)
1004+
{
1005+
case TOKEN_DOTS:
1006+
vStringCatS (repr, "...");
1007+
break;
1008+
1009+
case TOKEN_STRING:
1010+
case TOKEN_TEMPLATE_STRING:
1011+
vStringPut (repr, token->c);
1012+
vStringCat (repr, token->string);
1013+
vStringPut (repr, token->c);
1014+
break;
1015+
1016+
case TOKEN_IDENTIFIER:
1017+
case TOKEN_KEYWORD:
1018+
vStringCat (repr, token->string);
1019+
break;
1020+
1021+
default:
1022+
vStringPut (repr, token->c);
1023+
break;
1024+
}
1025+
}
1026+
9991027
static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vString *const repr)
10001028
{
10011029
int c;
@@ -1005,9 +1033,12 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
10051033
/* if we've got a token held back, emit it */
10061034
if (NextToken)
10071035
{
1036+
TRACE_PRINT("Emitting held token");
10081037
copyToken (token, NextToken, false);
10091038
deleteToken (NextToken);
10101039
NextToken = NULL;
1040+
if (repr)
1041+
reprToken (token, repr);
10111042
return;
10121043
}
10131044

@@ -1029,12 +1060,11 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
10291060
token->lineNumber = getInputLineNumber ();
10301061
token->filePosition = getInputFilePosition ();
10311062

1032-
if (repr && c != EOF)
1033-
{
1034-
if (i > 1)
1035-
vStringPut (repr, ' ');
1036-
vStringPut (repr, c);
1037-
}
1063+
/* special case to insert a separator */
1064+
if (repr && c != EOF && i > 1)
1065+
vStringPut (repr, ' ');
1066+
1067+
token->c = c;
10381068

10391069
switch (c)
10401070
{
@@ -1063,14 +1093,6 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
10631093
}
10641094

10651095
token->type = TOKEN_DOTS;
1066-
if (repr)
1067-
{
1068-
/* Adding two dots is enough here.
1069-
* The first one is already added with
1070-
* vStringPut (repr, c).
1071-
*/
1072-
vStringCatS (repr, "..");
1073-
}
10741096
break;
10751097
}
10761098
case ':': token->type = TOKEN_COLON; break;
@@ -1125,23 +1147,13 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
11251147
parseString (token->string, c);
11261148
token->lineNumber = getInputLineNumber ();
11271149
token->filePosition = getInputFilePosition ();
1128-
if (repr)
1129-
{
1130-
vStringCat (repr, token->string);
1131-
vStringPut (repr, c);
1132-
}
11331150
break;
11341151

11351152
case '`':
11361153
token->type = TOKEN_TEMPLATE_STRING;
11371154
parseTemplateString (token->string);
11381155
token->lineNumber = getInputLineNumber ();
11391156
token->filePosition = getInputFilePosition ();
1140-
if (repr)
1141-
{
1142-
vStringCat (repr, token->string);
1143-
vStringPut (repr, c);
1144-
}
11451157
break;
11461158

11471159
case '/':
@@ -1173,8 +1185,6 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
11731185
}
11741186
else
11751187
{
1176-
if (repr) /* remove the / we added */
1177-
vStringChop(repr);
11781188
if (d == '*')
11791189
{
11801190
skipToCharacterInInputFile2('*', '/');
@@ -1228,8 +1238,6 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
12281238
token->type = TOKEN_IDENTIFIER;
12291239
else
12301240
token->type = TOKEN_KEYWORD;
1231-
if (repr && vStringLength (token->string) > 1)
1232-
vStringCatS (repr, vStringValue (token->string) + 1);
12331241
}
12341242
break;
12351243
}
@@ -1278,15 +1286,17 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
12781286
token->type = TOKEN_SEMICOLON;
12791287
token->keyword = KEYWORD_NONE;
12801288
vStringClear (token->string);
1281-
if (repr)
1282-
vStringPut (token->string, '\n');
1289+
token->c = '\n';
12831290
}
12841291

12851292
#undef IS_STMT_SEPARATOR
12861293
#undef IS_BINARY_OPERATOR
12871294
}
12881295

12891296
LastTokenType = token->type;
1297+
1298+
if (repr)
1299+
reprToken (token, repr);
12901300
}
12911301

12921302
/* whether something we consider a keyword (either because it sometimes is or

0 commit comments

Comments
 (0)