1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
%{
/*
Copyright (C) 2000-2013 The PARI group.
This file is part of the GP2C package.
PARI/GP is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation. It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY WHATSOEVER.
Check the License for details. You should have received a copy of it, along
with the package; see the file 'COPYING'. If not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/
#include "config.h"
#include <unistd.h>
#include <string.h>
#include "header.h"
#include "parse.h"
#define MAX_INCLUDE_DEPTH 10
extern int yydebug;
static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
static int depth = 0;
static int linecount_stack[MAX_INCLUDE_DEPTH];
static int filecount_stack[MAX_INCLUDE_DEPTH];
static enum {Mread_lines,Min_braces} mode=Mread_lines;
static int start=0;
static int lastcom=-1;
static void countline(char *text,int len)
{
int i;
for (i=0; i<len; i++)
linecount+=(text[i]=='\n');
}
/* yytext is not global on OSF1 flex */
static void copytoken(char *yytext)
{
yylval.str.s=strdup(yytext);
if (yydebug) fprintf(stderr,"[%s]",yytext);
yylval.str.c=lastcom; lastcom=-1;
}
static void copyentry(char *yytext)
{
if (strchr(yytext,' '))
die(err_parse,"Space inside identifier name `%s'",yytext);
copytoken(yytext);
}
/* similarly for yyleng */
static void copystring(char *yytext, int yyleng)
{
yylval.str.s=strdup(yytext+1);
yylval.str.s[yyleng-2]=0;
if (yydebug) fprintf(stderr,"[%s]",yytext);
countline(yytext,yyleng);
yylval.str.c=lastcom; lastcom=-1;
}
static int yywrap(void ) {return 1;}
/* There are two start conditions:
-- toplevel
-- INITIAL
We start at toplevel. As soon as we see a non toplevel command, we go to
INITIAL. When the command is completed, we revert to toplevel. It is better to
stay at toplevel when we encounter non-command tokens (comments, white spaces).
The mode can be
Mread_lines: line by line mode
Min_braces : inside braces
*/
/* REAL_NOEXPO can be confused with member function to integer
* so we let parse.y handle them
*/
%}
HEXINTEGER 0x[0-9A-Za-z]([0-9A-Za-z ]*[0-9A-Za-z])?
BININTEGER 0b[01]([01 ]*[01])?
INTEGER [0-9]([0-9 ]*[0-9])?
EXPO [eE][+-]?{INTEGER}
REAL_NODOT {INTEGER}{EXPO}
REAL_TAIL {INTEGER}?"."{INTEGER}{EXPO}?
REAL_EXPO {INTEGER}?"."{EXPO}
REAL_NOEXPO {INTEGER}"."
REAL {REAL_NODOT}|{REAL_TAIL}|{REAL_EXPO}
STRING \"([^"\\]|(\\.))*\"
ENTRY [A-Za-z]([A-Za-z0-9_ ]*[A-Za-z0-9_])?
%x toplevel
%%
if (!start) {start=1; BEGIN(toplevel);}
{ENTRY} copyentry(yytext); return KENTRY;
{INTEGER} copytoken(yytext); return KINTEGER;
{HEXINTEGER} copytoken(yytext); return KINTEGER;
{BININTEGER} copytoken(yytext); return KINTEGER;
{REAL} copytoken(yytext); return KREAL;
{REAL_NOEXPO}/[^_A-Za-z.] copytoken(yytext); return KREAL;
{STRING} copystring(yytext,yyleng); return KSTRING;
<<EOF>> {
if ( --depth < 0 )
yyterminate();
else
{
yy_delete_buffer( YY_CURRENT_BUFFER );
yy_switch_to_buffer(include_stack[depth]);
linecount = linecount_stack[depth];
filecount = filecount_stack[depth];
}
}
<toplevel>\\r[^\n]+ { /*With credit to flex manpage*/
char *s=yytext+2;
size_t ls;
while(*s==' ' || *s=='\t') s++;
if ( depth >= MAX_INCLUDE_DEPTH )
die(-1,"\\r includes nested too deeply" );
linecount_stack[depth] = linecount;
filecount_stack[depth] = filecount;
include_stack[depth++] = YY_CURRENT_BUFFER;
ls = strlen(s);
if (s[0]=='"' && s[ls-1]=='"')
{
s[ls-1]=0;
s++;
}
yyin = fopen(s,"r");
if (!yyin)
{
char *t=(char*)malloc(strlen(s)+4);
sprintf(t,"%s.gp",s);
yyin = fopen(t,"r");
if (!yyin)
die(-1,"cannot open file `%s'",s);
s=t;
}
else
s = strdup(s);
linecount=1;
filecount=newsrcfile(s);
yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ) );
}
<toplevel>\\[a-qs-z][^\n]*|\?[^\n]* {
fprintf(stderr,"Warning:%s:%d: meta commands not implemented\n%s\n",
srcfile[filecount],linecount,yytext);
}
<toplevel>##?[ \t\r]*$ { /*We cannot recognize #<EOF> */
fprintf(stderr,"Warning:%s:%d: meta commands not implemented\n%s\n",
srcfile[filecount],linecount,yytext);
}
".." return KDOTDOT;
"->" return KARROW;
\)[ \t\r]*-> return KPARROW;
"\\/" return KDR;
">>" return KSR;
"<<" return KSL;
"<=" return KLE;
">=" return KGE;
"!="|"<>" return KNE;
"===" return KID;
"==" return KEQ;
"&&" return KAND;
"||" return KOR;
"++" return KPP;
"--" return KSS;
"+=" return KPE;
"-=" return KSE;
"*=" return KME;
"/=" return KDE;
"\\=" return KEUCE;
"\\/=" return KDRE;
"%=" return KMODE;
">>=" return KSRE;
"<<=" return KSLE;
"\n" {linecount++;if (mode==Mread_lines) {BEGIN(toplevel);return '\n';}}
<toplevel>"\n" {linecount++;return '\n';}
"{" {mode=Min_braces;BEGIN(INITIAL);}
"}" {mode=Mread_lines;BEGIN(toplevel);}
<INITIAL,toplevel>"\\\n" {linecount++;}
"=" { for( ; ; )
{
int c, nc, star=0;
for( ; ; )
{
c = input();
if ( c == EOF || c == 0 )
return c;
if ( c == '\n' )
linecount++;
else if ( c!=' ' && c!='\t' && c!='\r')
break;
}
/*Since only one char of look-ahead is available, we have to
handle the / or \ by ourself. Fortunately:
after =/, only * is valid,
after =\, only \ and \\n are valid.
Thus we handle them and reject the rest.
*/
if (c!='/' && c!='\\')
{
unput(c);
return '=';
}
nc=input();
if (c=='\\' && nc=='\n')
{
linecount++;
continue;
}
if ((c=='/' && nc!='*') || (c=='\\' && nc!='\\'))
{
fprintf(stderr,"%s:%d: parse error after =%c%c\n"
,srcfile[filecount],linecount,(char)c,(char)nc);
fprintf(stderr,"Errors found: aborting...\n");
exit(1);
}
if (c=='/')
{
for ( ; ; )
{
int c = input();
if ( c == EOF || c == 0 )
die(-1,"Unfinished comment");
if ( c == '/' && star) break;
if ( c == '*' ) star=1;
else star=0;
if ( c == '\n' ) linecount++;
}
}
else
{
for ( ; ; )
{
int c = input();
if ( c == EOF || c == 0)
die(-1,"Unfinished comment");
if ( c == '/' && star) c='\\';
if ( c == '*' ) star=1;
else star=0;
if ( c == '\n' )
{
linecount++;
break;
}
if ( c == '\r' )
continue;
}
}
}
}
<INITIAL,toplevel>[ \t\r]+
<INITIAL,toplevel>"/*"|"\\\\" {
int star=0;
int n;
int nl=0;
int lc=linecount;
if (lastcom==-1)
lastcom=newcomment();
n=lastcom;
pushcomment(n,'/');
pushcomment(n,'*');
if (yytext[0]=='/')
{
for ( ; ; )
{
int c = input();
if ( c == EOF || c == 0)
die(-1,"Unfinished comment");
pushcomment(n,c);
if ( c == '/' && star) break;
if ( c == '*' ) star=1;
else star=0;
if ( c == '\n' ) linecount++;
}
}
else
{
nl=1;
for ( ; ; )
{
int c = input();
if ( c == EOF || c == 0)
die(-1,"Unfinished comment");
if ( c == '/' && star) c='\\';
if ( c == '*' ) star=1;
else star=0;
if ( c == '\n' )
{
linecount++;
break;
}
if ( c == '\r' )
continue;
pushcomment(n,c);
}
pushcomment(n,' ');
pushcomment(n,'*');
pushcomment(n,'/');
pushcomment(n,'\n');
}
/*We hack: we put white spaces after comments in the comments
This makes output looking better in most cases.
Sadly we can't add white spaces that are before comments.
*/
for( ; ; )
{
int c = input();
if ( c == EOF || c == 0 )
return '\n';
if ( c == '\n' )
{
linecount++;
pushcomment(n,c);
nl=1;
}
else if ( c == ' ' || c == '\t')
{
if (nl==0)
pushcomment(n,c);
/*We only add spaces on the same line*/
}
else if ( c != '\r' )
{
unput(c);
break;
}
}
if (lc<linecount && mode==Mread_lines)
{
BEGIN(toplevel);
return '\n';
}
}
<toplevel>. {BEGIN(INITIAL); unput(yytext[0]);}
. return yytext[0];
%%
|