]> git.draconx.ca Git - gob-dx.git/blob - src/lexer.l
Release 0.90.1
[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 <*>MOTHERFUCKER         { fprintf(stderr,"You are a bad bad person!\n"); REJECT; }
71
72 \/\/.*$                 { ; /*comment, ignore*/ }
73 <C_CODE>\/\/.*$         { ; /*comment, ignore*/ }
74 <CLASS_CODE>\/\/.*$     { ; /*comment, ignore*/ }
75 <CLASS_CODE_I>\/\/.*$   { ; /*comment, ignore*/ }
76 \/\*            {BEGIN(COMMENT); before_comment = INITIAL; }
77 <C_CODE>\/\*    {BEGIN(COMMENT); before_comment = C_CODE; }
78 <CLASS_CODE>\/\*        {BEGIN(COMMENT); before_comment = CLASS_CODE; }
79 <CLASS_CODE_I>\/\*      {BEGIN(COMMENT); before_comment = CLASS_CODE_I; }
80 <COMMENT>\*\/   {BEGIN(before_comment);}
81 <COMMENT>.      { ; /* comment, ignore */ }
82 <COMMENT>\n     { ; /* comment, ignore */ }
83
84 ^\%h\{          {
85                         BEGIN(C_CODE);
86                         parenth_depth = 1;
87                         class_after_c = FALSE;
88                         header_c = TRUE;
89                         clear_cbuf();
90                         ccode_line = line_no;
91                 }
92 ^\%\{           {
93                         BEGIN(C_CODE);
94                         parenth_depth = 1;
95                         class_after_c = FALSE;
96                         header_c = FALSE;
97                         clear_cbuf();
98                         ccode_line = line_no;
99                 }
100 <C_CODE>^\%\}   {
101                         BEGIN(INITIAL);
102                         yylval.cbuf = cbuf;
103                         cbuf = NULL;
104                         if(header_c)
105                                 return HCODE;
106                         else
107                                 return CCODE;
108                 }
109
110 <C_CODE>\'\{\'          { add_to_cbuf(yytext); }
111 <C_CODE>\'\\\{\'        { add_to_cbuf(yytext); }
112 <C_CODE>\'\}\'          { add_to_cbuf(yytext); }
113 <C_CODE>\'\\\}\'        { add_to_cbuf(yytext); }
114         
115 <C_CODE>\\.     { add_to_cbuf(yytext); }
116 <C_CODE>\"      {
117                         BEGIN(C_CODE_STRING);
118                         add_to_cbuf(yytext);
119                 }
120 <C_CODE_STRING>\\.      {
121                                 add_to_cbuf(yytext);
122                         }
123 <C_CODE_STRING>\"       {
124                                 BEGIN(C_CODE);
125                                 add_to_cbuf(yytext);
126                         }
127 <C_CODE_STRING>.        {
128                                 add_to_cbuf(yytext);
129                         }
130
131 <C_CODE>\{      {
132                         parenth_depth++;
133                         add_to_cbuf(yytext);
134                 }
135 <C_CODE>\}      {
136                         parenth_depth--;
137                         if(parenth_depth<0) {
138                                 REJECT;
139                         } else if(parenth_depth==0 && class_after_c) {
140                                 BEGIN(CLASS_CODE_I);
141                                 yylval.cbuf = cbuf;
142                                 cbuf = NULL;
143                                 return CCODE;
144                         }
145                         add_to_cbuf(yytext);
146                 }
147
148 <C_CODE>.       { add_to_cbuf(yytext); }
149 <C_CODE>\n      { add_to_cbuf(yytext); }
150
151 class           {
152                         BEGIN(CLASS_CODE);
153                         return CLASS;
154                 }
155
156 <CLASS_CODE>from        {return FROM;}
157
158 <CLASS_CODE_I>void      {return VOID;}
159 <CLASS_CODE_I>struct    {return STRUCT;}
160 <CLASS_CODE_I>union     {return UNION;}
161 <CLASS_CODE_I>enum      {return ENUM;}
162 <CLASS_CODE_I>signed    {return SIGNED;}
163 <CLASS_CODE_I>unsigned  {return UNSIGNED;}
164 <CLASS_CODE_I>long      {return LONG;}
165 <CLASS_CODE_I>short     {return SHORT;}
166 <CLASS_CODE_I>int       {return INT;}
167 <CLASS_CODE_I>float     {return FLOAT;}
168 <CLASS_CODE_I>double    {return DOUBLE;}
169 <CLASS_CODE_I>char      {return CHAR;}
170 <CLASS_CODE_I>const     {return CONST;}
171
172 <CLASS_CODE_I>\.\.\.    {return THREEDOTS;}
173
174 <CLASS_CODE_I>public    {yylval.line = line_no; return PUBLIC;}
175 <CLASS_CODE_I>private   {yylval.line = line_no; return PRIVATE;}
176 <CLASS_CODE_I>argument  {yylval.line = line_no; return ARGUMENT;}
177 <CLASS_CODE_I>virtual   {yylval.line = line_no; return VIRTUAL;}
178 <CLASS_CODE_I>signal    {yylval.line = line_no; return SIGNAL;}
179 <CLASS_CODE_I>override  {yylval.line = line_no; return OVERRIDE;}
180 <CLASS_CODE_I>onerror   {return ONERROR;}
181 <CLASS_CODE_I>0|[1-9][0-9]*|0x[0-9a-fA-F]+|0[0-7]+|[0-9]*\.[0-9]+|\.[0-9][0-9]* {
182                         yylval.id = g_strdup(yytext);
183                         return NUMBER;
184                 }
185 <CLASS_CODE,CLASS_CODE_I>:?[A-Za-z_][A-Za-z0-9_]*(:[A-Za-z0-9_]*)+      {
186                         yylval.id = g_strdup(yytext);
187                         return TYPETOKEN;
188                 }
189 <CLASS_CODE,CLASS_CODE_I>[A-Za-z_][A-Za-z0-9_]* {
190                         yylval.id = g_strdup(yytext);
191                         return TOKEN;
192                 }
193
194 <CLASS_CODE>\{  {
195                         BEGIN(CLASS_CODE_I);
196                         return '{';
197                 }
198 <CLASS_CODE_I>\{        {
199                         BEGIN(C_CODE);
200                         parenth_depth=1;
201                         class_after_c = TRUE;
202                         ccode_line = line_no;
203                         yylval.line = line_no;
204                         return '{';
205                 }
206 <CLASS_CODE_I>\}        {
207                                 BEGIN(INITIAL);
208                                 return '}';
209                         }
210
211 <CLASS_CODE,CLASS_CODE_I,INITIAL>[\n\t ]        ;  /*ignore*/
212
213 <*>.            {
214                         yylval.line = line_no;
215                         return yytext[0];
216                 }