]> git.draconx.ca Git - gob-dx.git/blob - src/lexer.l
5f9f7423a211958a0cf35e100e055e0d7dda1d7a
[gob-dx.git] / src / lexer.l
1 /* GOB C Preprocessor
2  * Copyright (C) 1999 the Free Software Foundation.
3  *
4  * Author: George Lebl
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the  Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19  * USA.
20  */
21 %{
22
23 #include "config.h"
24 #include <glib.h>
25
26 #include "parse.h"
27
28 static int parenth_depth = 0;
29 static int before_comment = INITIAL;
30 static int class_after_c = FALSE;
31 static int header_c = FALSE;
32
33 static GString *cbuf = NULL;
34 int ccode_line = 1;
35
36 int line_no = 1;
37
38 static void
39 clear_cbuf(void)
40 {
41         if(!cbuf) {
42                 cbuf = g_string_new("");
43         } else {
44                 cbuf = g_string_assign(cbuf,"");
45         }
46 }
47
48 static void
49 add_to_cbuf(char *s)
50 {
51         if(!cbuf) {
52                 cbuf = g_string_new(s);
53         } else {
54                 cbuf = g_string_append(cbuf,s);
55         }
56 }
57
58 %}
59
60 %x COMMENT
61 %x C_CODE
62 %x C_CODE_STRING
63 %x CLASS_CODE
64 %x CLASS_CODE_I
65
66 %%
67
68 <*>\n                   { line_no++; REJECT; }
69
70 \/\/.*$                 { ; /*comment, ignore*/ }
71 <C_CODE>\/\/.*$         { ; /*comment, ignore*/ }
72 <CLASS_CODE>\/\/.*$     { ; /*comment, ignore*/ }
73 <CLASS_CODE_I>\/\/.*$   { ; /*comment, ignore*/ }
74 \/\*            {BEGIN(COMMENT); before_comment = INITIAL; }
75 <C_CODE>\/\*    {BEGIN(COMMENT); before_comment = C_CODE; }
76 <CLASS_CODE>\/\*        {BEGIN(COMMENT); before_comment = CLASS_CODE; }
77 <CLASS_CODE_I>\/\*      {BEGIN(COMMENT); before_comment = CLASS_CODE_I; }
78 <COMMENT>\*\/   {BEGIN(before_comment);}
79 <COMMENT>.      { ; /* comment, ignore */ }
80 <COMMENT>\n     { ; /* comment, ignore */ }
81
82 ^\%h\{          {
83                         BEGIN(C_CODE);
84                         parenth_depth = 1;
85                         class_after_c = FALSE;
86                         header_c = TRUE;
87                         clear_cbuf();
88                         ccode_line = line_no;
89                 }
90 ^\%\{           {
91                         BEGIN(C_CODE);
92                         parenth_depth = 1;
93                         class_after_c = FALSE;
94                         header_c = FALSE;
95                         clear_cbuf();
96                         ccode_line = line_no;
97                 }
98 <C_CODE>^\%\}   {
99                         BEGIN(INITIAL);
100                         yylval.cbuf = cbuf;
101                         cbuf = NULL;
102                         if(header_c)
103                                 return HCODE;
104                         else
105                                 return CCODE;
106                 }
107
108 <C_CODE>\'\{\'          { add_to_cbuf(yytext); }
109 <C_CODE>\'\\\{\'        { add_to_cbuf(yytext); }
110 <C_CODE>\'\}\'          { add_to_cbuf(yytext); }
111 <C_CODE>\'\\\}\'        { add_to_cbuf(yytext); }
112         
113 <C_CODE>\\.     { add_to_cbuf(yytext); }
114 <C_CODE>\"      {
115                         BEGIN(C_CODE_STRING);
116                         add_to_cbuf(yytext);
117                 }
118 <C_CODE_STRING>\\.      {
119                                 add_to_cbuf(yytext);
120                         }
121 <C_CODE_STRING>\"       {
122                                 BEGIN(C_CODE);
123                                 add_to_cbuf(yytext);
124                         }
125 <C_CODE_STRING>.        {
126                                 add_to_cbuf(yytext);
127                         }
128
129 <C_CODE>\{      {
130                         parenth_depth++;
131                         add_to_cbuf(yytext);
132                 }
133 <C_CODE>\}      {
134                         parenth_depth--;
135                         if(parenth_depth<0) {
136                                 REJECT;
137                         } else if(parenth_depth==0 && class_after_c) {
138                                 BEGIN(CLASS_CODE_I);
139                                 yylval.cbuf = cbuf;
140                                 cbuf = NULL;
141                                 return CCODE;
142                         }
143                         add_to_cbuf(yytext);
144                 }
145
146 <C_CODE>.       { add_to_cbuf(yytext); }
147 <C_CODE>\n      { add_to_cbuf(yytext); }
148
149 class           {
150                         BEGIN(CLASS_CODE);
151                         return CLASS;
152                 }
153
154 <CLASS_CODE>from        {return FROM;}
155
156 <CLASS_CODE_I>void      {return VOID;}
157 <CLASS_CODE_I>struct    {return STRUCT;}
158 <CLASS_CODE_I>union     {return UNION;}
159 <CLASS_CODE_I>enum      {return ENUM;}
160 <CLASS_CODE_I>signed    {return SIGNED;}
161 <CLASS_CODE_I>unsigned  {return UNSIGNED;}
162 <CLASS_CODE_I>long      {return LONG;}
163 <CLASS_CODE_I>short     {return SHORT;}
164 <CLASS_CODE_I>int       {return INT;}
165 <CLASS_CODE_I>float     {return FLOAT;}
166 <CLASS_CODE_I>double    {return DOUBLE;}
167 <CLASS_CODE_I>char      {return CHAR;}
168
169 <CLASS_CODE_I>public    {yylval.line = line_no; return PUBLIC;}
170 <CLASS_CODE_I>private   {yylval.line = line_no; return PRIVATE;}
171 <CLASS_CODE_I>argument  {yylval.line = line_no; return ARGUMENT;}
172 <CLASS_CODE_I>virtual   {yylval.line = line_no; return VIRTUAL;}
173 <CLASS_CODE_I>signal    {yylval.line = line_no; return SIGNAL;}
174 <CLASS_CODE_I>override  {yylval.line = line_no; return OVERRIDE;}
175 <CLASS_CODE_I>onerror   {return ONERROR;}
176 <CLASS_CODE_I>0|[1-9][0-9]*|0x[0-9a-fA-F]+|0[0-7]+|[0-9]*\.[0-9]+|\.[0-9][0-9]* {
177                         yylval.id = g_strdup(yytext);
178                         return NUMBER;
179                 }
180 <CLASS_CODE,CLASS_CODE_I>:?[A-Za-z_][A-Za-z0-9_]*(:[A-Za-z0-9_]*)+      {
181                         yylval.id = g_strdup(yytext);
182                         return TYPETOKEN;
183                 }
184 <CLASS_CODE,CLASS_CODE_I>[A-Za-z_][A-Za-z0-9_]* {
185                         yylval.id = g_strdup(yytext);
186                         return TOKEN;
187                 }
188
189 <CLASS_CODE>\{  {
190                         BEGIN(CLASS_CODE_I);
191                         return '{';
192                 }
193 <CLASS_CODE_I>\{        {
194                         BEGIN(C_CODE);
195                         parenth_depth=1;
196                         class_after_c = TRUE;
197                         ccode_line = line_no;
198                         yylval.line = line_no;
199                         return '{';
200                 }
201 <CLASS_CODE_I>\}        {
202                                 BEGIN(INITIAL);
203                                 return '}';
204                         }
205
206 <CLASS_CODE,CLASS_CODE_I,INITIAL>[\n\t ]        ;  /*ignore*/
207
208 <*>.            {
209                         yylval.line = line_no;
210                         return yytext[0];
211                 }