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
|
/* $Id: scmparser.cpp $ */
/** @file
* IPRT Testcase / Tool - Source Code Massager, Code Parsers.
*/
/*
* Copyright (C) 2010-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/dir.h>
#include <iprt/env.h>
#include <iprt/file.h>
#include <iprt/errcore.h>
#include <iprt/getopt.h>
#include <iprt/initterm.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/param.h>
#include <iprt/path.h>
#include <iprt/process.h>
#include <iprt/stream.h>
#include <iprt/string.h>
#include "scm.h"
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
typedef size_t (*PFNISCOMMENT)(const char *pchLine, size_t cchLine, bool fSecond);
/**
* Callback for checking if C++ line comment.
*/
static size_t isCppLineComment(const char *pchLine, size_t cchLine, bool fSecond)
{
if ( cchLine >= 2
&& pchLine[0] == '/'
&& pchLine[1] == '/')
{
if (!fSecond)
return 2;
if (cchLine >= 3 && pchLine[2] == '/')
return 3;
}
return 0;
}
/**
* Callback for checking if hash comment.
*/
static size_t isHashComment(const char *pchLine, size_t cchLine, bool fSecond)
{
if (cchLine >= 1 && *pchLine == '#')
{
if (!fSecond)
return 1;
if (cchLine >= 2 && pchLine[1] == '#')
return 2;
}
return 0;
}
/**
* Callback for checking if semicolon comment.
*/
static size_t isSemicolonComment(const char *pchLine, size_t cchLine, bool fSecond)
{
if (cchLine >= 1 && *pchLine == ';')
{
if (!fSecond)
return 1;
if (cchLine >= 2 && pchLine[1] == ';')
return 2;
}
return 0;
}
/** Macro for checking for a batch file comment prefix. */
#define IS_REM(a_pch, a_off, a_cch) \
( (a_off) + 3 <= (a_cch) \
&& ((a_pch)[(a_off) ] == 'R' || (a_pch)[(a_off) ] == 'r') \
&& ((a_pch)[(a_off) + 1] == 'E' || (a_pch)[(a_off) + 1] == 'e') \
&& ((a_pch)[(a_off) + 2] == 'M' || (a_pch)[(a_off) + 2] == 'm') \
&& ((a_off) + 3 == (a_cch) || RT_C_IS_SPACE((a_pch)[(a_off) + 3])) )
/**
* Callback for checking if batch comment.
*/
static size_t isBatchComment(const char *pchLine, size_t cchLine, bool fSecond)
{
if (!fSecond)
{
if (IS_REM(pchLine, 0, cchLine))
return 3;
}
else
{
/* Check for the 2nd in "rem rem" lines. */
if ( cchLine >= 4
&& RT_C_IS_SPACE(*pchLine)
&& IS_REM(pchLine, 1, cchLine))
return 4;
}
return 0;
}
/**
* Callback for checking if SQL comment.
*/
static size_t isSqlComment(const char *pchLine, size_t cchLine, bool fSecond)
{
if ( cchLine >= 2
&& pchLine[0] == '-'
&& pchLine[1] == '-')
{
if (!fSecond)
return 2;
if ( cchLine >= 3
&& pchLine[2] == '-')
return 3;
}
return 0;
}
/**
* Callback for checking if tick comment.
*/
static size_t isTickComment(const char *pchLine, size_t cchLine, bool fSecond)
{
if (cchLine >= 1 && *pchLine == '\'')
{
if (!fSecond)
return 1;
if (cchLine >= 2 && pchLine[1] == '\'')
return 2;
}
return 0;
}
/**
* Common worker for enumeratePythonComments and enumerateSimpleLineComments.
*
* @returns IPRT status code.
* @param pIn The input stream.
* @param pfnIsComment Comment tester function.
* @param pfnCallback The callback.
* @param pvUser The user argument for the callback.
* @param ppchLine Pointer to the line variable.
* @param pcchLine Pointer to the line length variable.
* @param penmEol Pointer to the line ending type variable.
* @param piLine Pointer to the line number variable.
* @param poff Pointer to the line offset variable. On input this
* is positioned at the start of the comment.
*/
static int handleLineComment(PSCMSTREAM pIn, PFNISCOMMENT pfnIsComment,
PFNSCMCOMMENTENUMERATOR pfnCallback, void *pvUser,
const char **ppchLine, size_t *pcchLine, PSCMEOL penmEol,
uint32_t *piLine, size_t *poff)
{
/* Unpack input/output variables. */
uint32_t iLine = *piLine;
const char *pchLine = *ppchLine;
size_t cchLine = *pcchLine;
size_t off = *poff;
SCMEOL enmEol = *penmEol;
/*
* Take down the basic info about the comment.
*/
SCMCOMMENTINFO Info;
Info.iLineStart = iLine;
Info.iLineEnd = iLine;
Info.offStart = (uint32_t)off;
Info.offEnd = (uint32_t)cchLine;
size_t cchSkip = pfnIsComment(&pchLine[off], cchLine - off, false);
Assert(cchSkip > 0);
off += cchSkip;
/* Determin comment type. */
Info.enmType = kScmCommentType_Line;
char ch;
cchSkip = 1;
if ( off < cchLine
&& ( (ch = pchLine[off]) == '!'
|| (cchSkip = pfnIsComment(&pchLine[off], cchLine - off, true)) > 0) )
{
unsigned ch2;
if ( off + cchSkip == cchLine
|| RT_C_IS_SPACE(ch2 = pchLine[off + cchSkip]) )
{
Info.enmType = ch != '!' ? kScmCommentType_Line_JavaDoc : kScmCommentType_Line_Qt;
off += cchSkip;
}
else if ( ch2 == '<'
&& ( off + cchSkip + 1 == cchLine
|| RT_C_IS_SPACE(pchLine[off + cchSkip + 1]) ))
{
Info.enmType = ch == '!' ? kScmCommentType_Line_JavaDoc_After : kScmCommentType_Line_Qt_After;
off += cchSkip + 1;
}
}
/*
* Copy body of the first line. Like for C, we ignore a single space in the first comment line.
*/
if (off < cchLine && RT_C_IS_SPACE(pchLine[off]))
off++;
size_t cchBody = cchLine;
while (cchBody > off && RT_C_IS_SPACE(pchLine[cchBody - 1]))
cchBody--;
cchBody -= off;
size_t cbBodyAlloc = RT_MAX(_1K, RT_ALIGN_Z(cchBody + 64, 128));
char *pszBody = (char *)RTMemAlloc(cbBodyAlloc);
if (!pszBody)
return VERR_NO_MEMORY;
memcpy(pszBody, &pchLine[off], cchBody);
pszBody[cchBody] = '\0';
Info.cBlankLinesBefore = cchBody == 0;
/*
* Look for more comment lines and append them to the body.
*/
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
iLine++;
/* Skip leading spaces. */
off = 0;
while (off < cchLine && RT_C_IS_SPACE(pchLine[off]))
off++;
/* Check if it's a comment. */
if ( off >= cchLine
|| (cchSkip = pfnIsComment(&pchLine[off], cchLine - off, false)) == 0)
break;
off += cchSkip;
/* Split on doxygen comment start (if not already in one). */
if ( Info.enmType == kScmCommentType_Line
&& off + 1 < cchLine
&& ( pfnIsComment(&pchLine[off], cchLine - off, true) > 0
|| ( pchLine[off + 1] == '!'
&& ( off + 2 == cchLine
|| pchLine[off + 2] != '!') ) ) )
{
off -= cchSkip;
break;
}
/* Append the body w/o trailing spaces and some leading ones. */
if (off < cchLine && RT_C_IS_SPACE(pchLine[off]))
off++;
while (off < cchLine && off < Info.offStart + 3 && RT_C_IS_SPACE(pchLine[off]))
off++;
size_t cchAppend = cchLine;
while (cchAppend > off && RT_C_IS_SPACE(pchLine[cchAppend - 1]))
cchAppend--;
cchAppend -= off;
size_t cchNewBody = cchBody + 1 + cchAppend;
if (cchNewBody >= cbBodyAlloc)
{
cbBodyAlloc = RT_MAX(cbBodyAlloc ? cbBodyAlloc * 2 : _1K, RT_ALIGN_Z(cchNewBody + 64, 128));
void *pvNew = RTMemRealloc(pszBody, cbBodyAlloc);
if (pvNew)
pszBody = (char *)pvNew;
else
{
RTMemFree(pszBody);
return VERR_NO_MEMORY;
}
}
if ( cchBody > 0
|| cchAppend > 0)
{
if (cchBody > 0)
pszBody[cchBody++] = '\n';
memcpy(&pszBody[cchBody], &pchLine[off], cchAppend);
cchBody += cchAppend;
pszBody[cchBody] = '\0';
}
else
Info.cBlankLinesBefore++;
/* Advance. */
Info.offEnd = (uint32_t)cchLine;
Info.iLineEnd = iLine;
}
/*
* Strip trailing empty lines in the body.
*/
Info.cBlankLinesAfter = 0;
while (cchBody >= 1 && pszBody[cchBody - 1] == '\n')
{
Info.cBlankLinesAfter++;
pszBody[--cchBody] = '\0';
}
/*
* Do the callback and return.
*/
int rc = pfnCallback(&Info, pszBody, cchBody, pvUser);
RTMemFree(pszBody);
*piLine = iLine;
*ppchLine = pchLine;
*pcchLine = cchLine;
*poff = off;
*penmEol = enmEol;
return rc;
}
/**
* Common string litteral handler.
*
* @returns new pchLine value.
* @param pIn The input string.
* @param chType The quotation type.
* @param pchLine The current line.
* @param ppchLine Pointer to the line variable.
* @param pcchLine Pointer to the line length variable.
* @param penmEol Pointer to the line ending type variable.
* @param piLine Pointer to the line number variable.
* @param poff Pointer to the line offset variable.
*/
static const char *handleStringLitteral(PSCMSTREAM pIn, char chType, const char *pchLine, size_t *pcchLine, PSCMEOL penmEol,
uint32_t *piLine, size_t *poff)
{
size_t off = *poff;
for (;;)
{
bool fEnd = false;
bool fEscaped = false;
size_t const cchLine = *pcchLine;
while (off < cchLine)
{
char ch = pchLine[off++];
if (!fEscaped)
{
if (ch != chType)
{
if (ch != '\\')
{ /* likely */ }
else
fEscaped = true;
}
else
{
fEnd = true;
break;
}
}
else
fEscaped = false;
}
if (fEnd)
break;
/* next line */
pchLine = ScmStreamGetLine(pIn, pcchLine, penmEol);
if (!pchLine)
break;
*piLine += 1;
off = 0;
}
*poff = off;
return pchLine;
}
/**
* Deals with comments in C and C++ code.
*
* @returns VBox status code / callback return code.
* @param pIn The stream to parse.
* @param pfnCallback The callback.
* @param pvUser The user parameter for the callback.
*/
static int enumerateCStyleComments(PSCMSTREAM pIn, PFNSCMCOMMENTENUMERATOR pfnCallback, void *pvUser)
{
AssertCompile('\'' < '/');
AssertCompile('"' < '/');
int rcRet = VINF_SUCCESS;
uint32_t iLine = 0;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
size_t off = 0;
while (off < cchLine)
{
unsigned ch = pchLine[off++];
if (ch > (unsigned)'/')
{ /* not interesting */ }
else if (ch == '/')
{
if (off < cchLine)
{
ch = pchLine[off++];
if (ch == '*')
{
/*
* Multiline comment. Find the end.
*
* Note! This is very similar to the python doc string handling further down.
*/
SCMCOMMENTINFO Info;
Info.iLineStart = iLine;
Info.offStart = (uint32_t)off - 2;
Info.iLineEnd = UINT32_MAX;
Info.offEnd = UINT32_MAX;
Info.cBlankLinesBefore = 0;
/* Determin comment type (same as for line-comments). */
Info.enmType = kScmCommentType_MultiLine;
if ( off < cchLine
&& ( (ch = pchLine[off]) == '*'
|| ch == '!') )
{
unsigned ch2;
if ( off + 1 == cchLine
|| RT_C_IS_SPACE(ch2 = pchLine[off + 1]) )
{
Info.enmType = ch == '*' ? kScmCommentType_MultiLine_JavaDoc : kScmCommentType_MultiLine_Qt;
off += 1;
}
else if ( ch2 == '<'
&& ( off + 2 == cchLine
|| RT_C_IS_SPACE(pchLine[off + 2]) ))
{
Info.enmType = ch == '*' ? kScmCommentType_MultiLine_JavaDoc_After
: kScmCommentType_MultiLine_Qt_After;
off += 2;
}
}
/*
* Copy the body and find the end of the multiline comment.
*/
size_t cbBodyAlloc = 0;
size_t cchBody = 0;
char *pszBody = NULL;
for (;;)
{
/* Parse the line up to the end-of-comment or end-of-line. */
size_t offLineStart = off;
size_t offLastNonBlank = off;
size_t offFirstNonBlank = ~(size_t)0;
while (off < cchLine)
{
ch = pchLine[off++];
if (ch != '*' || off >= cchLine || pchLine[off] != '/')
{
if (RT_C_IS_BLANK(ch))
{/* kind of likely */}
else
{
offLastNonBlank = off - 1;
if (offFirstNonBlank != ~(size_t)0)
{/* likely */}
else if ( ch != '*' /* ignore continuation-asterisks */
|| off > Info.offStart + 1 + 1
|| off > cchLine
|| ( off < cchLine
&& !RT_C_IS_SPACE(pchLine[off]))
|| pszBody == NULL)
offFirstNonBlank = off - 1;
}
}
else
{
Info.offEnd = (uint32_t)++off;
Info.iLineEnd = iLine;
break;
}
}
/* Append line content to the comment body string. */
size_t cchAppend;
if (offFirstNonBlank == ~(size_t)0)
cchAppend = 0; /* empty line */
else
{
if (pszBody)
offLineStart = RT_MIN(Info.offStart + 3, offFirstNonBlank);
else if (offFirstNonBlank > Info.offStart + 2) /* Skip one leading blank at the start of the comment. */
offLineStart++;
cchAppend = offLastNonBlank + 1 - offLineStart;
Assert(cchAppend <= cchLine);
}
size_t cchNewBody = cchBody + (cchBody > 0) + cchAppend;
if (cchNewBody >= cbBodyAlloc)
{
cbBodyAlloc = RT_MAX(cbBodyAlloc ? cbBodyAlloc * 2 : _1K, RT_ALIGN_Z(cchNewBody + 64, 128));
void *pvNew = RTMemRealloc(pszBody, cbBodyAlloc);
if (pvNew)
pszBody = (char *)pvNew;
else
{
RTMemFree(pszBody);
return VERR_NO_MEMORY;
}
}
if (cchBody > 0) /* no leading blank lines */
pszBody[cchBody++] = '\n';
else if (cchAppend == 0)
Info.cBlankLinesBefore++;
memcpy(&pszBody[cchBody], &pchLine[offLineStart], cchAppend);
cchBody += cchAppend;
pszBody[cchBody] = '\0';
/* Advance to the next line, if we haven't yet seen the end of this comment. */
if (Info.iLineEnd != UINT32_MAX)
break;
pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol);
if (!pchLine)
{
Info.offEnd = (uint32_t)cchLine;
Info.iLineEnd = iLine;
break;
}
iLine++;
off = 0;
}
/* Strip trailing empty lines in the body. */
Info.cBlankLinesAfter = 0;
while (cchBody >= 1 && pszBody[cchBody - 1] == '\n')
{
Info.cBlankLinesAfter++;
pszBody[--cchBody] = '\0';
}
/* Do the callback. */
int rc = pfnCallback(&Info, pszBody, cchBody, pvUser);
RTMemFree(pszBody);
if (RT_FAILURE(rc))
return rc;
if (rc > VINF_SUCCESS && rcRet == VINF_SUCCESS)
rcRet = rc;
}
else if (ch == '/')
{
/*
* Line comment. Join the other line comment guys.
*/
off -= 2;
int rc = handleLineComment(pIn, isCppLineComment, pfnCallback, pvUser,
&pchLine, &cchLine, &enmEol, &iLine, &off);
if (RT_FAILURE(rc))
return rc;
if (rcRet == VINF_SUCCESS)
rcRet = rc;
}
if (!pchLine)
break;
}
}
else if (ch == '"')
{
/*
* String litterals may include sequences that looks like comments. So,
* they needs special handling to avoid confusion.
*/
pchLine = handleStringLitteral(pIn, '"', pchLine, &cchLine, &enmEol, &iLine, &off);
}
/* else: We don't have to deal with character litterals as these shouldn't
include comment-like sequences. */
} /* for each character in the line */
iLine++;
} /* for each line in the stream */
int rcStream = ScmStreamGetStatus(pIn);
if (RT_SUCCESS(rcStream))
return rcRet;
return rcStream;
}
/**
* Deals with comments in Python code.
*
* @returns VBox status code / callback return code.
* @param pIn The stream to parse.
* @param pfnCallback The callback.
* @param pvUser The user parameter for the callback.
*/
static int enumeratePythonComments(PSCMSTREAM pIn, PFNSCMCOMMENTENUMERATOR pfnCallback, void *pvUser)
{
AssertCompile('#' < '\'');
AssertCompile('"' < '\'');
int rcRet = VINF_SUCCESS;
uint32_t iLine = 0;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
SCMCOMMENTINFO Info;
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
size_t off = 0;
while (off < cchLine)
{
char ch = pchLine[off++];
if ((unsigned char)ch > (unsigned char)'\'')
{ /* not interesting */ }
else if (ch == '#')
{
/*
* Line comment. Join paths with the others.
*/
off -= 1;
int rc = handleLineComment(pIn, isHashComment, pfnCallback, pvUser,
&pchLine, &cchLine, &enmEol, &iLine, &off);
if (RT_FAILURE(rc))
return rc;
if (rcRet == VINF_SUCCESS)
rcRet = rc;
if (!pchLine)
break;
}
else if (ch == '"' || ch == '\'')
{
/*
* String litterals may be doc strings and they may legally include hashes.
*/
const char chType = ch;
if ( off + 1 >= cchLine
|| pchLine[off] != chType
|| pchLine[off + 1] != chType)
pchLine = handleStringLitteral(pIn, chType, pchLine, &cchLine, &enmEol, &iLine, &off);
else
{
/*
* Doc string (/ long string).
*
* Note! This is very similar to the multiline C comment handling above.
*/
Info.iLineStart = iLine;
Info.offStart = (uint32_t)off - 1;
Info.iLineEnd = UINT32_MAX;
Info.offEnd = UINT32_MAX;
Info.cBlankLinesBefore = 0;
Info.enmType = kScmCommentType_DocString;
off += 2;
/* Copy the body and find the end of the doc string comment. */
size_t cbBodyAlloc = 0;
size_t cchBody = 0;
char *pszBody = NULL;
for (;;)
{
/* Parse the line up to the end-of-comment or end-of-line. */
size_t offLineStart = off;
size_t offLastNonBlank = off;
size_t offFirstNonBlank = ~(size_t)0;
bool fEscaped = false;
while (off < cchLine)
{
ch = pchLine[off++];
if (!fEscaped)
{
if ( off + 1 >= cchLine
|| ch != chType
|| pchLine[off] != chType
|| pchLine[off + 1] != chType)
{
if (RT_C_IS_BLANK(ch))
{/* kind of likely */}
else
{
offLastNonBlank = off - 1;
if (offFirstNonBlank != ~(size_t)0)
{/* likely */}
else if ( ch != '*' /* ignore continuation-asterisks */
|| off > Info.offStart + 1 + 1
|| off > cchLine
|| ( off < cchLine
&& !RT_C_IS_SPACE(pchLine[off]))
|| pszBody == NULL)
offFirstNonBlank = off - 1;
if (ch != '\\')
{/* likely */ }
else
fEscaped = true;
}
}
else
{
off += 2;
Info.offEnd = (uint32_t)off;
Info.iLineEnd = iLine;
break;
}
}
else
fEscaped = false;
}
/* Append line content to the comment body string. */
size_t cchAppend;
if (offFirstNonBlank == ~(size_t)0)
cchAppend = 0; /* empty line */
else
{
if (pszBody)
offLineStart = RT_MIN(Info.offStart + 3, offFirstNonBlank);
else if (offFirstNonBlank > Info.offStart + 2) /* Skip one leading blank at the start of the comment. */
offLineStart++;
cchAppend = offLastNonBlank + 1 - offLineStart;
Assert(cchAppend <= cchLine);
}
size_t cchNewBody = cchBody + (cchBody > 0) + cchAppend;
if (cchNewBody >= cbBodyAlloc)
{
cbBodyAlloc = RT_MAX(cbBodyAlloc ? cbBodyAlloc * 2 : _1K, RT_ALIGN_Z(cchNewBody + 64, 128));
void *pvNew = RTMemRealloc(pszBody, cbBodyAlloc);
if (pvNew)
pszBody = (char *)pvNew;
else
{
RTMemFree(pszBody);
return VERR_NO_MEMORY;
}
}
if (cchBody > 0) /* no leading blank lines */
pszBody[cchBody++] = '\n';
else if (cchAppend == 0)
Info.cBlankLinesBefore++;
memcpy(&pszBody[cchBody], &pchLine[offLineStart], cchAppend);
cchBody += cchAppend;
pszBody[cchBody] = '\0';
/* Advance to the next line, if we haven't yet seen the end of this comment. */
if (Info.iLineEnd != UINT32_MAX)
break;
pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol);
if (!pchLine)
{
Info.offEnd = (uint32_t)cchLine;
Info.iLineEnd = iLine;
break;
}
iLine++;
off = 0;
}
/* Strip trailing empty lines in the body. */
Info.cBlankLinesAfter = 0;
while (cchBody >= 1 && pszBody[cchBody - 1] == '\n')
{
Info.cBlankLinesAfter++;
pszBody[--cchBody] = '\0';
}
/* Do the callback. */
int rc = pfnCallback(&Info, pszBody, cchBody, pvUser);
RTMemFree(pszBody);
if (RT_FAILURE(rc))
return rc;
if (rc > VINF_SUCCESS && rcRet == VINF_SUCCESS)
rcRet = rc;
}
if (!pchLine)
break;
}
/* else: We don't have to deal with character litterals as these shouldn't
include comment-like sequences. */
} /* for each character in the line */
iLine++;
} /* for each line in the stream */
int rcStream = ScmStreamGetStatus(pIn);
if (RT_SUCCESS(rcStream))
return rcRet;
return rcStream;
}
/**
* Deals with comments in DOS batch files.
*
* @returns VBox status code / callback return code.
* @param pIn The stream to parse.
* @param pfnCallback The callback.
* @param pvUser The user parameter for the callback.
*/
static int enumerateBatchComments(PSCMSTREAM pIn, PFNSCMCOMMENTENUMERATOR pfnCallback, void *pvUser)
{
int rcRet = VINF_SUCCESS;
uint32_t iLine = 0;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol);
while (pchLine != NULL)
{
/*
* Skip leading blanks and check for 'rem'.
* At the moment we do not parse '::lable-comments'.
*/
size_t off = 0;
while (off + 3 < cchLine && RT_C_IS_SPACE(pchLine[off]))
off++;
if (!IS_REM(pchLine, off, cchLine))
{
iLine++;
pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol);
}
else
{
int rc = handleLineComment(pIn, isBatchComment, pfnCallback, pvUser,
&pchLine, &cchLine, &enmEol, &iLine, &off);
if (RT_FAILURE(rc))
return rc;
if (rcRet == VINF_SUCCESS)
rcRet = rc;
}
}
int rcStream = ScmStreamGetStatus(pIn);
if (RT_SUCCESS(rcStream))
return rcRet;
return rcStream;
}
/**
* Deals with comments in SQL files.
*
* @returns VBox status code / callback return code.
* @param pIn The stream to parse.
* @param pfnCallback The callback.
* @param pvUser The user parameter for the callback.
*/
static int enumerateSqlComments(PSCMSTREAM pIn, PFNSCMCOMMENTENUMERATOR pfnCallback, void *pvUser)
{
int rcRet = VINF_SUCCESS;
uint32_t iLine = 0;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol);
while (pchLine != NULL)
{
/*
* Skip leading blanks and check for '--'.
*/
size_t off = 0;
while (off + 3 < cchLine && RT_C_IS_SPACE(pchLine[off]))
off++;
if ( cchLine < 2
|| pchLine[0] != '-'
|| pchLine[1] != '-')
{
iLine++;
pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol);
}
else
{
int rc = handleLineComment(pIn, isSqlComment, pfnCallback, pvUser,
&pchLine, &cchLine, &enmEol, &iLine, &off);
if (RT_FAILURE(rc))
return rc;
if (rcRet == VINF_SUCCESS)
rcRet = rc;
}
}
int rcStream = ScmStreamGetStatus(pIn);
if (RT_SUCCESS(rcStream))
return rcRet;
return rcStream;
}
/**
* Deals with simple line comments.
*
* @returns VBox status code / callback return code.
* @param pIn The stream to parse.
* @param chStart The start of comment character.
* @param pfnIsComment Comment tester function.
* @param pfnCallback The callback.
* @param pvUser The user parameter for the callback.
*/
static int enumerateSimpleLineComments(PSCMSTREAM pIn, char chStart, PFNISCOMMENT pfnIsComment,
PFNSCMCOMMENTENUMERATOR pfnCallback, void *pvUser)
{
int rcRet = VINF_SUCCESS;
uint32_t iLine = 0;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
size_t off = 0;
while (off < cchLine)
{
char ch = pchLine[off++];
if (ch != chStart)
{ /* not interesting */ }
else
{
off -= 1;
int rc = handleLineComment(pIn, pfnIsComment, pfnCallback, pvUser,
&pchLine, &cchLine, &enmEol, &iLine, &off);
if (RT_FAILURE(rc))
return rc;
if (rcRet == VINF_SUCCESS)
rcRet = rc;
if (!pchLine)
break;
}
} /* for each character in the line */
iLine++;
} /* for each line in the stream */
int rcStream = ScmStreamGetStatus(pIn);
if (RT_SUCCESS(rcStream))
return rcRet;
return rcStream;
}
/**
* Enumerates the comments in the given stream, calling @a pfnCallback for each.
*
* @returns IPRT status code.
* @param pIn The stream to parse.
* @param enmCommentStyle The comment style of the source stream.
* @param pfnCallback The function to call.
* @param pvUser User argument to the callback.
*/
int ScmEnumerateComments(PSCMSTREAM pIn, SCMCOMMENTSTYLE enmCommentStyle, PFNSCMCOMMENTENUMERATOR pfnCallback, void *pvUser)
{
switch (enmCommentStyle)
{
case kScmCommentStyle_C:
return enumerateCStyleComments(pIn, pfnCallback, pvUser);
case kScmCommentStyle_Python:
return enumeratePythonComments(pIn, pfnCallback, pvUser);
case kScmCommentStyle_Semicolon:
return enumerateSimpleLineComments(pIn, ';', isSemicolonComment, pfnCallback, pvUser);
case kScmCommentStyle_Hash:
return enumerateSimpleLineComments(pIn, '#', isHashComment, pfnCallback, pvUser);
case kScmCommentStyle_Rem_Upper:
case kScmCommentStyle_Rem_Lower:
case kScmCommentStyle_Rem_Camel:
return enumerateBatchComments(pIn, pfnCallback, pvUser);
case kScmCommentStyle_Sql:
return enumerateSqlComments(pIn, pfnCallback, pvUser);
case kScmCommentStyle_Tick:
return enumerateSimpleLineComments(pIn, '\'', isTickComment, pfnCallback, pvUser);
default:
AssertFailedReturn(VERR_INVALID_PARAMETER);
}
}
|