Changeset 266 for trunk/idl-compiler


Ignore:
Timestamp:
Mar 25, 2007, 1:07:25 AM (18 years ago)
Author:
cinc
Message:

Getting along with parsing

Location:
trunk/idl-compiler
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/idl-compiler/Makefile

    r264 r266  
    5959
    6060
    61 $(OBJDIR)/%.o:  $(CDIR)/%.c
     61$(OBJDIR)/%.o:  $(CDIR)/%.c $(INCDIR)/parser.h
    6262        $(CC) -I $(INC) $(CFLAGS)  -o$@ $<
    6363
    64 $(OBJDIR)/%.o:  $(PARSERDIR)/%.c
     64$(OBJDIR)/%.o:  $(PARSERDIR)/%.c $(INCDIR)/parser.h
    6565        $(CC) -I $(INC) $(CFLAGS)  -o$@ $<
    6666
  • trunk/idl-compiler/c/nom-idl-compiler.c

    r265 r266  
    8383  {"NOMINSTANCEVAR", IDL_SYMBOL_INSTANCEVAR, KIND_UNKNOWN},
    8484  {"NOMOVERRIDE", IDL_SYMBOL_OVERRIDE, KIND_UNKNOWN},
     85  {"NOMREGISTEREDIFACE", IDL_SYMBOL_REGINTERFACE, KIND_TYPESPEC},
     86  {"native", IDL_SYMBOL_NATIVE, KIND_UNKNOWN},
    8587  {"gulong", IDL_SYMBOL_GULONG, KIND_TYPESPEC},
    8688  {"gint", IDL_SYMBOL_GINT, KIND_TYPESPEC},
    8789  {"gpointer", IDL_SYMBOL_GPOINTER, KIND_TYPESPEC},
    8890  {"gboolean", IDL_SYMBOL_GBOOLEAN, KIND_TYPESPEC},
     91  {"gchar", IDL_SYMBOL_GCHAR, KIND_TYPESPEC},
     92  {"void", IDL_SYMBOL_VOID, KIND_TYPESPEC},
    8993  {"in", IDL_SYMBOL_IN, KIND_DIRECTION},
    9094  {"out", IDL_SYMBOL_OUT, KIND_DIRECTION},
     
    155159}
    156160
     161/*
     162  The native keyword is used to introduce new types. That's coming
     163  from the Corba spec. Maybe we will change that some time.
     164
     165The current token is the 'native' keyword.
     166
     167  N:= G_TOKEN_SYMBOL IDENT ';'
     168 */
     169static void parseNative(void)
     170{
     171  GTokenValue value;
     172  PSYMBOL pCurSymbol=g_malloc0(sizeof(SYMBOL));
     173
     174  if(!matchNext(G_TOKEN_IDENTIFIER))
     175    {
     176      PSYMBOL pSymbol;
     177
     178      /* Check if it's a symbol. The following 'identifier' (word) is maybe alread
     179       registered as a symbol. */
     180      if(!matchNext(G_TOKEN_SYMBOL))
     181        {
     182          g_scanner_unexp_token(gScanner,
     183                                G_TOKEN_SYMBOL,
     184                                NULL,
     185                                NULL,
     186                                NULL,
     187                                "'native' statement is not followed by a valid identifier.",
     188                                TRUE); /* is_error */
     189          exit(1);
     190        }
     191      /* It's a symbol. Check if it's a typespec. */
     192      value=gScanner->value;
     193      pSymbol=value.v_symbol;
     194      if(!pSymbol || pSymbol->uiKind!=KIND_TYPESPEC)
     195        {
     196          g_scanner_unexp_token(gScanner,
     197                                G_TOKEN_SYMBOL,
     198                                NULL,
     199                                NULL,
     200                                NULL,
     201                                "'native' statement is not followed by a valid symbol.",
     202                                TRUE); /* is_error */
     203          exit(1);
     204        }
     205    }
     206
     207    value=gScanner->value;
     208    pCurSymbol->chrSymbolName=g_strdup(value.v_identifier);
     209    pCurSymbol->uiKind=KIND_TYPESPEC;
     210    pCurSymbol->uiSymbolToken=G_TOKEN_NONE;
     211    g_tree_insert(parseInfo.pSymbolTree, pCurSymbol, pCurSymbol->chrSymbolName);
     212    g_scanner_scope_add_symbol(gScanner, ID_SCOPE, pCurSymbol->chrSymbolName,
     213                             pCurSymbol);
     214
     215    if(!matchNext(';'))
     216      {
     217        getNextToken(); /* Make sure error references the correct token */
     218        g_scanner_unexp_token(gScanner,
     219                              ';',
     220                              NULL,
     221                              NULL,
     222                              NULL,
     223                              "Error in 'native' definition , Missing semicolon",
     224                              TRUE); /* is_error */
     225        exit(1);
     226      }
     227
     228}
    157229
    158230/**
     
    180252            case IDL_SYMBOL_INTERFACE:
    181253              parseInterface(token);
     254              break;
     255            case IDL_SYMBOL_NATIVE:
     256              parseNative();
    182257              break;
    183258            default:
     
    226301      default:
    227302        printToken(curToken);
    228         //  g_message("Token: %d (---)\t\t\t%c (LINE %d)", token, token, g_scanner_cur_line(gScanner));
    229303        break;
    230304      }
     
    246320}
    247321
     322/*
     323  Compare function for the tree holding our private symbols.
     324 */
    248325static gint funcSymbolCompare(gconstpointer a, gconstpointer b)
    249326{
     
    256333  return 1;
    257334};
     335
     336void funcMsgHandler(GScanner *gScanner,
     337                    gchar *message,
     338                    gboolean error)
     339{
     340
     341  g_printf("In file %s, line %d:\n\t%s\n", parseInfo.chrCurrentSourceFile,
     342           g_scanner_cur_line(gScanner)-parseInfo.uiLineCorrection, message);
     343}
     344
    258345/*
    259346
     
    263350  int a;
    264351  int fd;
    265   int idScope=0;
     352
    266353  GError *gError = NULL;
    267354  GOptionContext* gContext;
     
    310397
    311398  /* Create output file name */
    312 
    313   fd=open(argv[1], O_RDONLY);
     399  if(!strcmp(argv[1], "-"))
     400    fd=0; /* Read from stdin */
     401  else
     402    fd=open(argv[1], O_RDONLY);
    314403 
    315404  if(-1==fd)
     
    327416  curSymbol.pSymbols=idlSymbols;
    328417
     418  gScanner->msg_handler=funcMsgHandler;
    329419  pInterfaceArray=g_ptr_array_new();
    330420
     
    336426  gScanner->input_name=IDL_COMPILER_STRING;
    337427
    338   g_scanner_set_scope(gScanner, idScope);
     428  g_scanner_set_scope(gScanner, ID_SCOPE);
    339429  /* Load our own symbols into the scanner. We use the default scope for now. */
    340430  parseInfo.pSymbolTree=g_tree_new((GCompareFunc) funcSymbolCompare);
    341431  while(pSymbols->chrSymbolName)
    342432    {
    343       g_scanner_scope_add_symbol(gScanner, idScope, pSymbols->chrSymbolName,
     433#warning !!! Create a copy here so it is the same as with new symbols added later.
     434      g_scanner_scope_add_symbol(gScanner, ID_SCOPE, pSymbols->chrSymbolName,
    344435                                 pSymbols);
    345436      g_tree_insert(parseInfo.pSymbolTree, pSymbols, pSymbols->chrSymbolName);
     
    371462
    372463#endif
     464
     465
     466
     467
     468
     469
     470
     471
     472
  • trunk/idl-compiler/include/parser.h

    r265 r266  
    4646
    4747#define IDL_COMPILER_STRING "IDL compiler" /* Used inside warnings and errors */
     48#define ID_SCOPE 0
    4849
    4950/* Holding an overriden method */
     
    127128  IDL_SYMBOL_INSTANCEVAR,
    128129  IDL_SYMBOL_OVERRIDE,
     130  IDL_SYMBOL_REGINTERFACE,  /* Used for registered interfaces */
     131  IDL_SYMBOL_NATIVE,
    129132  /* Some GLib types */
    130133  IDL_SYMBOL_GULONG,           /* 275 */
     
    132135  IDL_SYMBOL_GPOINTER,
    133136  IDL_SYMBOL_GBOOLEAN,
     137  IDL_SYMBOL_GCHAR,
     138  IDL_SYMBOL_VOID,
    134139  /* Direction of method parameters */
    135140  IDL_SYMBOL_IN,
  • trunk/idl-compiler/parser_c/interface_parser.c

    r265 r266  
    4545/* The pointer array holding the interfaces we found */
    4646extern GPtrArray* pInterfaceArray;
    47 
     47extern PARSEINFO parseInfo;
     48
     49static void registerInterface(void)
     50{
     51  PSYMBOL pNewSymbol=g_malloc0(sizeof(SYMBOL));
     52
     53  g_ptr_array_add(pInterfaceArray, (gpointer) pCurInterface);
     54
     55  /* Any found interface is registered as a new type so it can be
     56     used in other classes. */
     57  pNewSymbol->chrSymbolName=g_strdup(pCurInterface->chrName); /* We create a copy here because
     58                                                                 when cleaning up the symbol space
     59                                                                 the string will be freed. */
     60  pNewSymbol->uiKind=KIND_TYPESPEC;
     61  pNewSymbol->uiSymbolToken=IDL_SYMBOL_REGINTERFACE;
     62  g_tree_insert(parseInfo.pSymbolTree, pNewSymbol, pNewSymbol->chrSymbolName);
     63  g_scanner_scope_add_symbol(gScanner, ID_SCOPE, pNewSymbol->chrSymbolName,
     64                             pNewSymbol);
     65}
    4866
    4967static PINTERFACE createInterfaceStruct()
     
    178196  interface is already defined.
    179197
    180   IS:= G_TOKEN_INDENTIFIER IB2
     198  IS:= G_TOKEN_SYMBOL IB2
     199
     200  It's G_TOKEN_SYMBOL here because every found interface is registered
     201  as a new symbol with GScanner.
    181202 */
    182203static void parseSubclassedIFace()
    183204{
     205  PSYMBOL pCurSymbol;
    184206
    185207  /* Parent interface */
    186   if(!matchNext(G_TOKEN_IDENTIFIER))
    187     {
    188       g_scanner_unexp_token(gScanner,
    189                             G_TOKEN_IDENTIFIER,
    190                             NULL,
    191                             NULL,
    192                             NULL,
    193                             "Parent interface name is missing.",
     208  if(!matchNext(G_TOKEN_SYMBOL))
     209    {
     210      g_scanner_unexp_token(gScanner,
     211                            G_TOKEN_SYMBOL,
     212                            NULL,
     213                            NULL,
     214                            NULL,
     215                            "Parent interface name is missing or unknown.",
    194216                            TRUE); /* is_error */
    195217      exit(1);
    196218    }
    197219  GTokenValue value=gScanner->value;
    198   pCurInterface->chrParent=g_strdup(value.v_identifier);
     220  /* Make sure it's the correct symbol */
     221  pCurSymbol=value.v_symbol;
     222
     223  if(IDL_SYMBOL_REGINTERFACE!=pCurSymbol->uiSymbolToken)
     224    {
     225      g_scanner_unexp_token(gScanner,
     226                            G_TOKEN_SYMBOL,
     227                            NULL,
     228                            NULL,
     229                            NULL,
     230                            "Parent interface name is unknown.",
     231                            TRUE); /* is_error */
     232      exit(1);
     233    }
     234  pCurInterface->chrParent=g_strdup(pCurSymbol->chrSymbolName);
    199235
    200236  /* Check if the parent interface is known. */
     
    209245                          TRUE); /* is_error */
    210246    exit(1);
    211 
    212247  }
    213248
     
    238273  interface:= I ';'                       // Forward declaration
    239274            | I '{' IB '}'
    240             | I ':' G_TOKEN_INDENTIFIER '{' IB '}'
     275            | I ':' G_TOKEN_SYMBOL '{' IB '}'
     276
     277            It's G_TOKEN_SYMBOL here because every found interface is registered
     278            as a new symbol with GScanner.
    241279 */
    242280void parseInterface(GTokenType token)
     
    246284  /* Get the interface name */
    247285  parseIFace(token);
     286  /* It's save to register the interface right here even if the struct is almost empty.
     287     If anything goes wrong later we will exit anyway. */
     288  registerInterface(); 
    248289
    249290  if(matchNext(';'))
    250291    {
    251292      pCurInterface->fIsForwardDeclaration=TRUE;
    252       g_ptr_array_add(pInterfaceArray, (gpointer) pCurInterface);
    253 
    254293    }
    255294  else if(matchNext(':'))
    256295    {
    257296      parseSubclassedIFace();
    258       g_ptr_array_add(pInterfaceArray, (gpointer) pCurInterface);
    259297    }
    260298  else if(matchNext('{'))
    261299    {
    262300      parseIFaceBody();
    263       g_ptr_array_add(pInterfaceArray, (gpointer) pCurInterface);
    264301    }
    265302  else
  • trunk/idl-compiler/parser_c/lineinfo_parser.c

    r264 r266  
    4040
    4141extern GScanner *gScanner;
    42  
     42extern PARSEINFO parseInfo;
    4343
    4444/*
     
    5050{
    5151  GTokenValue value;
    52   PSYMBOLINFO psi=(PSYMBOLINFO)gScanner->user_data;
    5352
    5453  /* Line number */
    5554  value=gScanner->value;
    56   psi->uiLineCorrection=value.v_int;
     55  parseInfo.uiLineCorrection=g_scanner_cur_line(gScanner)-value.v_int+1;
    5756
    5857  if(!matchNext(G_TOKEN_STRING))
     
    7069
    7170  /* Current source file */
    72   if(psi->chrCurrentSourceFile)
    73     g_free(psi->chrCurrentSourceFile);
     71  if(parseInfo.chrCurrentSourceFile)
     72    g_free(parseInfo.chrCurrentSourceFile);
    7473
    7574  value=gScanner->value;
    76   psi->chrCurrentSourceFile=g_strdup(value.v_string);
     75  parseInfo.chrCurrentSourceFile=g_strdup(value.v_string);
    7776
    7877  /* Trailing file include level info isn't used for now. Note that for the root
Note: See TracChangeset for help on using the changeset viewer.