stl_bvector.h (swap(_Bit_reference,_Bit_reference)): Move/rename...
[official-gcc.git] / gcc / treelang / lex.l
blob97b06a6b2198f6690a674c9d0b2840ef2e56a138
1 %{ /* -*- c -*- = mode for emacs editor
2 /* 
4    TREELANG lexical analysis
6    ---------------------------------------------------------------------
8    Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
9    
10    This program is free software; you can redistribute it and/or modify it 
11    under the terms of the GNU General Public License as published by the
12    Free Software Foundation; either version 2, or (at your option) any
13    later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.
24    
25    In other words, you are welcome to use, share and improve this program.
26    You are forbidden to forbid anyone else to use, share and improve
27    what you give them.   Help stamp out software-hoarding!  
28    
29    ---------------------------------------------------------------------
30    
31    Written by Tim Josling 1999-2001, based in part on other parts of
32    the GCC compiler. 
36 /* Avoid poisoned malloc problem.  */
37 #undef IN_GCC
39 #if 0
40 /* tree is defined as void* here to avoid any knowledge of tree stuff in this file.  */
41 typedef void *tree;
42 #endif
43 #include <stdio.h>
44 #if 0
45 #include <ctype.h>
46 #endif
47 #include <memory.h>
48 #include "ansidecl.h"
49 #include "config.h"
50 #include "system.h"
51 #include "diagnostic.h"
53 /* Token defs.  */
54 #include "treelang.h"
55 #include "parse.h"
57 extern int option_lexer_trace;
59 int yylex (void);
60 void update_yylval (int a); 
62 static int next_tree_lineno=1;
63 static int next_tree_charno=1;
65 static void update_lineno_charno (void);
66 static void dump_lex_value (int lexret);
68 #define SAVE_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
69    {fprintf (stderr, "\nlexer returning"); dump_lex_value (a);} return a;}
70 #define NOT_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
71    {fprintf (stderr, "\nlexer swallowing"); dump_lex_value (a);}}
75 %option nostack
76 %option nounput
77 %option noyywrap
78 %option pointer
79 %option nodefault
83  { 
84    yylval = my_malloc (sizeof (struct token));
85    ((struct token*)yylval)->lineno = next_tree_lineno;
86    ((struct token*)yylval)->charno = next_tree_charno;
87  }
89 [ \n]+ {
90   update_lineno_charno ();
91     NOT_RETURN (WHITESPACE);
93   
94 "//".*  {
95   /* Comment.  */
96     update_lineno_charno ();
97     NOT_RETURN (COMMENT);
99    
100 "{" {
101   update_lineno_charno ();
102   SAVE_RETURN (LEFT_BRACE);
104   
105 "}" {
106   update_lineno_charno ();
107   SAVE_RETURN (RIGHT_BRACE);
109   
110 "(" {
111   update_lineno_charno ();
112   SAVE_RETURN (LEFT_PARENTHESIS);
114   
115 ")" {
116   update_lineno_charno ();
117   SAVE_RETURN (RIGHT_PARENTHESIS);
119   
120 "," {
121   update_lineno_charno ();
122   SAVE_RETURN (COMMA);
124   
125 ";" {
126   update_lineno_charno ();
127   SAVE_RETURN (SEMICOLON);
129   
130 "+" {
131   update_lineno_charno ();
132   SAVE_RETURN (PLUS);
134   
135 "-" {
136   update_lineno_charno ();
137   SAVE_RETURN (MINUS);
139   
140 "=" {
141   update_lineno_charno ();
142   SAVE_RETURN (ASSIGN);
144   
145 "==" {
146   update_lineno_charno ();
147   SAVE_RETURN (EQUALS);
149   
150 [+-]?[0-9]+ {
151   update_lineno_charno ();
152   SAVE_RETURN (INTEGER);
154   
155 "external_reference" {
156   update_lineno_charno ();
157   SAVE_RETURN (EXTERNAL_REFERENCE);
159   
160 "external_definition" {
161   update_lineno_charno ();
162   SAVE_RETURN (EXTERNAL_DEFINITION);
164   
165 "static" {
166   update_lineno_charno ();
167   SAVE_RETURN (STATIC);
169   
170 "automatic" {
171   update_lineno_charno ();
172   SAVE_RETURN (STATIC);
174   
175 "int" {
176   update_lineno_charno ();
177   SAVE_RETURN (INT);
179   
180 "char" {
181   update_lineno_charno ();
182   SAVE_RETURN (CHAR);
184   
185 "void" {
186   update_lineno_charno ();
187   SAVE_RETURN (VOID);
189   
190 "unsigned" {
191   update_lineno_charno ();
192   SAVE_RETURN (UNSIGNED);
194   
195 "return" {
196   update_lineno_charno ();
197   SAVE_RETURN (RETURN);
199   
200 "if" {
201   update_lineno_charno ();
202   SAVE_RETURN (IF);
204   
205 "else" {
206   update_lineno_charno ();
207   SAVE_RETURN (ELSE);
209   
210 [A-Za-z_]+[A-Za-z_0-9]* {
211   update_lineno_charno ();
212   update_yylval (NAME); 
213   if (option_lexer_trace) 
214     {
215       fprintf (stderr, "\nlexer returning"); 
216       dump_lex_value (NAME);
217     } 
218   return NAME;
220   
221 [^\n]  {
222   update_lineno_charno ();
223   fprintf (stderr, "%s:%i:%i: Unrecognized character %c\n", in_fname, 
224            ((struct token*)yylval)->lineno, 
225            ((struct token*)yylval)->charno, yytext[0]);
226   errorcount++;
231 /* 
232    Update line number (1-) and character number (1-).  Call this
233    before processing the token.  */
235 static void 
236 update_lineno_charno (void)
238    /* Update the values we send to caller in case we sometimes don't
239       tell them about all the 'tokens' eg comments etc.  */
240    int yyl;
241    ((struct token*)yylval)->lineno = next_tree_lineno;
242    ((struct token*)yylval)->charno = next_tree_charno;
243    for ( yyl = 0; yyl < yyleng; ++yyl ) 
244       {
245          if ( yytext[yyl] == '\n' ) 
246             {
247                ++next_tree_lineno;
248                next_tree_charno = 1;
249             } 
250          else 
251            next_tree_charno++;
252       }
255 /* Fill in the fields of yylval - the value of the token.  The token
256    type is A.  */
257 void 
258 update_yylval (int a)
260   struct token* tok;
261   tok=yylval;
262   
263   tok->category = token_category;
264   tok->type = a;
265   tok->length = yyleng;
266   /* Have to copy yytext as it is just a ptr into the buffer at the
267      moment.  */
268   tok->chars = my_malloc (yyleng + 1);
269   memcpy (tok->chars, yytext, yyleng);
272 /* Trace the value LEXRET and the position and token details being
273    returned by the lexical analyser.  */
275 static void
276 dump_lex_value (int lexret) 
278   int ix;
279   fprintf (stderr, " %d l:%d c:%d ln:%d text=", lexret,
280          ((struct token*) yylval)->lineno,
281          ((struct token*) yylval)->charno,
282          ((struct token*) yylval)->length);
283   for (ix = 0; ix < yyleng; ix++) 
284     {
285       fprintf (stderr, "%c", yytext[ix]);
286     }
287   fprintf (stderr, " in hex:");
288   for (ix = 0; ix < yyleng; ix++) 
289     {
290       fprintf (stderr, " %2.2x", yytext[ix]);
291     }
292   fprintf (stderr, "\n");
293 }