Initial revision
[charm.git] / src / xlat / xl-lexsp.flex
1  
2 %{
3 #define YYSTYPE YSNPTR
4 #include "xl-lex.h"
5 #include "xl-yacc.tab.h"
6 #include "xl-sym.h"
7 #include <string.h>
8
9 extern YSNPTR yylval;
10 extern int MsgToStructFlag;
11 int CurrentInputLineNo=1;
12 int SavedLineNo;
13 int CurrentOutputLineNo=0;
14 char CurrentFileName[2*FILENAMELENGTH];
15 char SavedFileName[2*FILENAMELENGTH];
16 int OutputLineLength=0;
17 extern FILE *outfile;
18
19 extern SYMTABPTR GlobalModuleSearch();
20 extern char token[];
21 int PrevToken=0;
22
23 #define CREATE 1
24 #define NOCREATE 0
25
26 #ifdef yywrap
27 #undef yywrap
28 #endif
29 %}
30
31
32 %e 1500
33 %p 3900
34
35 EXP    ([eE][+-]?[0-9]+)
36 FS     [flFL]
37 IS     ([uU][lL]?|[lL][uU]?)
38 ASTRNG ([^"\\\n]|\\(['"?\\abfnrtv\n]|[0-7]{1,3}|[xX][0-9a-fA-F]{1,3}))*
39 WhiteSpace      [ \t]*
40 ID     [_a-zA-Z][_a-zA-Z0-9]*
41 WS     [ \t\n]*
42
43 %%
44 "@"             { strcpy(token,yytext);return(PrevToken=AT); }
45 "?"             { mywriteoutput("?",NOCREATE);strcpy(token,yytext);return(PrevToken=QUESTION); }
46 ":"             { strcpy(token,yytext);return(PrevToken=COLON); }
47 "|"             { mywriteoutput("|",NOCREATE);strcpy(token,yytext);return(PrevToken=OR); }
48 "&"             { mywriteoutput("&",NOCREATE);strcpy(token,yytext);return(PrevToken=AND); }
49 "^"             { mywriteoutput("^",NOCREATE);strcpy(token,yytext);return(PrevToken=HAT); }
50 "+"             { mywriteoutput("+",NOCREATE);strcpy(token,yytext);return(PrevToken=PLUS); }
51 "-"             { mywriteoutput("-",NOCREATE);strcpy(token,yytext);return(PrevToken=MINUS); }
52 "*"             { mywriteoutput("*",NOCREATE);strcpy(token,yytext);return(PrevToken=MULT); }
53 "/"             { mywriteoutput("/",NOCREATE);strcpy(token,yytext);return(PrevToken=DIV); }
54 "%"             { mywriteoutput("%",NOCREATE);strcpy(token,yytext);return(PrevToken=MOD); }
55 "("             { mywriteoutput("(",NOCREATE);strcpy(token,yytext);return(PrevToken=L_PAREN); }
56 ")"             { mywriteoutput(")",NOCREATE);strcpy(token,yytext);return(PrevToken=R_PAREN); }
57 "["             { mywriteoutput("[",NOCREATE);strcpy(token,yytext);return(PrevToken=L_SQUARE); }
58 "]"             { mywriteoutput("]",NOCREATE);strcpy(token,yytext);return(PrevToken=R_SQUARE); }
59 "."             { mywriteoutput(".",NOCREATE);strcpy(token,yytext);return(PrevToken=DOT); }
60 "!"             { mywriteoutput("!",NOCREATE);strcpy(token,yytext);return(PrevToken=EXCLAIM); }
61 "~"             { mywriteoutput("~",NOCREATE);strcpy(token,yytext);return(PrevToken=TILDE); }
62 "->"            { mywriteoutput("->",NOCREATE);strcpy(token,yytext);return(PrevToken=POINTERREF); }
63 "++"           { mywriteoutput("++",CREATE);strcpy(token,yytext);return(PrevToken=INCDEC); }
64 "--"           { mywriteoutput("--",CREATE);strcpy(token,yytext);return(PrevToken=INCDEC); }
65 "<<"           { mywriteoutput("<<",CREATE);strcpy(token,yytext);return(PrevToken=SHIFT); }
66 ">>"           { mywriteoutput(">>",CREATE);strcpy(token,yytext);return(PrevToken=SHIFT); }
67 "<"            { mywriteoutput("<",CREATE);strcpy(token,yytext);return(PrevToken=COMPARE); }
68 ">"            { mywriteoutput(">",CREATE);strcpy(token,yytext);return(PrevToken=COMPARE); }
69 "<="           { mywriteoutput("<=",CREATE);strcpy(token,yytext);return(PrevToken=COMPARE); }
70 ">="           { mywriteoutput(">=",CREATE);strcpy(token,yytext);return(PrevToken=COMPARE); }
71 "=="           { mywriteoutput("==",NOCREATE);strcpy(token,yytext);return(PrevToken=EQUALEQUAL); }
72 "!="           { mywriteoutput("!=",NOCREATE);strcpy(token,yytext);return(PrevToken=NOTEQUAL); }
73 "&&"           { mywriteoutput("&&",NOCREATE);strcpy(token,yytext);return(PrevToken=ANDAND); }
74 "||"           { mywriteoutput("||",NOCREATE);strcpy(token,yytext);return(PrevToken=OROR); }
75 "="            { mywriteoutput("=",NOCREATE);strcpy(token,yytext);return(PrevToken=EQUAL); }
76 "+="           { mywriteoutput("+=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
77 "-="           { mywriteoutput("-=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
78 "*="           { mywriteoutput("*=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
79 "/="           { mywriteoutput("/=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
80 "%="           { mywriteoutput("%=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
81 ">>="          { mywriteoutput(">>=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
82 "<<="          { mywriteoutput("<<=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
83 "&="           { mywriteoutput("&=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
84 "|="           { mywriteoutput("|=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
85 "^="           { mywriteoutput("^=",CREATE);strcpy(token,yytext);return(PrevToken=ASGNOP); }
86 ","            { mywriteoutput(",",NOCREATE);strcpy(token,yytext);return(PrevToken=COMMA); }
87 "{"            { strcpy(token,yytext);return(PrevToken=L_BRACE); }
88 "}"            { strcpy(token,yytext);return(PrevToken=R_BRACE); }
89 ";"            { mywriteoutput(";",NOCREATE);strcpy(token,yytext);return(PrevToken=SEMICOLON); }
90 ","[ \t\n]*"{"  { int i,count=0;
91                 
92                   strcpy(token,yytext);
93                   for (i=0;i<yyleng;i++)
94                         if (yytext[i]=='\n') count++;
95                   CurrentInputLineNo += count;
96                   mywriteoutput(",{ ",NOCREATE);return(PrevToken=COMMA_L_BRACE); 
97                 }
98 ","[ \t\n]*"}"  { int i,count=0;
99                 
100                   for (i=0;i<yyleng;i++)
101                         if (yytext[i]=='\n') count++;
102                   CurrentInputLineNo += count;
103                   mywriteoutput(",} ",NOCREATE);return(PrevToken=COMMA_R_BRACE); }
104
105 "#"{WhiteSpace}("line")?{WhiteSpace}[0-9]+{WhiteSpace}\"{ASTRNG}\" { int i=0,j=0;
106                                                   char temp[FILENAMELENGTH];
107
108                                                   while ((yytext[i]<'0')||
109                                                          (yytext[i]>'9')) 
110                                                         i++;
111                                                   while ((yytext[i]>='0') &&
112                                                          (yytext[i]<='9'))
113                                                         temp[j++]=yytext[i++];
114                                                   temp[j]='\0';
115                                                   CurrentInputLineNo = 
116                                                         atoi(temp);
117                                                   while (yytext[i]!='\"')
118                                                         i++;
119                                                   yytext[yyleng-1]='\0';
120                                                   strcpy(CurrentFileName,
121                                                                 yytext+i+1);
122                                                 }
123
124 {ID}                       { int retvalue;
125                 
126                               strcpy(token,yytext);
127                               yylval=GetYSN();
128                               yylval->string=MakeString(yytext);
129                               if ((retvalue=SearchKey(yytext)) == -1)
130                                   return(PrevToken=TypeToToken(IDType(yytext)));
131                               else { if (IsKey(yytext))
132                                         writeoutput(yytext,NOFREE);
133                                      if (retvalue==0) return(IDENTIFIER);
134                                      if (MsgToStructFlag && (retvalue==MESSAGE))
135                                         { retvalue=STRUCT;
136                                           writeoutput("struct ",NOFREE);
137                                         }
138                                      if (retvalue==MAIN)
139                                         if (PrevToken!=CHARE)
140                                                 return(PrevToken=IDENTIFIER);
141                                      return(PrevToken=retvalue);
142                                    }
143                             }
144
145 {ID}{WS}"::"{WS}{ID}    { SYMTABPTR worksymtab;
146                           char *modname,*name,*string;
147                           int i;char ch;
148
149                           strcpy(token,yytext);
150                           yylval=GetYSN();
151                           i=0;ch=yytext[0];
152                           string=GetMem(strlen(yytext));
153                           while ((ch!=' ')&&(ch!='\t')&&(ch!='\n')&&(ch!=':'))
154                                 { string[i]=ch;ch=yytext[++i]; }
155                           string[i]='\0';
156                           modname=yylval->modstring=MakeString(string);
157                           while ((ch==' ')||(ch=='\t')||(ch=='\n')||(ch==':'))
158                                 { if (ch=='\n') CurrentInputLineNo++;
159                                   ch=yytext[++i]; 
160                                 }
161                           name=yylval->string=MakeString(yytext+i);
162
163                           worksymtab=GlobalModuleSearch(name,modname);
164                           if (worksymtab==NULL)
165                                 { error("Bad Module Reference",EXIT);
166                                   return(ID_DCOLON_ID);
167                                 }
168                           switch (TypeToToken(worksymtab->idtype))
169                           { case IDENTIFIER : return(ID_DCOLON_ID);
170                             case TYPE_IDENTIFIER : return(TYPE_IDENTIFIER);
171                           }
172                         }
173
174 [0-9]+"."[0-9]*{EXP}?{FS}?   |
175 "."[0-9]+{EXP}?{FS}?                 |
176 [0-9]+{EXP}{FS}?                     |
177 [1-9][0-9]*{IS}?                     |
178 0[0-7]*{IS}?                         |
179 0[xX][0-9a-fA-F]+{IS}?          { mywriteoutput(yytext,CREATE);
180                                   strcpy(token,yytext);return(PrevToken=NUMBER);                
181                                 }
182
183 \'([^'\\\n]|\\(['"?\\abfnrtv]|[0-7]{1,3}|[xX][0-9a-fA-F]{1,3}))+\'   {
184                                 mywriteoutput(yytext,CREATE);
185                                   strcpy(token,yytext);return(PrevToken=CHAR_CONST);              }
186
187 \"{ASTRNG}\"                    { mywriteoutput(yytext,CREATE);
188                                   strcpy(token,yytext);return(PrevToken=STRING);
189                                 }
190
191 [ \t\f]+      { writeoutput(yytext,NOFREE); }        /* Skip whitespace...*/
192 [\n]          { CurrentInputLineNo++; WriteReturn(); 
193                 /*printf("%d Line\n",yylineno);*/
194                 }
195
196 "#"{WhiteSpace}("pragma"){WhiteSpace}[^\n]*     {writeoutput(yytext,NOFREE);
197                                         CurrentInputLineNo++;
198                                         WriteReturn();}
199
200 "#"{WhiteSpace}("ident"){WhiteSpace}[^\n]*      {writeoutput(yytext,NOFREE);
201                                         CurrentInputLineNo++;
202                                         WriteReturn();}
203
204 .               { error("Lexical Oddity",EXIT); }
205
206 %%
207
208 yywrap(){ PrevToken=0; return(1); }
209
210 mywriteoutput(string,flag)
211 char *string;
212 int flag;
213 { if (!flag) return;
214   yylval=GetYSN();
215   yylval->string=MakeString(string);
216 }
217
218 TypeToToken(type)
219 int type;
220 { switch (type)
221   { case TYPENAME :
222     case MESSAGENAME : return(TYPE_IDENTIFIER);
223     case MODULENAME :
224     case CHARENAME  :
225     case BOCNAME    :
226     case ENTRYNAME  :
227     default    : return(IDENTIFIER);
228   }
229 }