]> git.draconx.ca Git - gob-dx.git/blob - src/lexer.l
Release 0.0.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 <glib.h>
24
25 #include "y.tab.h"
26
27 static int parenth_depth = 0;
28 static int before_comment = INITIAL;
29 static int class_after_c = FALSE;
30 static int header_c = FALSE;
31
32 static GString *cbuf = NULL;
33
34 int line_no = 1;
35
36 static void
37 clear_cbuf(void)
38 {
39         if(!cbuf) {
40                 cbuf = g_string_new("");
41         } else {
42                 cbuf = g_string_assign(cbuf,"");
43         }
44 }
45
46 static void
47 add_to_cbuf(char *s)
48 {
49         if(!cbuf) {
50                 cbuf = g_string_new(s);
51         } else {
52                 cbuf = g_string_append(cbuf,s);
53         }
54 }
55
56 %}
57
58 %x COMMENT
59 %x C_CODE
60 %x C_CODE_STRING
61 %x CLASS_CODE
62 %x CLASS_CODE_I
63
64 %%
65
66 <*>\n                   { line_no++; REJECT; }
67
68 \/\/.*$                 { ; /*comment, ignore*/ }
69 <C_CODE>\/\/.*$         { ; /*comment, ignore*/ }
70 <CLASS_CODE>\/\/.*$     { ; /*comment, ignore*/ }
71 <CLASS_CODE_I>\/\/.*$   { ; /*comment, ignore*/ }
72 \/\*            {BEGIN(COMMENT); before_comment = INITIAL; }
73 <C_CODE>\/\*    {BEGIN(COMMENT); before_comment = C_CODE; }
74 <CLASS_CODE>\/\*        {BEGIN(COMMENT); before_comment = CLASS_CODE; }
75 <CLASS_CODE_I>\/\*      {BEGIN(COMMENT); before_comment = CLASS_CODE_I; }
76 <COMMENT>\*\/   {BEGIN(before_comment);}
77 <COMMENT>.      { ; /* comment, ignore */ }
78
79 ^\%h\{          {
80                         BEGIN(C_CODE);
81                         parenth_depth = 1;
82                         class_after_c = FALSE;
83                         header_c = TRUE;
84                         clear_cbuf();
85                 }
86 ^\%\{           {
87                         BEGIN(C_CODE);
88                         parenth_depth = 1;
89                         class_after_c = FALSE;
90                         header_c = FALSE;
91                         clear_cbuf();
92                 }
93 <C_CODE>^\%\}   {
94                         BEGIN(INITIAL);
95                         yylval.cbuf = cbuf;
96                         cbuf = NULL;
97                         if(header_c)
98                                 return HCODE;
99                         else
100                                 return CCODE;
101                 }
102
103 <C_CODE>\'\{\'          { add_to_cbuf(yytext); }
104 <C_CODE>\'\\\{\'        { add_to_cbuf(yytext); }
105 <C_CODE>\'\}\'          { add_to_cbuf(yytext); }
106 <C_CODE>\'\\\}\'        { add_to_cbuf(yytext); }
107         
108 <C_CODE>\\.     { add_to_cbuf(yytext); }
109 <C_CODE>\"      {
110                         BEGIN(C_CODE_STRING);
111                         add_to_cbuf(yytext);
112                 }
113 <C_CODE_STRING>\\.      {
114                                 add_to_cbuf(yytext);
115                         }
116 <C_CODE_STRING>\"       {
117                                 BEGIN(C_CODE);
118                                 add_to_cbuf(yytext);
119                         }
120 <C_CODE_STRING>.        {
121                                 add_to_cbuf(yytext);
122                         }
123
124 <C_CODE>\{      {
125                         parenth_depth++;
126                         add_to_cbuf(yytext);
127                 }
128 <C_CODE>\}      {
129                         parenth_depth--;
130                         if(parenth_depth<0) {
131                                 REJECT;
132                         } else if(parenth_depth==0 && class_after_c) {
133                                 BEGIN(CLASS_CODE_I);
134                                 yylval.cbuf = cbuf;
135                                 cbuf = NULL;
136                                 return CCODE;
137                         }
138                         add_to_cbuf(yytext);
139                 }
140
141 <C_CODE>.       { add_to_cbuf(yytext); }
142 <C_CODE>\n      { add_to_cbuf(yytext); }
143
144 class           {
145                         BEGIN(CLASS_CODE);
146                         return CLASS;
147                 }
148
149 <CLASS_CODE>from        {return FROM;}
150
151 <CLASS_CODE_I>void      {return VOID;}
152 <CLASS_CODE_I>struct    {return STRUCT;}
153 <CLASS_CODE_I>union     {return UNION;}
154 <CLASS_CODE_I>enum      {return ENUM;}
155 <CLASS_CODE_I>signed    {return SIGNED;}
156 <CLASS_CODE_I>unsigned  {return UNSIGNED;}
157 <CLASS_CODE_I>long      {return LONG;}
158 <CLASS_CODE_I>short     {return SHORT;}
159 <CLASS_CODE_I>int       {return INT;}
160 <CLASS_CODE_I>float     {return FLOAT;}
161 <CLASS_CODE_I>double    {return DOUBLE;}
162 <CLASS_CODE_I>char      {return CHAR;}
163
164 <CLASS_CODE_I>public    {yylval.line = line_no; return PUBLIC;}
165 <CLASS_CODE_I>private   {yylval.line = line_no; return PRIVATE;}
166 <CLASS_CODE_I>argument  {yylval.line = line_no; return ARGUMENT;}
167 <CLASS_CODE_I>virtual   {yylval.line = line_no; return VIRTUAL;}
168 <CLASS_CODE_I>signal    {yylval.line = line_no; return SIGNAL;}
169 <CLASS_CODE_I>last      {return LAST;}
170 <CLASS_CODE_I>first     {return FIRST;}
171 <CLASS_CODE_I>override  {yylval.line = line_no; return OVERRIDE;}
172 <CLASS_CODE_I>check     {return CHECK;}
173 <CLASS_CODE_I>null      {return CNULL;}
174 <CLASS_CODE_I>type      {return TYPE;}
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                         return '{';
198                 }
199 <CLASS_CODE_I>\}        {
200                                 BEGIN(INITIAL);
201                                 return '}';
202                         }
203
204 <CLASS_CODE,CLASS_CODE_I,INITIAL>[\n\t ]        ;  /*ignore*/
205
206 <*>.            {
207                         yylval.line = line_no;
208                         return yytext[0];
209                 }