source: trunk/idl-compiler/c/nom-idl-compiler.c@ 265

Last change on this file since 265 was 265, checked in by cinc, 18 years ago

Rewrote symbol handling.

File size: 10.3 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2* Version: CDDL 1.0/LGPL 2.1
3*
4* The contents of this file are subject to the COMMON DEVELOPMENT AND
5* DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6* this file except in compliance with the License. You may obtain a copy of
7* the License at http://www.sun.com/cddl/
8*
9* Software distributed under the License is distributed on an "AS IS" basis,
10* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11* for the specific language governing rights and limitations under the
12* License.
13*
14* The Original Code is "NOM" Netlabs Object Model
15*
16* The Initial Developer of the Original Code is
17* netlabs.org: Chris Wohlgemuth <cinc-ml@netlabs.org>.
18* Portions created by the Initial Developer are Copyright (C) 2007
19* the Initial Developer. All Rights Reserved.
20*
21* Contributor(s):
22*
23* Alternatively, the contents of this file may be used under the terms of
24* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
25* case the provisions of the LGPL are applicable instead of those above. If
26* you wish to allow use of your version of this file only under the terms of
27* the LGPL, and not to allow others to use your version of this file under
28* the terms of the CDDL, indicate your decision by deleting the provisions
29* above and replace them with the notice and other provisions required by the
30* LGPL. If you do not delete the provisions above, a recipient may use your
31* version of this file under the terms of any one of the CDDL or the LGPL.
32*
33* ***** END LICENSE BLOCK ***** */
34#define INCL_DOSPROCESS
35#define INCL_DOS
36#define INCL_DOSPROFILE
37#define INCL_DOSERRORS
38
39#include <os2.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43
44#include <io.h>
45#include <fcntl.h>
46#include <sys\stat.h>
47
48#include <glib.h>
49#include <glib/gprintf.h>
50
51#include "nom.h"
52#include "nomtk.h"
53
54#include "parser.h"
55
56static gchar* chrOutputDir="";
57static gchar* chrOutputName="";
58static gboolean fOptionEmitH=FALSE;
59static gboolean fOptionEmitIH=FALSE;
60static gboolean fOptionEmitC=FALSE;
61
62/* Command line options */
63static GOptionEntry gOptionEntries[] =
64{
65 {"directory", 'd', 0, G_OPTION_ARG_FILENAME, &chrOutputDir, "Output directory", NULL},
66 {"emit-h", 0, 0, G_OPTION_ARG_NONE, &fOptionEmitH, "Emmit a header file (*.h)", NULL},
67 {"emit-ih", 0, 0, G_OPTION_ARG_NONE, &fOptionEmitIH, "Emmit an include header (*.ih)", NULL},
68 {"emit-c", 0, 0, G_OPTION_ARG_NONE, &fOptionEmitC, "Emmit an implementation template (*.c)", NULL},
69 {"output", 'o', 0, G_OPTION_ARG_FILENAME, &chrOutputName, "Output name", NULL},
70 {NULL}
71};
72
73static char* chrOutputFileName="";
74
75/* The pointer array holding the interfaces we found */
76GPtrArray* pInterfaceArray;
77
78/* Symbols defined for our IDL language.
79 Keep this in synch with the defined enums! */
80SYMBOL idlSymbols[]={
81 {"interface", IDL_SYMBOL_INTERFACE, KIND_UNKNOWN}, /* 71 */
82 {"NOMCLASSVERSION", IDL_SYMBOL_CLSVERSION, KIND_UNKNOWN},
83 {"NOMINSTANCEVAR", IDL_SYMBOL_INSTANCEVAR, KIND_UNKNOWN},
84 {"NOMOVERRIDE", IDL_SYMBOL_OVERRIDE, KIND_UNKNOWN},
85 {"gulong", IDL_SYMBOL_GULONG, KIND_TYPESPEC},
86 {"gint", IDL_SYMBOL_GINT, KIND_TYPESPEC},
87 {"gpointer", IDL_SYMBOL_GPOINTER, KIND_TYPESPEC},
88 {"gboolean", IDL_SYMBOL_GBOOLEAN, KIND_TYPESPEC},
89 {"in", IDL_SYMBOL_IN, KIND_DIRECTION},
90 {"out", IDL_SYMBOL_OUT, KIND_DIRECTION},
91 {"inout", IDL_SYMBOL_INOUT, KIND_DIRECTION},
92 {"define", IDL_SYMBOL_DEFINE, KIND_UNKNOWN},
93 {"ifdef", IDL_SYMBOL_IFDEF, KIND_UNKNOWN},
94 {"endif", IDL_SYMBOL_ENDIF, KIND_UNKNOWN},
95 {NULL, 0, KIND_UNKNOWN}
96},*pSymbols=idlSymbols;
97
98GScanner *gScanner;
99GTokenType curToken=G_TOKEN_EOF;
100PINTERFACE pCurInterface=NULL;
101
102/* Holding info about current token. Referenced by gScanner. */
103SYMBOLINFO curSymbol;
104
105/* Holding the current state of parsing and pointers to necessary lists. */
106PARSEINFO parseInfo={0};
107
108PINTERFACE findInterfaceFromName(gchar* chrName)
109{
110 int a;
111
112 for(a=0;a<pInterfaceArray->len;a++)
113 {
114 PINTERFACE pif=g_ptr_array_index(pInterfaceArray, a);
115 if(!strcmp(chrName, pif->chrName))
116 return pif;
117 }
118
119 return NULL;
120}
121
122
123gchar* getTypeSpecStringFromCurToken(void)
124{
125 GTokenValue value;
126
127 value=gScanner->value;
128
129 switch(curToken)
130 {
131 case G_TOKEN_IDENTIFIER:
132 return g_strdup(value.v_identifier);
133 break;
134 case G_TOKEN_SYMBOL:
135 {
136 /* It's one of our symbols. */
137 PSYMBOL pCurSymbol;
138
139 pCurSymbol=value.v_symbol;
140 return g_strdup(pCurSymbol->chrSymbolName);
141 break;
142 }
143 default:
144 g_scanner_unexp_token(gScanner,
145 G_TOKEN_SYMBOL,
146 NULL,
147 NULL,
148 NULL,
149 "",
150 TRUE); /* is_error */
151
152 break;
153 }
154 return "unknown";
155}
156
157
158/**
159 This is the root parse function. Here starts the fun...
160 */
161void parseIt(void)
162{
163 while(g_scanner_peek_next_token(gScanner) != G_TOKEN_EOF) {
164 GTokenType token;
165
166 curToken=g_scanner_get_next_token(gScanner);
167 token=curToken;
168 GTokenValue value=gScanner->value;
169
170 switch(curToken)
171 {
172 case '#':
173 parseHash();
174 break;
175 case G_TOKEN_SYMBOL:
176 {
177 PSYMBOL pCurSymbol=value.v_symbol;
178 switch(pCurSymbol->uiSymbolToken)
179 {
180 case IDL_SYMBOL_INTERFACE:
181 parseInterface(token);
182 break;
183 default:
184 break;
185 }
186 break;
187 }
188#if 0
189 case G_TOKEN_IDENTIFIER:
190 g_message("Token: %d (G_TOKEN_IDENTIFIER)\t\t%s", token, value.v_identifier);
191 break;
192 case G_TOKEN_STRING:
193 g_message("Token: %d (G_TOKEN_STRING)\t\t\t%s", token, value.v_string);
194 break;
195 case G_TOKEN_LEFT_PAREN:
196 g_message("Token: %d (G_TOKEN_LEFT_PAREN)\t\t(", token);
197 break;
198 case G_TOKEN_RIGHT_PAREN:
199 g_message("Token: %d (G_TOKEN_RIGHT_PAREN)\t\t)", token);
200 break;
201 case ':':
202 g_message("Token: %d (colon)\t\t:", token);
203 break;
204 case ';':
205 g_message("Token: %d (semicolon)\t\t\t;", token);
206 break;
207 case '/':
208 g_message("Token: %d (slash)\t\t\t/ %s", token, value.v_comment);
209 break;
210 case G_TOKEN_COMMA:
211 g_message("Token: %d (G_TOKEN_COMMA)\t\t\t,", token);
212 break;
213 case G_TOKEN_INT:
214 g_message("Token: %d (G_TOKEN_INT)\t\t\t%ld", token, value.v_int);
215 break;
216 case IDL_SYMBOL_DEFINE:
217 g_message("Token: %d (IDL_SYMBOL_DEFINE)\t\t\t", token);
218 break;
219 case IDL_SYMBOL_IFDEF:
220 g_message("Token: %d (IDL_SYMBOL_IFDEF)\t\t\t", token);
221 break;
222 case IDL_SYMBOL_ENDIF:
223 g_message("Token: %d (IDL_SYMBOL_ENDIF)\t\t\t", token);
224 break;
225#endif
226 default:
227 printToken(curToken);
228 // g_message("Token: %d (---)\t\t\t%c (LINE %d)", token, token, g_scanner_cur_line(gScanner));
229 break;
230 }
231 }
232}
233
234/* Show help.
235 gContext must be valid.
236*/
237static void outputCompilerHelp(GOptionContext *gContext, gchar* chrExeName)
238{
239 GError *gError = NULL;
240 int argc2=2;
241 char *helpCmd[]={"","--help"};
242 char** argv2=helpCmd;
243 helpCmd[0]=chrExeName;
244
245 g_option_context_parse (gContext, &argc2, &argv2, &gError);
246}
247
248static gint funcSymbolCompare(gconstpointer a, gconstpointer b)
249{
250 if(a==b)
251 return 0;
252
253 if(a<b)
254 return -1;
255
256 return 1;
257};
258/*
259
260 */
261int main(int argc, char **argv)
262{
263 int a;
264 int fd;
265 int idScope=0;
266 GError *gError = NULL;
267 GOptionContext* gContext;
268
269 /* Parse command line options */
270 gContext = g_option_context_new ("file");
271 g_option_context_add_main_entries (gContext, gOptionEntries, NULL);
272
273 if(!g_option_context_parse (gContext, &argc, &argv, &gError))
274 {
275 outputCompilerHelp(gContext, argv[0]);
276 }
277
278 /* Correct emitter options? Exactly one emitter must be specified. */
279 a=0;
280 if(fOptionEmitH)
281 a++;
282 if(fOptionEmitIH)
283 a++;
284 if(fOptionEmitC)
285 a++;
286
287#if 0
288 if(!a){
289 g_printf("An emitter must be specified.\n\n");
290 outputCompilerHelp(gContext, argv[0]);
291 }
292 if(a>1){
293 g_printf("Only one emitter must be specified.\n\n");
294 outputCompilerHelp(gContext, argv[0]);
295 }
296#endif
297 g_option_context_free(gContext);
298
299
300 if(argc<2)
301 {
302 g_printf("No input file name given.\n\nUse %s --help for options.\n\n", argv[0]);
303 return 1;
304 }
305
306 for(a=0; a<argc; a++)
307 {
308 g_message("arg %d: %s", a, argv[a]);
309 }
310
311 /* Create output file name */
312
313 fd=open(argv[1], O_RDONLY);
314
315 if(-1==fd)
316 {
317 g_message("Can't open input file %s", argv[1]);
318 exit(1);
319 }
320
321 g_printf("\n");
322
323
324
325 gScanner=g_scanner_new(NULL);
326 gScanner->user_data=(gpointer)&curSymbol;
327 curSymbol.pSymbols=idlSymbols;
328
329 pInterfaceArray=g_ptr_array_new();
330
331 g_scanner_input_file(gScanner, fd);
332 /* No single line comments */
333 gScanner->config->skip_comment_single=FALSE;
334 gScanner->config->cpair_comment_single="";
335 /* This string is used in error messages of the parser */
336 gScanner->input_name=IDL_COMPILER_STRING;
337
338 g_scanner_set_scope(gScanner, idScope);
339 /* Load our own symbols into the scanner. We use the default scope for now. */
340 parseInfo.pSymbolTree=g_tree_new((GCompareFunc) funcSymbolCompare);
341 while(pSymbols->chrSymbolName)
342 {
343 g_scanner_scope_add_symbol(gScanner, idScope, pSymbols->chrSymbolName,
344 pSymbols);
345 g_tree_insert(parseInfo.pSymbolTree, pSymbols, pSymbols->chrSymbolName);
346 pSymbols++;
347 }
348 // gScanner->config->symbol_2_token=TRUE;
349
350 parseIt();
351
352 if(pInterfaceArray->len)
353 printInterface();
354
355 g_scanner_destroy(gScanner);
356 close(fd);
357
358 return 0;
359}
360
361#if 0
362 /* We are a folder somwhere in the chain */
363 nomRetval=WPFileSystem_wpQueryFileName((WPFileSystem*)wpParent, bFullPath, NULLHANDLE);
364
365 nomRetval=wpParent.wpQueryFileName(bFullPath, NULLHANDLE);
366
367 nomPath=NOMPathNew();
368 nomPath= (PNOMPath) NOMPath_assignCString(nomPath, _pszFullPath, ev);
369
370 return (PNOMPath)NOMPath_appendPath(nomRetval, nomPath, NULLHANDLE);
371
372#endif
Note: See TracBrowser for help on using the repository browser.