forked from FreeFem/FreeFem-sources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.cpp
1100 lines (988 loc) · 29.9 KB
/
lex.cpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- Mode : c++ -*-
//
// SUMMARY :
// USAGE :
// ORG :
// AUTHOR : Frederic Hecht
// E-MAIL : [email protected]
//
/*
This file is part of Freefem++
Freefem++ is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
Freefem++ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Freefem++; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <complex>
#include <string>
#include <iostream>
#include "error.hpp"
#include <ctype.h>
#include <stdlib.h>
#include <map>
#include "AFunction.hpp"
//class pfes;
#include <iomanip>
#include "lg.tab.hpp"
#include "lex.hpp"
extern YYSTYPE *plglval;
// New version of macro expantion more classical
// and more simple
// FH Jan. 2005
static const bool debugmacro = false;
/*inline char * newcopy(const char * s)
{
char *r(new char [strlen(s)+1]);
strcpy(r, s);return r;
}
*/
void mylex::Add(Key k,int i)
{
Check(!i,k,"mot clef");
Add(k,i,0);
}
// <<mylex_Add_Key_aType>>
void mylex::Add(Key k,aType t)
{
Check(!t,k,"type");
// TYPE defined at [[file:../lglib/lg.ypp::token type TYPE]]
Add(k,TYPE,t);
}
void mylex::AddF(Key k,aType t)
{
Check(!t,k,"FUNCTION");
Add(k,FUNCTION,t);
}
void mylex::AddSpace(Key k,aType t)
{
Check(!t,k,"FESPACE");
Add(k,FESPACE,t);
}
// <<mylex_InMotClef>> looks in MotClef for the token contained in buf (buf is set by by [[mylex_basescan]]) and returns
// its type as t and the grammar token id as r (eg [[file:~/ff/src/lglib/lg.ypp::TYPE]]).
bool mylex::InMotClef (aType & t, int & r) const {
const_iterator i= MotClef.find(buf);
if (i== MotClef.end()) {
t=0;r=ID;
return false;}
else {
r=i->second.first;
t=i->second.second;
ffassert(r);
return true;}}
// alh - 5/4/15 - <<mylex::InMotClef_string>> [[file:lex.hpp::InMotClef_string]] Same as [[mylex_InMotClef]], but checks
// another buffer than buf
bool mylex::InMotClef(const char *b,aType &t,int &r)const{
const_iterator i=MotClef.find(b);
if(i==MotClef.end()){t=0;r=ID;return false;}
else {
r=i->second.first;
t=i->second.second;
ffassert(r);
return true;
}
}
// <<mylex_Add_Key_int_aType>>
void mylex::Add(Key k,int r,aType t){
iterator ii= MotClef.find(k);
ffassert(ii==MotClef.end());
MotClef.insert(pair<const Key,Value>(k,Value(r,t))); }
// <<dump>>
void mylex::dump(ostream & f )
{
const_iterator i=MotClef.begin(),e=MotClef.end();
for (;i != e;i++)
{
f << " " << i->first << " " << i->second.first << " " << i->second.second->name() << endl;
}
}
inline bool isNLCR(istream & f,int c)
{
// eat CR NL or NL CR paire
int cc= f.peek();
bool ret=(c == 10 || c == 13) ;
if(ret && ( cc != c) && (cc == 10 || cc == 13) )
f.get();
return ret;
}
int mylex::EatCommentAndSpace(string *data)
{
// if data is !0 then add reading char in data
// return the last read char c
// --------------------
int c,caux,sep=0;
const int space=(int) ' ';
int incomment =0;
do {
incomment = 0;
c = source().peek();
// eat spaces
while (isspace(c) || c == 10 || c == 13 )
{sep=space;
c = source().get();
if(isNLCR(source(),c)) c='\n';
if (echo) cout << (char) c;
if(c=='\n') { linenumber++; if (echo) cout << setw(5) <<linenumber << " : " ;};
if(data) *data+=char(c);
c=source().peek();
}
// eat comment
if(c=='/')
{ c = source().get();
caux=source().peek();
if(caux =='/') incomment = 1;
else if (caux == '*' ) incomment = 2;
if(!incomment) source().putback(c);
}
if(incomment==1)
{ sep=space;
if (echo) cout << "//" ;source().get();
if(data) *data+="//";
do {c=source().get();
if(isNLCR(source(),c)) c='\n';
if (echo) cout << (char) c;
if(data) *data+=char(c);
if(c=='\n') { linenumber++; if (echo) cout << setw(5) <<linenumber << " : " ;};
}
while( (c!= '\n') && (c!= 10) && (c!= 13) && ( c != EOF) );
}
else if(incomment==2)
{ sep=space;
if (echo) cout << "/*" ;
if(data) *data+="/*";
source().get();
do {
c=source().get();
if(isNLCR(source(),c)) c='\n';
if (echo) cout << (char) c ;
if(data) *data+=char(c);
if(c=='\n') { linenumber++; if (echo) cout << setw(5) <<linenumber << " : " ;};
caux = source().peek();
} while(c != EOF && !(c=='*' && caux=='/') ) ;
if(c != EOF)
{ c = source().get();
if (echo) cout << (char) c ;
if(data) *data+=char(c);
}
else erreur( " Unterminated comment");
}
} while (incomment);
return (c==EOF) ? c : sep;
}
// <<mylex_basescan>>
int mylex::basescanprint(int lvl)
{
int r=basescan();
if (! lexdebug && echo && lvl==0 ) print(cout);
return r;
}
int mylex::basescan()
{
// extern long mpirank;
int c;
buf[0]=0;
buf[1]=0;
buf[2]=0;
buf[3]=0; //
debut:
TheCurrentLine=linenumber;
// modif FH
if (firsttime)
{
firsttime=false;
if(echo) cout << setw(5) <<linenumber << " : " ;
}
EatCommentAndSpace(); // [[mylex::EatCommentAndSpace]]
c =source().get(); // the current char
char nc = source().peek(); // next char
buf[0]=c;
buf[1]=nc;
buf[2]=0;
int ret = c;
if (c == EOF)
{
//if (echo) cout << "ENDOFFILE "<< endl;
if (close() ) goto debut;
buf[0]=0;
return ENDOFFILE;
}
// <<found_a_number>>
else if (isdigit(c) || (c=='.' && isdigit(nc))) {
int i=1;
buf[0]=c;
bool real= (c=='.');
while ( isdigit(nc) && i< 50 ) buf[i++]=source().get(),nc=source().peek();
if (!real && (nc == '.')) real=true,buf[i++]=source().get(),nc=source().peek();
while ( isdigit(nc) && i< 50 ) buf[i++]=source().get(),nc=source().peek();
if (nc =='E' || nc =='e' || nc =='D' || nc =='d') {real=true;
buf[i++]=source().get(),nc=source().peek();
if (nc =='+' || nc =='-' || isdigit(nc)) buf[i++]=source().get(),nc=source().peek();
while ( isdigit(nc) && i< 50 ) buf[i++]=source().get(),nc=source().peek();
}
if (i>= 50) erreur("Number too long");
buf[i]=0;
if (nc=='i' )
{ buf[i++]=source().get(),buf[i]=0,plglval->dnum = atof(buf),ret=CNUM;}
else
if (real)
plglval->dnum = atof(buf),ret=DNUM;
else
plglval->lnum = atoi(buf),ret=LNUM;
if(lexdebug) cout << " NUM : " << buf << " "<<endl;
}
// <<found_an_identifier>>
else if (isalpha(c) || c=='\\')
{
ret = ID;
int i;
for (i = 1; i < 256 && isalnum(source().peek()); i++)
buf[i]=source().get();
if (i == 256)
erreur ("Identifier too long");
buf[i] = 0;
}
// <<found_a_string>>
else if (c=='"')
{
int i;
char cc,ccc;
for ( i = 0,cc=source().peek();
i < 256 && ( (isprint(cc)|isspace(cc)) && cc !='\n' && cc !='"');
cc=source().peek(),++i
)
{
if ( cc == '\\') // escape
{
cc= source().get();
cc= source().get();
ccc= source().peek();
switch (cc) {
case 'n': buf[i]='\n';break;
case 'r': buf[i]='\r';break;
case 'f': buf[i]='\f';break;
case 't': buf[i]='\t';break;
case 'a': buf[i]='\a';break;
case 10:
case 13:
cc='\n';
if(ccc!=cc && (ccc==10 || ccc==13)) source().get();// NL CR eat ...
linenumber++;
//if (echo) cout << setw(5) <<linenumber << " : " ;
default : buf[i]=cc ;break;
}
}
else
buf[i] = source().get();
}
if (i == 256) erreur ("String too long");
buf[i] = 0;
if(source().get() != '"') erreur("End of String could not be found");
plglval->str = newcopy(buf);
if(lexdebug) cout << "STRING=" << '"' << buf << '"' << endl;
ret= STRING;
}
// Anything else (eg brackets and operators)
else
{
ret = c;
switch(c) {
case '{':
case '%':
case '}':
case '(':
case ')':
case '[':
case ']':
case ',':
case ';':
case '#':
break;
case '*':
if (nc == '=') ret=MULEQ;
break;
case '/':
if (nc == '=') ret=DIVEQ;
break;
case '^':
case '~':
case '\'':
case '_':
case '?':
break;
case '.':
if (nc == '*') ret = DOTSTAR;
else if (nc == '/') ret = DOTSLASH;
break;
case ':':
if (nc == '=') ret= SET;
break;
case '&':
if (nc == '&') ret= AND;
break;
case '|':
if (nc == '|') ret= OR;
break;
case '!':
if (nc == '=') ret=NE;
break;
case '<':
if (nc == '=') ret=LE;
if (nc == '<') ret=LTLT;
if (nc == '>') ret= NE;
break;
case '>':
if (nc == '=') ret=GE;
if (nc == '>') ret=GTGT;
break;
case '=':
if (nc == '=')
ret=EQ;
break;
case '-':
if (nc == '>') ret=ARROW;
if (nc == '-') ret=MOINSMOINS;
if (nc == '=') ret=MOINSEQ;
break;
case '+':
if (nc == '+') ret=PLUSPLUS;
if (nc == '=') ret=PLUSEQ;
break;
default:
cerr << "'" << (char) c << (char) nc << "' <=> " << (int) c << " is " ;
erreur (" Unexpected character");
}
if( (ret == DOTSTAR) || (ret==DOTSLASH))
{
source().get();
nc = source().peek();
if(nc == '=' )
{
buf[2]='=';// ad FH 19 april 2012 (bug in macro )
buf[3]=0;
source().get();
ret = (ret == DOTSTAR) ?DOTMULEQ : DOTDIVEQ;
}
}
else if (ret!=c) source().get();
else buf[1] = 0;
strcpy(plglval->oper,buf);
if(lexdebug) cout << "Special '" << plglval->oper << "' " << ret << " ";
}
typetoken=ret;
return ret;
}
// <<mylex_scan1>>
int mylex::scan1()
{
// extern long mpirank;
// bool echo = mpirank == 0;
int ret= basescan(); // [[mylex_basescan]]
if(debugmacro) cout << " scan1 " << ret << " " << token() << " " << ID << endl;
while ( ret == ID && (
SetMacro(ret) // correction mars 2014 FH
|| CallMacro(ret) // correction mars 2014 FH
|| IFMacro(ret)
)
)
; // correction mars 2014 FH
return ret;
}
// <<mylex_scan>>
int mylex::scan(int lvl)
{
int ret= scan1();
// ID defined at [[file:../lglib/lg.ypp::ID]] and detected at [[found_an_identifier]]
if ( ret == ID) {
if (! InMotClef(plglval->type,ret)) {
int ft = FindType(buf);
// FESPACE, FESPACE1, FESPACE3 defined at [[file:../lglib/lg.ypp::FESPACE]]
int feid3[4] ={ ID,FESPACE1,FESPACE,FESPACE3};
assert ( ft >= 0 && ft <= 3) ;
ret = feid3[ft];
plglval->str = newcopy(buf);
}}
if ( ret =='{') { //cout << " listMacroDef->push_back"<< endl;
listMacroDef->push_back( MapMacroDef() );}
else if (ret == '}') {//cout << " listMacroDef->pop_back"<< endl;
listMacroDef->pop_back( );}
if (! lexdebug && echo && lvl==0 ) print(cout);
return ret;
}
string mylex::token() const
{
int i=-1;
string f;
// found at [[found_a_string]]
if (typetoken == STRING)
{
f += '"';
while (buf[++i])
if (buf[i]=='\n') f += "\\n";
else if (buf[i]=='"') f += "\\\"";
else f += buf[i] ;
f+= '"';
}
else
while (buf[++i])
if (buf[i]=='\n') f += "\\n";
else f += buf[i] ;
return f;
}
void mylex::print(ostream &f) const
{
int i=-1;
int k=0;
if (typetoken == STRING)
{
f <<'"';
while (buf[++i]) { k++;
if (buf[i]=='\n') k++, f << "\\n";
else if (buf[i]=='"') k++,f << "\\\"";
else f << buf[i] ;
if ( k%50==49) f << "\n ... : ";
}
f << '"';
}
else
while (buf[++i])
if (buf[i]=='\n') f << "\\n";
else f << buf[i] ;
}
char * mylex::match(int i)
{ if ( i != basescanprint() ) {// basescan -> scan1 change 2/2/2007 (non pas des substitution de parametres FH)
cerr << " we waiting for a '" << char(i) <<"'" << endl;
ErrorScan(" err macro ");};
return buf; }
bool mylex::AddMacro(string m,string def)
{
bool ok=1;
char *macroname=newcopy(m.c_str());
int nbparam =0;
MacroData macroparm;
macroparm.f="";
macroparm.l=0;
macroparm.d.push_back(def);
MapMacroDef & MacroDef =listMacroDef->back();
MapMacroDef::const_iterator i=MacroDef.find(macroname);
if(verbosity>3) cout << " add macro "<< m << "->" << def << endl;
if ( i == MacroDef.end() )
MacroDef[macroname]=macroparm;
else {
cerr << " Error add macro varf: "<< m << "->" << def << " the macro exist"<< endl;
ErrorScan("Error in AddMacro parameters");
ok = false;
}
return ok;
}
bool mylex::SetMacro(int &ret)
{
char endmacro[]="EndMacro";
char newmacro[]="NewMacro";
bool rt=false;
int oldmacro=1;
if (strncmp(buf,"macro",6)==0 || (oldmacro=strncmp(buf,newmacro,9))==0 )
{
if(echo) print(cout);//xxxxxx
char *macroname=newcopy(match(ID));
//if(echo) print(cout);
int nbparam =0;
MacroData macroparm;
int rr=basescanprint();
string def;
if (rr!='(')
def += buf;
else
{ // a '(' after macro name
rr = basescanprint();
if (rr != ')' )
do {
if (nbparam++>= 100)
{ cerr << "in macro " <<macroname << endl;
ErrorScan(" Too much (more than 100) parameters"); }
//cout << " \t\t " << ID << " " << rr << " " << buf << endl;
if (rr != ID) {
cerr <<" Erreur waiting of an ID: " << buf << " " << rr << endl;
erreur("in macro def: waiting for a ID"); }
macroparm.d.push_back(buf);
rr = basescanprint();
if (rr == ')') break;
if ( rr != ',') erreur("in macro def: waiting , or ) ");
rr = basescanprint();
} while(1);
}
int kmacro=0;
macroparm.l = linenumber;// s
macroparm.f = file(); //
do {
int lk=0,nl=0;
string item;
int i = source().get();
if (i == EOF) { cerr << "in macro " <<macroname << endl;
ErrorScan(" ENDOFFILE in macro definition. remark:a macro end with // or EndMacro");}
int ii = source().peek();
if(isNLCR(source(),i)) { linenumber++; nl=1;}
else if(isalpha(i) && isalpha(ii) ) {// Modif F.H
//def +=char(i);
item = char(i);
i = source().get();
while(isalpha(i))
{
item += char(i);
i = source().get();
}
if( item == newmacro) kmacro++;
if( item == endmacro) {
if (kmacro==0)
{ if(echo)cout <<item ;
source().putback(i); break;}
kmacro--;
}
if(echo) cout << item ;
def += item;
item ="";
ii = source().peek();
}
if(oldmacro)
{
if (i == '/' && ii == '/') { source().putback('/'); break;}
}
def +=char(i);
if(echo)cout << char(i) ;
if(echo && nl==1) cout << setw(5) <<linenumber << " # " ;
} while(1);
macroparm.d.push_back(def);
// if(echo) cout << "macro " << macroname ;
// for (size_t i=0;i<macroparm.d.size()-1;i++)
// if(echo) cout << ( (i == 0) ? '(' : ',') << macroparm.d[i];
if (nbparam)
if(echo) cout << " ) " ;
for (size_t i=0;i<def.size();i++)
if (def[i] == 10 || (def[i] == 13 ) )
{
def[i]='\n';
linenumber++; if(echo) cout << '\n' << setw(5) <<linenumber << " : " ;
}
else
if(echo) cout << def[i] ;
// cout << macroparm.back() ;
MapMacroDef & MacroDef =listMacroDef->back();
MapMacroDef::const_iterator i=MacroDef.find(macroname);
if ( i == MacroDef.end() )
MacroDef[macroname]=macroparm;
else {
cerr << "ERREUR in macro name:" <<macroname << endl;
ErrorScan(" The macro already exists, sorry");}
rt=true;
ret= basescan();
}
return rt;
}
std::string trim(std::string s, const char* t = " \t\n\r\f\v")
{
s.erase(0, s.find_first_not_of(t));
s.erase(s.find_last_not_of(t) + 1);
return s;
}
bool mylex::IFMacro(int &ret)
{// A faire !!!! F.H
bool rt=false;
const char *ifm="IFMACRO";
const char *ife="ENDIFMACRO";
int kif=0;
int isnot=0;
int lgifm=0;
if (strncmp(buf,ifm,8)==0 )
{
if (! lexdebug && echo ) print(cout);
string val;
int rr=basescanprint();
if (rr!='(') {ErrorScan(" missing '(' after IFMACRO ");}
rr=basescanprint();
if( rr=='!') { isnot =1;rr=basescanprint();}
if (rr != ID)
cerr <<"IFMACRO: Erreur waiting of an ID: " << buf << " " << rr << endl;
string id =buf;
int withval=0;
rr=basescanprint();
if( rr == ',') {
while(1) {
int i = source().get();
if (i == EOF) { cerr << "in IFMACRO " <<id << endl;
ErrorScan(" ENDOFFILE in IFMACRO definition. remark:a end with ENDIFMACRO ");}
if( char(i) ==')') break;
val +=char(i);
withval=1;
}
if (! lexdebug && echo ) cout << val;
source().putback(')');
rr=basescanprint();
}
if (rr!=')') {ErrorScan(" missing ')' after IFMACRO(macro) ");}
int kmacro=0;
if(debugmacro) cout << " IFMacro:: " << id << endl;
lgifm=linenumber;
string def;
do {
int lk=0,nl=0;
string item;
int i = source().get();
if (i == EOF) { cerr << "in IFMACRO " <<id << endl;
ErrorScan(" ENDOFFILE in IFMACRO definition. remark:a end with ENDIFMACRO ");}
int ii = source().peek();
//
if(isNLCR(source(),i)) { linenumber++; nl=1;}
//
else if(isalpha(i) && isalpha(ii) ) {// Modif F.H
//def +=char(i);
item = char(i);
i = source().get();
while(isalpha(i))
{
item += char(i);
i = source().get();
}
if( item == ifm) kif++;
if( item == ife) {
if (kif==0)
{ source().putback(i); break;}
kif--;
}
if(echo) cout << item ;
def += item;
item ="";
ii = source().peek();
}
def +=char(i);
if(echo)cout << char(i) ;
if(echo && nl==1) cout << setw(5) <<linenumber << " & " ;
} while(1);
if (! lexdebug && echo ) cout << ife ;
bool exist=false;
MapMacroDef::const_iterator j;
for (list<MapMacroDef>::const_iterator i=listMacroDef->begin(); i != listMacroDef->end(); i++)
{
j=i->find(id.c_str());
if( j != i->end()) { exist =true; break;}
}
if(withval && exist)
{
const MacroData & macroparm= j->second;
if(macroparm.d.size()>0)
{
const string & mval = macroparm.d[macroparm.d.size()-1];
if(debugmacro) cout << " check IFMACRO '"<< val << "' '"<< mval <<"'"<<endl;
exist = trim(mval) == trim(val);
rt=true;
}
}
if(debugmacro) cout << "IFMacro def: " << def << "\n .. exist "<< exist << " " << isnot << " "<< (exist == (isnot==0)) << " \n....\n";
if(exist == (isnot==0))
{
//string * ifmc= newstring(" ifmacro ");
input(def,new string(file()),lgifm);
}
ret = scan1();
}
return rt;
}
bool mylex::CallMacro(int &ret)
{
// New Version with direct macro expansion
// FH jan 2005
// -----------------------------------------
// add Stringification,FILE, LINE march 2014 FH..
if(strncmp(buf,"Stringification",16)==0)
{
if(debugmacro) cout <<"call Stringification : " << buf << endl;
if(echo) cout << buf << "(" ;
string p,cmm;
int lvll=0;
match('(');
int kend=')';
while (1) {
cmm="";
int sep = EatCommentAndSpace(&cmm);
p += cmm;
int rr = scan1();// do macro expantion
if(lvll && rr==')') lvll--; // if ( then we eat next )
else if (rr=='(') lvll++ ; // eat next
else if (lvll<=0)
{
if (rr==kend ) break;
else if (rr==')' || rr==',') {
cerr << "Error in Stringification "
<< ", we wait for "<< char(kend) << " and we get " << char(rr)<< endl;
ErrorScan(" Wrong number of parameter in Stringification call");
}}
if(debugmacro)cout << " ..." << rr << " " << token()<< " " << level << endl;
if (rr==ENDOFFILE) ErrorScan(" Stringification in macro ");
if(echo) cout << token();
p += token();
//if(echo) cout <<buf;
}
if(echo) cout << p << ")";
plglval->str = newcopy(p.c_str());
ret = STRING;
return false;
}
// <<FILE_macro>>
else if(strncmp(buf,"FILE",5)==0)
{
if(echo) cout << buf ;
plglval->str = newcopy(filename() );
ret = STRING;
return false;
}
// <<LINE_macro>>
else if(strncmp(buf,"LINE",5)==0)
{
if(echo) cout << buf ;
plglval->lnum = linenumber;
ret=LNUM;
return false;
}
else
for (list<MapMacroDef>::const_iterator i=listMacroDef->begin(); i != listMacroDef->end(); i++)
{
MapMacroDef::const_iterator j= i->find(buf);
if (j != i->end()) {
// int inmacroold=withmacropara;
const MacroData & macroparm= j->second;
int nbparam= macroparm.d.size()-1;
if(debugmacro) cout <<"call macro : " << buf << endl;
string * macronn= newstring(" macro: ");
*macronn+= buf;
*macronn+=" in ";
*macronn+= macroparm.f;
if(echo) cout << buf ;
MapMacroParam lp;
if (nbparam > 0 ) {
match('(');
for (int k=0;k<nbparam;k++)
{
string p;
int kend= ( k+1 == nbparam) ? ')' : ',';
int lvl=0;
int lvll=0;
while (1) {
int sep = EatCommentAndSpace();
int rr = basescanprint();// basescan -> scan1 change 2/2/2007 ( not change to pass macro name as a parameter)
if ( (rr=='}') && (--lvll==0) )
continue; // remove first {
else if ( (rr=='{') && (++lvll==1) )
continue; // remove last }
else if(lvll==0) // count the open close () []
{
if(lvl && rr==')') lvl--; // if ( then we eat next )
else if(lvl && rr==']') lvl--; // if ( then we eat next )
else if (rr=='(') lvl++ ; // eat next
else if (rr=='[') lvl++ ; // eat next
else if (lvl<=0)
{
if (rr==kend ) break;
else if (rr==')' || rr==',') {// Correction FH 2/06/2004
cerr << "Error in macro expansion "<< j->first
<< ", we wait for "<< char(kend) << " and we get " << char(rr)<< endl;
cerr << " number of macro parameter in definition is " << nbparam << endl;
ErrorScan(" Wrong number of parameter in macro call");
}}}
if (rr==ENDOFFILE) ErrorScan(" ENDOFFILE in macro usage");
if(sep==' ') p+=' ';
p += token(); // Correction FH 2/06/2004 of string parameter
}
if(debugmacro)
cout << "macro arg "<< k << " :" << macroparm.d[k] << " -> " << p << endl;
lp.insert(pair<string,string>(macroparm.d[k],p));
//lp[macroparm[k]] = p;
}
}
if(debugmacro)
cout << " input in : -> " << macroparm.d[nbparam] << " <-> " << nbparam << endl;
input(macroparm.d[nbparam],0,macroparm.l);// no file no error here FH.
// ici il faut faire la substitution de parameter
// -----------------------------------------------
string expandtxt;
bool echosave=echo;
while(1) {
int c= EatCommentAndSpace(&expandtxt); // eat comment to save it;
if (c == EOF) break;
ret = basescan();
if(debugmacro) cout << " ret = " << ret << token() << endl;
if(ret== ENDOFFILE) break;
if (nbparam && ret == ID)
{
MapMacroParam::const_iterator j=lp.find(buf);
if ( j != lp.end())
expandtxt+=j->second;
else
expandtxt+=token();
}
else if (ret!='#') // macro concatenation operator
expandtxt+=token();
}
echo=echosave;
if(debugmacro) cout <<" (macro) eadin : " << expandtxt << endl;
input(expandtxt,macronn,macroparm.l);
ret = scan1(); // Correction FH 6/06/2004 of string parameter
return true;
}
}
return false;
}
void mylex::xxxx::open(mylex *lex,const char * ff)
{
l=0;
nf=f=0;
// Try to open the given file name without any extra path from [[file:lex.hpp::ffincludedir]]
if(lex->ffincludedir.empty()) // if no liste
nf=f= new ifstream(ff,ios_base::binary); // modif of win32
// If it worked, set [[file:lex.hpp::filename]] to ff, otherwise try to add path elements from
// [[file:lex.hpp::ffincludedir]]
if (!f || !*f)
{
if ( f) { delete f; nf=f=0; }
// for every potential path element
for (ICffincludedir k=lex->ffincludedir.begin();
k!=lex->ffincludedir.end();
++k)
{
string dif_ff(*k+ff);
if (verbosity>=50) lex->cout << " --lex open :" << dif_ff << endl;
nf=f= new ifstream(dif_ff.c_str(),ios_base::binary);
if ( f) {
// If path works, set [[file:lex.hpp::filename]] and close test stream
if ( f->good()) {
filename = newstring(dif_ff);
break;
}
delete f; nf=f=0;
}
}
}
else
filename=newstring(ff);
// Error message if no path was right
if (!f || !*f) {
lex->cout << " Error openning file " <<ff<< " in: " <<endl;
for (ICffincludedir k=lex->ffincludedir.begin();
k!=lex->ffincludedir.end();
++k)
lex->cout << " -- try :\"" << *k+ff << "\"\n";
lgerror("lex: Error input openning file ");
}
}
void mylex::xxxx::readin(mylex *lex,const string & s,const string *name, int macroargs)
{
filename=name;
macroarg=macroargs;
l=0;
nf=f= new istringstream(s.c_str());
if (!f || !*f) {
lex->cout << " Error readin string :" <<s<< endl;
if(f) delete f;
nf = f =0;
lgerror("lex: Error readin macro ");
}
}
void mylex::xxxx::close()
{
// ALH_BUG Why does this segfault under Windows? Probably needs valgrind to find out.
#if !defined(ENABLE_FFCS) || !defined(WIN32)
if(nf) delete nf;
#endif
if(filename && (macroarg==0)) delete filename; // [[file:lex.hpp::filename]]
f=nf=0;
}