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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
|
/******************************************************************************
*
*
*
* Copyright (C) 1997-2005 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
* Documents produced by Doxygen are derivative works derived from the
* input used in their production; they are not affected by this license.
*
*/
/* This code is based on the work done by the MoxyPyDoxy team
* (Linda Leong, Mike Rivera, Kim Truong, and Gabriel Estrada)
* in Spring 2005 as part of CS 179E: Compiler Design Project
* at the University of California, Riverside; the course was
* taught by Peter H. Froehlich <phf@acm.org>.
*/
%{
#include <stdio.h>
#include <qvaluestack.h>
#include "pycode.h"
#include "message.h"
#include "scanner.h"
#include "entry.h"
#include "doxygen.h"
#include "outputlist.h"
#include "util.h"
#include "membername.h"
#include "searchindex.h"
#define YY_NEVER_INTERACTIVE 1
static ClassSDict g_codeClassSDict(17);
static QCString g_curClassName;
static QStrList g_curClassBases;
static CodeOutputInterface * g_code;
static const char * g_inputString; //!< the code fragment as text
static int g_inputPosition; //!< read offset during parsing
static const char * g_currentFontClass;
static bool g_needsTermination;
static int g_inputLines; //!< number of line in the code fragment
static int g_yyLineNr; //!< current line number
static FileDef * g_sourceFileDef;
static Definition * g_currentDefinition;
static MemberDef * g_currentMemberDef;
static bool g_includeCodeFragment;
static QCString g_realScope;
static bool g_insideBody;
static int g_bodyCurlyCount;
static bool g_searchingForBody;
static QCString g_classScope;
static int g_paramParens;
//static int g_anchorCount;
static bool g_exampleBlock;
static QCString g_exampleName;
static QCString g_exampleFile;
static QCString g_type;
static QCString g_name;
static bool g_doubleStringIsDoc;
static bool g_doubleQuote;
static bool g_noSuiteFound;
static int g_stringContext;
static QValueStack<uint> g_indents; //!< Tracks indentation levels for scoping in python
static void endFontClass();
static void adjustScopesAndSuites(unsigned indentLength);
/*! Represents a stack of variable to class mappings as found in the
* code. Each scope is enclosed in pushScope() and popScope() calls.
* Variables are added by calling addVariables() and one can search
* for variable using findVariable().
*/
class PyVariableContext
{
public:
static const ClassDef *dummyContext;
class Scope : public SDict<ClassDef>
{
public:
Scope() : SDict<ClassDef>(17) {}
};
PyVariableContext()
{
m_scopes.setAutoDelete(TRUE);
}
virtual ~PyVariableContext()
{
}
void pushScope()
{
m_scopes.append(new Scope);
}
void popScope()
{
if (m_scopes.count()>0)
{
m_scopes.remove(m_scopes.count()-1);
}
}
void clear()
{
m_scopes.clear();
m_globalScope.clear();
}
void clearExceptGlobal()
{
m_scopes.clear();
}
void addVariable(const QCString &type,const QCString &name);
ClassDef *findVariable(const QCString &name);
private:
Scope m_globalScope;
QList<Scope> m_scopes;
};
void PyVariableContext::addVariable(const QCString &type,const QCString &name)
{
//printf("PyVariableContext::addVariable(%s,%s)\n",type.data(),name.data());
QCString ltype = type.simplifyWhiteSpace();
QCString lname = name.simplifyWhiteSpace();
Scope *scope = m_scopes.count()==0 ? &m_globalScope : m_scopes.getLast();
ClassDef *varType;
if (
(varType=g_codeClassSDict[ltype]) || // look for class definitions inside the code block
(varType=getResolvedClass(g_currentDefinition,g_sourceFileDef,ltype)) // look for global class definitions
)
{
scope->append(lname,varType); // add it to a list
}
else
{
if (m_scopes.count()>0) // for local variables add a dummy entry so the name
// is hidden to avoid FALSE links to global variables with the same name
// TODO: make this work for namespaces as well!
{
scope->append(lname,dummyContext);
}
}
}
ClassDef *PyVariableContext::findVariable(const QCString &name)
{
if (name.isEmpty()) return 0;
ClassDef *result = 0;
QListIterator<Scope> sli(m_scopes);
Scope *scope;
// search from inner to outer scope
for (sli.toLast();(scope=sli.current());--sli)
{
result = scope->find(name);
if (result)
{
return result;
}
}
// nothing found -> also try the global scope
result=m_globalScope.find(name);
return result;
}
static PyVariableContext g_theVarContext;
const ClassDef *PyVariableContext::dummyContext = (ClassDef*)0x8;
class PyCallContext
{
public:
struct Ctx
{
Ctx() : name(g_name), type(g_type), cd(0) {}
QCString name;
QCString type;
ClassDef *cd;
};
PyCallContext()
{
m_classList.append(new Ctx);
m_classList.setAutoDelete(TRUE);
}
virtual ~PyCallContext() {}
void setClass(ClassDef *cd)
{
Ctx *ctx = m_classList.getLast();
if (ctx)
{
ctx->cd=cd;
}
}
void pushScope()
{
m_classList.append(new Ctx);
}
void popScope()
{
if (m_classList.count()>1)
{
Ctx *ctx = m_classList.getLast();
if (ctx)
{
g_name = ctx->name;
g_type = ctx->type;
}
m_classList.removeLast();
}
else
{
}
}
void clear()
{
m_classList.clear();
m_classList.append(new Ctx);
}
ClassDef *getClass() const
{
Ctx *ctx = m_classList.getLast();
if (ctx)
return ctx->cd;
else
return 0;
}
private:
QList<Ctx> m_classList;
};
static PyCallContext g_theCallContext;
/*! counts the number of lines in the input */
static int countLines()
{
const char *p=g_inputString;
char c;
int count=1;
while ((c=*p))
{
p++ ;
if (c=='\n') count++;
}
if (p>g_inputString && *(p-1)!='\n')
{ // last line does not end with a \n, so we add an extra
// line and explicitly terminate the line after parsing.
count++,
g_needsTermination=TRUE;
}
return count;
}
static void setCurrentDoc(const QCString &name,const QCString &base,const QCString &anchor="")
{
static bool searchEngineEnabled=Config_getBool("SEARCHENGINE");
if (searchEngineEnabled)
{
Doxygen::searchIndex->setCurrentDoc(name,base,anchor);
}
}
static void addToSearchIndex(const char *text)
{
static bool searchEngineEnabled=Config_getBool("SEARCHENGINE");
if (searchEngineEnabled)
{
Doxygen::searchIndex->addWord(text,FALSE);
}
}
static ClassDef *stripClassName(const char *s)
{
int pos=0;
QCString type = s;
QCString className;
QCString templSpec;
while (extractClassNameFromType(type,pos,className,templSpec))
{
QCString clName=className+templSpec;
ClassDef *cd=0;
if (!g_classScope.isEmpty())
{
cd=getResolvedClass(g_currentDefinition,g_sourceFileDef,g_classScope+"::"+clName);
}
if (cd==0)
{
cd=getResolvedClass(g_currentDefinition,g_sourceFileDef,clName);
}
//printf("stripClass trying `%s' = %p\n",clName.data(),cd);
if (cd)
{
return cd;
}
}
return 0;
}
/*! start a new line of code, inserting a line number if g_sourceFileDef
* is TRUE. If a definition starts at the current line, then the line
* number is linked to the documentation of that definition.
*/
static void startCodeLine()
{
//if (g_currentFontClass) { g_code->endFontClass(); }
if (g_sourceFileDef)
{
//QCString lineNumber,lineAnchor;
//lineNumber.sprintf("%05d",g_yyLineNr);
//lineAnchor.sprintf("l%05d",g_yyLineNr);
Definition *d = g_sourceFileDef->getSourceDefinition(g_yyLineNr);
//printf("startCodeLine %d d=%p\n",g_yyLineNr,d);
//g_code->startLineNumber();
if (!g_includeCodeFragment && d && d->isLinkableInProject())
{
g_currentDefinition = d;
g_currentMemberDef = g_sourceFileDef->getSourceMember(g_yyLineNr);
g_insideBody = FALSE;
g_searchingForBody = TRUE;
g_realScope = d->name().copy();
g_classScope = d->name().copy();
//printf("Real scope: `%s'\n",g_realScope.data());
g_bodyCurlyCount = 0;
QCString lineAnchor;
lineAnchor.sprintf("l%05d",g_yyLineNr);
if (g_currentMemberDef)
{
g_code->writeLineNumber(g_currentMemberDef->getReference(),
g_currentMemberDef->getOutputFileBase(),
g_currentMemberDef->anchor(),g_yyLineNr);
setCurrentDoc(
g_currentMemberDef->qualifiedName(),
g_sourceFileDef->getSourceFileBase(),
lineAnchor);
}
else
{
g_code->writeLineNumber(d->getReference(),
d->getOutputFileBase(),
0,g_yyLineNr);
setCurrentDoc(
d->qualifiedName(),
g_sourceFileDef->getSourceFileBase(),
lineAnchor);
}
}
else
{
//g_code->codify(lineNumber);
g_code->writeLineNumber(0,0,0,g_yyLineNr);
}
//g_code->endLineNumber();
}
g_code->startCodeLine();
if (g_currentFontClass)
{
g_code->startFontClass(g_currentFontClass);
}
}
static void codify(char* text)
{
g_code->codify(text);
}
static void endCodeLine()
{
if (g_currentFontClass) { g_code->endFontClass(); }
g_code->endCodeLine();
}
/*! writes a link to a fragment \a text that may span multiple lines, inserting
* line numbers for each line. If \a text contains newlines, the link will be
* split into multiple links with the same destination, one for each line.
*/
static void writeMultiLineCodeLink(CodeOutputInterface &ol,
const char *ref,const char *file,
const char *anchor,const char *text)
{
bool done=FALSE;
char *p=(char *)text;
while (!done)
{
char *sp=p;
char c;
while ((c=*p++) && c!='\n');
if (c=='\n')
{
g_yyLineNr++;
*(p-1)='\0';
//printf("writeCodeLink(%s,%s,%s,%s)\n",ref,file,anchor,sp);
ol.writeCodeLink(ref,file,anchor,sp);
endCodeLine();
if (g_yyLineNr<g_inputLines)
{
startCodeLine();
}
}
else
{
//printf("writeCodeLink(%s,%s,%s,%s)\n",ref,file,anchor,sp);
ol.writeCodeLink(ref,file,anchor,sp);
done=TRUE;
}
}
}
static void codifyLines(char *text)
{
//printf("codifyLines(%d,\"%s\")\n",g_yyLineNr,text);
char *p=text,*sp=p;
char c;
bool done=FALSE;
while (!done)
{
sp=p;
while ((c=*p++) && c!='\n');
if (c=='\n')
{
g_yyLineNr++;
*(p-1)='\0';
g_code->codify(sp);
endCodeLine();
if (g_yyLineNr<g_inputLines)
{
// Re-enable sometime
startCodeLine();
}
}
else
{
g_code->codify(sp);
done=TRUE;
}
}
}
static void addDocCrossReference(MemberDef *src,MemberDef *dst)
{
if (dst->isTypedef() || dst->isEnumerate()) return; // don't add types
//printf("addDocCrossReference src=%s,dst=%s\n",src->name().data(),dst->name().data());
if (Config_getBool("REFERENCED_BY_RELATION") &&
(src->isFunction() || src->isSlot())
)
{
dst->addSourceReferencedBy(src);
}
if ((Config_getBool("REFERENCES_RELATION") || Config_getBool("CALL_GRAPH")) &&
(src->isFunction() || src->isSlot())
)
{
src->addSourceReferences(dst);
}
}
static bool getLinkInScope(const QCString &c, // scope
const QCString &m, // member
const char *memberText, // exact text
CodeOutputInterface &ol,
const char *text
)
{
MemberDef *md;
ClassDef *cd;
FileDef *fd;
NamespaceDef *nd;
GroupDef *gd;
//printf("Trying `%s'::`%s'\n",c.data(),m.data());
if (getDefs(c,m,"()",md,cd,fd,nd,gd,FALSE,g_sourceFileDef) &&
md->isLinkable())
{
//printf("Found!\n");
//Definition *d=0;
//if (cd) d=cd; else if (nd) d=nd; else if (fd) d=fd; else d=gd;
Definition *d = md->getOuterScope()==Doxygen::globalScope ?
md->getBodyDef() : md->getOuterScope();
if (md->getGroupDef()) d = md->getGroupDef();
if (d && d->isLinkable())
{
g_theCallContext.setClass(stripClassName(md->typeString()));
//printf("g_currentDefinition=%p g_currentMemberDef=%p g_insideBody=%d\n",
// g_currentDefinition,g_currentMemberDef,g_insideBody);
if (g_currentDefinition && g_currentMemberDef &&
md!=g_currentMemberDef && g_insideBody)
{
addDocCrossReference(g_currentMemberDef,md);
}
//printf("d->getReference()=`%s' d->getOutputBase()=`%s' name=`%s' member name=`%s'\n",d->getReference().data(),d->getOutputFileBase().data(),d->name().data(),md->name().data());
writeMultiLineCodeLink(ol,md->getReference(),
md->getOutputFileBase(),
md->anchor(),
text ? text : memberText);
addToSearchIndex(text ? text : memberText);
return TRUE;
}
}
return FALSE;
}
static bool getLink(const char *className,
const char *memberName,
CodeOutputInterface &ol,
const char *text=0)
{
QCString m=removeRedundantWhiteSpace(memberName);
QCString c=className;
if (!getLinkInScope(c,m,memberName,ol,text))
{
if (!g_curClassName.isEmpty())
{
if (!c.isEmpty()) c.prepend("::");
c.prepend(g_curClassName);
return getLinkInScope(c,m,memberName,ol,text);
}
return FALSE;
}
return TRUE;
}
/*
For a given string in the source code,
finds its class or global id and links to it.
As of June 1, '05, this ONLY finds classes
*/
static void generateClassOrGlobalLink(CodeOutputInterface &ol,char *clName,
bool /*typeOnly*/=FALSE)
{
QCString className=clName;
// Don't do anything for empty text
if (className.isEmpty()) return;
ClassDef *cd=0,*lcd=0; /** Class def that we may find */
MemberDef *md=0; /** Member def that we may find */
bool isLocal=FALSE;
// printf("generateClassOrGlobalLink(className=%s)\n",className.data());
if ((lcd=g_theVarContext.findVariable(className))==0) // not a local variable
{
Definition *d = g_currentDefinition;
cd = getResolvedClass(d,g_sourceFileDef,className,&md);
//printf("d=%p g_sourceFileDef=%p\n",d,g_currentDefinition);
//printf("is found as a type %s\n",cd?cd->name().data():"<null>");
if (cd==0 && md==0) // also see if it is variable or enum or enum value
{
if (getLink(g_classScope,clName,ol,clName))
{
return;
}
}
}
else
{
if (lcd!=PyVariableContext::dummyContext)
{
g_theCallContext.setClass(lcd);
}
isLocal=TRUE;
//fprintf(stderr,"is a local variable cd=%p!\n",cd);
}
if (cd && cd->isLinkable()) // is it a linkable class
{
writeMultiLineCodeLink(ol,cd->getReference(),cd->getOutputFileBase(),0,clName);
addToSearchIndex(className);
if (md)
{
Definition *d = md->getOuterScope()==Doxygen::globalScope ?
md->getBodyDef() : md->getOuterScope();
if (md->getGroupDef()) d = md->getGroupDef();
if (d && d->isLinkable() && md->isLinkable() && g_currentMemberDef)
{
addDocCrossReference(g_currentMemberDef,md);
}
}
}
else // not a class, maybe a global member
{
/*
This code requires a going-over in order to
make it work for Python
//printf("class %s not linkable! cd=%p md=%p typeOnly=%d\n",clName,cd,md,typeOnly);
if (!isLocal && (md!=0 || (cd==0 && !typeOnly))) // not a class, see if it is a global enum/variable/typedef.
{
if (md==0) // not found as a typedef
{
md = setCallContextForVar(clName);
//printf("setCallContextForVar(%s) md=%p g_currentDefinition=%p\n",clName,md,g_currentDefinition);
if (md && g_currentDefinition)
{
//fprintf(stderr,"%s accessible from %s? %d md->getOuterScope=%s\n",
// md->name().data(),g_currentDefinition->name().data(),
// isAccessibleFrom(g_currentDefinition,g_sourceFileDef,md),
// md->getOuterScope()->name().data());
}
if (md && g_currentDefinition &&
isAccessibleFrom(g_currentDefinition,g_sourceFileDef,md)==-1)
{
md=0; // variable not accessible
}
}
if (md)
{
//printf("is a global md=%p g_currentDefinition=%s\n",md,g_currentDefinition?g_currentDefinition->name().data():"<none>");
if (md->isLinkable())
{
writeMultiLineCodeLink(ol,md->getReference(),md->getOutputFileBase(),md->anchor(),clName);
addToSearchIndex(clName);
if (g_currentMemberDef)
{
addDocCrossReference(g_currentMemberDef,md);
}
return;
}
}
}
*/
// nothing found, just write out the word
codifyLines(clName);
addToSearchIndex(clName);
}
}
/*
As of June 1, this function seems to work
for file members, but scopes are not
being correctly tracked for classes
so it doesn't work for classes yet.
*/
static void generateFunctionLink(CodeOutputInterface &ol,char *funcName)
{
//CodeClassDef *ccd=0;
ClassDef *ccd=0;
QCString locScope=g_classScope.copy();
QCString locFunc=removeRedundantWhiteSpace(funcName);
//fprintf(stdout,"*** locScope=%s locFunc=%s\n",locScope.data(),locFunc.data());
int i=locFunc.findRev("::");
if (i>0)
{
locScope=locFunc.left(i);
locFunc=locFunc.right(locFunc.length()-i-2).stripWhiteSpace();
}
//printf("generateFunctionLink(%s) classScope=`%s'\n",locFunc.data(),locScope.data());
if (!locScope.isEmpty() && (ccd=g_codeClassSDict[locScope]))
{
//printf("using classScope %s\n",g_classScope.data());
BaseClassListIterator bcli(*ccd->baseClasses());
for ( ; bcli.current() ; ++bcli)
{
if (getLink(bcli.current()->classDef->name(),locFunc,ol,funcName))
{
return;
}
}
}
if (!getLink(locScope,locFunc,ol,funcName))
{
generateClassOrGlobalLink(ol,funcName);
}
return;
}
static void findMemberLink(CodeOutputInterface &ol,char *symName)
{
//printf("Member reference: %s scope=%s member=%s\n",
// yytext,
// g_currentDefinition?g_currentDefinition->name().data():"<none>",
// g_currentMemberDef?g_currentMemberDef->name().data():"<none>"
// );
if (g_currentDefinition)
{
DefinitionList *dl = Doxygen::symbolMap->find(symName);
if (dl)
{
DefinitionListIterator dli(*dl);
Definition *sym;
for (dli.toFirst();(sym=dli.current());++dli)
{
//printf("sym %s outerScope=%s equal=%d\n",
// sym->name().data(),sym->getOuterScope()->name().data(),
// sym->getOuterScope()==g_currentDefinition);
if (sym->getOuterScope() &&
sym->getOuterScope()->definitionType()==Definition::TypeClass &&
g_currentDefinition->definitionType()==Definition::TypeClass)
{
ClassDef *cd = (ClassDef*)sym->getOuterScope();
ClassDef *thisCd = (ClassDef *)g_currentDefinition;
QCString anchor;
if (sym->definitionType()==Definition::TypeMember)
{
anchor=((MemberDef *)sym)->anchor();
}
// TODO: find the nearest base class in case cd is a base class of
// thisCd
if (cd==thisCd)
{
writeMultiLineCodeLink(ol,sym->getReference(),
sym->getOutputFileBase(),
anchor,
symName);
return;
}
}
}
}
}
//printf("sym %s not found\n",&yytext[5]);
codify(symName);
}
static void startFontClass(const char *s)
{
endFontClass();
g_code->startFontClass(s);
g_currentFontClass=s;
}
static void endFontClass()
{
if (g_currentFontClass)
{
g_code->endFontClass();
g_currentFontClass=0;
}
}
#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size);
static int yyread(char *buf,int max_size)
{
int c=0;
while( c < max_size && g_inputString[g_inputPosition] )
{
*buf = g_inputString[g_inputPosition++] ;
c++; buf++;
}
return c;
}
%}
BB [ \t]+
B [ \t]*
NEWLINE \n
DIGIT [0-9]
LETTER [A-Za-z]
NONEMPTY [A-Za-z0-9_]
EXPCHAR [#(){}\[\],:.%/\\=`*~|&<>!;+-]
NONEMPTYEXP [^ \t\n:]
PARAMNONEMPTY [^ \t\n():]
IDENTIFIER ({LETTER}|"_")({LETTER}|{DIGIT}|"_")*
BORDER ([^A-Za-z0-9])
POUNDCOMMENT "#".*
TRISINGLEQUOTE "'''"
TRIDOUBLEQUOTE "\"\"\""
LONGSTRINGCHAR [^\\"']
ESCAPESEQ ("\\")(.)
LONGSTRINGITEM ({LONGSTRINGCHAR}|{ESCAPESEQ})
SMALLQUOTE ("\"\""|"\""|"'"|"''")
LONGSTRINGBLOCK ({LONGSTRINGITEM}+|{SMALLQUOTE})
SHORTSTRING ("'"{SHORTSTRINGITEM}*"'"|'"'{SHORTSTRINGITEM}*'"')
SHORTSTRINGITEM ({SHORTSTRINGCHAR}|{ESCAPESEQ})
SHORTSTRINGCHAR [^\\\n"]
STRINGLITERAL {STRINGPREFIX}?( {SHORTSTRING} | {LONGSTRING})
STRINGPREFIX ("r"|"u"|"ur"|"R"|"U"|"UR"|"Ur"|"uR")
KEYWORD ("lambda"|"import"|"class"|"assert"|"as"|"from"|"global"|"def"|"True"|"False")
FLOWKW ("or"|"and"|"is"|"not"|"print"|"for"|"in"|"if"|"try"|"except"|"yield"|"raise"|"break"|"continue"|"pass"|"if"|"return"|"while"|"elif"|"else"|"finally")
QUOTES ("\""[^"]*"\"")
SINGLEQUOTES ("'"[^']*"'")
LONGINTEGER {INTEGER}("l"|"L")
INTEGER ({DECIMALINTEGER}|{OCTINTEGER}|{HEXINTEGER})
DECIMALINTEGER ({NONZERODIGIT}{DIGIT}*|"0")
OCTINTEGER "0"{OCTDIGIT}+
HEXINTEGER "0"("x"|"X"){HEXDIGIT}+
NONZERODIGIT [1-9]
OCTDIGIT [0-7]
HEXDIGIT ({DIGIT}|[a-f]|[A-F])
FLOATNUMBER ({POINTFLOAT}|{EXPONENTFLOAT})
POINTFLOAT ({INTPART}?{FRACTION}|{INTPART}".")
EXPONENTFLOAT ({INTPART}|{POINTFLOAT}){EXPONENT}
INTPART {DIGIT}+
FRACTION "."{DIGIT}+
EXPONENT ("e"|"E")("+"|"-")?{DIGIT}+
IMAGNUMBER ({FLOATNUMBER}|{INTPART})("j"|"J")
ATOM ({IDENTIFIER}|{LITERAL}|{ENCLOSURE})
ENCLOSURE ({PARENTH_FORM}|{LIST_DISPLAY}|{DICT_DISPLAY}|{STRING_CONVERSION})
LITERAL ({STRINGLITERAL}|{INTEGER}|{LONGINTEGER}|{FLOATNUMBER}|{IMAGNUMBER})
PARENTH_FORM "("{EXPRESSION_LIST}?")"
TEST ({AND_TEST}("or"{AND_TEST})*|{LAMBDA_FORM})
TESTLIST {TEST}( ","{TEST})*","?
LIST_DISPLAY "["{LISTMAKER}?"]"
LISTMAKER {EXPRESSION}({LIST_FOR}|(","{EXPRESSION})*","?)
LIST_ITER ({LIST_FOR}|{LIST_IF})
LIST_FOR "for"{EXPRESSION_LIST}"in"{TESTLIST}{LIST_ITER}?
LIST_IF "if"{TEST}{LIST_ITER}?
DICT_DISPLAY "\{"{KEY_DATUM_LIST}?"\}"
KEY_DATUM_LIST {KEY_DATUM}(","{KEY_DATUM})*","?
KEY_DATUM {EXPRESSION}":"{EXPRESSION}
STRING_CONVERSION "`"{EXPRESSION_LIST}"`"
PRIMARY ({ATOM}|{ATTRIBUTEREF}|{SUBSCRIPTION}|{SLICING}|{CALL})
ATTRIBUTEREF {PRIMARY}"."{IDENTIFIER}
SUBSCRIPTION {PRIMARY}"["{EXPRESSION_LIST}"]"
SLICING ({SIMPLE_SLICING}|{EXTENDED_SLICING})
SIMPLE_SLICING {PRIMARY}"["{SHORT_SLICE}"]"
EXTENDED_SLICING {PRIMARY}"["{SLICE_LIST}"]"
SLICE_LIST {SLICE_ITEM}(","{SLICE_ITEM})*","?
SLICE_ITEM ({EXPRESSION}|{PROPER_SLICE}|{ELLIPSIS})
PROPER_SLICE ({SHORT_SLICE}|{LONG_SLICE})
SHORT_SLICE {LOWER_BOUND}?":"{UPPER_BOUND}?
LONG_SLICE {SHORT_SLICE}":"{STRIDE}?
LOWER_BOUND {EXPRESSION}
UPPER_BOUND {EXPRESSION}
STRIDE {EXPRESSION}
ELLIPSIS "..."
CALL {PRIMARY}"("({ARGUMENT_LIST}","?)?")"
ARGUMENT_LIST ({POSITIONAL_ARGUMENTS}(","{KEYWORD_ARGUMENTS})?(",""*"{EXPRESSION})?(",""**"{EXPRESSION})?|{KEYWORD_ARGUMENTS}(",""*"{EXPRESSION})?(",""**"{EXPRESSION})?|"*"{EXPRESSION}(",""**"{EXPRESSION})?|"**"{EXPRESSION})
POSITIONAL_ARGUMENTS {EXPRESSION}(","{EXPRESSION})*
KEYWORD_ARGUMENTS {KEYWORD_ITEM}(","{KEYWORD_ITEM})*
KEYWORD_ITEM {IDENTIFIER}"="{EXPRESSION}
POWER {PRIMARY}("**"{U_EXPR})?
U_EXPR ({POWER}|"-"{U_EXPR}|"+"{U_EXPR}|"\~"{U_EXPR})
M_EXPR ({U_EXPR}|{M_EXPR}"*"{U_EXPR}|{M_EXPR}"//"{U_EXPR}|{M_EXPR}"/"{U_EXPR}|{M_EXPR}"\%"{U_EXPR})
A_EXPR ({M_EXPR}|{A_EXPR}"+"{M_EXPR}|{A_EXPR}"-"{M_EXPR}
SHIFT_EXPR ({A_EXPR}|{SHIFT_EXPR}("<<"|">>"){A_EXPR})
AND_EXPR ({SHIFT_EXPR}|{AND_EXPR}"\;SPMamp;"{SHIFT_EXPR}
XOR_EXPR ({AND_EXPR}|{XOR_EXPR}"\textasciicircum"{AND_EXPR})
OR_EXPR ({XOR_EXPR}|{OR_EXPR}"|"{ XOR_EXPR})
COMPARISON {OR_EXPR}({COMP_OPERATOR}{OR_EXPR})*
COMP_OPERATOR ("<"|">"|"=="|">="|"<="|"<>"|"!="|"is""not"?|"not"?"in")
EXPRESSION ({OR_TEST}|{LAMBDA_FORM})
OR_TEST ({AND_TEST}|{OR_TEST}"or"{AND_TEST})
AND_TEST ({NOT_TEST}|{AND_TEST}"and"{NOT_TEST})
NOT_TEST ({COMPARISON}|"not"{NOT_TEST})
LAMBDA_FORM "lambda"{PARAMETER_LIST}?":"{EXPRESSION}
EXPRESSION_LIST {EXPRESSION}(","{EXPRESSION})*","?
SIMPLE_STMT ({EXPRESSION_STMT}|{ASSERT_STMT}|{ASSIGNMENT_STMT}|{AUGMENTED_ASSIGNMENT_STMT}|{PASS_STMT}|{DEL_STMT}|{PRINT_STMT}|{RETURN_STMT}|{YIELD_STMT}|{RAISE_STMT}|{BREAK_STMT}|{CONTINUE_STMT}|{IMPORT_STMT}|{GLOBAL_STMT}|{EXEC_STMT})
EXPRESSION_STMT {EXPRESSION_LIST}
ASSERT_STMT "assert"{EXPRESSION}(","{EXPRESSION})?
ASSIGNMENT_STMT ({TARGET_LIST}"=")+{EXPRESSION_LIST}
TARGET_LIST {TARGET}(","{TARGET})*","?
TARGET ({IDENTIFIER}|"("{TARGET_LIST}")"|"["{TARGET_LIST}"]"|{ATTRIBUTEREF}|{SUBSCRIPTION}|{SLICING})
%option noyywrap
%option nounput
%x Body
%x FunctionDec
%x FunctionParams
%x ClassDec
%x ClassInheritance
%x Suite
%x SuiteCaptureIndent
%x SuiteStart
%x SuiteMaintain
%x SuiteContinuing
%x LongString
%x SingleQuoteString
%x DoubleQuoteString
%x TripleString
%%
<Body,Suite>{
"def"{BB} {
startFontClass("keyword");
codify(yytext);
endFontClass();
BEGIN( FunctionDec );
}
"class"{BB} {
startFontClass("keyword");
codify(yytext);
endFontClass();
BEGIN( ClassDec );
}
"None" {
startFontClass("keywordtype");
codify(yytext);
endFontClass();
}
"self."{IDENTIFIER} {
codify("self.");
findMemberLink(*g_code,&yytext[5]);
}
}
<ClassDec>{IDENTIFIER} {
generateClassOrGlobalLink(*g_code,yytext);
// codify(yytext);
g_curClassName = yytext;
g_curClassBases.clear();
BEGIN( ClassInheritance );
}
<ClassInheritance>{
({BB}|[(,)]) {
codify(yytext);
}
{IDENTIFIER} {
// The parser
// is assuming
// that ALL identifiers
// in this state
// are base classes;
// it doesn't check to see
// that the first parenthesis
// has been seen.
// This is bad - it should
// probably be more strict
// about what to accept.
g_curClassBases.inSort(yytext);
generateClassOrGlobalLink(*g_code,yytext);
// codify(yytext);
}
":" {
codify(yytext);
// Assume this will
// be a one-line suite;
// found counter-example
// in SuiteStart.
// Push a class scope
ClassDef *classDefToAdd = new ClassDef("<code>",1,g_curClassName,ClassDef::Class,0,0,FALSE);
g_codeClassSDict.append(g_curClassName,classDefToAdd);
char *s=g_curClassBases.first();
while (s)
{
ClassDef *baseDefToAdd;
baseDefToAdd=g_codeClassSDict[s];
// Try to find class in global
// scope
if (baseDefToAdd==0)
{
baseDefToAdd=getResolvedClass(g_currentDefinition,g_sourceFileDef,s);
}
if (baseDefToAdd && baseDefToAdd!=classDefToAdd)
{
classDefToAdd->insertBaseClass(baseDefToAdd,s,Public,Normal);
}
s=g_curClassBases.next();
}
// Reset class-parsing variables.
g_curClassName.resize(0);
g_curClassBases.clear();
g_noSuiteFound = TRUE;
BEGIN( SuiteStart );
}
}
<FunctionDec>{
{IDENTIFIER} {
generateFunctionLink(*g_code,yytext);
}
{B}"(" {
codify(yytext);
BEGIN( FunctionParams );
}
}
<FunctionParams>{
({BB}|",") {
// Parses delimiters
codify(yytext);
}
({IDENTIFIER}|{PARAMNONEMPTY}+) {
codify(yytext);
}
")" {
codify(yytext);
}
":" {
codify(yytext);
// Assume this will
// be a one-line suite;
// found counter-example
// in SuiteStart.
g_noSuiteFound = TRUE;
BEGIN( SuiteStart );
}
}
<Body,Suite>{
{KEYWORD} {
// Position-sensitive rules!
// Must come AFTER keyword-triggered rules
// Must come BEFORE identifier NONEMPTY-like rules
// to syntax highlight.
startFontClass("keyword");
codify(yytext);
endFontClass();
}
{FLOWKW} {
startFontClass("keywordflow");
codify(yytext);
endFontClass();
}
{IDENTIFIER} {
codify(yytext);
}
}
<SuiteStart>{
{BB} {
codify(yytext);
}
{KEYWORD} {
startFontClass("keyword");
codifyLines(yytext);
endFontClass();
// No indentation necesary
g_noSuiteFound = FALSE;
}
{FLOWKW} {
startFontClass("keywordflow");
codifyLines(yytext);
endFontClass();
// No indentation necesary
g_noSuiteFound = FALSE;
}
{IDENTIFIER} {
codify(yytext);
}
{POUNDCOMMENT} {
// This eats EVERYTHING
// except the newline
startFontClass("comment");
codifyLines(yytext);
endFontClass();
}
{NEWLINE} {
codifyLines(yytext);
if ( g_noSuiteFound )
{
// printf("New suite to capture! [%d]\n", g_yyLineNr);
BEGIN ( SuiteCaptureIndent );
}
}
}
<SuiteCaptureIndent>{
"\n"|({BB}"\n") {
// Blankline - ignore, keep looking for indentation.
codifyLines(yytext);
}
{BB} {
// This state lasts momentarily,
// to check the indentation
// level that is about to be
// used.
codifyLines(yytext);
g_indents.push(yyleng);
// printf("Captured indent of %d [line %d]\n", yyleng, g_yyLineNr);
BEGIN( Suite );
}
}
<SuiteMaintain>{
{BB}/({NONEMPTY}|{EXPCHAR}) {
// This implements poor
// indendation-tracking;
// should be improved.
// (translate tabs to space, etc)
codifyLines(yytext);
adjustScopesAndSuites(yyleng);
}
"\n"|({BB}"\n") {
// If this ever succeeds,
// it means that this is
// a blank line, and
// can be ignored.
codifyLines(yytext);
}
""/({NONEMPTY}|{EXPCHAR}) {
// Default rule; matches
// the empty string, assuming
// real text starts here.
// Just go straight to Body.
adjustScopesAndSuites(0);
}
}
<Suite>{NEWLINE} {
codifyLines(yytext);
BEGIN( SuiteMaintain );
}
<Body>{IDENTIFIER} {
codify(yytext);
}
<Body>{NEWLINE} {
codifyLines(yytext);
}
<SingleQuoteString>{ // Single quoted string like 'That\'s a """nice""" string!'
\\{B}\n { // line continuation
codifyLines(yytext);
}
\\. { // espaced char
codify(yytext);
}
{STRINGPREFIX}?{TRIDOUBLEQUOTE} { // tripple double quotes
codify(yytext);
}
"'" { // end of the string
codify(yytext);
endFontClass();
BEGIN(g_stringContext);
}
[^"'\n\\]+ { // normal chars
codify(yytext);
}
. { // normal char
codify(yytext);
}
}
<DoubleQuoteString>{ // Double quoted string like "That's \"a '''nice'''\" string!"
\\{B}\n { // line continuation
codifyLines(yytext);
}
\\. { // espaced char
codify(yytext);
}
{STRINGPREFIX}?{TRISINGLEQUOTE} { // tripple single quotes
codify(yytext);
}
"\"" { // end of the string
codify(yytext);
endFontClass();
BEGIN(g_stringContext);
}
[^"'\n\\]+ { // normal chars
codify(yytext);
}
. { // normal char
codify(yytext);
}
}
<TripleString>{
{TRIDOUBLEQUOTE} |
{TRISINGLEQUOTE} {
codify(yytext);
if (g_doubleQuote==(yytext[0]=='"'))
{
endFontClass();
BEGIN(g_stringContext);
}
}
{LONGSTRINGBLOCK} {
codifyLines(yytext);
}
\n {
codifyLines(yytext);
}
. {
codify(yytext);
}
}
/*
<*>({NONEMPTY}|{EXPCHAR}|{BB}) { // This should go one character at a time.
codify(yytext);
// printf("[pycode] '%s' [ state %d ] [line %d] no match\n",
// yytext, YY_START, g_yyLineNr);
//endFontClass();
BEGIN(Body);
}
*/
<*>{STRINGPREFIX}?{TRISINGLEQUOTE} |
<*>{STRINGPREFIX}?{TRIDOUBLEQUOTE} {
startFontClass("stringliteral");
g_stringContext=YY_START;
g_doubleQuote=yytext[yyleng-1]=='"';
codify(yytext);
BEGIN(TripleString);
}
<*>{STRINGPREFIX}?"'" { // single quoted string
startFontClass("stringliteral");
g_stringContext=YY_START;
codify(yytext);
BEGIN(SingleQuoteString);
}
<*>{STRINGPREFIX}?"\"" { // double quoted string
startFontClass("stringliteral");
g_stringContext=YY_START;
codify(yytext);
BEGIN(DoubleQuoteString);
}
<*>{POUNDCOMMENT} {
if (YY_START==SingleQuoteString ||
YY_START==DoubleQuoteString ||
YY_START==TripleString
)
{
REJECT;
}
// This eats EVERYTHING
// except the newline
startFontClass("comment");
codifyLines(yytext);
endFontClass();
}
<*>{NEWLINE} {
codifyLines(yytext);
//printf("[pycode] %d NEWLINE [line %d] no match\n",
// YY_START, g_yyLineNr);
//endFontClass();
BEGIN(Body);
}
<*>. {
codify(yytext);
// printf("[pycode] '%s' [ state %d ] [line %d] no match\n",
// yytext, YY_START, g_yyLineNr);
//endFontClass();
BEGIN(Body);
}
%%
/*@ ----------------------------------------------------------------------------
*/
void resetPythonCodeParserState()
{
g_currentDefinition = 0;
g_currentMemberDef = 0;
g_doubleStringIsDoc = FALSE;
g_paramParens = 0;
g_indents.clear();
BEGIN( Body );
}
/*!
Examines current stack of white-space indentations;
re-syncs the parser with the correct scope.
*/
static void adjustScopesAndSuites(unsigned indentLength)
{
// States to pop
if (!g_indents.isEmpty() && indentLength < g_indents.top())
{
while (!g_indents.isEmpty() && indentLength < g_indents.top())
{
// printf("Exited scope indent of [%d]\n", g_indents.top());
g_indents.pop(); // Pop the old suite's indentation
g_currentMemberDef=0;
if (g_currentDefinition)
g_currentDefinition=g_currentDefinition->getOuterScope();
}
}
// Are there any remaining indentation levels for suites?
if (!g_indents.isEmpty())
{
BEGIN( Suite );
}
else
{
BEGIN( Body );
}
}
void parsePythonCode(CodeOutputInterface &od,const char *className,
const QCString &s,bool exBlock, const char *exName,
FileDef *fd,int startLine,int endLine,bool inlineFragment,
MemberDef *)
{
//printf("***parseCode()\n");
//--- some code to eliminate warnings---
className = "";
exBlock = FALSE;
exName = "";
inlineFragment = "";
//--------------------------------------
if (s.isEmpty()) return;
g_code = &od;
g_inputString = s;
g_inputPosition = 0;
g_currentFontClass = 0;
g_needsTermination = FALSE;
if (endLine!=-1)
g_inputLines = endLine+1;
else
g_inputLines = countLines();
if (startLine!=-1)
g_yyLineNr = startLine;
else
g_yyLineNr = 1;
g_exampleBlock = exBlock;
g_exampleName = exName;
g_sourceFileDef = fd;
// Starts line 1 on the output
startCodeLine();
pycodeYYrestart( pycodeYYin );
pycodeYYlex();
if (!g_indents.isEmpty())
{
// printf("Exited pysourceparser in inconsistent state!\n");
}
if (g_needsTermination)
{
endFontClass();
g_code->endCodeLine();
}
return;
}
#if !defined(YY_FLEX_SUBMINOR_VERSION)
extern "C" { // some bogus code to keep the compiler happy
void pycodeYYdummy() { yy_flex_realloc(0,0); }
}
#else
#error "You seem to be using a version of flex newer than 2.5.4. These are currently incompatible with 2.5.4, and do NOT work with doxygen! Please use version 2.5.4 or expect things to be parsed wrongly! A bug report has been submitted (#732132)."
#endif
|