28
28
* @param {string } source Source to process
29
29
* @param {string|Object.<string,string>= } baseDirOrIncludes Source base directory used for includes (node.js only)
30
30
* or an object containing all the included sources by filename. Defaults to the current working directory.
31
+ * @param {boolean } preserveLineNumbers When removing blocks of code, replace the block with blank lines so that
32
+ * line numbers are preserved, as long as #include is not used
31
33
* @constructor
32
34
*/
33
- var Preprocessor = function Preprocessor ( source , baseDirOrIncludes ) {
35
+ var Preprocessor = function Preprocessor ( source , baseDirOrIncludes , preserveLineNumbers ) {
34
36
35
37
/**
36
38
* Source code to pre-process.
52
54
*/
53
55
this . includes = typeof baseDirOrIncludes == 'object' ? baseDirOrIncludes : { } ;
54
56
57
+ /**
58
+ * Preserve line numbers when removing blocks of code
59
+ * @type {boolean }
60
+ */
61
+ this . preserveLineNumbers = typeof preserveLineNumbers == 'boolean' ? preserveLineNumbers : false ;
62
+
55
63
/**
56
64
* Whether running inside of node.js or not.
57
65
* @type {boolean }
115
123
*/
116
124
var GLOB_EXP = / (?: ^ | [ ^ \\ ] ) \* / ;
117
125
126
+ /**
127
+ * @type {!RegExp }
128
+ * @inner
129
+ */
130
+ var NOT_LINE_ENDING = / [ ^ \r \n ] / g;
131
+
118
132
/**
119
133
* Strips slashes from an escaped string.
120
134
* @param {string } str Escaped string
315
329
}
316
330
var before = stack . pop ( ) ;
317
331
verbose ( " pop: " + JSON . stringify ( before ) ) ;
318
- include = this . source . substring ( before [ "lastIndex" ] , match . index ) ;
332
+
333
+ if ( this . preserveLineNumbers ) {
334
+ include = this . source . substring ( before [ "index" ] , before [ "lastIndex" ] ) . replace ( NOT_LINE_ENDING , "" ) +
335
+ this . source . substring ( before [ "lastIndex" ] , match . index ) +
336
+ this . source . substring ( match . index , Preprocessor . ENDIF . lastIndex ) . replace ( NOT_LINE_ENDING , "" ) ;
337
+ } else {
338
+ include = this . source . substring ( before [ "lastIndex" ] , match . index ) ;
339
+ }
340
+
319
341
if ( before [ "include" ] ) {
320
342
verbose ( " incl: " + Preprocessor . nlToStr ( include ) + ", 0-" + before [ 'index' ] + " + " + include . length + " bytes + " + Preprocessor . ENDIF . lastIndex + "-" + this . source . length ) ;
321
343
this . source = this . source . substring ( 0 , before [ "index" ] ) + include + this . source . substring ( Preprocessor . ENDIF . lastIndex ) ;
322
- } else {
344
+ } else if ( this . preserveLineNumbers ) {
345
+ verbose ( " excl(\\n): " + Preprocessor . nlToStr ( include ) + ", 0-" + before [ 'index' ] + " + " + Preprocessor . ENDIF . lastIndex + "-" + this . source . length ) ;
346
+ include = include . replace ( NOT_LINE_ENDING , "" ) ;
347
+ this . source = this . source . substring ( 0 , before [ "index" ] ) + include + this . source . substring ( Preprocessor . ENDIF . lastIndex ) ;
348
+ } else {
323
349
verbose ( " excl: " + Preprocessor . nlToStr ( include ) + ", 0-" + before [ 'index' ] + " + " + Preprocessor . ENDIF . lastIndex + "-" + this . source . length ) ;
324
350
include = "" ;
325
351
this . source = this . source . substring ( 0 , before [ "index" ] ) + this . source . substring ( Preprocessor . ENDIF . lastIndex ) ;
352
378
var define = match2 [ 1 ] ;
353
379
verbose ( " def: " + match2 [ 1 ] ) ;
354
380
this . defines . push ( define ) ;
355
- this . source = this . source . substring ( 0 , match . index ) + indent + this . source . substring ( Preprocessor . DEFINE . lastIndex ) ;
381
+ var lineEnding = ""
382
+ if ( this . preserveLineNumbers ) {
383
+ lineEnding = this . source . substring ( match . index , Preprocessor . DEFINE . lastIndex ) . replace ( NOT_LINE_ENDING , "" ) ;
384
+ }
385
+ this . source = this . source . substring ( 0 , match . index ) + indent + lineEnding + this . source . substring ( Preprocessor . DEFINE . lastIndex ) ;
356
386
Preprocessor . EXPR . lastIndex = match . index ;
357
387
verbose ( " continue at " + Preprocessor . EXPR . lastIndex ) ;
358
388
}
385
415
global [ "dcodeIO" ] [ "Preprocessor" ] = Preprocessor ;
386
416
}
387
417
388
- } ) ( this ) ;
418
+ } ) ( this ) ;
0 commit comments