| 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) 2008
|
|---|
| 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 |
|
|---|
| 35 | /*
|
|---|
| 36 | Main file containing the class parser. Whenever a valid keyword is found
|
|---|
| 37 | a specialized parser function in another source file is called from here.
|
|---|
| 38 | */
|
|---|
| 39 | #ifdef __OS2__
|
|---|
| 40 | # include <os2.h>
|
|---|
| 41 | #endif /* __OS2__ */
|
|---|
| 42 |
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <string.h>
|
|---|
| 45 |
|
|---|
| 46 | #include <glib.h>
|
|---|
| 47 | #include <glib/gprintf.h>
|
|---|
| 48 | #include "parser.h"
|
|---|
| 49 |
|
|---|
| 50 | extern GScanner *gScanner;
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | static PINTERFACE createInterfaceStruct()
|
|---|
| 54 | {
|
|---|
| 55 | PINTERFACE pInterface;
|
|---|
| 56 |
|
|---|
| 57 | pInterface=(PINTERFACE)g_malloc0(sizeof(INTERFACE));
|
|---|
| 58 |
|
|---|
| 59 | //pInterface->pInstanceVarArray=g_ptr_array_new();
|
|---|
| 60 | pInterface->pMethodArray=g_ptr_array_new();
|
|---|
| 61 | // pInterface->pOverrideArray=g_ptr_array_new();
|
|---|
| 62 |
|
|---|
| 63 | return pInterface;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 | static void registerInterface(void)
|
|---|
| 69 | {
|
|---|
| 70 | PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
|
|---|
| 71 | PSYMBOL pNewSymbol=g_malloc0(sizeof(SYMBOL));
|
|---|
| 72 |
|
|---|
| 73 | //g_message("In %s for %s", __FUNCTION__, pParseInfo->pCurInterface->chrName);
|
|---|
| 74 |
|
|---|
| 75 | pParseInfo->pCurInterface->pSymbolIFace=pNewSymbol;
|
|---|
| 76 |
|
|---|
| 77 | if(!strcmp(pParseInfo->chrRootSourceFile, pParseInfo->pCurInterface->chrSourceFileName))
|
|---|
| 78 | pParseInfo->pCurInterface->fIsInRootFile=TRUE;
|
|---|
| 79 |
|
|---|
| 80 | g_ptr_array_add(pParseInfo->pInterfaceArray, (gpointer) pParseInfo->pCurInterface);
|
|---|
| 81 |
|
|---|
| 82 | /* Any found interface is registered as a new type so it can be
|
|---|
| 83 | used in other classes. */
|
|---|
| 84 | pNewSymbol->chrSymbolName=g_strdup(pParseInfo->pCurInterface->chrName); /* We create a copy here because
|
|---|
| 85 | when cleaning up the symbol space
|
|---|
| 86 | the string will be freed. */
|
|---|
| 87 | pNewSymbol->uiKind=KIND_TYPESPEC;
|
|---|
| 88 | pNewSymbol->uiSymbolToken=IDL_SYMBOL_REGINTERFACE;
|
|---|
| 89 | g_tree_insert(pParseInfo->pSymbolTree, pNewSymbol, pNewSymbol->chrSymbolName);
|
|---|
| 90 | g_scanner_scope_add_symbol(gScanner, ID_SCOPE, pNewSymbol->chrSymbolName,
|
|---|
| 91 | pNewSymbol);
|
|---|
| 92 | /* For legacy support and convenience we automatically register a pointer type
|
|---|
| 93 | to the interface. */
|
|---|
| 94 | pNewSymbol=g_malloc0(sizeof(SYMBOL));
|
|---|
| 95 | pParseInfo->pCurInterface->pSymbolIFacePtr=pNewSymbol;
|
|---|
| 96 | pNewSymbol->uiKind=KIND_TYPESPEC;
|
|---|
| 97 | pNewSymbol->uiSymbolToken=IDL_SYMBOL_REGINTERFACE;
|
|---|
| 98 | pNewSymbol->chrSymbolName=g_strconcat("P", pParseInfo->pCurInterface->chrName, NULL);
|
|---|
| 99 | g_tree_insert(pParseInfo->pSymbolTree, pNewSymbol, pNewSymbol->chrSymbolName);
|
|---|
| 100 | g_scanner_scope_add_symbol(gScanner, ID_SCOPE, pNewSymbol->chrSymbolName,
|
|---|
| 101 | pNewSymbol);
|
|---|
| 102 | //g_message("%s: %s", __FUNCTION__, pNewSymbol->chrSymbolName);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | static void deRegisterInterface(PINTERFACE pif)
|
|---|
| 106 | {
|
|---|
| 107 | PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
|
|---|
| 108 |
|
|---|
| 109 | /* Remove the interface from our list */
|
|---|
| 110 | g_ptr_array_remove(pParseInfo->pInterfaceArray, (gpointer) pif);
|
|---|
| 111 |
|
|---|
| 112 | /* Any found interface was registered as a new type so it can be
|
|---|
| 113 | used in other classes. */
|
|---|
| 114 | g_tree_remove(pParseInfo->pSymbolTree, pif->pSymbolIFace);
|
|---|
| 115 |
|
|---|
| 116 | g_scanner_scope_remove_symbol(gScanner, ID_SCOPE, pif->pSymbolIFace->chrSymbolName);
|
|---|
| 117 | /* For legacy support and convenience we automatically registered a pointer type
|
|---|
| 118 | to the interface. */
|
|---|
| 119 | g_tree_remove(pParseInfo->pSymbolTree, pif->pSymbolIFacePtr);
|
|---|
| 120 | g_scanner_scope_remove_symbol(gScanner, ID_SCOPE, pif->pSymbolIFacePtr->chrSymbolName);
|
|---|
| 121 | /* We don't clean up. Looking at the whole mess with string dupes and stuff in side the
|
|---|
| 122 | structs I just decided to use a GC instead... */
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | /*
|
|---|
| 128 | Function to parse the body of a class.
|
|---|
| 129 | Current token is '{'.
|
|---|
| 130 |
|
|---|
| 131 | CBODY:= '}' //This is an empty class body
|
|---|
| 132 | | CLASSMETHODS '}'
|
|---|
| 133 |
|
|---|
| 134 | */
|
|---|
| 135 | static void parseCBody(void)
|
|---|
| 136 | {
|
|---|
| 137 | PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
|
|---|
| 138 |
|
|---|
| 139 | while(g_scanner_peek_next_token(gScanner)!= G_TOKEN_EOF && g_scanner_peek_next_token(gScanner)!='}')
|
|---|
| 140 | {
|
|---|
| 141 | PSYMBOL pCurSymbol;
|
|---|
| 142 | GTokenValue value;
|
|---|
| 143 |
|
|---|
| 144 | /* Method implementations must start with "public" which is registered as a symbol. Here we check if
|
|---|
| 145 | the token is a symbol. */
|
|---|
| 146 | exitIfNotMatchNext(G_TOKEN_SYMBOL, "Method implementation must start with 'public'.");
|
|---|
| 147 |
|
|---|
| 148 | value=gScanner->value;
|
|---|
| 149 | pCurSymbol=value.v_symbol;
|
|---|
| 150 |
|
|---|
| 151 | /* Check if token is "public". */
|
|---|
| 152 | if(!pCurSymbol || pCurSymbol->uiSymbolToken!=NOMC_SYMBOL_PUBLIC)
|
|---|
| 153 | {
|
|---|
| 154 | g_scanner_unexp_token(gScanner,
|
|---|
| 155 | G_TOKEN_SYMBOL,
|
|---|
| 156 | NULL, NULL, NULL,
|
|---|
| 157 | "'impl'.",
|
|---|
| 158 | TRUE); /* is_error */
|
|---|
| 159 | cleanupAndExit(1);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /* Get name, parameters and stuff. Print the body. */
|
|---|
| 163 | parseClassMethod();
|
|---|
| 164 | };
|
|---|
| 165 |
|
|---|
| 166 | exitIfNotMatchNext('}', "No closing of 'class' section.");
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | /*
|
|---|
| 173 | Current token is CLASSIDENT.
|
|---|
| 174 |
|
|---|
| 175 | CLASSBODY:= '{' CBODY
|
|---|
| 176 | | '{' CBODY ';'
|
|---|
| 177 |
|
|---|
| 178 | */
|
|---|
| 179 | static void parseClassBody(void)
|
|---|
| 180 | {
|
|---|
| 181 |
|
|---|
| 182 | exitIfNotMatchNext('{', "No opening brace for class body.");
|
|---|
| 183 |
|
|---|
| 184 | parseCBody();
|
|---|
| 185 |
|
|---|
| 186 | /* Remove a terminating ';' from the input if present. */
|
|---|
| 187 | matchNext(';');
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 | /*
|
|---|
| 192 | Parse the class name. If we already encountered this class the name is registered as a
|
|---|
| 193 | symbol. IF this is the first time the name is an identifier.
|
|---|
| 194 |
|
|---|
| 195 | Note that the current token is the 'class' keyword.
|
|---|
| 196 |
|
|---|
| 197 | CLASSIDENT:= G_TOKEN_IDENTIFIER
|
|---|
| 198 | | IDL_SYMBOL_INTERFACE
|
|---|
| 199 | */
|
|---|
| 200 | static gchar* parseClassIdent(void)
|
|---|
| 201 | {
|
|---|
| 202 | PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
|
|---|
| 203 |
|
|---|
| 204 | if(matchNext(G_TOKEN_IDENTIFIER))
|
|---|
| 205 | {
|
|---|
| 206 | /* Save interface info */
|
|---|
| 207 | GTokenValue value=gScanner->value;
|
|---|
| 208 |
|
|---|
| 209 | return g_strdup(value.v_identifier);
|
|---|
| 210 | }
|
|---|
| 211 | else
|
|---|
| 212 | {
|
|---|
| 213 | PSYMBOL pCurSymbol;
|
|---|
| 214 | GTokenValue value;
|
|---|
| 215 |
|
|---|
| 216 | /* If the interface name is a symbol, it means the interface was
|
|---|
| 217 | already registered before. Maybe because of a forward statement.
|
|---|
| 218 | We will check that in the function which called us. */
|
|---|
| 219 | exitIfNotMatchNext(G_TOKEN_SYMBOL, "Keyword 'class' must be followed by an identifier");
|
|---|
| 220 |
|
|---|
| 221 | /* Check if it's one of our interface symbols */
|
|---|
| 222 | value=gScanner->value;
|
|---|
| 223 | pCurSymbol=value.v_symbol;
|
|---|
| 224 | if(IDL_SYMBOL_REGINTERFACE!=pCurSymbol->uiSymbolToken)
|
|---|
| 225 | {
|
|---|
| 226 | /* No, some other symbol */
|
|---|
| 227 | g_scanner_unexp_token(gScanner,
|
|---|
| 228 | G_TOKEN_SYMBOL,
|
|---|
| 229 | NULL, NULL, NULL,
|
|---|
| 230 | "Keyword 'class' is not followed by a valid identifier.",
|
|---|
| 231 | TRUE); /* is_error */
|
|---|
| 232 | cleanupAndExit(1);
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | /* Save interface name */
|
|---|
| 236 | return g_strdup(pCurSymbol->chrSymbolName);
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | static gchar* parseParentClassIdent(void)
|
|---|
| 242 | {
|
|---|
| 243 | g_message("Line %d: Error in class declaration", g_scanner_cur_line(gScanner));
|
|---|
| 244 | cleanupAndExit(0);
|
|---|
| 245 |
|
|---|
| 246 | return NULL;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 | static void doForwardClassDeclaration(void)
|
|---|
| 251 | {
|
|---|
| 252 | PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
|
|---|
| 253 | PINTERFACE pif;
|
|---|
| 254 |
|
|---|
| 255 | /* Check if we already have a (maybe forward) declaration */
|
|---|
| 256 | pif=findInterfaceFromName(pParseInfo->pCurInterface->chrName);
|
|---|
| 257 | if(pif)
|
|---|
| 258 | {
|
|---|
| 259 | /* One forward declaration is enough... */
|
|---|
| 260 | g_free(pParseInfo->pCurInterface->chrName);
|
|---|
| 261 | g_free(pParseInfo->pCurInterface);
|
|---|
| 262 | }
|
|---|
| 263 | else
|
|---|
| 264 | {
|
|---|
| 265 | pParseInfo->pCurInterface->chrSourceFileName=g_strdup(pParseInfo->chrCurrentSourceFile);
|
|---|
| 266 | pParseInfo->pCurInterface->fIsForwardDeclaration=TRUE;
|
|---|
| 267 | /* It's save to register the interface right here even if the struct is almost empty.
|
|---|
| 268 | If anything goes wrong later we will exit anyway. */
|
|---|
| 269 | registerInterface();
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 | static void doClassDeclaration(void)
|
|---|
| 275 | {
|
|---|
| 276 | PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
|
|---|
| 277 |
|
|---|
| 278 | PINTERFACE pif;
|
|---|
| 279 | gchar *chrTemp=pParseInfo->pCurInterface->chrName;
|
|---|
| 280 |
|
|---|
| 281 | /* Check if we already have a (maybe forward) declaration */
|
|---|
| 282 | pif=findInterfaceFromName(pParseInfo->pCurInterface->chrName);
|
|---|
| 283 | if(pif)
|
|---|
| 284 | {
|
|---|
| 285 | if(pif->fIsForwardDeclaration)
|
|---|
| 286 | {
|
|---|
| 287 | /* Remove the forward declaration and insert the real thing afterwards. */
|
|---|
| 288 | deRegisterInterface(pif);
|
|---|
| 289 | }
|
|---|
| 290 | else
|
|---|
| 291 | {
|
|---|
| 292 | /* ItËs the declaration from the *.h file. Save a pointer to this information. */
|
|---|
| 293 | pParseInfo->pClassDefinition=pif;
|
|---|
| 294 | deRegisterInterface(pif);
|
|---|
| 295 | }
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | pParseInfo->pCurInterface->chrName=chrTemp;
|
|---|
| 299 | pParseInfo->pCurInterface->chrSourceFileName=g_strdup(pParseInfo->chrCurrentSourceFile);
|
|---|
| 300 |
|
|---|
| 301 | /* It's save to register the interface right here even if the struct is almost empty.
|
|---|
| 302 | If anything goes wrong later we will exit anyway. */
|
|---|
| 303 | registerInterface();
|
|---|
| 304 |
|
|---|
| 305 | /* The class definition in *.nom files does not contain all the stuff an interface may define. We use the found
|
|---|
| 306 | interface to fill the gaps. If we donËt have an interface something went wrong and we quit. */
|
|---|
| 307 | if(!pParseInfo->pClassDefinition)
|
|---|
| 308 | {
|
|---|
| 309 | g_message("Line %d: Error during class parsing. No class definition found. MAke sure you included the *.ih file.", g_scanner_cur_line(gScanner));
|
|---|
| 310 | cleanupAndExit(0);
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | pParseInfo->pCurInterface->chrParent=g_strdup(pParseInfo->pClassDefinition->chrParent);
|
|---|
| 314 |
|
|---|
| 315 | /* Note: We donËt support subclasses yet. */
|
|---|
| 316 | if(matchNext(':'))
|
|---|
| 317 | {
|
|---|
| 318 | parseParentClassIdent();
|
|---|
| 319 | }
|
|---|
| 320 | parseClassBody();
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /*
|
|---|
| 324 | Parse a class declaration. The current token is the 'class' keyword.
|
|---|
| 325 |
|
|---|
| 326 | class:= CLASSIDENT ';' // Forward declaration
|
|---|
| 327 | | CLASSIDENT ':' PARENTCLASSIDENT CLASSBODY // Subclass (not used yet!)
|
|---|
| 328 | | CLASSIDENT CLASSBODY
|
|---|
| 329 | */
|
|---|
| 330 | void parseClass(GTokenType token)
|
|---|
| 331 | {
|
|---|
| 332 | PPARSEINFO pParseInfo=(PPARSEINFO)gScanner->user_data;
|
|---|
| 333 | pParseInfo->pCurInterface=createInterfaceStruct();
|
|---|
| 334 |
|
|---|
| 335 | /* Get the class name. */
|
|---|
| 336 | pParseInfo->pCurInterface->chrName=parseClassIdent();
|
|---|
| 337 |
|
|---|
| 338 | /* Check for forward declaration */
|
|---|
| 339 | if(matchNext(';'))
|
|---|
| 340 | {
|
|---|
| 341 | doForwardClassDeclaration();
|
|---|
| 342 | }
|
|---|
| 343 | else
|
|---|
| 344 | {
|
|---|
| 345 | /* This is the real thing. */
|
|---|
| 346 | doClassDeclaration();
|
|---|
| 347 | }/* not forward declaration */
|
|---|
| 348 |
|
|---|
| 349 | #if 0
|
|---|
| 350 | g_printf("\n\n");
|
|---|
| 351 | /* In printdata.c */
|
|---|
| 352 | printAllInterfaces();
|
|---|
| 353 | g_printf("\n\n");
|
|---|
| 354 | printInterface(pParseInfo->pCurInterface);
|
|---|
| 355 | #endif
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|