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