Skip to content

Commit 3ffd74b

Browse files
committed
Python: don't make reference tags for 'self'
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 42b349d commit 3ffd74b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

Units/parser-python.r/reftag.d/expected.tags

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ X input.py /^class X(object):$/;" c roles:def
44
__init__ input.py /^ def __init__(self, n):$/;" m class:X roles:def
55
self input.py /^ def __init__(self, n):$/;" z member:X.__init__ file: roles:def
66
n input.py /^ def __init__(self, n):$/;" z member:X.__init__ file: roles:def
7-
self input.py /^ self.n = n$/;" Y member:X.__init__ roles:ref
87
n input.py /^ self.n = n$/;" Y member:X.__init__ roles:ref
98
n input.py /^ self.n = n$/;" Y member:X.__init__ roles:ref
109
val input.py /^ def val(self):$/;" m class:X roles:def
1110
self input.py /^ def val(self):$/;" z member:X.val file: roles:def
12-
self input.py /^ return self.n$/;" Y member:X.val roles:ref
1311
n input.py /^ return self.n$/;" Y member:X.val roles:ref
1412
one input.py /^def one():$/;" f roles:def
1513
X input.py /^ return X(1)$/;" Y function:one roles:ref

parsers/python.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ enum {
4444
KEYWORD_lambda,
4545
KEYWORD_pass,
4646
KEYWORD_return,
47+
48+
/* Used only in readTokenFullNoRefTag to represent the identifiers
49+
that should not be tagged as reference tags. */
50+
KEYWORD___noreftag_id,
51+
4752
KEYWORD_REST
4853
};
4954
typedef int keywordId; /* to allow KEYWORD_NONE */
@@ -174,6 +179,7 @@ static const keywordTable PythonKeywordTable[] = {
174179
{ "lambda", KEYWORD_lambda },
175180
{ "pass", KEYWORD_pass },
176181
{ "return", KEYWORD_return },
182+
{ "self", KEYWORD___noreftag_id },
177183
};
178184

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

505-
static void readTokenFullNoRefTag (tokenInfo *const token, bool inclWhitespaces)
511+
static void readTokenFullNoRefTag (tokenInfo *const token, bool inclWhitespaces, bool *noReftagId)
506512
{
507513
int c;
508514
int n;
@@ -689,11 +695,17 @@ static void readTokenFullNoRefTag (tokenInfo *const token, bool inclWhitespaces)
689695
}
690696
else
691697
{
698+
*noReftagId = false;
692699
/* FIXME: handle U, B, R and F string prefixes? */
693700
readIdentifier (token->string, c);
694701
token->keyword = lookupKeyword (vStringValue (token->string), Lang_python);
695702
if (token->keyword == KEYWORD_NONE)
696703
token->type = TOKEN_IDENTIFIER;
704+
else if (token->keyword == KEYWORD___noreftag_id)
705+
{
706+
token->type = TOKEN_IDENTIFIER;
707+
*noReftagId = true;
708+
}
697709
else
698710
token->type = TOKEN_KEYWORD;
699711
}
@@ -719,9 +731,11 @@ static void readTokenFullNoRefTag (tokenInfo *const token, bool inclWhitespaces)
719731

720732
static void readTokenFull (tokenInfo *const token, bool inclWhitespaces)
721733
{
722-
readTokenFullNoRefTag (token, inclWhitespaces);
734+
bool noReftagId;
735+
readTokenFullNoRefTag (token, inclWhitespaces, &noReftagId);
723736

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

0 commit comments

Comments
 (0)