Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (8.15 KB)

1
/*==================================================
2
 * File: msg2.lex
3
 *
4
 *  Lexical analyser for OMNeT++ MSG-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
 * MSG-2 reserved words:
20
 *    non-component:   cplusplus
21
 *    component:       struct message class noncobject enum
22
 *    inheritance:     extends
23
 *    field types:     char short int long double unsigned
24
 *    field modifiers: abstract readonly
25
 */
26

    
27
D  [0-9]
28
L  [a-zA-Z_]
29
X  [0-9a-fA-F]
30
E  [Ee][+-]?{D}+
31
S  [ \t\v\n\r\f]
32

    
33
%x cplusplusbody
34
%x stringliteral
35

    
36
/* the following option keeps isatty() out */
37
%option never-interactive
38

    
39
%{
40
#include <string.h>
41
#include "nedyydefs.h"
42
#include "nedexception.h"
43
#include "msg2.tab.hh"
44

    
45
#define yylloc msg2yylloc
46
extern YYSTYPE yylval;
47
extern YYLTYPE yylloc;
48

    
49
// wrap symbols to allow several .lex files coexist
50
#define comment     msgcomment
51
#define countChars  msgcount
52
#define extendCount msgextendCount
53

    
54
void comment();
55
void countChars();
56
void extendCount();
57

    
58
#define TEXTBUF_LEN 1024
59
static char textbuf[TEXTBUF_LEN];
60

    
61
USING_NAMESPACE
62

    
63
%}
64

    
65
%%
66
"//"                    { comment(); }
67

    
68
"namespace"             { countChars(); return NAMESPACE; }
69
"cplusplus"             { countChars(); return CPLUSPLUS; }
70
"struct"                { countChars(); return STRUCT; }
71
"message"               { countChars(); return MESSAGE; }
72
"packet"                { countChars(); return PACKET; }
73
"class"                 { countChars(); return CLASS; }
74
"noncobject"            { countChars(); return NONCOBJECT; }
75
"enum"                  { countChars(); return ENUM; }
76
"extends"               { countChars(); return EXTENDS; }
77
"abstract"              { countChars(); return ABSTRACT; }
78
"readonly"              { countChars(); return READONLY; }
79
"properties"            { countChars(); return PROPERTIES; }
80
"fields"                { countChars(); return FIELDS; }
81

    
82
"bool"                  { countChars(); return BOOLTYPE; }
83
"char"                  { countChars(); return CHARTYPE; }
84
"short"                 { countChars(); return SHORTTYPE; }
85
"int"                   { countChars(); return INTTYPE; }
86
"long"                  { countChars(); return LONGTYPE; }
87
"double"                { countChars(); return DOUBLETYPE; }
88
"unsigned"              { countChars(); return UNSIGNED_; }
89
"string"                { countChars(); return STRINGTYPE; }
90
"true"                  { countChars(); return TRUE_; }
91
"false"                 { countChars(); return FALSE_; }
92

    
93
{L}({L}|{D})*           { countChars(); return NAME; }
94
{D}+                    { countChars(); return INTCONSTANT; }
95
0[xX]{X}+               { countChars(); return INTCONSTANT; }
96
{D}+{E}                 { countChars(); return REALCONSTANT; }
97
{D}*"."{D}+({E})?       { countChars(); return REALCONSTANT; }
98
\'[^\']\'               { countChars(); return CHARCONSTANT; }
99

    
100
\"                      { countChars(); BEGIN(stringliteral); }
101
<stringliteral>{
102
      \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! */ }
103
      \\\n              { extendCount(); /* line continuation */ }
104
      \\\"              { extendCount(); /* qouted quote */ }
105
      \\[^\n\"]         { extendCount(); /* qouted char */ }
106
      [^\\\n\"]+        { extendCount(); /* character inside string literal */ }
107
      \"                { extendCount(); BEGIN(INITIAL); return STRINGCONSTANT; /* closing quote */ }
108
}
109

    
110
"{{"                    { countChars(); BEGIN(cplusplusbody); }
111
<cplusplusbody>"}}"     { extendCount(); BEGIN(INITIAL); return CPLUSPLUSBODY; }
112
<cplusplusbody>{S}      { extendCount(); }
113
<cplusplusbody>.        { extendCount(); }
114

    
115
";"                     { countChars(); return ';'; }
116
","                     { countChars(); return ','; }
117
":"                     { countChars(); return ':'; }
118
"="                     { countChars(); return '='; }
119
"("                     { countChars(); return '('; }
120
")"                     { countChars(); return ')'; }
121
"["                     { countChars(); return '['; }
122
"]"                     { countChars(); return ']'; }
123
"{"                     { countChars(); return '{'; }
124
"}"                     { countChars(); return '}'; }
125
"."                     { countChars(); return '.'; }
126
"?"                     { countChars(); return '?'; }
127
"@"                     { countChars(); return '@'; }
128

    
129
   /* XXX are the next ones really needed? */
130

    
131
"::"                    { countChars(); return DOUBLECOLON; }
132
"||"                    { countChars(); return OR; }
133
"&&"                    { countChars(); return AND; }
134
"##"                    { countChars(); return XOR; }
135
"!"                     { countChars(); return NOT; }
136

    
137
"|"                     { countChars(); return BIN_OR; }
138
"&"                     { countChars(); return BIN_AND; }
139
"#"                     { countChars(); return BIN_XOR; }
140
"~"                     { countChars(); return BIN_COMPL; }
141
"<<"                    { countChars(); return SHIFT_LEFT; }
142
">>"                    { countChars(); return SHIFT_RIGHT; }
143

    
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

    
153
"=="                    { countChars(); return EQ; }
154
"!="                    { countChars(); return NE; }
155
"<="                    { countChars(); return LE; }
156
">="                    { countChars(); return GE; }
157

    
158
"\xEF\xBB\xBF"          { /* UTF-8 BOM mark, ignore */ }
159
{S}                     { countChars(); }
160
.                       { countChars(); return INVALID_CHAR; }
161

    
162
%%
163

    
164
int yywrap()
165
{
166
     return 1;
167
}
168

    
169
/*
170
 * - discards all remaining characters of a line of
171
 *   text from the inputstream.
172
 * - the characters are read with the input() and
173
 *   unput() functions.
174
 * - input() is sometimes called yyinput()...
175
 */
176
#ifdef __cplusplus
177
#define input  yyinput
178
#endif
179

    
180
/* the following #define is needed for broken flex versions */
181
#define yytext_ptr yytext
182

    
183
void comment()
184
{
185
    int c;
186
    while ((c = input())!='\n' && c!=0 && c!=EOF);
187
    if (c=='\n') unput(c);
188
}
189

    
190
/*
191
 * - counts the line and column number of the current token in `pos'
192
 * - keeps a record of the complete current line in `textbuf[]'
193
 * - yytext[] is the current token passed by (f)lex
194
 */
195
static void _count(bool updateprevpos)
196
{
197
    static int textbuflen;
198
    int i;
199

    
200
    /* printf("DBG: countChars(): prev=%d,%d  pos=%d,%d yytext=>>%s<<\n",
201
           prevpos.li, prevpos.co, pos.li, pos.co, yytext);
202
    */
203

    
204
    /* init textbuf */
205
    if (pos.li==1 && pos.co==0) {
206
        textbuf[0]='\0'; textbuflen=0;
207
    }
208

    
209
    if (updateprevpos) {
210
        prevpos = pos;
211
    }
212
    for (i = 0; yytext[i] != '\0'; i++) {
213
        if (yytext[i] == '\n') {
214
            pos.co = 0;
215
            pos.li++;
216
            textbuflen=0; textbuf[0]='\0';
217
        } else if (yytext[i] == '\t')
218
            pos.co += 8 - (pos.co % 8);
219
        else
220
            pos.co++;
221
        if (yytext[i] != '\n') {
222
            if (textbuflen < TEXTBUF_LEN-5) {
223
                textbuf[textbuflen++]=yytext[i]; textbuf[textbuflen]='\0';
224
            }
225
            else if (textbuflen == TEXTBUF_LEN-5) {
226
                strcpy(textbuf+textbuflen, "...");
227
                textbuflen++;
228
            } else {
229
                /* line too long -- ignore */
230
            }
231
        }
232
    }
233
    /* printf("li=%d co=%d\n", pos.li, pos.co); good for debugging... */
234
    yylloc.first_line   = prevpos.li;
235
    yylloc.first_column = prevpos.co;
236
    yylloc.last_line    = pos.li;
237
    yylloc.last_column  = pos.co;
238
}
239

    
240
void countChars()
241
{
242
    _count(true);
243
}
244

    
245
void extendCount()
246
{
247
    _count(false);
248
}
249

    
250