2 /* This file is part of GCC.
4 GCC is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 3, or (at your option) any later
9 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
21 #include "coretypes.h"
30 #include "diagnostic-core.h"
31 #include "langhooks.h"
32 #include "langhooks-def.h"
42 #include "py-dot-codes.def"
46 #include "py-runtime.h"
50 /* Stack required for INDENT and DEDENT tokens @see
51 * - http://docs.python.org/reference/lexical_analysis.html
54 static VEC(gpy_int,gc) * gpy_indent_stack;
56 extern int yyparse( void );
58 static bool dedent = false;
59 static bool eof = false;
61 #define YY_USER_ACTION \
62 printf("dedent = <%i>!\n", dedent); \
65 printf("user_action - yytext = <%s>!\n", yytext); \
67 /* Copy yytext because unput() trashes yytext */ \
68 char *yycopy = xstrdup( yytext ); \
69 for ( i = yyleng - 1; i >= 0; --i ) \
84 ID [_a-zA-Z][a-zA_Z0-9_$]*
85 qstring \"[^\"\n]*[\"\n]
91 class { return CLASS; }
94 break { return BREAK; }
95 continue { return CONTINUE; }
96 return { return RETURN; }
98 while { return WHILE; }
99 print { return PRINT; }
102 elif { return ELIF; }
103 else { return ELSE; }
122 "==" { return EQUAL_EQUAL; }
124 "!=" { return NOT_EQUAL; }
126 "<=" { return LESS_EQUAL; }
127 ">" { return GREATER; }
128 ">=" { return GREATER_EQUAL; }
131 "and" { return AND; }
132 "not" { return NOT; }
134 "True" { return V_TRUE; }
135 "False" { return V_FALSE; }
138 yylval.string= xstrdup( (yytext+1) );
139 if( yylval.string[ yyleng-2 ] != '\"' ) {
140 error("Un-termintated character string!\n");
143 yylval.string[yyleng-2] = '\0';
151 if( mpfr_set_str( x, yytext, 10, GMP_RNDU) )
153 fatal_error("error initilizing integer value <%s>!\n", yytext );
155 yylval.integer = mpfr_get_si( x, GMP_RNDU );
161 printf("IDENTIFIER!\n");
162 yylval.string= xstrdup( yytext );
167 printf("newline rule!\n");
169 printf("chk = <%i>!\n", chk );
170 if( (chk != INDENT) && (eof == false ) )
172 int top_len = VEC_index( gpy_int, gpy_indent_stack,
173 VEC_length( gpy_int, gpy_indent_stack )-1 );
174 printf("whoop not indent top_len = <%i>!\n", top_len );
177 printf("pop and dedent!\n");
178 VEC_pop( gpy_int, gpy_indent_stack );
182 else if( chk == INDENT )
184 int top_len = VEC_index( gpy_int, gpy_indent_stack,
185 VEC_length( gpy_int, gpy_indent_stack )-1 );
186 int indent = yyleng/4;
188 if( top_len <= indent )
190 if( top_len != indent )
192 VEC_pop( gpy_int, gpy_indent_stack );
196 else if( eof == true )
203 printf("yytext = <%s>!\n", yytext );
206 /* Copy yytext because unput() trashes yytext */
207 char *yycopy = xstrdup( yytext );
208 for ( i = yyleng - 1; i >= 0; --i )
212 printf("newline!\n");
217 int top_len = VEC_index( gpy_int, gpy_indent_stack,
218 VEC_length( gpy_int, gpy_indent_stack )-1 );
219 int indent = yyleng/4;
221 if( top_len <= indent )
223 if( top_len != indent )
225 printf("pushing indent <%i>!\n", indent);
226 VEC_safe_push( gpy_int, gc, gpy_indent_stack, indent );
231 else if( top_len > indent )
234 VEC_pop( gpy_int, gpy_indent_stack );
240 printf("EOF rule!\n"); eof = true;
241 int top_len = VEC_index( gpy_int, gpy_indent_stack,
242 VEC_length( gpy_int, gpy_indent_stack )-1 );
245 printf("DEDENT EOF!\n");
246 VEC_pop( gpy_int, gpy_indent_stack );
260 int gpy_lex_parse( const char * gpy_in )
262 VEC_safe_push( gpy_int, gc, gpy_indent_stack, 0 );
265 FILE * fd = fopen( gpy_in, "rb" );
266 debug("trying to open <%s>!\n", gpy_in);
276 fprintf(stderr, "error opening <%s>!\n", gpy_in );