Revert previous change, SKIP had a versionadded elsewhere
[python.git] / Parser / parsetok.c
blob77a2cac9fbf2ba0af21a6be9a2770cbc98b3b80b
2 /* Parser-tokenizer link implementation */
4 #include "pgenheaders.h"
5 #include "tokenizer.h"
6 #include "node.h"
7 #include "grammar.h"
8 #include "parser.h"
9 #include "parsetok.h"
10 #include "errcode.h"
11 #include "graminit.h"
13 int Py_TabcheckFlag;
16 /* Forward */
17 static node *parsetok(struct tok_state *, grammar *, int, perrdetail *, int);
18 static void initerr(perrdetail *err_ret, const char* filename);
20 /* Parse input coming from a string. Return error code, print some errors. */
21 node *
22 PyParser_ParseString(const char *s, grammar *g, int start, perrdetail *err_ret)
24 return PyParser_ParseStringFlagsFilename(s, NULL, g, start, err_ret, 0);
27 node *
28 PyParser_ParseStringFlags(const char *s, grammar *g, int start,
29 perrdetail *err_ret, int flags)
31 return PyParser_ParseStringFlagsFilename(s, NULL,
32 g, start, err_ret, flags);
35 node *
36 PyParser_ParseStringFlagsFilename(const char *s, const char *filename,
37 grammar *g, int start,
38 perrdetail *err_ret, int flags)
40 struct tok_state *tok;
42 initerr(err_ret, filename);
44 if ((tok = PyTokenizer_FromString(s)) == NULL) {
45 err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM;
46 return NULL;
49 tok->filename = filename ? filename : "<string>";
50 if (Py_TabcheckFlag || Py_VerboseFlag) {
51 tok->altwarning = (tok->filename != NULL);
52 if (Py_TabcheckFlag >= 2)
53 tok->alterror++;
56 return parsetok(tok, g, start, err_ret, flags);
59 /* Parse input coming from a file. Return error code, print some errors. */
61 node *
62 PyParser_ParseFile(FILE *fp, const char *filename, grammar *g, int start,
63 char *ps1, char *ps2, perrdetail *err_ret)
65 return PyParser_ParseFileFlags(fp, filename, g, start, ps1, ps2,
66 err_ret, 0);
69 node *
70 PyParser_ParseFileFlags(FILE *fp, const char *filename, grammar *g, int start,
71 char *ps1, char *ps2, perrdetail *err_ret, int flags)
73 struct tok_state *tok;
75 initerr(err_ret, filename);
77 if ((tok = PyTokenizer_FromFile(fp, ps1, ps2)) == NULL) {
78 err_ret->error = E_NOMEM;
79 return NULL;
81 tok->filename = filename;
82 if (Py_TabcheckFlag || Py_VerboseFlag) {
83 tok->altwarning = (filename != NULL);
84 if (Py_TabcheckFlag >= 2)
85 tok->alterror++;
89 return parsetok(tok, g, start, err_ret, flags);
92 /* Parse input coming from the given tokenizer structure.
93 Return error code. */
95 static char with_msg[] =
96 "%s:%d: Warning: 'with' will become a reserved keyword in Python 2.6\n";
98 static char as_msg[] =
99 "%s:%d: Warning: 'as' will become a reserved keyword in Python 2.6\n";
101 static void
102 warn(const char *msg, const char *filename, int lineno)
104 if (filename == NULL)
105 filename = "<string>";
106 PySys_WriteStderr(msg, filename, lineno);
109 static node *
110 parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
111 int flags)
113 parser_state *ps;
114 node *n;
115 int started = 0, handling_import = 0, handling_with = 0;
117 if ((ps = PyParser_New(g, start)) == NULL) {
118 fprintf(stderr, "no mem for new parser\n");
119 err_ret->error = E_NOMEM;
120 PyTokenizer_Free(tok);
121 return NULL;
123 #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
124 if (flags & PyPARSE_WITH_IS_KEYWORD)
125 ps->p_flags |= CO_FUTURE_WITH_STATEMENT;
126 #endif
128 for (;;) {
129 char *a, *b;
130 int type;
131 size_t len;
132 char *str;
133 int col_offset;
135 type = PyTokenizer_Get(tok, &a, &b);
136 if (type == ERRORTOKEN) {
137 err_ret->error = tok->done;
138 break;
140 if (type == ENDMARKER && started) {
141 type = NEWLINE; /* Add an extra newline */
142 handling_with = handling_import = 0;
143 started = 0;
144 /* Add the right number of dedent tokens,
145 except if a certain flag is given --
146 codeop.py uses this. */
147 if (tok->indent &&
148 !(flags & PyPARSE_DONT_IMPLY_DEDENT))
150 tok->pendin = -tok->indent;
151 tok->indent = 0;
154 else
155 started = 1;
156 len = b - a; /* XXX this may compute NULL - NULL */
157 str = (char *) PyObject_MALLOC(len + 1);
158 if (str == NULL) {
159 fprintf(stderr, "no mem for next token\n");
160 err_ret->error = E_NOMEM;
161 break;
163 if (len > 0)
164 strncpy(str, a, len);
165 str[len] = '\0';
167 #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
168 /* This is only necessary to support the "as" warning, but
169 we don't want to warn about "as" in import statements. */
170 if (type == NAME &&
171 len == 6 && str[0] == 'i' && strcmp(str, "import") == 0)
172 handling_import = 1;
174 /* Warn about with as NAME */
175 if (type == NAME &&
176 !(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
177 if (len == 4 && str[0] == 'w' && strcmp(str, "with") == 0)
178 warn(with_msg, err_ret->filename, tok->lineno);
179 else if (!(handling_import || handling_with) &&
180 len == 2 && str[0] == 'a' &&
181 strcmp(str, "as") == 0)
182 warn(as_msg, err_ret->filename, tok->lineno);
184 else if (type == NAME &&
185 (ps->p_flags & CO_FUTURE_WITH_STATEMENT) &&
186 len == 4 && str[0] == 'w' && strcmp(str, "with") == 0)
187 handling_with = 1;
188 #endif
189 if (a >= tok->line_start)
190 col_offset = a - tok->line_start;
191 else
192 col_offset = -1;
194 if ((err_ret->error =
195 PyParser_AddToken(ps, (int)type, str, tok->lineno, col_offset,
196 &(err_ret->expected))) != E_OK) {
197 if (err_ret->error != E_DONE)
198 PyObject_FREE(str);
199 break;
203 if (err_ret->error == E_DONE) {
204 n = ps->p_tree;
205 ps->p_tree = NULL;
207 else
208 n = NULL;
210 PyParser_Delete(ps);
212 if (n == NULL) {
213 if (tok->lineno <= 1 && tok->done == E_EOF)
214 err_ret->error = E_EOF;
215 err_ret->lineno = tok->lineno;
216 if (tok->buf != NULL) {
217 size_t len;
218 assert(tok->cur - tok->buf < INT_MAX);
219 err_ret->offset = (int)(tok->cur - tok->buf);
220 len = tok->inp - tok->buf;
221 err_ret->text = (char *) PyObject_MALLOC(len + 1);
222 if (err_ret->text != NULL) {
223 if (len > 0)
224 strncpy(err_ret->text, tok->buf, len);
225 err_ret->text[len] = '\0';
228 } else if (tok->encoding != NULL) {
229 node* r = PyNode_New(encoding_decl);
230 r->n_str = tok->encoding;
231 r->n_nchildren = 1;
232 r->n_child = n;
233 tok->encoding = NULL;
234 n = r;
237 PyTokenizer_Free(tok);
239 return n;
242 static void
243 initerr(perrdetail *err_ret, const char *filename)
245 err_ret->error = E_OK;
246 err_ret->filename = filename;
247 err_ret->lineno = 0;
248 err_ret->offset = 0;
249 err_ret->text = NULL;
250 err_ret->token = -1;
251 err_ret->expected = -1;