From 8b426b9dcad51ee64bb47c0cbde7236080662ce3 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 19 Jan 2019 11:34:52 -0800 Subject: [PATCH] Make recognition of type comments conditional on a flag (off by default) --- Parser/tokenizer.c | 76 ++++++++++++++++++++++++---------------------- Parser/tokenizer.h | 2 ++ 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 17af219178c4..d9d7f999466a 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -86,6 +86,7 @@ tok_new(void) tok->decoding_readline = NULL; tok->decoding_buffer = NULL; #endif + tok->type_comments = 0; return tok; } @@ -1253,51 +1254,54 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end) if (c == '#') { const char *prefix, *p, *type_start; - while (c != EOF && c != '\n') + while (c != EOF && c != '\n') { c = tok_nextc(tok); + } - p = tok->start; - prefix = type_comment_prefix; - while (*prefix && p < tok->cur) { - if (*prefix == ' ') { - while (*p == ' ' || *p == '\t') + if (tok->type_comments) { + p = tok->start; + prefix = type_comment_prefix; + while (*prefix && p < tok->cur) { + if (*prefix == ' ') { + while (*p == ' ' || *p == '\t') + p++; + } else if (*prefix == *p) { p++; - } else if (*prefix == *p) { - p++; - } else { - break; - } + } else { + break; + } - prefix++; - } + prefix++; + } - /* This is a type comment if we matched all of type_comment_prefix. */ - if (!*prefix) { - int is_type_ignore = 1; - tok_backup(tok, c); /* don't eat the newline or EOF */ + /* This is a type comment if we matched all of type_comment_prefix. */ + if (!*prefix) { + int is_type_ignore = 1; + tok_backup(tok, c); /* don't eat the newline or EOF */ - type_start = p; + type_start = p; - is_type_ignore = tok->cur >= p + 6 && memcmp(p, "ignore", 6) == 0; - p += 6; - while (is_type_ignore && p < tok->cur) { - if (*p == '#') - break; - is_type_ignore = is_type_ignore && (*p == ' ' || *p == '\t'); - p++; - } + is_type_ignore = tok->cur >= p + 6 && memcmp(p, "ignore", 6) == 0; + p += 6; + while (is_type_ignore && p < tok->cur) { + if (*p == '#') + break; + is_type_ignore = is_type_ignore && (*p == ' ' || *p == '\t'); + p++; + } - if (is_type_ignore) { - /* If this type ignore is the only thing on the line, consume the newline also. */ - if (blankline) { - tok_nextc(tok); - tok->atbol = 1; + if (is_type_ignore) { + /* If this type ignore is the only thing on the line, consume the newline also. */ + if (blankline) { + tok_nextc(tok); + tok->atbol = 1; + } + return TYPE_IGNORE; + } else { + *p_start = (char *) type_start; /* after type_comment_prefix */ + *p_end = tok->cur; + return TYPE_COMMENT; } - return TYPE_IGNORE; - } else { - *p_start = (char *) type_start; /* after type_comment_prefix */ - *p_end = tok->cur; - return TYPE_COMMENT; } } } diff --git a/Parser/tokenizer.h b/Parser/tokenizer.h index 096ce687ec54..9639c658b1c2 100644 --- a/Parser/tokenizer.h +++ b/Parser/tokenizer.h @@ -70,6 +70,8 @@ struct tok_state { const char* enc; /* Encoding for the current str. */ const char* str; const char* input; /* Tokenizer's newline translated copy of the string. */ + + int type_comments; /* Whether to look for type comments */ }; extern struct tok_state *PyTokenizer_FromString(const char *, int); -- 2.47.3