Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / misc / config_file / cf_lexical.lpp
blob0dbab00677294d02ef20a36365e89d477ca224d7
1 %{\r
2 \r
3 /* Includes */\r
4 \r
5 #include "nel/misc/debug.h"\r
6 #include "nel/misc/file.h"\r
7 #include "nel/misc/mem_stream.h"\r
8 \r
9 #include <vector>\r
10 #include <string>\r
13 using namespace std;\r
14 using namespace NLMISC;\r
16 /* Constantes */\r
18 // WARNING!!!! DEBUG_PRINTF are commented using // so IT MUST HAVE NO INSTRUCTION AFTER A DEBUG_PRINTF OR THEY LL BE COMMENTED\r
20 //#define DEBUG_PRINTF  InfoLog->displayRaw\r
21 #ifdef __GNUC__\r
22 #define DEBUG_PRINTF(format, args...)\r
23 #else // __GNUC__\r
24 #define DEBUG_PRINTF    // InfoLog->displayRaw\r
25 #endif // __GNUC__\r
27 #define YY_NEVER_INTERACTIVE 1\r
29 #ifdef WIN32\r
30 #define YY_NO_UNISTD_H 1\r
31 #include <io.h>\r
32 #define read _read\r
33 #define isatty _isatty\r
34 #endif\r
36 /* Types */\r
38 enum cf_type { T_UNKNOWN, T_INT, T_STRING, T_REAL };\r
40 struct cf_value\r
41 {\r
42         cf_type Type;\r
43         int             Int;\r
44         double  Real;\r
45         char    String[1024];\r
46 };\r
47 // use to parse the file, opened by CConfigFile::reparse()\r
48 CMemStream cf_ifile;\r
50 #define YY_INPUT(buf,result,max_size) { \\r
51         if (cf_ifile.length() == 0) \\r
52         { \\r
53                 DEBUG_PRINTF("YY_INPUT: eof");\\r
54                 result = YY_NULL; \\r
55         } else { \\r
56                 uint32 nbc = std::min((uint32)max_size, (uint32)(cf_ifile.length() - cf_ifile.getPos())); \\r
57                 DEBUG_PRINTF("YY_INPUT: wanted %d bytes, will read %d\n", max_size, nbc);\\r
58                 cf_ifile.serialBuffer ((uint8 *)buf, nbc); \\r
59                 result = nbc; \\r
60         } \\r
61 }\r
63 /* special include, need to know cf_value */\r
65 #include "cf_gramatical.h"\r
67 /* Externals */\r
69 extern int cf_CurrentLine;\r
71 /* Variables */\r
73 bool cf_Ignore;\r
75 void comment ();\r
77 %}\r
79 %option nounput prefix="cf"\r
80 %option 8bit full\r
81 %pointer\r
83 alpha           [A-Za-z]\r
84 digit           [0-9]\r
85 variable        ({alpha}|[_])({alpha}|{digit}|[_])*\r
86 num1            {digit}+\.([eE][-+]?{digit}+)?\r
87 num2            {digit}*\.{digit}+([eE][-+]?{digit}+)?\r
88 real            {num1}|{num2}\r
89 int                     {digit}+\r
90 hex                     0x[0-9a-fA-F]+\r
91 string          \"[^\"\n]*\"\r
93 %%\r
95 "+"                     { if (!cf_Ignore) return PLUS; }\r
96 "-"                     { if (!cf_Ignore) return MINUS; }\r
97 "*"                     { if (!cf_Ignore) return MULT; }\r
98 "/"                     { if (!cf_Ignore) return DIVIDE; }\r
99 ")"                     { if (!cf_Ignore) return RPAREN; }\r
100 "("                     { if (!cf_Ignore) return LPAREN; }\r
101 "="                     { if (!cf_Ignore) return ASSIGN; }\r
102 "+="            { if (!cf_Ignore) return ADD_ASSIGN; }\r
103 ";"                     { if (!cf_Ignore) return SEMICOLON; }\r
104 "}"                     { if (!cf_Ignore) return RBRACE; }\r
105 "{"                     { if (!cf_Ignore) return LBRACE; }\r
106 ","                     { if (!cf_Ignore) return COMMA; }\r
107 "#fileline"     { if (!cf_Ignore) return FILELINE; }\r
110 (\ |\t)         { /* ignore tabulation and spaces */; }\r
112 "\n"            {\r
113                                 /* ignore new line but count them */\r
114                                 cf_CurrentLine++;\r
115                                 DEBUG_PRINTF("*****line++ %d\n", cf_CurrentLine);\r
116                         }\r
118 "//"            { comment(); }\r
120 \/\*            { /* Start of a comment */ cf_Ignore = true; }\r
122 \*\/            { /* End of a comment */ cf_Ignore = false; }\r
124 {string}        { /* A string */\r
125                                 if (!cf_Ignore)\r
126                                 {\r
127                                         cflval.Val.Type = T_STRING;\r
128                                         if (strlen(yytext+1) >= sizeof(cflval.Val.String))\r
129                                         {\r
130                                                 strcpy (cflval.Val.String, "");\r
131                                                 DEBUG_PRINTF("lex: string '%s' exceeds max length\n", yytext);\r
132                                                 return STRING;\r
133                                         }\r
134                                         strcpy (cflval.Val.String, yytext+1);\r
135                                         cflval.Val.String[strlen(cflval.Val.String)-1] = '\0';\r
136                                         DEBUG_PRINTF("lex: string '%s' '%s'\n", yytext, cflval.Val.String);\r
137                                         return STRING;\r
138                                 }\r
139                         }\r
141 {variable}      { /* A variable */\r
142                                 if (!cf_Ignore)\r
143                                 {\r
144                                         cflval.Val.Type = T_STRING;\r
145                                         if (strlen(yytext+1) >= sizeof(cflval.Val.String))\r
146                                         {\r
147                                                 strcpy (cflval.Val.String, "");\r
148                                                 DEBUG_PRINTF("lex: string '%s' exceeds max length\n", yytext);\r
149                                                 return VARIABLE;\r
150                                         }\r
151                                         strcpy (cflval.Val.String, yytext);\r
152                                         DEBUG_PRINTF("lex: variable '%s' '%s'\n", yytext, cflval.Val.String);\r
153                                         return VARIABLE;\r
154                                 }\r
155                         }\r
157 {real}          { /* A real */\r
158                                 if (!cf_Ignore)\r
159                                 {\r
160                                         cflval.Val.Type = T_REAL;\r
161                                         cflval.Val.Real = atof (yytext);\r
162                                         DEBUG_PRINTF("lex: real '%s' '%f\n", yytext, cflval.Val.Real);\r
163                                         return REAL;\r
164                                 }\r
165                         }\r
167 {int}           { /* An int */\r
168                                 if (!cf_Ignore)\r
169                                 {\r
170                                         cflval.Val.Type = T_INT;\r
171                                         cflval.Val.Int = atoi (yytext);\r
172                                         DEBUG_PRINTF("lex: int '%s' '%d'\n", yytext, cflval.Val.Int);\r
173                                         return INTEGER;\r
174                                 }\r
175                         }\r
177 {hex}           { /* An hex int */\r
178                                 if (!cf_Ignore)\r
179                                 {\r
180                                         cflval.Val.Type = T_INT;\r
181                                         sscanf (yytext, "%x", &(cflval.Val.Int));\r
182                                         DEBUG_PRINTF("lex: hexa '%s' '0x%x' '%d'\n", yytext, cflval.Val.Int, cflval.Val.Int);\r
183                                         return INTEGER;\r
184                                 }\r
185                         }\r
187 %%\r
189 int cfwrap()\r
191         return 1;\r
194 //"//".*\n      { /* ignore one line comment but count the new line */ cf_CurrentLine++; DEBUG_PRINTF("*****line++ %d\n", cf_CurrentLine); }\r
195 void comment ()\r
197         int c;\r
199         do\r
200         {\r
201                 c = yyinput ();\r
202         }\r
203         while (c != '\n' && c != -1);\r
205         if (c == '\n')\r
206                 cf_CurrentLine++;\r