2 /* Parser-tokenizer link implementation */
4 #include "pgenheaders.h"
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. */
22 PyParser_ParseString(const char *s
, grammar
*g
, int start
, perrdetail
*err_ret
)
24 return PyParser_ParseStringFlagsFilename(s
, NULL
, g
, start
, err_ret
, 0);
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
);
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
;
49 tok
->filename
= filename
? filename
: "<string>";
50 if (Py_TabcheckFlag
|| Py_VerboseFlag
) {
51 tok
->altwarning
= (tok
->filename
!= NULL
);
52 if (Py_TabcheckFlag
>= 2)
56 return parsetok(tok
, g
, start
, err_ret
, flags
);
59 /* Parse input coming from a file. Return error code, print some errors. */
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
,
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
;
81 tok
->filename
= filename
;
82 if (Py_TabcheckFlag
|| Py_VerboseFlag
) {
83 tok
->altwarning
= (filename
!= NULL
);
84 if (Py_TabcheckFlag
>= 2)
89 return parsetok(tok
, g
, start
, err_ret
, flags
);
92 /* Parse input coming from the given tokenizer structure.
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";
102 warn(const char *msg
, const char *filename
, int lineno
)
104 if (filename
== NULL
)
105 filename
= "<string>";
106 PySys_WriteStderr(msg
, filename
, lineno
);
110 parsetok(struct tok_state
*tok
, grammar
*g
, int start
, perrdetail
*err_ret
,
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
);
123 #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
124 if (flags
& PyPARSE_WITH_IS_KEYWORD
)
125 ps
->p_flags
|= CO_FUTURE_WITH_STATEMENT
;
135 type
= PyTokenizer_Get(tok
, &a
, &b
);
136 if (type
== ERRORTOKEN
) {
137 err_ret
->error
= tok
->done
;
140 if (type
== ENDMARKER
&& started
) {
141 type
= NEWLINE
; /* Add an extra newline */
142 handling_with
= handling_import
= 0;
144 /* Add the right number of dedent tokens,
145 except if a certain flag is given --
146 codeop.py uses this. */
148 !(flags
& PyPARSE_DONT_IMPLY_DEDENT
))
150 tok
->pendin
= -tok
->indent
;
156 len
= b
- a
; /* XXX this may compute NULL - NULL */
157 str
= (char *) PyObject_MALLOC(len
+ 1);
159 fprintf(stderr
, "no mem for next token\n");
160 err_ret
->error
= E_NOMEM
;
164 strncpy(str
, a
, len
);
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. */
171 len
== 6 && str
[0] == 'i' && strcmp(str
, "import") == 0)
174 /* Warn about with as 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)
189 if (a
>= tok
->line_start
)
190 col_offset
= a
- tok
->line_start
;
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
)
203 if (err_ret
->error
== E_DONE
) {
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
) {
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
) {
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
;
233 tok
->encoding
= NULL
;
237 PyTokenizer_Free(tok
);
243 initerr(perrdetail
*err_ret
, const char *filename
)
245 err_ret
->error
= E_OK
;
246 err_ret
->filename
= filename
;
249 err_ret
->text
= NULL
;
251 err_ret
->expected
= -1;