mirror of
https://github.com/python/cpython
synced 2024-09-16 03:29:57 +00:00
Add checking for inconsistent tab usage
This commit is contained in:
parent
e77a992a0d
commit
926f13a081
|
@ -125,6 +125,11 @@ tok_new()
|
||||||
tok->prompt = tok->nextprompt = NULL;
|
tok->prompt = tok->nextprompt = NULL;
|
||||||
tok->lineno = 0;
|
tok->lineno = 0;
|
||||||
tok->level = 0;
|
tok->level = 0;
|
||||||
|
tok->filename = NULL;
|
||||||
|
tok->altwarning = 0;
|
||||||
|
tok->alterror = 0;
|
||||||
|
tok->alttabsize = 1;
|
||||||
|
tok->altindstack[0] = 0;
|
||||||
return tok;
|
return tok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -422,6 +427,24 @@ PyToken_TwoChars(c1, c2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
indenterror(tok)
|
||||||
|
struct tok_state *tok;
|
||||||
|
{
|
||||||
|
if (tok->alterror) {
|
||||||
|
tok->done = E_INDENT;
|
||||||
|
tok->cur = tok->inp;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (tok->altwarning) {
|
||||||
|
fprintf(stderr, "%s: inconsistent tab/space usage\n",
|
||||||
|
tok->filename);
|
||||||
|
tok->altwarning = 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Get next token, after space stripping etc. */
|
/* Get next token, after space stripping etc. */
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -440,15 +463,19 @@ PyTokenizer_Get(tok, p_start, p_end)
|
||||||
/* Get indentation level */
|
/* Get indentation level */
|
||||||
if (tok->atbol) {
|
if (tok->atbol) {
|
||||||
register int col = 0;
|
register int col = 0;
|
||||||
|
register int altcol = 0;
|
||||||
tok->atbol = 0;
|
tok->atbol = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = tok_nextc(tok);
|
c = tok_nextc(tok);
|
||||||
if (c == ' ')
|
if (c == ' ')
|
||||||
col++;
|
col++, altcol++;
|
||||||
else if (c == '\t')
|
else if (c == '\t') {
|
||||||
col = (col/tok->tabsize + 1) * tok->tabsize;
|
col = (col/tok->tabsize + 1) * tok->tabsize;
|
||||||
|
altcol = (altcol/tok->alttabsize + 1)
|
||||||
|
* tok->alttabsize;
|
||||||
|
}
|
||||||
else if (c == '\014') /* Control-L (formfeed) */
|
else if (c == '\014') /* Control-L (formfeed) */
|
||||||
col = 0; /* For Emacs users */
|
col = altcol = 0; /* For Emacs users */
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -469,6 +496,10 @@ PyTokenizer_Get(tok, p_start, p_end)
|
||||||
if (!blankline && tok->level == 0) {
|
if (!blankline && tok->level == 0) {
|
||||||
if (col == tok->indstack[tok->indent]) {
|
if (col == tok->indstack[tok->indent]) {
|
||||||
/* No change */
|
/* No change */
|
||||||
|
if (altcol != tok->altindstack[tok->indent]) {
|
||||||
|
if (indenterror(tok))
|
||||||
|
return ERRORTOKEN;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (col > tok->indstack[tok->indent]) {
|
else if (col > tok->indstack[tok->indent]) {
|
||||||
/* Indent -- always one */
|
/* Indent -- always one */
|
||||||
|
@ -478,8 +509,13 @@ PyTokenizer_Get(tok, p_start, p_end)
|
||||||
tok->cur = tok->inp;
|
tok->cur = tok->inp;
|
||||||
return ERRORTOKEN;
|
return ERRORTOKEN;
|
||||||
}
|
}
|
||||||
|
if (altcol <= tok->altindstack[tok->indent]) {
|
||||||
|
if (indenterror(tok))
|
||||||
|
return ERRORTOKEN;
|
||||||
|
}
|
||||||
tok->pendin++;
|
tok->pendin++;
|
||||||
tok->indstack[++tok->indent] = col;
|
tok->indstack[++tok->indent] = col;
|
||||||
|
tok->altindstack[tok->indent] = altcol;
|
||||||
}
|
}
|
||||||
else /* col < tok->indstack[tok->indent] */ {
|
else /* col < tok->indstack[tok->indent] */ {
|
||||||
/* Dedent -- any number, must be consistent */
|
/* Dedent -- any number, must be consistent */
|
||||||
|
@ -495,6 +531,10 @@ PyTokenizer_Get(tok, p_start, p_end)
|
||||||
tok->cur = tok->inp;
|
tok->cur = tok->inp;
|
||||||
return ERRORTOKEN;
|
return ERRORTOKEN;
|
||||||
}
|
}
|
||||||
|
if (altcol != tok->altindstack[tok->indent]) {
|
||||||
|
if (indenterror(tok))
|
||||||
|
return ERRORTOKEN;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,6 +62,12 @@ struct tok_state {
|
||||||
int lineno; /* Current line number */
|
int lineno; /* Current line number */
|
||||||
int level; /* () [] {} Parentheses nesting level */
|
int level; /* () [] {} Parentheses nesting level */
|
||||||
/* Used to allow free continuations inside them */
|
/* Used to allow free continuations inside them */
|
||||||
|
/* Stuff for checking on different tab sizes */
|
||||||
|
char *filename; /* For error messages */
|
||||||
|
int altwarning; /* Issue warning if alternate tabs don't match */
|
||||||
|
int alterror; /* Issue error if alternate tabs don't match */
|
||||||
|
int alttabsize; /* Alternate tab spacing */
|
||||||
|
int altindstack[MAXINDENT]; /* Stack of alternate indents */
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct tok_state *PyTokenizer_FromString Py_PROTO((char *));
|
extern struct tok_state *PyTokenizer_FromString Py_PROTO((char *));
|
||||||
|
|
Loading…
Reference in a new issue