1 /* Authors: Arnaud Legrand */
3 /* This program is free software; you can redistribute it and/or modify it
4 under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/sysdep.h"
9 #include"surf/surf_parse.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(parse, surf ,"Logging specific to the SURF module");
13 YY_BUFFER_STATE input_buffer;
29 char string_buf[MAX_STR_CONST];
30 char *string_buf_ptr = NULL;
34 comment_caller = INITIAL;
43 <comment>[^*\n]* /* eat anything that's not a '*' */
44 <comment>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */
45 <comment>\n {++line_pos;char_pos=0;}
46 <comment>"*"+"/" BEGIN(comment_caller);
48 \" string_buf_ptr = string_buf; char_pos++; BEGIN(str);
50 <str>\" { /* saw closing quote - all done */
52 *string_buf_ptr = '\0';
53 surf_parse_text=string_buf;
56 /* return string constant token type and
62 /* error - unterminated string constant */
63 /* generate error message */
67 /* octal escape sequence */
70 (void) sscanf( surf_parse_text + 1, "%o", &result );
73 /* error, constant is out-of-bounds */
75 *string_buf_ptr++ = result;
80 /* generate error - bad escape sequence; something
81 * like '\48' or '\0777777'
85 <str>\\n {*string_buf_ptr++ = '\n'; char_pos++;}
86 <str>\\t {*string_buf_ptr++ = '\t'; char_pos++;}
87 <str>\\r {*string_buf_ptr++ = '\r'; char_pos++;}
88 <str>\\b {*string_buf_ptr++ = '\b'; char_pos++;}
89 <str>\\f {*string_buf_ptr++ = '\f'; char_pos++;}
91 <str>\\(.|\n) {*string_buf_ptr++ = surf_parse_text[1];
92 if(surf_parse_text[1]=='\n') {
93 ++line_pos;char_pos=0;
98 char *yptr = surf_parse_text;
101 *string_buf_ptr++ = *yptr++;
105 ({letter}|{digit})* { char_pos+= strlen(surf_parse_text); return(TOKEN_WORD);}
106 "(" { char_pos++; return(TOKEN_LP);}
107 ")" { char_pos++;return(TOKEN_RP);}
108 "</" { char_pos+=2; return(TOKEN_END_SECTION);}
109 "<" { char_pos++; return(TOKEN_BEGIN_SECTION);}
110 ">" { char_pos++;return(TOKEN_CLOSURE);}
111 "\n" { line_pos++; char_pos=0; return(TOKEN_NEWLINE);}
114 /* {space}+ { return(TOKEN_SPACE);} */
116 static void __print_val(void) {
118 case TOKEN_LP : {printf("TOKEN_LP ");break;}
119 case TOKEN_RP : {printf("TOKEN_RP ");break;}
120 case TOKEN_BEGIN_SECTION : {printf("TOKEN_BEGIN_SECTION ");break;}
121 case TOKEN_END_SECTION : {printf("TOKEN_END_SECTION ");break;}
122 case TOKEN_CLOSURE : {printf("TOKEN_CLOSURE ");break;}
123 case TOKEN_WORD : {printf("TOKEN_WORD ");break;}
124 case TOKEN_NEWLINE : {printf("TOKEN_NEWLINE\n");return;}
125 case TOKEN_EMPTY : {printf("TOKEN_EMPTY\n");return;}
126 default : {printf("Unknown token %d\n", tok_num);return;}
128 /* if(strcmp(surf_parse_text,"")!=0) */
129 printf("-->%s<-- [line %d, pos %d]\n",surf_parse_text,line_pos,char_pos);
131 /* printf("--><-- [line %d, pos %d]\n",line_pos,char_pos); */
136 e_surf_token_t surf_parse(void) {
137 tok_num = surf_parse_lex();
139 char_pos += strlen(surf_parse_text);
143 void find_section(const char* file, const char* section_name)
145 e_surf_token_t token;
148 surf_parse_open(file);
150 while((token=surf_parse())) {
151 if(token!=TOKEN_BEGIN_SECTION) continue;
154 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
155 if(strcmp(surf_parse_text,section_name)==0) found=1;
158 xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
163 CRITICAL2("Could not find %s section in %s\n",section_name,file);
167 void close_section(const char* section_name)
169 e_surf_token_t token;
172 xbt_assert1((token==TOKEN_WORD),"Parse error line %d",line_pos);
173 xbt_assert1((strcmp(surf_parse_text,section_name)==0),
174 "Closing section does not match the opening one (%s).",
178 xbt_assert1((token==TOKEN_CLOSURE),"Parse error line %d",line_pos);
183 void surf_parse_open(const char *file) {
184 file_to_parse = fopen(file,"r");
185 xbt_assert1((file_to_parse), "Unable to open \"%s\"\n",file)
186 input_buffer = surf_parse__create_buffer( file_to_parse, 10 );
187 surf_parse__switch_to_buffer(input_buffer);
194 void surf_parse_close(void) {
195 surf_parse__delete_buffer(input_buffer);
196 fclose(file_to_parse);
204 /* Local variables: */