Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Parser-tokenizer link implementation */ |
| 3 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | #include "tokenizer.h" |
| 6 | #include "node.h" |
| 7 | #include "grammar.h" |
| 8 | #include "parser.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | #include "parsetok.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | #include "errcode.h" |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 11 | #include "graminit.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | |
| 14 | /* Forward */ |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 15 | static node *parsetok(struct tok_state *, grammar *, int, perrdetail *, int *); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 16 | static int initerr(perrdetail *err_ret, const char* filename); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 17 | |
| 18 | /* Parse input coming from a string. Return error code, print some errors. */ |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 19 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 20 | PyParser_ParseString(const char *s, grammar *g, int start, perrdetail *err_ret) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 21 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | return PyParser_ParseStringFlagsFilename(s, NULL, g, start, err_ret, 0); |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 26 | PyParser_ParseStringFlags(const char *s, grammar *g, int start, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | perrdetail *err_ret, int flags) |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 28 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | return PyParser_ParseStringFlagsFilename(s, NULL, |
| 30 | g, start, err_ret, flags); |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 34 | PyParser_ParseStringFlagsFilename(const char *s, const char *filename, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | grammar *g, int start, |
| 36 | perrdetail *err_ret, int flags) |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 37 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | int iflags = flags; |
| 39 | return PyParser_ParseStringFlagsFilenameEx(s, filename, g, start, |
| 40 | err_ret, &iflags); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | node * |
| 44 | PyParser_ParseStringFlagsFilenameEx(const char *s, const char *filename, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | grammar *g, int start, |
| 46 | perrdetail *err_ret, int *flags) |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 47 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | struct tok_state *tok; |
| 49 | int exec_input = start == file_input; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 50 | |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 51 | if (initerr(err_ret, filename) < 0) |
| 52 | return NULL; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 53 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | if (*flags & PyPARSE_IGNORE_COOKIE) |
| 55 | tok = PyTokenizer_FromUTF8(s, exec_input); |
| 56 | else |
| 57 | tok = PyTokenizer_FromString(s, exec_input); |
| 58 | if (tok == NULL) { |
| 59 | err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM; |
| 60 | return NULL; |
| 61 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 62 | |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 63 | #ifndef PGEN |
| 64 | Py_INCREF(err_ret->filename); |
| 65 | tok->filename = err_ret->filename; |
| 66 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | return parsetok(tok, g, start, err_ret, flags); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 70 | /* Parse input coming from a file. Return error code, print some errors. */ |
| 71 | |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 72 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 73 | PyParser_ParseFile(FILE *fp, const char *filename, grammar *g, int start, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | char *ps1, char *ps2, perrdetail *err_ret) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | return PyParser_ParseFileFlags(fp, filename, NULL, |
| 77 | g, start, ps1, ps2, err_ret, 0); |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | node * |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 81 | PyParser_ParseFileFlags(FILE *fp, const char *filename, const char *enc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | grammar *g, int start, |
| 83 | char *ps1, char *ps2, perrdetail *err_ret, int flags) |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 84 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | int iflags = flags; |
| 86 | return PyParser_ParseFileFlagsEx(fp, filename, enc, g, start, ps1, |
| 87 | ps2, err_ret, &iflags); |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | node * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | PyParser_ParseFileFlagsEx(FILE *fp, const char *filename, |
| 92 | const char *enc, grammar *g, int start, |
| 93 | char *ps1, char *ps2, perrdetail *err_ret, int *flags) |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 94 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | struct tok_state *tok; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 96 | |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 97 | if (initerr(err_ret, filename) < 0) |
| 98 | return NULL; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | if ((tok = PyTokenizer_FromFile(fp, (char *)enc, ps1, ps2)) == NULL) { |
| 101 | err_ret->error = E_NOMEM; |
| 102 | return NULL; |
| 103 | } |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 104 | #ifndef PGEN |
| 105 | Py_INCREF(err_ret->filename); |
| 106 | tok->filename = err_ret->filename; |
| 107 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | return parsetok(tok, g, start, err_ret, flags); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 109 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | |
Neal Norwitz | e4993c7 | 2006-03-16 06:01:25 +0000 | [diff] [blame] | 111 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 112 | #if 0 |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 113 | static char with_msg[] = |
| 114 | "%s:%d: Warning: 'with' will become a reserved keyword in Python 2.6\n"; |
| 115 | |
| 116 | static char as_msg[] = |
| 117 | "%s:%d: Warning: 'as' will become a reserved keyword in Python 2.6\n"; |
| 118 | |
| 119 | static void |
| 120 | warn(const char *msg, const char *filename, int lineno) |
| 121 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | if (filename == NULL) |
| 123 | filename = "<string>"; |
| 124 | PySys_WriteStderr(msg, filename, lineno); |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 125 | } |
Neal Norwitz | fc85c92 | 2006-03-17 05:44:46 +0000 | [diff] [blame] | 126 | #endif |
Brett Cannon | e3944a5 | 2009-04-01 05:08:41 +0000 | [diff] [blame] | 127 | #endif |
Guido van Rossum | da62ecc | 2001-07-17 16:53:11 +0000 | [diff] [blame] | 128 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 129 | /* Parse input coming from the given tokenizer structure. |
| 130 | Return error code. */ |
| 131 | |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 132 | static node * |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 133 | parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | int *flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | parser_state *ps; |
| 137 | node *n; |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 138 | int started = 0; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | if ((ps = PyParser_New(g, start)) == NULL) { |
| 141 | fprintf(stderr, "no mem for new parser\n"); |
| 142 | err_ret->error = E_NOMEM; |
| 143 | PyTokenizer_Free(tok); |
| 144 | return NULL; |
| 145 | } |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 146 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | if (*flags & PyPARSE_BARRY_AS_BDFL) |
| 148 | ps->p_flags |= CO_FUTURE_BARRY_AS_BDFL; |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 149 | #endif |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | for (;;) { |
| 152 | char *a, *b; |
| 153 | int type; |
| 154 | size_t len; |
| 155 | char *str; |
| 156 | int col_offset; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | type = PyTokenizer_Get(tok, &a, &b); |
| 159 | if (type == ERRORTOKEN) { |
| 160 | err_ret->error = tok->done; |
| 161 | break; |
| 162 | } |
| 163 | if (type == ENDMARKER && started) { |
| 164 | type = NEWLINE; /* Add an extra newline */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | started = 0; |
| 166 | /* Add the right number of dedent tokens, |
| 167 | except if a certain flag is given -- |
| 168 | codeop.py uses this. */ |
| 169 | if (tok->indent && |
| 170 | !(*flags & PyPARSE_DONT_IMPLY_DEDENT)) |
| 171 | { |
| 172 | tok->pendin = -tok->indent; |
| 173 | tok->indent = 0; |
| 174 | } |
| 175 | } |
| 176 | else |
| 177 | started = 1; |
| 178 | len = b - a; /* XXX this may compute NULL - NULL */ |
| 179 | str = (char *) PyObject_MALLOC(len + 1); |
| 180 | if (str == NULL) { |
| 181 | fprintf(stderr, "no mem for next token\n"); |
| 182 | err_ret->error = E_NOMEM; |
| 183 | break; |
| 184 | } |
| 185 | if (len > 0) |
| 186 | strncpy(str, a, len); |
| 187 | str[len] = '\0'; |
Guido van Rossum | da62ecc | 2001-07-17 16:53:11 +0000 | [diff] [blame] | 188 | |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 189 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | if (type == NOTEQUAL) { |
| 191 | if (!(ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) && |
| 192 | strcmp(str, "!=")) { |
Antoine Pitrou | 9ec2593 | 2011-11-13 01:01:23 +0100 | [diff] [blame] | 193 | PyObject_FREE(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | err_ret->error = E_SYNTAX; |
| 195 | break; |
| 196 | } |
| 197 | else if ((ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) && |
| 198 | strcmp(str, "<>")) { |
Antoine Pitrou | 9ec2593 | 2011-11-13 01:01:23 +0100 | [diff] [blame] | 199 | PyObject_FREE(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | err_ret->text = "with Barry as BDFL, use '<>' " |
| 201 | "instead of '!='"; |
| 202 | err_ret->error = E_SYNTAX; |
| 203 | break; |
| 204 | } |
| 205 | } |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 206 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | if (a >= tok->line_start) |
| 208 | col_offset = a - tok->line_start; |
| 209 | else |
| 210 | col_offset = -1; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | if ((err_ret->error = |
| 213 | PyParser_AddToken(ps, (int)type, str, |
| 214 | tok->lineno, col_offset, |
| 215 | &(err_ret->expected))) != E_OK) { |
| 216 | if (err_ret->error != E_DONE) { |
| 217 | PyObject_FREE(str); |
| 218 | err_ret->token = type; |
| 219 | } |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (err_ret->error == E_DONE) { |
| 225 | n = ps->p_tree; |
| 226 | ps->p_tree = NULL; |
Meador Inge | fa21bf0 | 2012-01-19 01:08:41 -0600 | [diff] [blame] | 227 | |
Benjamin Peterson | 79c1f96 | 2012-01-19 08:48:11 -0500 | [diff] [blame] | 228 | #ifndef PGEN |
Meador Inge | fa21bf0 | 2012-01-19 01:08:41 -0600 | [diff] [blame] | 229 | /* Check that the source for a single input statement really |
| 230 | is a single statement by looking at what is left in the |
| 231 | buffer after parsing. Trailing whitespace and comments |
| 232 | are OK. */ |
| 233 | if (start == single_input) { |
| 234 | char *cur = tok->cur; |
| 235 | char c = *tok->cur; |
| 236 | |
Benjamin Peterson | cff9237 | 2012-01-19 17:46:13 -0500 | [diff] [blame] | 237 | for (;;) { |
| 238 | while (c == ' ' || c == '\t' || c == '\n' || c == '\014') |
| 239 | c = *++cur; |
Meador Inge | fa21bf0 | 2012-01-19 01:08:41 -0600 | [diff] [blame] | 240 | |
Benjamin Peterson | cff9237 | 2012-01-19 17:46:13 -0500 | [diff] [blame] | 241 | if (!c) |
| 242 | break; |
| 243 | |
| 244 | if (c != '#') { |
| 245 | err_ret->error = E_BADSINGLE; |
| 246 | PyNode_Free(n); |
| 247 | n = NULL; |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | /* Suck up comment. */ |
| 252 | while (c && c != '\n') |
| 253 | c = *++cur; |
Meador Inge | fa21bf0 | 2012-01-19 01:08:41 -0600 | [diff] [blame] | 254 | } |
| 255 | } |
Benjamin Peterson | 79c1f96 | 2012-01-19 08:48:11 -0500 | [diff] [blame] | 256 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | } |
| 258 | else |
| 259 | n = NULL; |
Christian Heimes | b1b3efc | 2008-03-26 23:24:27 +0000 | [diff] [blame] | 260 | |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 261 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | *flags = ps->p_flags; |
Christian Heimes | 4d6ec85 | 2008-03-26 22:34:47 +0000 | [diff] [blame] | 263 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | PyParser_Delete(ps); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | if (n == NULL) { |
Benjamin Peterson | 758888d | 2011-05-30 11:12:38 -0500 | [diff] [blame] | 267 | if (tok->done == E_EOF) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | err_ret->error = E_EOF; |
| 269 | err_ret->lineno = tok->lineno; |
| 270 | if (tok->buf != NULL) { |
| 271 | size_t len; |
| 272 | assert(tok->cur - tok->buf < INT_MAX); |
| 273 | err_ret->offset = (int)(tok->cur - tok->buf); |
| 274 | len = tok->inp - tok->buf; |
| 275 | err_ret->text = (char *) PyObject_MALLOC(len + 1); |
| 276 | if (err_ret->text != NULL) { |
| 277 | if (len > 0) |
| 278 | strncpy(err_ret->text, tok->buf, len); |
| 279 | err_ret->text[len] = '\0'; |
| 280 | } |
| 281 | } |
| 282 | } else if (tok->encoding != NULL) { |
| 283 | /* 'nodes->n_str' uses PyObject_*, while 'tok->encoding' was |
| 284 | * allocated using PyMem_ |
| 285 | */ |
| 286 | node* r = PyNode_New(encoding_decl); |
| 287 | if (r) |
| 288 | r->n_str = PyObject_MALLOC(strlen(tok->encoding)+1); |
| 289 | if (!r || !r->n_str) { |
| 290 | err_ret->error = E_NOMEM; |
| 291 | if (r) |
| 292 | PyObject_FREE(r); |
| 293 | n = NULL; |
| 294 | goto done; |
| 295 | } |
| 296 | strcpy(r->n_str, tok->encoding); |
| 297 | PyMem_FREE(tok->encoding); |
| 298 | tok->encoding = NULL; |
| 299 | r->n_nchildren = 1; |
| 300 | r->n_child = n; |
| 301 | n = r; |
| 302 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 303 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 304 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | PyTokenizer_Free(tok); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | return n; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | } |
Guido van Rossum | 0c156a5 | 2001-10-20 14:27:56 +0000 | [diff] [blame] | 309 | |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 310 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 311 | initerr(perrdetail *err_ret, const char *filename) |
Guido van Rossum | 0c156a5 | 2001-10-20 14:27:56 +0000 | [diff] [blame] | 312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | err_ret->error = E_OK; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | err_ret->lineno = 0; |
| 315 | err_ret->offset = 0; |
| 316 | err_ret->text = NULL; |
| 317 | err_ret->token = -1; |
| 318 | err_ret->expected = -1; |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 319 | #ifndef PGEN |
| 320 | if (filename) |
| 321 | err_ret->filename = PyUnicode_DecodeFSDefault(filename); |
| 322 | else |
| 323 | err_ret->filename = PyUnicode_FromString("<string>"); |
| 324 | if (err_ret->filename == NULL) { |
| 325 | err_ret->error = E_ERROR; |
| 326 | return -1; |
| 327 | } |
| 328 | #endif |
| 329 | return 0; |
Guido van Rossum | 0c156a5 | 2001-10-20 14:27:56 +0000 | [diff] [blame] | 330 | } |