Skip to content

Commit

Permalink
Merge pull request #4008 from b4n/js/implicit-repr
Browse files Browse the repository at this point in the history
jscript: Fix representation of held tokens
  • Loading branch information
masatake authored May 21, 2024
2 parents c7f0806 + 33b90f1 commit fcffa4b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
1 change: 1 addition & 0 deletions Units/parser-javascript.r/github-issue-4005.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--sort=no
2 changes: 2 additions & 0 deletions Units/parser-javascript.r/github-issue-4005.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test1 input.js /^test1 = ({}) => {}$/;" f
test2 input.js /^test2 = ({}$/;" f
4 changes: 4 additions & 0 deletions Units/parser-javascript.r/github-issue-4005.d/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test1 = ({}) => {}

test2 = ({}
) => {}
70 changes: 40 additions & 30 deletions parsers/jscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ typedef struct sTokenInfo {
MIOPos filePosition;
int nestLevel;
bool dynamicProp;
int c;
} tokenInfo;

/*
Expand Down Expand Up @@ -367,6 +368,7 @@ static void copyToken (tokenInfo *const dest, const tokenInfo *const src,
dest->type = src->type;
dest->keyword = src->keyword;
dest->dynamicProp = src->dynamicProp;
dest->c = src->c;
vStringCopy(dest->string, src->string);
if (include_non_read_info)
{
Expand Down Expand Up @@ -996,6 +998,32 @@ static void parseTemplateString (vString *const string)
while (c != EOF);
}

static void reprToken (const tokenInfo *const token, vString *const repr)
{
switch (token->type)
{
case TOKEN_DOTS:
vStringCatS (repr, "...");
break;

case TOKEN_STRING:
case TOKEN_TEMPLATE_STRING:
vStringPut (repr, token->c);
vStringCat (repr, token->string);
vStringPut (repr, token->c);
break;

case TOKEN_IDENTIFIER:
case TOKEN_KEYWORD:
vStringCat (repr, token->string);
break;

default:
vStringPut (repr, token->c);
break;
}
}

static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vString *const repr)
{
int c;
Expand All @@ -1005,9 +1033,12 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
/* if we've got a token held back, emit it */
if (NextToken)
{
TRACE_PRINT("Emitting held token");
copyToken (token, NextToken, false);
deleteToken (NextToken);
NextToken = NULL;
if (repr)
reprToken (token, repr);
return;
}

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

if (repr && c != EOF)
{
if (i > 1)
vStringPut (repr, ' ');
vStringPut (repr, c);
}
/* special case to insert a separator */
if (repr && c != EOF && i > 1)
vStringPut (repr, ' ');

token->c = c;

switch (c)
{
Expand Down Expand Up @@ -1063,14 +1093,6 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
}

token->type = TOKEN_DOTS;
if (repr)
{
/* Adding two dots is enough here.
* The first one is already added with
* vStringPut (repr, c).
*/
vStringCatS (repr, "..");
}
break;
}
case ':': token->type = TOKEN_COLON; break;
Expand Down Expand Up @@ -1125,23 +1147,13 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
parseString (token->string, c);
token->lineNumber = getInputLineNumber ();
token->filePosition = getInputFilePosition ();
if (repr)
{
vStringCat (repr, token->string);
vStringPut (repr, c);
}
break;

case '`':
token->type = TOKEN_TEMPLATE_STRING;
parseTemplateString (token->string);
token->lineNumber = getInputLineNumber ();
token->filePosition = getInputFilePosition ();
if (repr)
{
vStringCat (repr, token->string);
vStringPut (repr, c);
}
break;

case '/':
Expand Down Expand Up @@ -1173,8 +1185,6 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
}
else
{
if (repr) /* remove the / we added */
vStringChop(repr);
if (d == '*')
{
skipToCharacterInInputFile2('*', '/');
Expand Down Expand Up @@ -1228,8 +1238,6 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
token->type = TOKEN_IDENTIFIER;
else
token->type = TOKEN_KEYWORD;
if (repr && vStringLength (token->string) > 1)
vStringCatS (repr, vStringValue (token->string) + 1);
}
break;
}
Expand Down Expand Up @@ -1278,15 +1286,17 @@ static void readTokenFullRaw (tokenInfo *const token, bool include_newlines, vSt
token->type = TOKEN_SEMICOLON;
token->keyword = KEYWORD_NONE;
vStringClear (token->string);
if (repr)
vStringPut (token->string, '\n');
token->c = '\n';
}

#undef IS_STMT_SEPARATOR
#undef IS_BINARY_OPERATOR
}

LastTokenType = token->type;

if (repr)
reprToken (token, repr);
}

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

0 comments on commit fcffa4b

Please sign in to comment.