fixed many errors and cleaned up includes
[official-gcc.git] / gcc / python / py-lexer.l
blob9ede61282d9fa2753856ad1450616ff94be2e66e
1 %{
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
7 version.
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
12 for more details.
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/>.  */
18 #include "config.h"
19 #include "system.h"
20 #include "ansidecl.h"
21 #include "coretypes.h"
22 #include "opts.h"
23 #include "tree.h"
24 #include "gimple.h"
25 #include "toplev.h"
26 #include "debug.h"
27 #include "options.h"
28 #include "flags.h"
29 #include "convert.h"
30 #include "diagnostic-core.h"
31 #include "langhooks.h"
32 #include "langhooks-def.h"
33 #include "target.h"
35 #include <gmp.h>
36 #include <mpfr.h>
38 #include "vec.h"
39 #include "hashtab.h"
41 #include "gpython.h"
42 #include "py-dot-codes.def"
43 #include "py-dot.h"
44 #include "py-vec.h"
45 #include "py-tree.h"
46 #include "py-runtime.h"
48 #include "y.py.h"
50 /* Stack required for INDENT and DEDENT tokens @see
51  * - http://docs.python.org/reference/lexical_analysis.html
52  */
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);                           \
63   if( dedent )                                                  \
64     {                                                           \
65       printf("user_action - yytext = <%s>!\n", yytext);         \
66       int i;                                                    \
67       /* Copy yytext because unput() trashes yytext */          \
68       char *yycopy = xstrdup( yytext );                         \
69       for ( i = yyleng - 1; i >= 0; --i )                       \
70         unput( yycopy[i] );                                     \
71       free( yycopy );                                           \
72                                                                 \
73       dedent = false;                                           \
74       return DEDENT;                                            \
75     }
76   
80 %option yylineno
81 %option debug
83 DIGIT    [0-9]
84 ID       [_a-zA-Z][a-zA_Z0-9_$]*
85 qstring  \"[^\"\n]*[\"\n]
89 #.*                     ; /* comment */
91 class                   {  return CLASS; }
92 def                     {  return DEF; }
94 break                   {  return BREAK; }
95 continue                {  return CONTINUE; }
96 return                  {  return RETURN; }
97 for                     {  return FOR; }
98 while                   {  return WHILE; }
99 print                   {  return PRINT; }
101 if                      {  return IF; }
102 elif                    {  return ELIF; }
103 else                    {  return ELSE; }
105 \[                      {  return '['; }
106 \]                      {  return ']'; }
107 \(                      {  return '('; }
108 \)                      {  return ')'; }
110 ";"                     {  return ';'; }
111 ","                     {  return ','; }
112 "."                     {  return '.'; }
113 ":"                     {  return ':'; }
115 "="                     { return '='; }
116 "+"                     { return '+'; }
117 "-"                     { return '-'; }
118 "/"                     { return '/'; }
119 "*"                     { return '*'; }
120 "|"                     { return '|'; }
122 "=="                    { return EQUAL_EQUAL; }
124 "!="                    { return NOT_EQUAL; }
125 "<"                     { return LESS; }
126 "<="                    { return LESS_EQUAL; }
127 ">"                     { return GREATER; }
128 ">="                    { return GREATER_EQUAL; }
130 "or"                    { return OR; }
131 "and"                   { return AND; }
132 "not"                   { return NOT; }
134 "True"                  { return V_TRUE; }
135 "False"                 { return V_FALSE; }
137 {qstring}               {
138                           yylval.string= xstrdup( (yytext+1) );
139                           if( yylval.string[ yyleng-2 ] != '\"' ) {
140                             error("Un-termintated character string!\n");
141                           }
142                           else {
143                             yylval.string[yyleng-2] = '\0';
144                           }
145                           return STRING;
146                         }
148 {DIGIT}+                {
149                           mpfr_t x;
150                           mpfr_init2( x, 32 );
151                           if( mpfr_set_str( x, yytext, 10, GMP_RNDU) )
152                             {
153                               fatal_error("error initilizing integer value <%s>!\n", yytext );
154                             }
155                           yylval.integer = mpfr_get_si( x, GMP_RNDU );
156                           mpfr_clear( x );
157                           return INTEGER;
158                         }
160 {ID}                    {
161                           printf("IDENTIFIER!\n");
162                           yylval.string= xstrdup( yytext );
163                           return IDENTIFIER;
164                         }
166 "\n"                    {
167                           printf("newline rule!\n");
168                           int chk = yylex( );
169                           printf("chk = <%i>!\n", chk );
170                           if( (chk != INDENT) && (eof == false ) )
171                             {
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 );
175                               if( top_len != 0 )
176                                 {
177                                   printf("pop and dedent!\n");
178                                   VEC_pop( gpy_int, gpy_indent_stack );
179                                   dedent = true;
180                                 }
181                             }
182                           else if( chk == INDENT )
183                             {
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 )
189                                 {
190                                   if( top_len != indent )
191                                     {
192                                       VEC_pop( gpy_int, gpy_indent_stack );
193                                     }
194                                 }
195                             }
196                           else if( eof == true )
197                             {
198                               eof = false;
199                               if( chk == DEDENT )
200                                 dedent = true;
201                             }
203                           printf("yytext = <%s>!\n", yytext );
205                           int i;
206                           /* Copy yytext because unput() trashes yytext */
207                           char *yycopy = xstrdup( yytext );
208                           for ( i = yyleng - 1; i >= 0; --i )
209                             unput( yycopy[i] );
210                           free( yycopy );
212                           printf("newline!\n");
213                           return NEWLINE;
214                         }
216 "    "+                 {
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 )
222                             {
223                               if( top_len != indent )
224                                 {
225                                   printf("pushing indent <%i>!\n", indent);
226                                   VEC_safe_push( gpy_int, gc, gpy_indent_stack, indent );
227                                 }
228                               printf("INDENT!\n");
229                               return INDENT;
230                             }
231                           else if( top_len > indent )
232                             {
233                               printf("DEDENT!\n");
234                               VEC_pop( gpy_int, gpy_indent_stack );
235                               return DEDENT;
236                             }
237                          }
239 <<EOF>>                {
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 );
243                          if( top_len != 0 )
244                            {
245                              printf("DEDENT EOF!\n");
246                              VEC_pop( gpy_int, gpy_indent_stack );
247                              return DEDENT;
248                            }
249                          else
250                            return 0;
251                        }
255 int yywrap( void )
257   return 1;
260 int gpy_lex_parse( const char * gpy_in )
262   VEC_safe_push( gpy_int, gc, gpy_indent_stack, 0 );
264   int retval = 1;
265   FILE * fd = fopen( gpy_in, "rb" );
266   debug("trying to open <%s>!\n", gpy_in);
267   if( fd )
268     {
269       yyin = fd;
270       retval = yyparse( );
271       fclose( fd );
272       yylex_destroy( );
273     }
274   else
275     {
276       fprintf(stderr, "error opening <%s>!\n", gpy_in );
277       retval = 0;
278     }
279   return retval;