source: trunk/third/ORBit/libIDL/lexer.l @ 15271

Revision 15271, 9.2 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15270, which included commits to RCS files with non-trunk default branches.
Line 
1/***************************************************************************
2
3    lexer.l (IDL lex scanner)
4
5    Copyright (C) 1998, 1999 Andrew T. Veliath
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with this library; if not, write to the Free
19    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21    $Id: lexer.l,v 1.1.1.1 2000-11-10 00:49:16 ghudson Exp $
22
23***************************************************************************/
24%{
25#include <assert.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <ctype.h>
29#include <string.h>
30#include "rename.h"
31#include "util.h"
32#include "parser.h"
33
34#ifdef XP_MAC
35#  include <unix.h>
36#  define YY_NEVER_INTERACTIVE 1
37#endif
38
39/* Eliminate warning */
40#define YY_NO_UNPUT 1
41
42#define YY_INPUT(buf,result,the_max_size)       do {                            \
43        if (__IDL_inputcb == NULL) {                                            \
44                if ((result = fread (buf, 1, the_max_size, yyin)) == YY_NULL && \
45                    ferror (yyin))                                              \
46                        YY_FATAL_ERROR ("input in scanner failed");             \
47        } else {                                                                \
48                union IDL_input_data data;                                      \
49                                                                                \
50                data.fill.buffer = buf;                                         \
51                data.fill.max_size = the_max_size;                              \
52                result = (*__IDL_inputcb) (IDL_INPUT_REASON_FILL, &data,        \
53                                           __IDL_inputcb_user_data);            \
54                if (result < 0)                                                 \
55                        YY_FATAL_ERROR ("input callback returned failure");     \
56        }                                                                       \
57} while (0)
58
59#define tokreturn(token)                        do {    \
60        __IDL_prev_token_line = __IDL_cur_token_line;   \
61        __IDL_cur_token_line = __IDL_cur_line;          \
62        return token;                                   \
63} while (0)
64
65#define SELECT_START                            \
66        /* Parser driven start conditions */    \
67        if (__IDL_flagsi & IDLFP_PROPERTIES)    \
68                BEGIN (PROP);                   \
69        else if (__IDL_flagsi & IDLFP_NATIVE)   \
70                BEGIN (NATIVE);                 \
71        /* Global syntax start conditions */    \
72        else if (__IDL_flags & IDLF_XPIDL)      \
73                BEGIN (XP);                     \
74        else if (__IDL_flags & IDLF_CODEFRAGS)  \
75                BEGIN (CFRG);
76
77#define SELECT_RESTART                          \
78        SELECT_START                            \
79        else                                    \
80                BEGIN (INITIAL);
81
82extern void             __IDL_do_pragma                 (const char *s);
83static int              count_nl                        (const char *s);
84
85#ifdef YYDEBUG
86extern int                              yydebug;
87#endif
88int                                     __IDL_prev_token_line;
89int                                     __IDL_cur_token_line;
90static int                              warn_underscores;
91static char *                           codefrag_desc;
92static GSList *                         codefrag_list;
93static GSList *                         codefrag_list_tail;
94%}
95
96whitespace              [ \t\v\f\r]*
97whitespacenl            [ \t\v\f\r\n]*
98newline                 \n
99cpp_pragma              ^{whitespace}#{whitespace}pragma{whitespace}.*
100cpp_status              ^{whitespace}#.*
101b8_int                  0[0-9]*
102b10_uint                [1-9][0-9]*
103b16_int                 0[xX][0-9A-Fa-f]+
104float_lit               [0-9]*\.[0-9]+([eE]-?[0-9]+)?|[0-9]+\.?([eE]-?[0-9]+)?
105fixed_lit               ([0-9]*\.[0-9]+|-?[0-9]+\.?[0-9]*)[dD]
106declspec                __declspec{whitespacenl}\({whitespacenl}[A-Za-z]*{whitespacenl}\)
107happy_ident             [A-Za-z][A-Za-z0-9]*
108err1_ident              _[A-Za-z0-9_]+
109warn1_ident             [A-Za-z][A-Za-z0-9_]*
110prop_key                [A-Za-z][A-Za-z0-9_]*
111prop_value              \([^\)]+\)
112native_type             [^\)]+\)
113sqstring                \'[^\'\n]*[\'\n]
114dqstring                \"[^\"\n]*[\"\n]
115
116%p 5000
117
118%s XP
119
120%x PROP
121%x NATIVE
122
123%s CFRG
124%x CFRGX
125
126%%
127
128        SELECT_START;
129
130<INITIAL,XP,CFRG>^%\{.*                                 {
131        char *s = yytext + 2;
132
133        while (isspace (*s)) ++s;
134        codefrag_desc = g_strdup (s);
135        codefrag_list = codefrag_list_tail = NULL;
136
137        if (!(__IDL_flags & IDLF_XPIDL || __IDL_flags & IDLF_CODEFRAGS))
138                yyerror ("Code fragment syntax not enabled");
139        else
140                BEGIN (CFRGX);
141}
142<CFRGX>^%\}.*                                           {
143        yylval.tree = IDL_codefrag_new (codefrag_desc, codefrag_list);
144        tokreturn (TOK_CODEFRAG);
145}
146<CFRGX>.*                                               {
147        char *s;
148        GSList *slist;
149
150        s = g_strdup (yytext);
151        slist = g_slist_alloc ();
152        slist->data = s;
153
154        if (codefrag_list == NULL) {
155                codefrag_list = slist;
156                codefrag_list_tail = slist;
157        } else {
158                codefrag_list_tail->next = slist;
159                codefrag_list_tail = slist;
160        }
161}
162<*>{cpp_pragma}                                         {
163        int n;
164        char *p = yytext;
165        char *s, *t;
166
167        while (isspace (*p) || *p == '#') ++p;
168        s = p;
169        sscanf (p, "%*6s%n", &n); s += n;
170        while (isspace (*s)) ++s;
171
172        t = s + strlen(s) - 1;
173        while(isspace(*t) && t > s) *(t--) = '\0'; /* Chomp trailing spaces */
174
175        __IDL_do_pragma (s);
176}
177<*>{cpp_status}                                         {
178        char *starttext;
179        char *filename;
180        char *filename2;
181        int line;
182
183        starttext = yytext;
184        while (isspace (*starttext)) ++starttext;
185        filename = g_malloc (strlen (starttext) + 1);
186
187        sscanf (starttext, "# %d %s", &line, filename);
188
189        if (*filename == '"') {
190                filename2 = g_strdup (filename + 1);
191                filename2[strlen (filename) - 2] = 0;
192                g_free (filename);
193        } else
194                filename2 = filename;
195
196        IDL_file_set (filename2, line);
197
198        g_free (filename2);
199}
200<*>{whitespace}                                         ;
201{b8_int}                                                {
202        sscanf (yytext, "%" IDL_LL "o", &yylval.integer);
203        tokreturn (TOK_INTEGER);
204}
205{b10_uint}                                              {
206        sscanf (yytext, "%" IDL_LL "u", &yylval.integer);
207        tokreturn (TOK_INTEGER);
208}
209{b16_int}                                               {
210        sscanf (yytext + 2, "%" IDL_LL "x", &yylval.integer);
211        tokreturn (TOK_INTEGER);
212}
213{fixed_lit}                                             {
214        yylval.str = g_strdup (yytext);
215        tokreturn (TOK_FIXEDP);
216}
217{float_lit}                                             {
218        yylval.floatp = atof (yytext);
219        tokreturn (TOK_FLOATP);
220}
221FALSE                   tokreturn (TOK_FALSE);
222Object                  tokreturn (TOK_OBJECT);
223TRUE                    tokreturn (TOK_TRUE);
224any                     tokreturn (TOK_ANY);
225attribute               tokreturn (TOK_ATTRIBUTE);
226boolean                 tokreturn (TOK_BOOLEAN);
227case                    tokreturn (TOK_CASE);
228char                    tokreturn (TOK_CHAR);
229const                   tokreturn (TOK_CONST);
230context                 tokreturn (TOK_CONTEXT);
231default                 tokreturn (TOK_DEFAULT);
232double                  tokreturn (TOK_DOUBLE);
233enum                    tokreturn (TOK_ENUM);
234exception               tokreturn (TOK_EXCEPTION);
235fixed                   tokreturn (TOK_FIXED);
236float                   tokreturn (TOK_FLOAT);
237in                      tokreturn (TOK_IN);
238inout                   tokreturn (TOK_INOUT);
239interface               tokreturn (TOK_INTERFACE);
240long                    tokreturn (TOK_LONG);
241module                  tokreturn (TOK_MODULE);
242native                  tokreturn (TOK_NATIVE);
243octet                   tokreturn (TOK_OCTET);
244oneway                  tokreturn (TOK_ONEWAY);
245out                     tokreturn (TOK_OUT);
246raises                  tokreturn (TOK_RAISES);
247readonly                tokreturn (TOK_READONLY);
248sequence                tokreturn (TOK_SEQUENCE);
249short                   tokreturn (TOK_SHORT);
250string                  tokreturn (TOK_STRING);
251struct                  tokreturn (TOK_STRUCT);
252switch                  tokreturn (TOK_SWITCH);
253typedef                 tokreturn (TOK_TYPEDEF);
254union                   tokreturn (TOK_UNION);
255unsigned                tokreturn (TOK_UNSIGNED);
256<XP>\.\.\.              tokreturn (TOK_VARARGS);
257void                    tokreturn (TOK_VOID);
258wchar                   tokreturn (TOK_WCHAR);
259wstring                 tokreturn (TOK_WSTRING);
260::                      tokreturn (TOK_OP_SCOPE);
261\>\>                    tokreturn (TOK_OP_SHR);
262\<\<                    tokreturn (TOK_OP_SHL);
263{declspec}                                              {
264        char *s = g_strdup (yytext);
265
266        /* Get the parenthesized expression (ignoring whitespace) */
267        sscanf (yytext, "__declspec %*[(] %[A-Za-z_] %*[)]", s);
268        yylval.str = s;
269        __IDL_cur_line += count_nl (yytext);
270        tokreturn (TOK_DECLSPEC);
271}
272{happy_ident}                                           {
273        if (__IDL_flags & IDLF_TYPECODES && strcmp (yytext, "TypeCode") == 0)
274                tokreturn (TOK_TYPECODE);
275        yylval.str = g_strdup (yytext);
276        tokreturn (TOK_IDENT);
277}
278{err1_ident}                                            {
279        yyerrorv ("`%s' is not a valid identifier in IDL", yytext);
280        yyerror ("(Identifiers cannot start with an underscore)");
281        yylval.str = g_strdup (yytext);
282        tokreturn (TOK_IDENT);
283}
284{warn1_ident}                                           {
285        if (!warn_underscores) {
286                yywarningv (IDL_WARNING2,
287                           "`%s' underscores within identifiers are discouraged for use "
288                           "with C-language IDL mappings", yytext);
289                warn_underscores = 1;
290        }
291        yylval.str = g_strdup (yytext);
292        tokreturn (TOK_IDENT);
293}
294<PROP>]                                                 {
295        __IDL_flagsi &= ~IDLFP_PROPERTIES;
296        SELECT_RESTART;
297        tokreturn (yytext[0]);
298}
299<PROP>{prop_key}                                        {
300        yylval.str = g_strdup (yytext);
301        tokreturn (TOK_PROP_KEY);
302}
303<PROP>{prop_value}                                      {
304        yylval.str = g_strdup (yytext + 1);
305        yylval.str[strlen (yylval.str) - 1] = 0;
306        tokreturn (TOK_PROP_VALUE);
307}
308<NATIVE>{native_type}                                   {
309        __IDL_flagsi &= ~IDLFP_NATIVE;
310        yylval.str = g_strdup (yytext);
311        yylval.str[strlen (yylval.str) - 1] = 0;
312        tokreturn (TOK_NATIVE_TYPE);
313}
314{sqstring}                                              {
315        yylval.str = g_strdup (yytext + 1);
316        yylval.str[strlen (yytext) - 2] = 0;
317        tokreturn (TOK_SQSTRING);
318}
319{dqstring}                                              {
320        yylval.str = g_strdup (yytext + 1);
321        yylval.str[strlen (yytext) - 2] = 0;
322        tokreturn (TOK_DQSTRING);
323}
324<*>{newline}            ++__IDL_cur_line;
325<*>\/\/.*               ;
326<*>\/\*                                                 {
327        int c;
328
329        while (1) {
330                while ((c = input ()) != '*' && c != EOF)
331                        if (c == '\n') ++__IDL_cur_line;
332                if (c == '*') {
333                        while ((c = input ()) == '*') ;
334                        if (c == '/') break;
335                }
336                if (c == '\n') ++__IDL_cur_line;
337                if (c == EOF) {
338                        yywarning (IDL_WARNING1, "End of file in comment");
339                        break;
340                }
341        }
342}
343<*>.                    tokreturn (yytext[0]);
344
345%%
346
347void __IDL_lex_init (void)
348{
349        __IDL_inputcb = NULL;
350        __IDL_cur_line = 1;
351        __IDL_cur_token_line = 0;
352        __IDL_prev_token_line = 0;
353        __IDL_cur_filename = NULL;
354        __IDL_cur_fileinfo = NULL;
355        warn_underscores = 0;
356}
357
358void __IDL_lex_cleanup (void)
359{
360        __IDL_cur_filename = NULL;
361        YY_NEW_FILE;
362}
363
364int yywrap (void)
365{
366        return 1;
367}
368
369static int count_nl (const char *s)
370{
371        int i;
372
373        for (i = 0;
374             (s = strchr (s, '\n')) != NULL;
375             ++s, ++i) ;
376
377        return i;
378}
379
380/*
381 * Local variables:
382 * mode: C
383 * c-basic-offset: 8
384 * tab-width: 8
385 * indent-tabs-mode: t
386 * End:
387 */
Note: See TracBrowser for help on using the repository browser.