Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / expr.lex @ a3be1d55

History | View | Annotate | Download (7.44 KB)

1 01873262 Georg Kunz
/*==================================================
2
 * File: expr.lex
3
 *
4
 *  Lexical analyser for OMNeT++ NED-2 expressions.
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 withcppclass
23
 *    sections:      types parameters gates submodules connections allowunconnected
24
 *    param types:   double int string bool xml function
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 "expryydefs.h"
46
#include "cexception.h"
47
#include "expr.tab.hh"
48
49
using OPP::opp_strdup;
50
51
#define yylval expryylval
52
extern YYSTYPE yylval;
53
54
// wrap symbols to allow several .lex files coexist
55
#define comment     exprcomment
56
#define countChars  exprcount
57
#define extendCount exprextendCount
58
59
void comment();
60
void countChars();
61
void extendCount();
62
63
#define TEXTBUF_LEN 1024
64
static char textbuf[TEXTBUF_LEN];
65
66
// buffer to collect characters during extendCount()
67
static std::string extendbuf;
68
69
#include "simutil.h"  // opp_strdup()
70
%}
71
72
%%
73
"//"                     { comment(); }
74
75
"double"                 { countChars(); return DOUBLETYPE; }
76
"int"                    { countChars(); return INTTYPE; }
77
"string"                 { countChars(); return STRINGTYPE; }
78
"bool"                   { countChars(); return BOOLTYPE; }
79
"xml"                    { countChars(); return XMLTYPE; }
80
81
"true"                   { countChars(); return TRUE_; }
82
"false"                  { countChars(); return FALSE_; }
83
"this"                   { countChars(); return THIS_; }
84
"default"                { countChars(); return DEFAULT_; }
85
"ask"                  { countChars(); return ASK_; }
86
"const"                  { countChars(); return CONST_; }
87
"sizeof"                 { countChars(); return SIZEOF_; }
88
"index"                  { countChars(); return INDEX_; }
89
"xmldoc"                 { countChars(); return XMLDOC_; }
90
91
{L}({L}|{D})*            { countChars(); yylval = opp_strdup(yytext); return NAME; }
92
{D}+                     { countChars(); yylval = opp_strdup(yytext); return INTCONSTANT; }
93
0[xX]{X}+                { countChars(); yylval = opp_strdup(yytext); return INTCONSTANT; }
94
{D}+{E}                  { countChars(); yylval = opp_strdup(yytext); return REALCONSTANT; }
95
{D}*"."{D}+({E})?        { countChars(); yylval = opp_strdup(yytext); return REALCONSTANT; }
96
97
\"                       { BEGIN(stringliteral); countChars(); }
98
<stringliteral>{
99
      \n                 { BEGIN(INITIAL); throw std::runtime_error("Error parsing expression: 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! */  }
100
      \\\n               { extendCount(); /* line continuation */ }
101
      \\\"               { extendCount(); /* qouted quote */ }
102
      \\[^\n\"]          { extendCount(); /* qouted char */ }
103
      [^\\\n\"]+         { extendCount(); /* character inside string literal */ }
104
      \"                 { extendCount(); yylval = opp_strdup(extendbuf.c_str()); BEGIN(INITIAL); return STRINGCONSTANT; /* closing quote */ }
105
}
106
107
","                      { countChars(); return ','; }
108
":"                      { countChars(); return ':'; }
109
"="                      { countChars(); return '='; }
110
"("                      { countChars(); return '('; }
111
")"                      { countChars(); return ')'; }
112
"["                      { countChars(); return '['; }
113
"]"                      { countChars(); return ']'; }
114
"."                      { countChars(); return '.'; }
115
"?"                      { countChars(); return '?'; }
116
117
"||"                     { countChars(); return OR_; }
118
"&&"                     { countChars(); return AND_; }
119
"##"                     { countChars(); return XOR_; }
120
"!"                      { countChars(); return NOT_; }
121
122
"|"                      { countChars(); return BINOR_; }
123
"&"                      { countChars(); return BINAND_; }
124
"#"                      { countChars(); return BINXOR_; }
125
"~"                      { countChars(); return BINCOMPL_; }
126
"<<"                     { countChars(); return SHIFTLEFT_; }
127
">>"                     { countChars(); return SHIFTRIGHT_; }
128
129
"^"                      { countChars(); return '^'; }
130
"+"                      { countChars(); return '+'; }
131
"-"                      { countChars(); return '-'; }
132
"*"                      { countChars(); return '*'; }
133
"/"                      { countChars(); return '/'; }
134
"%"                      { countChars(); return '%'; }
135
"<"                      { countChars(); return '<'; }
136
">"                      { countChars(); return '>'; }
137
138
"=="                     { countChars(); return EQ_; }
139
"!="                     { countChars(); return NE_; }
140
"<="                     { countChars(); return LE_; }
141
">="                     { countChars(); return GE_; }
142
143
{S}                      { countChars(); }
144
.                        { countChars(); return INVALID_CHAR; }
145
146
%%
147
148
int yywrap()
149
{
150
     return 1;
151
}
152
153
/*
154
 * - discards all remaining characters of a line of
155
 *   text from the inputstream.
156
 * - the characters are read with the input() and
157
 *   unput() functions.
158
 * - input() is sometimes called yyinput()...
159
 */
160
#ifdef __cplusplus
161
#define input  yyinput
162
#endif
163
164
/* the following #define is needed for broken flex versions */
165
#define yytext_ptr yytext
166
167
void comment()
168
{
169
    int c;
170
    while ((c = input())!='\n' && c!=0 && c!=EOF);
171
    if (c=='\n') unput(c);
172
}
173
174
/*
175
 * - counts the line and column number of the current token in `pos'
176
 * - keeps a record of the complete current line in `textbuf[]'
177
 * - yytext[] is the current token passed by (f)lex
178
 */
179
static void _count(bool updateprevpos)
180
{
181
    static int textbuflen;
182
    int i;
183
184
    /* printf("DBG: countChars(): prev=%d,%d  xpos=%d,%d yytext=>>%s<<\n",
185
           xprevpos.li, xprevpos.co, xpos.li, xpos.co, yytext);
186
    */
187
188
    /* init textbuf */
189
    if (xpos.li==1 && xpos.co==0) {
190
        textbuf[0]='\0'; textbuflen=0;
191
    }
192
193
    if (updateprevpos) {
194
        extendbuf = "";
195
        xprevpos = xpos;
196
    }
197
    extendbuf += yytext;
198
    for (i = 0; yytext[i] != '\0'; i++) {
199
        if (yytext[i] == '\n') {
200
            xpos.co = 0;
201
            xpos.li++;
202
            textbuflen=0; textbuf[0]='\0';
203
        } else if (yytext[i] == '\t')
204
            xpos.co += 8 - (xpos.co % 8);
205
        else
206
            xpos.co++;
207
        if (yytext[i] != '\n') {
208
            if (textbuflen < TEXTBUF_LEN-5) {
209
                textbuf[textbuflen++]=yytext[i]; textbuf[textbuflen]='\0';
210
            }
211
            else if (textbuflen == TEXTBUF_LEN-5) {
212
                strcpy(textbuf+textbuflen, "...");
213
                textbuflen++;
214
            } else {
215
                /* line too long -- ignore */
216
            }
217
        }
218
    }
219
}
220
221
void countChars()
222
{
223
    _count(true);
224
}
225
226
void extendCount()
227
{
228
    _count(false);
229
}