Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a convenient function
[simgrid.git] / src / surf / surf_parse.l
index 0d10054..9111248 100644 (file)
@@ -1,7 +1,9 @@
-/* Authors: Arnaud Legrand                                                  */
+/*     $Id$     */
+
+/* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
 
 /* This program is free software; you can redistribute it and/or modify it
  under the terms of the license (GNU LGPL) which comes with this package. */
* under the terms of the license (GNU LGPL) which comes with this package. */
 
 %option noyywrap
 %{
 #include "xbt/log.h"
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(parse, surf ,"Logging specific to the SURF  module");
 
-  YY_BUFFER_STATE input_buffer;
-  FILE *file_to_parse;
+  YY_BUFFER_STATE surf_input_buffer;
+  FILE *surf_file_to_parse;
 
-  int line_pos = 1;
-  int char_pos = 0;
-  int tok_num = 0;
+  int surf_line_pos = 1;
+  int surf_char_pos = 0;
+  int surf_tok_num = 0;
 %}
 
 %x comment foo str
@@ -42,16 +44,16 @@ digit           [0-9]
 
 <comment>[^*\n]*        /* eat anything that's not a '*' */
 <comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
-<comment>\n             {++line_pos;char_pos=0;}
+<comment>\n             {++surf_line_pos;surf_char_pos=0;}
 <comment>"*"+"/"        BEGIN(comment_caller);
 
-\"      string_buf_ptr = string_buf; char_pos++; BEGIN(str);
+\"      string_buf_ptr = string_buf; surf_char_pos++; BEGIN(str);
 
 <str>\"        { /* saw closing quote - all done */
         BEGIN(INITIAL);
         *string_buf_ptr = '\0';
        surf_parse_text=string_buf;
-       char_pos++;
+       surf_char_pos++;
        return TOKEN_WORD;
         /* return string constant token type and
          * value to parser
@@ -73,7 +75,7 @@ digit           [0-9]
                 /* error, constant is out-of-bounds */
 
         *string_buf_ptr++ = result;
-       char_pos++;
+       surf_char_pos++;
         }
 
 <str>\\[0-9]+ {
@@ -82,16 +84,16 @@ digit           [0-9]
          */
         }
 
-<str>\\n  {*string_buf_ptr++ = '\n';   char_pos++;}
-<str>\\t  {*string_buf_ptr++ = '\t';   char_pos++;}
-<str>\\r  {*string_buf_ptr++ = '\r';   char_pos++;}
-<str>\\b  {*string_buf_ptr++ = '\b';   char_pos++;}
-<str>\\f  {*string_buf_ptr++ = '\f';   char_pos++;}
+<str>\\n  {*string_buf_ptr++ = '\n';   surf_char_pos++;}
+<str>\\t  {*string_buf_ptr++ = '\t';   surf_char_pos++;}
+<str>\\r  {*string_buf_ptr++ = '\r';   surf_char_pos++;}
+<str>\\b  {*string_buf_ptr++ = '\b';   surf_char_pos++;}
+<str>\\f  {*string_buf_ptr++ = '\f';   surf_char_pos++;}
 
 <str>\\(.|\n)  {*string_buf_ptr++ = surf_parse_text[1];        
                 if(surf_parse_text[1]=='\n') {
-                 ++line_pos;char_pos=0;
-               } else { char_pos++;}
+                 ++surf_line_pos;surf_char_pos=0;
+               } else { surf_char_pos++;}
                }
 
 <str>[^\\\n\"]+        {
@@ -99,22 +101,22 @@ digit           [0-9]
 
         while ( *yptr )
          *string_buf_ptr++ = *yptr++;
-          char_pos++;
+          surf_char_pos++;
         }
 
-({letter}|{digit})*     { char_pos+= strlen(surf_parse_text); return(TOKEN_WORD);}
-"("                     { char_pos++; return(TOKEN_LP);}
-")"                     { char_pos++;return(TOKEN_RP);}
-"</"                    { char_pos+=2; return(TOKEN_END_SECTION);}
-"<"                     { char_pos++; return(TOKEN_BEGIN_SECTION);}
-">"                     { char_pos++;return(TOKEN_CLOSURE);}
-"\n"                    { line_pos++; char_pos=0; return(TOKEN_NEWLINE);}
-. { char_pos++;}
+({letter}|{digit})*     { surf_char_pos+= strlen(surf_parse_text); return(TOKEN_WORD);}
+"("                     { surf_char_pos++; return(TOKEN_LP);}
+")"                     { surf_char_pos++;return(TOKEN_RP);}
+"</"                    { surf_char_pos+=2; return(TOKEN_END_SECTION);}
+"<"                     { surf_char_pos++; return(TOKEN_BEGIN_SECTION);}
+">"                     { surf_char_pos++;return(TOKEN_CLOSURE);}
+"\n"                    { surf_line_pos++; surf_char_pos=0; return(TOKEN_NEWLINE);}
+. { surf_char_pos++;}
 %%
 /* {space}+                { return(TOKEN_SPACE);} */
 
 static void __print_val(void) {
-  switch(tok_num) {
+  switch(surf_tok_num) {
   case TOKEN_LP      : {printf("TOKEN_LP ");break;}
   case TOKEN_RP      : {printf("TOKEN_RP ");break;}
   case TOKEN_BEGIN_SECTION : {printf("TOKEN_BEGIN_SECTION ");break;}
@@ -123,43 +125,130 @@ static void __print_val(void) {
   case TOKEN_WORD    : {printf("TOKEN_WORD ");break;}
   case TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
   case TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
-  default             : {printf("Unknown token %d\n", tok_num);return;}
+  default             : {printf("Unknown token %d\n", surf_tok_num);return;}
   }
 /*   if(strcmp(surf_parse_text,"")!=0) */
-    printf("-->%s<-- [line %d, pos %d]\n",surf_parse_text,line_pos,char_pos);
+    printf("-->%s<-- [line %d, pos %d]\n",surf_parse_text,surf_line_pos,surf_char_pos);
 /*   else */
-/*     printf("--><-- [line %d, pos %d]\n",line_pos,char_pos); */
+/*     printf("--><-- [line %d, pos %d]\n",surf_line_pos,surf_char_pos); */
 
   return;
 }
 
 e_surf_token_t surf_parse(void) {
-  tok_num = surf_parse_lex();
-  __print_val();
-  char_pos += strlen(surf_parse_text);
-  return(tok_num);
+  surf_tok_num = surf_parse_lex();
+/*   __print_val(); */
+  surf_char_pos += strlen(surf_parse_text);
+  return(surf_tok_num);
+}
+
+void find_section(const char* file, const char* section_name)
+{
+  e_surf_token_t token;
+  int found = 0;
+
+  surf_parse_open(file);
+
+  while((token=surf_parse())) {
+    if(token!=TOKEN_BEGIN_SECTION) continue;
+
+    token=surf_parse();
+    xbt_assert1((token==TOKEN_WORD),"Parse error line %d",surf_line_pos);
+    if(strcmp(surf_parse_text,section_name)==0) found=1;
+
+    token=surf_parse();
+    xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",surf_line_pos);
+
+    if(found) return;
+  }
+
+  CRITICAL2("Could not find %s section in %s\n",section_name,file);
+  xbt_abort();
+}
+
+void close_section(const char* section_name)
+{
+  e_surf_token_t token;
+
+  token=surf_parse();
+  xbt_assert1((token==TOKEN_WORD),"Parse error line %d",surf_line_pos);
+  xbt_assert1((strcmp(surf_parse_text,section_name)==0), 
+             "Closing section does not match the opening one (%s).", 
+             section_name);
+  
+  token=surf_parse();
+  xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",surf_line_pos);
+
+  surf_parse_close();
 }
 
 void  surf_parse_open(const char *file) {
-  file_to_parse = fopen(file,"r");
-  xbt_assert1((file_to_parse), "Unable to open \"%s\"\n",file)
-  input_buffer = surf_parse__create_buffer( file_to_parse, 10 );
-  surf_parse__switch_to_buffer(input_buffer);
-
-  line_pos = 1;
-  char_pos = 0;
-  tok_num = 0;
+  surf_file_to_parse = fopen(file,"r");
+  xbt_assert1((surf_file_to_parse), "Unable to open \"%s\"\n",file);
+  surf_input_buffer = surf_parse__create_buffer( surf_file_to_parse, 10 );
+  surf_parse__switch_to_buffer(surf_input_buffer);
+
+  surf_line_pos = 1;
+  surf_char_pos = 0;
+  surf_tok_num = 0;
 }
 
 void  surf_parse_close(void) {
-  surf_parse__delete_buffer(input_buffer);
-  fclose(file_to_parse);
+  surf_parse__delete_buffer(surf_input_buffer);
+  fclose(surf_file_to_parse);
 
-  line_pos = 1;
-  char_pos = 0;
-  tok_num = 0;
+  surf_line_pos = 1;
+  surf_char_pos = 0;
+  surf_tok_num = 0;
 }
 
+void surf_parse_double(double *value)
+{ 
+  e_surf_token_t token;
+  int ret = 0;
+
+  token = surf_parse();                /* power_scale */
+  xbt_assert1((token == TOKEN_WORD), "Parse error line %d", surf_line_pos);
+  ret = sscanf(surf_parse_text, "%lg", value);
+  xbt_assert2((ret==1), "Parse error line %d : %s not a number", surf_line_pos,
+             surf_parse_text);
+}
+
+void surf_parse_trace(tmgr_trace_t *trace)
+{
+  e_surf_token_t token;
+  
+  token = surf_parse();                /* power_trace */
+  xbt_assert1((token == TOKEN_WORD), "Parse error line %d", surf_line_pos);
+  if (strcmp(surf_parse_text, "") == 0)
+    *trace = NULL;
+  else
+    *trace = tmgr_trace_new(surf_parse_text);
+}
+
+void surf_parse_deployment_line(char **host, int *argc, char ***argv)
+{
+  e_surf_token_t token;
+
+
+  /* Parse Host name */
+  *host = xbt_strdup(surf_parse_text);  
+
+  *argc = 0;
+  *argv = NULL;
+
+  /* Parse command line */
+  while((token = surf_parse())) {
+    if(token == TOKEN_NEWLINE) return; 
+    xbt_assert1((token == TOKEN_WORD), "Parse error line %d", surf_line_pos);
+
+    (*argc)++;
+    *argv=xbt_realloc (*argv, (*argc) * sizeof(char*));
+    (*argv)[(*argc)-1]=xbt_strdup(surf_parse_text);
+  }
+}
+
+
 /*  Local variables: */
 /*  mode: c */
 /*  End: */