-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpp.js
1338 lines (1130 loc) · 36.7 KB
/
cpp.js
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
/* @license
cpp.js - Simple implementation of the C Preprocessor in Javascript
Copyright (c) 2011, Alexander Christoph Gessler
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the cpp.js team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the cpp.js Development Team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function cpp_js(settings) {
"use strict";
var trim = function (str) {
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
};
var strip_cpp_comments = function(str) {
// very loosely based on http://james.padolsey.com/javascript/removing-comments-in-javascript/,
// but removed JS-specific stuff and added handling of line continuations. Also, newlines
// are generally preserved to keep line numbers intact.
str = ('__' + str.replace(/\r\n/g,'\n') + '__').split('');
var block_comment = false, line_comment = false, quote = false, lines_lost = 0;
for (var i = 0, l = str.length; i < l; i++) {
if (quote) {
if ((str[i] === "'" || str[i] === '"') && str[i-1] !== '\\') {
quote = false;
}
continue;
}
if (block_comment) {
if (str[i] === '*' && str[i+1] === '/') {
str[i+1] = '';
block_comment = false;
}
str[i] = '';
if (str[i] === '\n') {
++lines_lost;
}
continue;
}
if (line_comment) {
if (str[i+1] === '\n') {
line_comment = false;
}
str[i] = '';
continue;
}
if (str[i] === '\n') {
if (str[i-1] == '\\') {
// line continuation, replace by whitespace
str[i-1] = '';
str[i] = '';
++lines_lost;
}
else {
while(lines_lost > 0) {
str[i] += '\n';
--lines_lost;
}
}
}
quote = str[i] === "'" || str[i] === '"';
if (str[i] === '/') {
if (str[i+1] === '*') {
str[i] = '';
block_comment = true;
continue;
}
if (str[i+1] === '/') {
str[i] = '';
line_comment = true;
continue;
}
}
}
return str.join('').slice(2, -2);
};
var is_string_boundary = function(text, idx) {
return (text[idx] == '"' || text[idx] == "'") &&
(!idx || text[idx-1] != '\\' ||
(idx > 1 && text[idx-2] == '\\'));
};
// dictionary of default settings, including default error handlers
var default_settings = {
signal_char : '#',
warn_func : function(s) {
console.log(s);
},
error_func : function(s) {
console.log(s);
throw s;
},
comment_stripper : strip_cpp_comments,
include_func : null,
completion_func : null,
pragma_func : function(pragma) {
return null;
}
};
// apply default settings
if (settings) {
for(var k in default_settings) {
if (!(k in settings)) {
settings[k] = default_settings[k];
}
}
}
else {
settings = default_settings;
}
if (settings.include_func && !settings.completion_func) {
settings.error_func("include_func but not completion_func specified");
}
// make sure that execution never continues when an error occurs.
var user_err = settings.error_func;
settings.error_func = function(e) {
user_err(e);
throw e;
}
// generate a 3 tuple (command, arguments, code_block)
var block_re = new RegExp("^"+settings.signal_char+
"(\\w+)[ \t]*(.*?)[ \t]*$","m"
);
// match identifiers according to 6.4.2.1, do not match 'defined',
// do not match quote strings either
var is_identifier_re = /\b(d(?!efined)|[a-ce-zA-Z_])\w*(?![\w"])/g;
// same, but checks if the entire string is an identifier
var is_identifier_only_re = /^(d(?!efined)|[a-ce-zA-Z_])\w*$/g;
// same, but checks if the entire string is a macro
var is_macro_only_re = /^((?:d(?!efined)|[a-ce-zA-Z_])\w*)\s*\((.*)\)$/g;
// defined <identifier>
var defined_no_parens_re = /defined\s+([a-zA-Z_]\w*)/g;
// defined (<identifier>)
var defined_re = /defined\s*\((\s*[a-zA-Z_]\w*\s*)\)/g;
// __defined_magic_<identifier>_ (a special sentinel value used to
// temporarily exclude operands to defined from macro substitution.
var defined_magic_sentinel_re = /__defined_magic_([a-zA-Z_]\w*)_/;
// Match hexadecimal, octal and decimal integer literals with or
// without L,l,U,u suffix and separate all components.
var is_integer_re = /\b(\+|-|)(0|0x|)([1-9a-f][0-9a-f]*|0)([ul]*)\b/ig;
// Grab doubly quoted strings
var is_string_re = /"(.*?)"/g;
// Grab compound assignments. Extra fix for !=, ==, <=, >= needed
var is_assignment_re = /[+\-*%\/&^|]?=/g;
// Grab instances of the increment/decrement operators
var is_increment_re = /--|\+\+/g;
// Grav <included_file> or "included_file"
var include_re = /(?:(<)(.*)>|"(.*)")(.*)/;
// Magic token to signify the '##' token (to keep it from being
// treated as the operator of the same signature).
var pseudo_token_doublesharp = '__doublesharp_magic__';
var is_pseudo_token_doublesharp = new RegExp(pseudo_token_doublesharp,'g');
// Magic token to signify the ' ' token (to keep it from being
// treated as token boundary).
var pseudo_token_space = '__whitespace_magic__';
var is_pseudo_token_space = new RegExp(pseudo_token_space,'g');
var pseudo_token_empty = '__empty_magic__';
var is_pseudo_token_empty = new RegExp(pseudo_token_empty,'g');
var pseudo_token_nosubs = '__nosubs__';
var is_pseudo_token_nosubs = new RegExp(pseudo_token_nosubs,'g');
// List of preprocessing tokens.
var pp_special_token_list = {
'==':1,
'!=':1,
'+':1,
'-':1,
'*':1,
'/':1,
'%':1,
'<=':1,
'>=':1,
'<':1,
'>':1,
'=':1,
'+=':1,
'*=':1,
'/=':1,
'&=':1,
'|=':1,
'^=':1,
'#':1,
'##':1,
'->':1
};
var state = {};
var macro_cache = {};
var eval_mask = null;
var max_macro_length = 0;
var macro_counts_by_length = {};
return {
// ----------------------
// (public) Clear the current status code. i.e. reset all defines.
clear : function() {
state = {};
macro_counts_by_length = {};
macro_cache = {};
max_macro_length = 0;
},
// ----------------------
// (public) Check if macro `k` is defined.
defined : function(k) {
return k in state;
},
// ----------------------
// (public) Define macro `k` with replacement value `v`. To define macros with
// parameters, include the parameter list in the macro name, i.e.
// k <= "foo(a,b)", v <= "a ## b". The function invokes the error
// callback if the macro contains syntax errors.
define : function(k,v) {
var macro = this._get_macro_info(k);
if (!this._is_identifier(k) && !macro) {
settings.error_func("not a valid preprocessor identifier: '" + k + "'");
}
if (typeof v === 'number') {
v = v.toString(10);
}
if (macro) {
k = macro.name;
this.undefine(k);
// This inserts the macro into the macro cache, which
// holds pre-parsed data to simplify substitution.
macro_cache[k] = macro;
}
else {
this.undefine(k);
}
state[k] = v || '';
// macro length table housekeeping
macro_counts_by_length[k.length] = (macro_counts_by_length[k.length] || 0 ) + 1;
if (k.length > max_macro_length) {
max_macro_length = k.length;
}
},
// ----------------------
// (public) Undefine `k`. A no-op if `k` is not defined.
undefine : function(k) {
if(k in state) {
delete state[k];
// update macro length table
var nl = macro_counts_by_length[k.length] - 1;
if (k.length === max_macro_length && !nl) {
max_macro_length = 0;
for (var i = k.length-1; i >= 0; --i) {
if (macro_counts_by_length[i]) {
max_macro_length = i;
break;
}
}
}
macro_counts_by_length[k.length] = nl;
delete macro_cache[k];
}
else {
// this happens if the user includes the parameter list
// in the name. This is not part of the specification,
// but implemented for reasons of API symmetry.
var macro = this._get_macro_info(k);
if (macro) {
this.undefine(macro.name);
}
}
},
// ----------------------
// (public) Given a dictionary of macro_name, replacement pairs, invoke
// `define` on all of them.
define_multiple : function(dict) {
for(var k in dict) {
this.define(k,dict[k]);
}
},
// ----------------------
// (public) Preprocess `text` and return the preprocessed text (or receive
// a completion callback if asynchronous processing is enabled). `name` is
// an optional string that is used in error messages as file name.
run : function(text, name) {
name = name || '<unnamed>';
if (!text) {
error('input empty or null');
}
text = settings.comment_stripper(text);
var blocks = text.split(block_re);
var out = new Array(Math.floor(blocks.length/3) + 2), outi = 0;
for (var i = 0; i < out.length; ++i) {
out[i] = '';
}
var ifs_nested = 0, ifs_failed = 0, if_done = false, line = 1, command;
var if_stack = [];
// wrapped error function, augments line number and file
var error = function(text) {
settings.error_func("(cpp) error # "+name+":"+line+": " + text);
};
// wrapped warning function, augments line number and file
var warn = function(text) {
settings.warn_func("(cpp) warning # "+name+":"+line+": " + text);
};
var skip = false;
var self = this;
var process_directive = function(command, elem, i) {
switch (command) {
case "define":
var head, tail;
elem = trim(elem);
var par_count = undefined;
for (var j = 0; j < elem.length; ++j) {
if (elem[j] == '(') {
par_count = (par_count || 0) + 1;
}
else if ((elem[j] == ')' && --par_count === 0) || elem[j].match(/\s/) && par_count === undefined) {
if (elem[j] == ')') {
++j;
}
head = elem.slice(0,j);
tail = trim( elem.slice(j) );
break;
}
}
if (par_count) {
error('unbalanced parentheses in define: ' + elem);
}
if (head === undefined) {
head = elem;
}
if (self.defined(head)) {
warn(head + ' redefined');
}
if (!self._is_identifier(head) && !self._is_macro(head)) {
error("not a valid preprocessor identifier: '" + head + "'");
}
self.define(head, tail);
break;
case "undef":
self.undefine(elem);
break;
case "include":
elem = self.subs(elem, {}, error, warn);
var parts = elem.match(include_re);
if (parts[4]) {
error("unrecognized characters in include: " + elem);
}
var file = (parts[2] || '') + (parts[3] || '');
if (!settings.include_func) {
error("include directive not supported, " +
"no handler specified");
}
settings.include_func(file, parts[1] === '<', function(contents) {
if (contents === null) {
error("failed to access include file: " +
file);
}
var s = {};
for(var k in settings) {
s[k] = settings[k];
}
var processor;
s.completion_func = function(data, lines, new_state) {
out.length = outi;
outi += lines.length;
out = out.concat(lines);
// grab any state changes
self._set_state(processor);
for (++i; i < blocks.length; ++i) {
if(!process_block(i,blocks[i])) {
return false;
}
}
self._result(out, state);
};
// construct a child preprocessor and let it share our
// state.
processor = cpp_js(s);
processor._set_state(self);
processor.run(contents, file);
});
return false;
case "error":
error("#error: " + elem);
break;
case "pragma":
if(!settings.pragma_func(elem)) {
warn('ignoring unrecognized #pragma: ' + elem);
}
break;
default:
warn("unrecognized preprocessor command: "
+ command
);
break;
};
return true;
};
var process_block = function(i, elem) {
var elem = blocks[i];
switch(i % 3) {
// code line, apply macro substitutions and copy to output.
case 0:
line += elem.split('\n').length-1;
if (!ifs_failed && trim(elem).length) {
out[outi++] = self.subs(elem, error, warn);
}
break;
// preprocessor statement, such as ifdef, endif, ..
case 1:
//++line;
command = elem;
break;
// the rest of the preprocessor line, this is where expression
// evaluation happens
case 2:
var done = true;
switch (command) {
case "ifdef":
case "ifndef":
if (!elem) {
error("expected identifier after " +
command);
}
// translate ifdef/ifndef to regular if by using defined()
elem = "(defined " + elem + ")";
if(command == 'ifndef') {
elem = '!' + elem;
}
// fallthrough
case "if":
if_stack.push(false);
if (!elem.length) {
error("expected identifier after if");
}
// fallthrough
case "else":
case "elif":
var not_reached = false;
if (command == 'elif' || command == 'else') {
not_reached = if_stack[if_stack.length-1];
if (ifs_failed > 0) {
--ifs_failed;
}
if (command == 'else' && elem.length) {
warn('ignoring tokens after else');
}
}
if (ifs_failed > 0 || not_reached ||
(command != 'else' &&
!self._eval(elem, error, warn)
)){
++ifs_failed;
}
else {
// we run self branch, so skip any further else/
// elsif branches
if_stack[if_stack.length-1] = true;
}
break;
case "endif":
if(!if_stack.length) {
error("endif with no matching if");
}
if (ifs_failed > 0) {
--ifs_failed;
}
if_stack.pop();
// ignore trailing junk on endifs
break;
default:
done = ifs_failed > 0;
};
// not done yet, so this is a plain directive (i.e. include)
if(!done) {
if(!process_directive(command, elem, i)) {
return false;
}
}
break;
}
return true;
};
for (var i = 0; i < blocks.length; ++i) {
if(!process_block(i,blocks[i])) {
return null;
}
}
if(if_stack.length > 0) {
error("unexpected EOF, expected endif");
}
return this._result(out, state);
},
// ----------------------
// (public) Given a `text`, substitute macros until no further substitutions
// are possible. `blacklist` is an optional set of macro names to be ignored,
// these are not substituted and remain as is.
// `error` and `warn` are optional callbacks, by default the corresponding
// callbacks from settings are used. Users should never assign a value to
// `nest_sub`, which is used to keep track of recursive invocations internally.
subs : function(text, blacklist_in, error, warn, nest_sub) {
error = error || settings.error_func;
warn = warn || settings.warn_func;
var TOTALLY_BLACK = 1e10;
// create a copy of the blacklist and make sure that all incoming
// macros are totally blacked out.
var blacklist = {};
if (blacklist_in) {
for (var k in blacklist_in) {
blacklist[k] = TOTALLY_BLACK;
}
}
nest_sub = nest_sub || 0;
var new_text = text;
var rex = /\b.|["']/g, m_boundary;
// XXX This scales terribly. Possible optimization:
// use KMP for substring searches
var pieces = [], last = 0, in_string = false;
while (m_boundary = rex.exec(new_text)) {
var idx = m_boundary.index;
if (is_string_boundary(new_text, idx)) {
in_string = !in_string;
}
if (in_string) {
continue;
}
for (var i = Math.min(new_text.length - idx,max_macro_length); i >= 1; --i) {
if(!macro_counts_by_length[i]) {
continue;
}
var k = new_text.slice(idx,idx+i);
if (k in state) {
// if this would be a match, but the macro is blacklisted,
// we need to skip it alltogether or parts of it might be
// interpreted as macros on their own.
if (blacklist[k] > idx) {
pieces.push(new_text.slice(0,idx));
pieces.push(pseudo_token_nosubs+k);
new_text = new_text.slice(idx+k.length);
rex.lastIndex = 0;
// adjust blacklist indices
for(var kk in blacklist) {
if (blacklist[kk] != TOTALLY_BLACK) {
if (blacklist[kk] > idx) {
blacklist[kk] -= idx+k.length;
}
else delete blacklist[kk];
}
};
break;
}
else {
delete blacklist[k];
}
var sub;
if (this._is_macro(k)) {
sub = this._subs_macro(new_text, k, {},
error, warn, nest_sub, idx
);
}
else {
sub = this._subs_simple(new_text, k, {},
error, warn, nest_sub, idx
);
}
if (sub === null) {
continue;
}
// handle # and ## operator
sub[0] = this._handle_ops(sub[0], error, warn);
// handle _Pragma()
sub[0] = this._handle_pragma(sub[0], error, warn);
// XXX a bit too expensive ... but not too easy to avoid.
pieces.push(new_text.slice(0,idx));
new_text = sub[0] + new_text.slice(idx+sub[1]);
rex.lastIndex = 0;
// adjust blacklist indices
for(var kk in blacklist) {
if (blacklist[kk] != TOTALLY_BLACK) {
if (blacklist[kk] > idx) {
blacklist[kk] = (sub[0].length-sub[1]) +( blacklist[kk] - idx);
}
else delete blacklist[kk];
}
}
// rescan this string, but keep the macro that we just replaced
// blacklisted until we're beyond the replacement. This
// prevents infinite recursion and is also mandated by the
// standard and crucial for proper evaluation of several of
// its more ... evil ehm elaborate samples.
blacklist[k] = sub[0].length;
break;
}
}
}
pieces.push(new_text);
new_text = pieces.join('');
// if macro substitution is complete, re-introduce any
// '##' tokens previously substituted in order to keep them
// from being treated as operators. Same for spaces and empty
// tokens.
if (!nest_sub) {
new_text = this._remove_sentinels(new_text);
}
return new_text;
},
// ----------------------
// Transfer the state from another cpp.js instance to us.
_set_state : function(other) {
other = other._get_state();
state = other.state;
macro_counts_by_length = other.macro_counts_by_length;
macro_cache = other.macro_cache;
max_macro_length = other.max_macro_length;
},
// ----------------------
// Get a dictionary containing the full processing state of us
_get_state : function(other) {
return {
state : state,
macro_counts_by_length : macro_counts_by_length,
macro_cache : macro_cache,
max_macro_length : max_macro_length
};
},
// ----------------------
// Given an array of single lines, produce the result text by merging lines
// and trimming the result. The function also invokes the user-defined
// completion callback, but it also returns the preprocessed text to the caller.
_result : function(arr, state) {
// drop empty lines at the end
for (var i = arr.length-1; i >= 0; --i) {
if (!arr[i]) {
arr.pop();
}
else {
break;
}
}
var text = arr.join('\n');
if (settings.completion_func) {
settings.completion_func(text,arr, state);
}
return text;
},
// ----------------------
// Check if `identifier` is a well-formed identifier according to C rules.
_is_identifier : function(identifier) {
// Note: important to use match() because test() would update
// the 'lastIndex' property on the regex.
return !!identifier.match(is_identifier_only_re);
},
// ----------------------
// Check if `macro` is a well-formed macro name.
_is_macro : function(macro) {
return this._get_macro_info(macro) != null;
},
// ----------------------
// Check if `tok` is a special preprocessor token (such as ==, <=, >=).
// These tokens are handled differently when participating on either side
// of the ## operator.
_is_pp_special_token : function(tok) {
return trim(tok) in pp_special_token_list;
},
// ----------------------
// Get the description dictionary for a macro named `k` or null if the macro
// is malformed (i.e. syntax wrong). Does not add new macros to the macro
// cache but uses the cache to speed-up looking up known macros.
_get_macro_info : function(k) {
if (macro_cache[k]) {
return macro_cache[k];
}
var m = is_macro_only_re.exec(k);
if (!m) {
return null;
}
is_macro_only_re.lastIndex = 0;
var params = m[2].split(',');
if (params.length === 1 && !trim(params[0])) {
// parameterless macro (i.e. #define p () )
params = [];
}
else {
for (var i = 0; i < params.length; ++i) {
var t = params[i] = trim(params[i]);
if(!this._is_identifier(t) && !this._is_macro(t)) {
return null;
}
}
}
// ES 1.8's sticky flag would be useful, but sadly it is not
// universally supported yet.
var pat = new RegExp(m[1] + '\\s*\\(','g');
return {
params:params,
pat:pat,
name:m[1],
full:k
};
},
// ----------------------
// Remove all sentinel strings (i.e. placeholders for spaces
// or empty tokens to indicate placeholder tokens) from the
// given string.
_remove_sentinels : function(new_text) {
new_text = new_text.replace(is_pseudo_token_doublesharp,'##');
new_text = new_text.replace(is_pseudo_token_space,' ');
new_text = new_text.replace(is_pseudo_token_empty,'');
new_text = new_text.replace(is_pseudo_token_nosubs,'');
return new_text;
},
// ----------------------
// Evaluate the _Pragma(string) preprocessor operator in the given
// (partially substituted) sequence of preprocessor tokens.
_handle_pragma : function(text, error, warn) {
var self = this;
// XXX obviously RE aren't sufficient here either, do proper parse.
return text.replace(/_Pragma\s*\(\s*"(.*?([^\\]|\\\\))"\s*\)/g, function(match, pragma) {
// destringize
pragma = pragma.replace(/\\"/g,'"').replace(/\\\\/g,'\\');
pragma = self._remove_sentinels(pragma);
pragma = self._concatenate_strings(pragma);
if (!settings.pragma_func(pragma)) {
warn('unrecognized _Pragma(): ' + pragma);
}
// always substitute an empty string so processing
// can continue.
return '';
});
},
// ----------------------
// Concatenate neighbouring string literals such as " hello "
// "world " and return the result.
_concatenate_strings : function(text) {
var in_string = false, last = null, last_taken = 0;
var text_out = [];
for (var i = 0; i < text.length; ++i) {
if (is_string_boundary(text,i)) {
if (in_string) {
last = i;
}
else if (last !== null) {
text_out.push(text.slice(last_taken, last));
last_taken = i+1;
}
in_string = !in_string;
}
else if (!text[i].match(/\s/)){
text_out.push(text.slice(last_taken, i));
last_taken = i;
last = null;
}
}
text_out.push(text.slice(last_taken));
return text_out.join('');
},
// ----------------------
// Evaluate the '##' and '#' preprocessor operator in the given (partially
// substituted) sequence of preprocessor tokens.
_handle_ops : function(text, error, warn) {
// XXX The code below is not only extremely slow, it also doesn't
// take into account that the # operator can only be applied to
// macro parameter, an information that is no longer available
// at this point.
// 6.10.3.2: "The order of evaluation of # and ## operators
// is unspecified.". We pick '##' first.
var op, pieces = [], in_string = false;
for (var op = 0; op < text.length-1; ++op) {
if (is_string_boundary(text,op)) {
in_string = !in_string;
continue;
}
if (text[op] !== '#' || in_string) {
continue;
}
var is_concat = text[op+1] === '#';
var left = null, right = null;
// identify the tokens on either side of the ## operator or
// only on the right side of the # operator.
var in_inner_string = false, nest = 0;
if(is_concat) {
for (var i = op-1; i >= 0; --i) {
if (!text[i].match(/\s/)) {
if (is_string_boundary(text,i)) {
in_inner_string = !in_inner_string;
}
else if (text[i] === '(') {
++nest;
}
else if (text[i] === ')') {
--nest;
}
left = text[i] + (left || '');
}
else if (left !== null) {
if(!in_inner_string && !nest) {
break;
}
left = ' ' + left;
}
}
++i;
}
else {
i = op;
}
in_inner_string = false;
nest = 0;
var first_space = true;
for (var j = op+(is_concat?2:1); j < text.length; ++j) {
if (!text[j].match(/\s/)) {
first_space = true;
if (is_string_boundary(text,j)) {
in_inner_string = !in_inner_string;
}
else if (text[j] === '(') {
++nest;
}
else if (text[j] === ')') {
--nest;
}
right = (right || '') + text[j];
}
else if (right !== null && !in_inner_string && !nest) {
break;
}