Skip to content

Instantly share code, notes, and snippets.

@dogamak
Created August 5, 2020 17:35
Show Gist options
  • Save dogamak/179dcd764c4a152802a301be8c4622e5 to your computer and use it in GitHub Desktop.
Save dogamak/179dcd764c4a152802a301be8c4622e5 to your computer and use it in GitHub Desktop.
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index f3c1d9b..75fc902 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -1203,21 +1203,16 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
}
}
tok_backup(tok, c);
- if (c == '#' || c == '\n' || c == '\\') {
+
+ if (c == '#' || c == '\n' || c == '\\' || c == EOF) {
/* Lines with only whitespace and/or comments
and/or a line continuation character
shouldn't affect the indentation and are
not passed to the parser as NEWLINE tokens,
except *totally* empty lines in interactive
mode, which signal the end of a command group. */
- if (col == 0 && c == '\n' && tok->prompt != NULL) {
- blankline = 0; /* Let it through */
- }
- else if (tok->prompt != NULL && tok->lineno == 1) {
- /* In interactive mode, if the first line contains
- only spaces and/or a comment, let it through. */
+ if (tok->prompt != NULL) {
blankline = 0;
- col = altcol = 0;
}
else {
blankline = 1; /* Ignore completely */
@@ -1225,6 +1220,7 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
/* We can't jump back right here since we still
may need to skip to the end of a comment */
}
+
if (!blankline && tok->level == 0) {
if (col == tok->indstack[tok->indent]) {
/* No change */
@@ -1232,41 +1228,50 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
return indenterror(tok);
}
}
+ else if (tok->indent == 0 && tok->indstack[0] == 0) {
+ tok->indstack[0] = col;
+ tok->altindstack[0] = altcol;
+ }
else if (col > tok->indstack[tok->indent]) {
+ /* Dedent -- any number, must be consistent */
+ while (tok->indent > 0 &&
+ col > tok->indstack[tok->indent]) {
+ tok->pendin--;
+ tok->indent--;
+ }
+ if (col != tok->indstack[tok->indent]) {
+ tok->done = E_DEDENT;
+ tok->cur = tok->inp;
+ return ERRORTOKEN;
+ }
+ if (altcol != tok->altindstack[tok->indent]) {
+ return indenterror(tok);
+ }
+ }
+ else /* col < tok->indstack[tok->indent] */ {
/* Indent -- always one */
if (tok->indent+1 >= MAXINDENT) {
tok->done = E_TOODEEP;
tok->cur = tok->inp;
return ERRORTOKEN;
}
- if (altcol <= tok->altindstack[tok->indent]) {
+ if (altcol >= tok->altindstack[tok->indent]) {
return indenterror(tok);
}
tok->pendin++;
tok->indstack[++tok->indent] = col;
tok->altindstack[tok->indent] = altcol;
}
- else /* col < tok->indstack[tok->indent] */ {
- /* Dedent -- any number, must be consistent */
- while (tok->indent > 0 &&
- col < tok->indstack[tok->indent]) {
- tok->pendin--;
- tok->indent--;
- }
- if (col != tok->indstack[tok->indent]) {
- tok->done = E_DEDENT;
- tok->cur = tok->inp;
- return ERRORTOKEN;
- }
- if (altcol != tok->altindstack[tok->indent]) {
- return indenterror(tok);
- }
- }
}
}
tok->start = tok->cur;
+ if (c == EOF && tok->indent != 0) {
+ tok->indent--;
+ return DEDENT;
+ }
+
/* Return pending indents/dedents */
if (tok->pendin != 0) {
if (tok->pendin < 0) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment