Skip to content

Commit

Permalink
Python: don't make reference tags for 'self'
Browse files Browse the repository at this point in the history
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Dec 18, 2023
1 parent 42b349d commit 3ffd74b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 0 additions & 2 deletions Units/parser-python.r/reftag.d/expected.tags
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ X input.py /^class X(object):$/;" c roles:def
__init__ input.py /^ def __init__(self, n):$/;" m class:X roles:def
self input.py /^ def __init__(self, n):$/;" z member:X.__init__ file: roles:def
n input.py /^ def __init__(self, n):$/;" z member:X.__init__ file: roles:def
self input.py /^ self.n = n$/;" Y member:X.__init__ roles:ref
n input.py /^ self.n = n$/;" Y member:X.__init__ roles:ref
n input.py /^ self.n = n$/;" Y member:X.__init__ roles:ref
val input.py /^ def val(self):$/;" m class:X roles:def
self input.py /^ def val(self):$/;" z member:X.val file: roles:def
self input.py /^ return self.n$/;" Y member:X.val roles:ref
n input.py /^ return self.n$/;" Y member:X.val roles:ref
one input.py /^def one():$/;" f roles:def
X input.py /^ return X(1)$/;" Y function:one roles:ref
Expand Down
18 changes: 16 additions & 2 deletions parsers/python.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ enum {
KEYWORD_lambda,
KEYWORD_pass,
KEYWORD_return,

/* Used only in readTokenFullNoRefTag to represent the identifiers
that should not be tagged as reference tags. */
KEYWORD___noreftag_id,

KEYWORD_REST
};
typedef int keywordId; /* to allow KEYWORD_NONE */
Expand Down Expand Up @@ -174,6 +179,7 @@ static const keywordTable PythonKeywordTable[] = {
{ "lambda", KEYWORD_lambda },
{ "pass", KEYWORD_pass },
{ "return", KEYWORD_return },
{ "self", KEYWORD___noreftag_id },
};

/* Taken from https://docs.python.org/3/reference/lexical_analysis.html#keywords */
Expand Down Expand Up @@ -502,7 +508,7 @@ static void ungetToken (tokenInfo *const token)
copyToken (NextToken, token);
}

static void readTokenFullNoRefTag (tokenInfo *const token, bool inclWhitespaces)
static void readTokenFullNoRefTag (tokenInfo *const token, bool inclWhitespaces, bool *noReftagId)
{
int c;
int n;
Expand Down Expand Up @@ -689,11 +695,17 @@ static void readTokenFullNoRefTag (tokenInfo *const token, bool inclWhitespaces)
}
else
{
*noReftagId = false;
/* FIXME: handle U, B, R and F string prefixes? */
readIdentifier (token->string, c);
token->keyword = lookupKeyword (vStringValue (token->string), Lang_python);
if (token->keyword == KEYWORD_NONE)
token->type = TOKEN_IDENTIFIER;
else if (token->keyword == KEYWORD___noreftag_id)
{
token->type = TOKEN_IDENTIFIER;
*noReftagId = true;
}
else
token->type = TOKEN_KEYWORD;
}
Expand All @@ -719,9 +731,11 @@ static void readTokenFullNoRefTag (tokenInfo *const token, bool inclWhitespaces)

static void readTokenFull (tokenInfo *const token, bool inclWhitespaces)
{
readTokenFullNoRefTag (token, inclWhitespaces);
bool noReftagId;
readTokenFullNoRefTag (token, inclWhitespaces, &noReftagId);

if (token->type == TOKEN_IDENTIFIER
&& (!noReftagId)
/* Don't make a ref tag for a number. */
&& (vStringLength (token->string) > 0 &&
!isdigit ((unsigned char)vStringChar (token->string, 0)))
Expand Down

0 comments on commit 3ffd74b

Please sign in to comment.