1 %{ /* -*- c -*- = mode for emacs editor
4 TREELANG lexical analysis
6 ---------------------------------------------------------------------
8 Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
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
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.
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.
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!
29 ---------------------------------------------------------------------
31 Written by Tim Josling 1999-2001, based in part on other parts of
36 /* Avoid poisoned malloc problem. */
40 /* tree is defined as void* here to avoid any knowledge of tree stuff in this file. */
51 #include "diagnostic.h"
57 extern int option_lexer_trace;
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);}}
84 yylval = my_malloc (sizeof (struct token));
85 ((struct token*)yylval)->lineno = next_tree_lineno;
86 ((struct token*)yylval)->charno = next_tree_charno;
90 update_lineno_charno ();
91 NOT_RETURN (WHITESPACE);
96 update_lineno_charno ();
101 update_lineno_charno ();
102 SAVE_RETURN (LEFT_BRACE);
106 update_lineno_charno ();
107 SAVE_RETURN (RIGHT_BRACE);
111 update_lineno_charno ();
112 SAVE_RETURN (LEFT_PARENTHESIS);
116 update_lineno_charno ();
117 SAVE_RETURN (RIGHT_PARENTHESIS);
121 update_lineno_charno ();
126 update_lineno_charno ();
127 SAVE_RETURN (SEMICOLON);
131 update_lineno_charno ();
136 update_lineno_charno ();
141 update_lineno_charno ();
142 SAVE_RETURN (ASSIGN);
146 update_lineno_charno ();
147 SAVE_RETURN (EQUALS);
151 update_lineno_charno ();
152 SAVE_RETURN (INTEGER);
155 "external_reference" {
156 update_lineno_charno ();
157 SAVE_RETURN (EXTERNAL_REFERENCE);
160 "external_definition" {
161 update_lineno_charno ();
162 SAVE_RETURN (EXTERNAL_DEFINITION);
166 update_lineno_charno ();
167 SAVE_RETURN (STATIC);
171 update_lineno_charno ();
172 SAVE_RETURN (STATIC);
176 update_lineno_charno ();
181 update_lineno_charno ();
186 update_lineno_charno ();
191 update_lineno_charno ();
192 SAVE_RETURN (UNSIGNED);
196 update_lineno_charno ();
197 SAVE_RETURN (RETURN);
201 update_lineno_charno ();
206 update_lineno_charno ();
210 [A-Za-z_]+[A-Za-z_0-9]* {
211 update_lineno_charno ();
212 update_yylval (NAME);
213 if (option_lexer_trace)
215 fprintf (stderr, "\nlexer returning");
216 dump_lex_value (NAME);
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]);
232 Update line number (1-) and character number (1-). Call this
233 before processing the token. */
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. */
241 ((struct token*)yylval)->lineno = next_tree_lineno;
242 ((struct token*)yylval)->charno = next_tree_charno;
243 for ( yyl = 0; yyl < yyleng; ++yyl )
245 if ( yytext[yyl] == '\n' )
248 next_tree_charno = 1;
255 /* Fill in the fields of yylval - the value of the token. The token
258 update_yylval (int a)
263 tok->category = token_category;
265 tok->length = yyleng;
266 /* Have to copy yytext as it is just a ptr into the buffer at the
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. */
276 dump_lex_value (int lexret)
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++)
285 fprintf (stderr, "%c", yytext[ix]);
287 fprintf (stderr, " in hex:");
288 for (ix = 0; ix < yyleng; ix++)
290 fprintf (stderr, " %2.2x", yytext[ix]);
292 fprintf (stderr, "\n");