libnxsrv and nxcore moved to VS2005 (nxcore cannot be linked because libnxsl not...
[public/netxms.git] / src / snmp / nxmibc / parser.l
1 %option never-interactive
2
3 %a 14000
4 %p 12000
5 %e 2000
6 %n 1000
7 %o 20000
8
9 %x COMMENT
10 %x KEYWORD
11 %x IMPORTS
12 %x MACRO
13 %x MACRO_BODY
14 %x EXPORTS
15 WHITESPC [[:blank:]\t\n\r]
16
17 %{
18 /*
19 * We now build a lexical analyzer to be used by a higher-level parser.
20 */
21
22 /*
23 *
24 * This library is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Library General Public
26 * License as published by the Free Software Foundation; either
27 * version 2 of the License, or (at your option) any later version.
28 *
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Library General Public License for more details.
33 *
34 * You should have received a copy of the GNU Library General Public
35 * License along with this library; if not, write to the Free
36 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 *
38 * THIS SOFTWARE IS PROVIDED ``AS IS''
39 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
42 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
43 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48 * POSSIBILITY OF SUCH DAMAGE.
49 */
50
51 #define YY_NEVER_INTERACTIVE 1
52 #define YY_NO_UNISTD_H
53 #define ECHO
54
55 #include "mibparse.h"
56 #include "parser.tab.hpp"/* token codes from the parser */
57 #include <nms_util.h>
58
59 #if defined(_WIN32)
60 #pragma warning(disable: 4018)
61 #if defined(__64BIT__)
62 #pragma warning(disable: 4244 4267)
63 #endif
64 #endif
65
66 int mperror(char *pszMsg);
67
68 int g_nCurrLine;
69
70
71 //
72 // State stack
73 //
74
75 static int m_nStateStack[32];
76 static int m_nCurrStackPos;
77
78 void InitStateStack(void)
79 {
80 m_nCurrStackPos = 0;
81 }
82
83 static void PushState(int nState)
84 {
85 m_nStateStack[m_nCurrStackPos++] = YYSTATE;
86 BEGIN(nState);
87 }
88
89 static void PopState(void);
90
91
92 //
93 // Coint newlines in a text block
94 //
95
96 static void CountNewlines(char *pszStr, int len)
97 {
98 int i;
99
100 for(i = 0; i < len; i++)
101 if (pszStr[i] == '\n')
102 g_nCurrLine++;
103 }
104
105 %}
106
107
108 %%
109
110 <INITIAL,COMMENT,KEYWORD,IMPORTS,EXPORTS,MACRO,MACRO_BODY>{WHITESPC}+ {
111 int i;
112 char *ptr;
113
114 CountNewlines(mptext, mpleng);
115 for(i = 0, ptr = mptext; i < mpleng; i++, ptr++)
116 {
117 if (*ptr == '\n')
118 {
119 switch( YYSTATE )
120 {
121 case INITIAL:
122 case IMPORTS:
123 case EXPORTS:
124 case MACRO:
125 case MACRO_BODY:
126 break;
127 case COMMENT:
128 PopState();
129 break;
130 case KEYWORD:
131 /* Terminate the keyword state and revert to comment */
132 PopState();
133 /* Terminate the comment state and revert to previous */
134 PopState();
135 break;
136 default:
137 PopState();
138 break;
139 }
140 }
141 }
142 }
143
144 <INITIAL,COMMENT,KEYWORD,IMPORTS,EXPORTS>"--" {
145 switch(YYSTATE)
146 {
147 case INITIAL:
148 case IMPORTS:
149 case EXPORTS:
150 /* first one shifts us into comment mode */
151 PushState(COMMENT);
152 break;
153 case COMMENT:
154 PopState();
155 break;
156 case KEYWORD:
157 /* Ignore other states for now */
158 mperror("Cannot embed comments within keyword definition");
159 break;
160 default:
161 break;
162 }
163 }
164
165 <COMMENT>KEYWORD {
166 PushState(KEYWORD);
167 return KEYWORD_SYM;
168 }
169
170 <KEYWORD>VALUE return KEYWORD_VALUE_SYM;
171 <KEYWORD>BINDING return KEYWORD_BIND_SYM;
172
173 <INITIAL,IMPORTS,EXPORTS,KEYWORD>"," return COMMA_SYM;
174
175 <INITIAL,IMPORTS,EXPORTS>";" {
176 if ((YYSTATE == IMPORTS) || (YYSTATE == EXPORTS))
177 {
178 /* a semi colon terminates the import or export state */
179 PopState();
180 }
181 return SEMI_COLON_SYM;
182 }
183
184 <IMPORTS>FROM return FROM_SYM;
185
186 <INITIAL,KEYWORD>"{" return LEFT_BRACE_SYM;
187 <INITIAL,KEYWORD>"}" return RIGHT_BRACE_SYM;
188 <INITIAL>"[" return LEFT_BRACKET_SYM;
189 <INITIAL>"]" return RIGHT_BRACKET_SYM;
190 <INITIAL>"." return DOT_SYM;
191 <INITIAL>"|" return BAR_SYM;
192 <INITIAL>"(" return LEFT_PAREN_SYM;
193 <INITIAL>")" return RIGHT_PAREN_SYM;
194 <INITIAL>DEFINITIONS return DEFINITIONS_SYM;
195 <INITIAL,KEYWORD,MACRO>"::=" return ASSIGNMENT_SYM;
196 <INITIAL>FROM return FROM_SYM;
197 <INITIAL>IMPORTS {
198 BEGIN(IMPORTS);
199 return IMPORTS_SYM;
200 }
201 <INITIAL>EXPORTS {
202 BEGIN(EXPORTS);
203 return EXPORTS_SYM;
204 }
205 <INITIAL>REVISION return REVISION_SYM;
206 <INITIAL>ORGANIZATION return ORGANIZATION_SYM;
207 <INITIAL>CONTACT-INFO return CONTACT_SYM;
208 <INITIAL>LAST-UPDATED return UPDATE_SYM;
209 <INITIAL>DESCRIPTION return DESCRIPTION_SYM;
210 <INITIAL>MODULE-IDENTITY return MODULE_IDENTITY_SYM;
211 <INITIAL>MODULE-COMPLIANCE return MODULE_COMPLIANCE_SYM;
212 <INITIAL>OBJECTS return OBJECTS_SYM;
213 <INITIAL>GROUP return GROUP_SYM;
214 <INITIAL>AGENT[ |\t]*CAPABILITIES return AGENT_CAPABILITIES_SYM;
215 <INITIAL>OBJECT-TYPE return OBJECT_TYPE_SYM;
216 <INITIAL>OBJECT-GROUP return OBJECT_GROUP_SYM;
217 <INITIAL>OBJECT-IDENTITY return OBJECT_IDENTITY_SYM;
218 <INITIAL>MANDATORY-GROUPS return MANDATORY_GROUPS_SYM;
219 <INITIAL>NOTIFICATION-GROUP return NOTIFICATION_GROUP_SYM;
220 <INITIAL>NOTIFICATION-TYPE return NOTIFICATION_TYPE_SYM;
221 <INITIAL>INTEGER return INTEGER_SYM;
222 <INITIAL>Integer32 return INTEGER32_SYM;
223 <INITIAL>UInteger32 return UNSIGNED32_SYM;
224 <INITIAL>Unsigned32 return UNSIGNED32_SYM;
225 <INITIAL>Counter return COUNTER_SYM;
226 <INITIAL>Counter32 return COUNTER32_SYM;
227 <INITIAL>Counter64 return COUNTER64_SYM;
228 <INITIAL>Gauge32 return GAUGE32_SYM;
229 <INITIAL>TimeTicks return TIMETICKS_SYM;
230 <INITIAL>IpAddress return IP_ADDRESS_SYM;
231 <INITIAL>BITS return BITS_SYM;
232 <INITIAL>STRING return STRING_SYM;
233 <INITIAL>OCTET return OCTET_SYM;
234 <INITIAL>Opaque return OPAQUE_SYM;
235 <INITIAL>SEQUENCE return SEQUENCE_SYM;
236 <INITIAL>OF return OF_SYM;
237 <INITIAL>ACCESS return ACCESS_SYM;
238 <INITIAL>SYNTAX return SYNTAX_SYM;
239 <INITIAL>WRITE-SYNTAX return WRITE_SYNTAX_SYM;
240 <INITIAL>STATUS return STATUS_SYM;
241 <INITIAL>REFERENCE return REFERENCE_SYM;
242 <INITIAL>INDEX return INDEX_SYM;
243 <INITIAL>DEFVAL return DEFVAL_SYM;
244 <INITIAL>TEXTUAL-CONVENTION return TEXTUAL_CONVENTION_SYM;
245 <INITIAL>DISPLAY-HINT return DISPLAY_HINT_SYM;
246 <INITIAL>MAX-ACCESS return MAX_ACCESS_SYM;
247 <INITIAL>MIN-ACCESS return MIN_ACCESS_SYM;
248 <INITIAL>NOTIFICATIONS return NOTIFICATIONS_SYM;
249 <INITIAL>SIZE return SIZE_SYM;
250 <INITIAL>SUPPORTS return SUPPORTS_SYM;
251 <INITIAL>INCLUDES return INCLUDES_SYM;
252 <INITIAL>CHOICE return CHOICE_SYM;
253 <INITIAL>VARIATION return VARIATION_SYM;
254 <INITIAL>EXPLICIT return EXPLICIT_SYM;
255 <INITIAL>IMPLICIT return IMPLICIT_SYM;
256 <INITIAL>IMPLIED return IMPLIED_SYM;
257 <INITIAL>TAGS return TAGS_SYM;
258 <INITIAL>AUTOMATIC return AUTOMATIC_SYM;
259 <INITIAL>UNITS return UNITS_SYM;
260 <INITIAL>AUGMENTS return AUGMENTS_SYM;
261 <INITIAL>ENTERPRISE return ENTERPRISE_SYM;
262 <INITIAL>VARIABLES return VARIABLES_SYM;
263 <INITIAL>TRAP-TYPE return TRAP_TYPE_SYM;
264 <INITIAL>PRODUCT-RELEASE return PRODUCT_RELEASE_SYM;
265 <INITIAL>CREATION-REQUIRES return CREATION_REQUIRES_SYM;
266 <INITIAL>OBJECT[ ]*IDENTIFIER return OBJECT_IDENTIFIER_SYM;
267 <INITIAL>OBJECT return OBJECT_SYM;
268
269 <INITIAL>[A-Z][a-zA-Z0-9]*[-a-zA-Z0-9]*{WHITESPC}*MACRO {
270 /*
271 * This code initiates the macro state. There are two states associated
272 * with a macro definition. the MACRO state indicates that we have found
273 * a MACRO keyword preceeded by an identifier. In this state the assignment
274 * operator is processed normally (returning ASSIGNMENT_SYM) to allow the
275 * parser to detect the macro header (BEGIN_SYM).
276 * The BEGIN_SYM switches us into the MACRO_BODY state where all characters
277 * are processed as tokens (TOKEN_SYM returns a char *).
278 *
279 * The macro footer (END_SYM) switches the state back to INITIAL. This may
280 * be incorrect and it may be necessary to preserve and switch back to the
281 * preceeding state.
282 */
283 int length, tl, count, multiSpace;
284
285 tl = mpleng - 5; // Strip trailing MACRO
286 mplval.pszString = (char *)malloc(mpleng);
287 CountNewlines(mptext, mpleng);
288 for(length = 0, count = 0, multiSpace = 0; count < tl; count++)
289 {
290 switch(mptext[count])
291 {
292 case '\n':
293 case '\r':
294 case '\t':
295 case ' ':
296 if (multiSpace == 0)
297 {
298 mplval.pszString[length++] = ' ';
299 multiSpace = 1;
300 }
301 break;
302 default:
303 mplval.pszString[length++] = mptext[count];
304 multiSpace = 0;
305 break;
306 }
307 }
308 PushState(MACRO);
309 mplval.pszString[length] = 0;
310 StrStrip(mplval.pszString);
311 return MACRO_SYM;
312 }
313
314 <INITIAL,MACRO>BEGIN {
315 if (YYSTATE == MACRO)
316 PushState(MACRO_BODY);
317 return BEGIN_SYM;
318 }
319
320 <INITIAL,MACRO_BODY>END {
321 /*
322 * If an END symbol is encountered while in the MACRO_BODY state
323 * the state will be reverted to initial. To revert to the previous state
324 * it must be preserved in the MACRO detection and reassigned here
325 */
326 switch( YYSTATE )
327 {
328 case MACRO_BODY:
329 PopState(); /* should revert to MACRO and fall through */
330 case MACRO:
331 PopState(); /* should clear the MACRO state */
332 break;
333 default:
334 break;
335 }
336 return END_SYM;
337 }
338
339 <MACRO_BODY>([[:alnum:][:punct:]]*) {
340 return TOKEN_SYM;
341 }
342
343 <INITIAL>MODULE {
344 mplval.pszString = NULL;
345 return MODULE_SYM;
346 }
347
348 <INITIAL>MODULE[[:blank:]]+[A-Z][a-zA-Z0-9]*[-a-zA-Z0-9]* {
349 char *ptr = mptext;
350 int leng = mpleng;
351
352 ptr += 6; /* skip past "MODULE" */
353 leng -= 6; /* skip past "MODULE" */
354 CountNewlines(ptr, leng);
355 while(isspace(*ptr))
356 {
357 ptr++;
358 leng--;
359 }
360 mplval.pszString = strdup(ptr);
361 return MODULE_SYM;
362 }
363
364 <INITIAL,KEYWORD>\'[0-1]*\'[Bb] {
365 mplval.pszString = (char *)malloc(mpleng);
366 strncpy(mplval.pszString, mptext + 1, mpleng -1); // strip "'"s
367 mplval.pszString[mpleng - 3] = '\0';
368 return BSTRING_SYM;
369 }
370
371 <INITIAL,KEYWORD>\'[0-9A-Fa-f]*\'[Hh] {
372 mplval.pszString = (char*)malloc(mpleng);
373 strncpy(mplval.pszString, mptext + 1, mpleng - 1); // strip "'"s
374 mplval.pszString[mpleng - 3] = '\0';
375 if (strlen(mplval.pszString) <= 8)
376 {
377 mplval.number.value.nInt32 = strtoul(mplval.pszString, NULL, 16);
378 mplval.number.nType = 0;
379 return NUMBER_SYM;
380 }
381 return HSTRING_SYM;
382 }
383
384 <INITIAL,KEYWORD>\"([^\"]|"\"\"")*\" {
385 CountNewlines(mptext, mpleng);
386 mplval.pszString = (char*)malloc(mpleng);
387 strncpy(mplval.pszString, mptext + 1, mpleng - 1); // strip '"'s
388 mplval.pszString[mpleng - 2] = 0;
389 return CSTRING_SYM;
390 }
391
392 <INITIAL,IMPORTS,EXPORTS,KEYWORD>[A-Z][a-zA-Z0-9_\-\/]* {
393 mplval.pszString = strdup(mptext);
394 return UCASEFIRST_IDENT_SYM;
395 }
396
397 <INITIAL,IMPORTS,EXPORTS,KEYWORD>[a-z][a-zA-Z0-9_\-\/]* {
398 mplval.pszString = strdup(mptext);
399 return LCASEFIRST_IDENT_SYM;
400 }
401
402 <INITIAL>([-]|0x)?[1-9][0-9]* {
403 INT64 nInt64 = strtoull(mptext, NULL, 0);
404 LONG nInt32 = strtol(mptext, NULL, 0);
405
406 if ((INT64)nInt32 != nInt64)
407 {
408 mplval.number.nType = 1;
409 mplval.number.value.nInt64 = nInt64;
410 }
411 else
412 {
413 mplval.number.nType = 0;
414 mplval.number.value.nInt32 = nInt32;
415 }
416 return NUMBER_SYM;
417 }
418
419 <INITIAL>0 { /*allow zero as first digit on single digit #'s*/
420 mplval.number.nType = 0;
421 mplval.number.value.nInt32 = 0;
422 return NUMBER_SYM;
423 }
424
425 <INITIAL,COMMENT,KEYWORD,EXPORTS>. {
426 CountNewlines(mptext, mpleng);
427 }
428
429 %%
430
431
432 static void PopState(void)
433 {
434 if (m_nCurrStackPos > 0)
435 {
436 BEGIN(m_nStateStack[--m_nCurrStackPos]);
437 }
438 else
439 {
440 BEGIN(INITIAL);
441 }
442 }