OSDN Git Service

2007-10-19 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / treelang / lex.l
1 /* -*- c -*- = mode for emacs editor
2
3    TREELANG lexical analysis
4
5    ---------------------------------------------------------------------
6
7    Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002, 2003,
8    2004, 2005, 2007 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 3, 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; see the file COPYING3.  If not see
22    <http://www.gnu.org/licenses/>.
23    
24    In other words, you are welcome to use, share and improve this program.
25    You are forbidden to forbid anyone else to use, share and improve
26    what you give them.   Help stamp out software-hoarding!  
27    
28    ---------------------------------------------------------------------
29    
30    Written by Tim Josling 1999-2001, based in part on other parts of
31    the GCC compiler.  */
32
33 %{
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "input.h"
39 #include "tree.h"
40
41 /* Token defs.  */
42 #include "treelang.h"
43 #include "parse.h"
44 #include "treetree.h"
45 #include "toplev.h"
46
47 extern int option_lexer_trace;
48
49 int yylex (void);
50 void update_yylval (int a); 
51
52 static int next_tree_charno = 1;
53 static int lineno = 1;
54  
55 static void update_lineno_charno (void);
56 static void dump_lex_value (int lexret);
57  
58 #define SAVE_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
59    {fprintf (stderr, "\nlexer returning"); dump_lex_value (a);} return a;}
60 #define NOT_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
61    {fprintf (stderr, "\nlexer swallowing"); dump_lex_value (a);}}
62 #ifndef USE_MAPPED_LOCATION
63 #undef LINEMAP_POSITION_FOR_COLUMN
64 #define LINEMAP_POSITION_FOR_COLUMN(INPUT, LINETABLE, COL)
65 #endif
66 %}
67
68 %option nostack
69 %option nounput
70 %option noyywrap
71 %option pointer
72 %option nodefault
73
74 %%
75
76  { 
77    /* ??? Should really allocate only what we need.  */
78    yylval = my_malloc (sizeof (struct prod_token_parm_item));
79    LINEMAP_POSITION_FOR_COLUMN (input_location, line_table,
80                                 next_tree_charno);
81    ((struct prod_token_parm_item *)yylval)->tp.tok.location = input_location;
82    ((struct prod_token_parm_item *)yylval)->tp.tok.charno = next_tree_charno;
83  }
84
85 [ \n\t]+ {
86   update_lineno_charno ();
87   NOT_RETURN (WHITESPACE);
88 }
89   
90 "//".*  {
91   /* Comment.  */
92   update_lineno_charno ();
93   NOT_RETURN (COMMENT);
94 }
95    
96 "{" {
97   update_lineno_charno ();
98   SAVE_RETURN (LEFT_BRACE);
99 }
100   
101 "}" {
102   update_lineno_charno ();
103   SAVE_RETURN (RIGHT_BRACE);
104 }
105   
106 "(" {
107   update_lineno_charno ();
108   SAVE_RETURN (LEFT_PARENTHESIS);
109 }
110   
111 ")" {
112   update_lineno_charno ();
113   SAVE_RETURN (RIGHT_PARENTHESIS);
114 }
115   
116 "," {
117   update_lineno_charno ();
118   SAVE_RETURN (COMMA);
119 }
120   
121 ";" {
122   update_lineno_charno ();
123   SAVE_RETURN (SEMICOLON);
124 }
125   
126 "+" {
127   update_lineno_charno ();
128   SAVE_RETURN (tl_PLUS);
129 }
130   
131 "-" {
132   update_lineno_charno ();
133   SAVE_RETURN (tl_MINUS);
134 }
135   
136 "=" {
137   update_lineno_charno ();
138   SAVE_RETURN (ASSIGN);
139 }
140   
141 "==" {
142   update_lineno_charno ();
143   SAVE_RETURN (EQUALS);
144 }
145   
146 [+-]?[0-9]+ {
147   update_lineno_charno ();
148   SAVE_RETURN (INTEGER);
149 }
150   
151 "external_reference" {
152   update_lineno_charno ();
153   SAVE_RETURN (EXTERNAL_REFERENCE);
154 }
155   
156 "external_definition" {
157   update_lineno_charno ();
158   SAVE_RETURN (EXTERNAL_DEFINITION);
159 }
160   
161 "static" {
162   update_lineno_charno ();
163   SAVE_RETURN (STATIC);
164 }
165   
166 "automatic" {
167   update_lineno_charno ();
168   SAVE_RETURN (AUTOMATIC);
169 }
170   
171 "int" {
172   update_lineno_charno ();
173   SAVE_RETURN (INT);
174 }
175   
176 "char" {
177   update_lineno_charno ();
178   SAVE_RETURN (CHAR);
179 }
180   
181 "void" {
182   update_lineno_charno ();
183   SAVE_RETURN (VOID);
184 }
185   
186 "unsigned" {
187   update_lineno_charno ();
188   SAVE_RETURN (UNSIGNED);
189 }
190   
191 "return" {
192   update_lineno_charno ();
193   SAVE_RETURN (tl_RETURN);
194 }
195   
196 "if" {
197   update_lineno_charno ();
198   SAVE_RETURN (IF);
199 }
200   
201 "else" {
202   update_lineno_charno ();
203   SAVE_RETURN (ELSE);
204 }
205   
206 [A-Za-z_]+[A-Za-z_0-9]* {
207   update_lineno_charno ();
208   update_yylval (NAME); 
209   if (option_lexer_trace) 
210     {
211       fprintf (stderr, "\nlexer returning"); 
212       dump_lex_value (NAME);
213     } 
214   return NAME;
215 }
216   
217 [^\n]  {
218   update_lineno_charno ();
219   error ("%HUnrecognized character %qc.", 
220          &((struct prod_token_parm_item *)yylval)->tp.tok.location,
221          yytext[0]);
222 }
223
224 %%
225
226 /* 
227    Update line number (1-) and character number (1-).  Call this
228    before processing the token.  */
229
230 static void 
231 update_lineno_charno (void)
232 {
233    /* Update the values we send to caller in case we sometimes don't
234       tell them about all the 'tokens' eg comments etc.  */
235    int yyl;
236    LINEMAP_POSITION_FOR_COLUMN (input_location, line_table,
237                                 next_tree_charno);
238    ((struct prod_token_parm_item *)yylval)->tp.tok.location = input_location;
239    ((struct prod_token_parm_item *)yylval)->tp.tok.charno = next_tree_charno;
240
241    for ( yyl = 0; yyl < yyleng; ++yyl ) 
242       {
243          if ( yytext[yyl] == '\n' ) 
244             {
245 #ifdef USE_MAPPED_LOCATION
246               source_location s = linemap_line_start (line_table, ++lineno,
247                                                       80);
248               input_location = s;
249 #else
250               input_line = ++lineno;
251 #endif
252               next_tree_charno = 1;
253             } 
254          else 
255            next_tree_charno++;
256       }
257 }
258
259 /* Fill in the fields of yylval - the value of the token.  The token
260    type is A.  */
261 void 
262 update_yylval (int a)
263 {
264   struct prod_token_parm_item * tok;
265   tok = yylval;
266   
267   tok->category = token_category;
268   tok->type = a;
269   tok->tp.tok.length = yyleng;
270   /* Have to copy yytext as it is just a ptr into the buffer at the
271      moment.  */
272   tok->tp.tok.chars = (const unsigned char *) get_string (yytext, yyleng);
273 }
274
275 /* Trace the value LEXRET and the position and token details being
276    returned by the lexical analyser.  */
277
278 static void
279 dump_lex_value (int lexret) 
280 {
281   int ix;
282
283   fprintf (stderr, " %d l:%d c:%d ln:%d text=", lexret,
284            LOCATION_LINE (((struct prod_token_parm_item *)
285                           yylval)->tp.tok.location),
286            ((struct prod_token_parm_item *) yylval)->tp.tok.charno,
287            ((struct prod_token_parm_item *) yylval)->tp.tok.length);
288
289   for (ix = 0; ix < yyleng; ix++) 
290     {
291       fprintf (stderr, "%c", yytext[ix]);
292     }
293   fprintf (stderr, " in hex:");
294   for (ix = 0; ix < yyleng; ix++) 
295     {
296       fprintf (stderr, " %2.2x", yytext[ix]);
297     }
298   fprintf (stderr, "\n");
299 }  
300