Statistics
| Branch: | Revision:

root / src / nedxml / ned2.lex @ 68da4f12

History | View | Annotate | Download (9.37 KB)

1 01873262 Georg Kunz
/*==================================================
2
 * File: ned2.lex
3
 *
4
 *  Lexical analyser for OMNeT++ NED-2.
5
 *
6
 *  Author: Andras Varga
7
 *
8
 ==================================================*/
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992,2005 Andras Varga
12
13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
18
/*
19
 * NED-2 reserved words:
20
 *    non-component: import package property
21
 *    component:     module simple network channel interface
22
 *    inheritance:   extends like
23
 *    sections:      types parameters gates submodules connections allowunconnected
24
 *    param types:   double int string bool xml volatile
25
 *    gate types:    input output inout
26
 *    conditional:   if
27
 *    connections:   --> <-- <--> while ..
28
 *    expressions:   true false default const sizeof index xmldoc
29
 */
30
31
D  [0-9]
32
L  [a-zA-Z_]
33
X  [0-9a-fA-F]
34
E  [Ee][+-]?{D}+
35
S  [ \t\v\n\r\f]
36
37
%x cplusplusbody
38
%x stringliteral
39
40
/* the following option keeps isatty() out */
41
%option never-interactive
42
43
%{
44
#include <string.h>
45
#include "nedyydefs.h"
46
#include "nedexception.h"
47
#include "ned2.tab.hh"
48
49
#define yylloc ned2yylloc
50
extern YYSTYPE yylval;
51
extern YYLTYPE yylloc;
52
53
// wrap symbols to allow several .lex files coexist
54
#define comment     ned2comment
55
#define countChars  ned2count
56
#define extendCount ned2extendCount
57
58
void comment();
59
void countChars();
60
void extendCount();
61
62
#define TEXTBUF_LEN 1024
63
static char textbuf[TEXTBUF_LEN];
64
65
USING_NAMESPACE
66
67
%}
68
69
%%
70
"@expr@"                 { return EXPRESSION_SELECTOR; /* must agree with nedparser.cc */ }
71
72
"//"                     { comment(); }
73
74
"import"                 { countChars(); return IMPORT; }
75
"package"                { countChars(); return PACKAGE; }
76
"property"               { countChars(); return PROPERTY; }
77
78
"module"                 { countChars(); return MODULE; }
79
"simple"                 { countChars(); return SIMPLE; }
80
"network"                { countChars(); return NETWORK; }
81
"channel"                { countChars(); return CHANNEL; }
82
"moduleinterface"        { countChars(); return MODULEINTERFACE; }
83
"channelinterface"       { countChars(); return CHANNELINTERFACE; }
84
85
"extends"                { countChars(); return EXTENDS; }
86
"like"                   { countChars(); return LIKE; }
87
88
"types"                  { countChars(); return TYPES; }
89
"parameters"             { countChars(); return PARAMETERS; }
90
"gates"                  { countChars(); return GATES; }
91
"submodules"             { countChars(); return SUBMODULES; }
92
"connections"            { countChars(); return CONNECTIONS; }
93
"allowunconnected"       { countChars(); return ALLOWUNCONNECTED; }
94
95
"double"                 { countChars(); return DOUBLETYPE; }
96
"int"                    { countChars(); return INTTYPE; }
97
"string"                 { countChars(); return STRINGTYPE; }
98
"bool"                   { countChars(); return BOOLTYPE; }
99
"xml"                    { countChars(); return XMLTYPE; }
100
"volatile"               { countChars(); return VOLATILE; }
101
102
"input"                  { countChars(); return INPUT_; }
103
"output"                 { countChars(); return OUTPUT_; }
104
"inout"                  { countChars(); return INOUT_; }
105
106
"if"                     { countChars(); return IF; }
107
"for"                    { countChars(); return FOR; }
108
109
"-->"                    { countChars(); return RIGHTARROW; }
110
"<--"                    { countChars(); return LEFTARROW; }
111
"<-->"                   { countChars(); return DBLARROW; }
112
".."                     { countChars(); return TO; }
113
114
"true"                   { countChars(); return TRUE_; }
115
"false"                  { countChars(); return FALSE_; }
116
"this"                   { countChars(); return THIS_; }
117
"default"                { countChars(); return DEFAULT; }
118
"ask"                    { countChars(); return ASK; }
119
"const"                  { countChars(); return CONST_; }
120
"sizeof"                 { countChars(); return SIZEOF; }
121
"index"                  { countChars(); return INDEX_; }
122
"xmldoc"                 { countChars(); return XMLDOC; }
123
124
{L}({L}|{D})*            { countChars(); return NAME; }
125
{D}+                     { countChars(); return INTCONSTANT; }
126
0[xX]{X}+                { countChars(); return INTCONSTANT; }
127
{D}+{E}                  { countChars(); return REALCONSTANT; }
128
{D}*"."{D}+({E})?        { countChars(); return REALCONSTANT; }
129
130
\"                       { countChars(); BEGIN(stringliteral); }
131
<stringliteral>{
132
      \n                 { BEGIN(INITIAL); throw NEDException("unterminated string literal (append backslash to line for multi-line strings)"); /* NOTE: BEGIN(INITIAL) is important, otherwise parsing of the next file (!) will start from the <stringliteral> state! */ }
133
      \\\n               { extendCount(); /* line continuation */ }
134
      \\\"               { extendCount(); /* qouted quote */ }
135
      \\[^\n\"]          { extendCount(); /* qouted char */ }
136
      [^\\\n\"]+         { extendCount(); /* character inside string literal */ }
137
      \"                 { extendCount(); BEGIN(INITIAL); return STRINGCONSTANT; /* closing quote */ }
138
}
139
140
"**"                     { countChars(); return DOUBLEASTERISK; }
141
"++"                     { countChars(); return PLUSPLUS; }
142
143
"$"                      { countChars(); return '$'; }
144
"@"                      { countChars(); return '@'; }
145
";"                      { countChars(); return ';'; }
146
","                      { countChars(); return ','; }
147
":"                      { countChars(); return ':'; }
148
"="                      { countChars(); return '='; }
149
"("                      { countChars(); return '('; }
150
")"                      { countChars(); return ')'; }
151
"["                      { countChars(); return '['; }
152
"]"                      { countChars(); return ']'; }
153
"{"                      { countChars(); return '{'; }
154
"}"                      { countChars(); return '}'; }
155
"."                      { countChars(); return '.'; }
156
"?"                      { countChars(); return '?'; }
157
158
"||"                     { countChars(); return OR; }
159
"&&"                     { countChars(); return AND; }
160
"##"                     { countChars(); return XOR; }
161
"!"                      { countChars(); return NOT; }
162
163
"|"                      { countChars(); return BIN_OR; }
164
"&"                      { countChars(); return BIN_AND; }
165
"#"                      { countChars(); return BIN_XOR; }
166
"~"                      { countChars(); return BIN_COMPL; }
167
"<<"                     { countChars(); return SHIFT_LEFT; }
168
">>"                     { countChars(); return SHIFT_RIGHT; }
169
170
"^"                      { countChars(); return '^'; }
171
"+"                      { countChars(); return '+'; }
172
"-"                      { countChars(); return '-'; }
173
"*"                      { countChars(); return '*'; }
174
"/"                      { countChars(); return '/'; }
175
"%"                      { countChars(); return '%'; }
176
"<"                      { countChars(); return '<'; }
177
">"                      { countChars(); return '>'; }
178
179
"=="                     { countChars(); return EQ; }
180
"!="                     { countChars(); return NE; }
181
"<="                     { countChars(); return LE; }
182
">="                     { countChars(); return GE; }
183
184
"\xEF\xBB\xBF"           { /* UTF-8 BOM mark, ignore */ }
185
{S}                      { countChars(); }
186
.                        { countChars(); return INVALID_CHAR; }
187
188
%%
189
190
int yywrap()
191
{
192
     return 1;
193
}
194
195
/*
196
 * - discards all remaining characters of a line of
197
 *   text from the inputstream.
198
 * - the characters are read with the input() and
199
 *   unput() functions.
200
 * - input() is sometimes called yyinput()...
201
 */
202
#ifdef __cplusplus
203
#define input  yyinput
204
#endif
205
206
/* the following #define is needed for broken flex versions */
207
#define yytext_ptr yytext
208
209
void comment()
210
{
211
    int c;
212
    while ((c = input())!='\n' && c!=0 && c!=EOF);
213
    if (c=='\n') unput(c);
214
}
215
216
/*
217
 * - counts the line and column number of the current token in `pos'
218
 * - keeps a record of the complete current line in `textbuf[]'
219
 * - yytext[] is the current token passed by (f)lex
220
 */
221
static void _count(bool updateprevpos)
222
{
223
    static int textbuflen;
224
    int i;
225
226
    /* printf("DBG: countChars(): prev=%d,%d  pos=%d,%d yytext=>>%s<<\n",
227
           prevpos.li, prevpos.co, pos.li, pos.co, yytext);
228
    */
229
230
    /* init textbuf */
231
    if (pos.li==1 && pos.co==0) {
232
        textbuf[0]='\0'; textbuflen=0;
233
    }
234
235
    if (updateprevpos) {
236
        prevpos = pos;
237
    }
238
    for (i = 0; yytext[i] != '\0'; i++) {
239
        if (yytext[i] == '\n') {
240
            pos.co = 0;
241
            pos.li++;
242
            textbuflen=0; textbuf[0]='\0';
243
        } else if (yytext[i] == '\t')
244
            pos.co += 8 - (pos.co % 8);
245
        else
246
            pos.co++;
247
        if (yytext[i] != '\n') {
248
            if (textbuflen < TEXTBUF_LEN-5) {
249
                textbuf[textbuflen++]=yytext[i]; textbuf[textbuflen]='\0';
250
            }
251
            else if (textbuflen == TEXTBUF_LEN-5) {
252
                strcpy(textbuf+textbuflen, "...");
253
                textbuflen++;
254
            } else {
255
                /* line too long -- ignore */
256
            }
257
        }
258
    }
259
    /* printf("li=%d co=%d\n", pos.li, pos.co); good for debugging... */
260
    yylloc.first_line   = prevpos.li;
261
    yylloc.first_column = prevpos.co;
262
    yylloc.last_line    = pos.li;
263
    yylloc.last_column  = pos.co;
264
}
265
266
void countChars()
267
{
268
    _count(true);
269
}
270
271
void extendCount()
272
{
273
    _count(false);
274
}
275