/
preprocessor.js
1054 lines (836 loc) · 33 KB
/
preprocessor.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
/*
* Preprocessor.js
* Objective-J
*
* Created by Francisco Tolmasky.
* Copyright 2008-2010, 280 North, Inc.
*
* Modified to output NodObjC syntax by Nathan Rajlich
* Copyright 2012
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
var TOKEN_ACCESSORS = "accessors",
TOKEN_CLASS = "class",
TOKEN_END = "end",
TOKEN_FUNCTION = "function",
TOKEN_IMPLEMENTATION = "implementation",
TOKEN_IMPORT = "import",
TOKEN_EACH = "each",
TOKEN_OUTLET = "outlet",
TOKEN_ACTION = "action",
TOKEN_NEW = "new",
TOKEN_SELECTOR = "selector",
TOKEN_SUPER = "super",
TOKEN_VAR = "var",
TOKEN_IN = "in",
TOKEN_PRAGMA = "pragma",
TOKEN_MARK = "mark",
TOKEN_EQUAL = '=',
TOKEN_PLUS = '+',
TOKEN_MINUS = '-',
TOKEN_COLON = ':',
TOKEN_COMMA = ',',
TOKEN_PERIOD = '.',
TOKEN_ASTERISK = '*',
TOKEN_SEMICOLON = ';',
TOKEN_LESS_THAN = '<',
TOKEN_OPEN_BRACE = '{',
TOKEN_CLOSE_BRACE = '}',
TOKEN_GREATER_THAN = '>',
TOKEN_OPEN_BRACKET = '[',
TOKEN_DOUBLE_QUOTE = '"',
TOKEN_PREPROCESSOR = '@',
TOKEN_HASH = '#',
TOKEN_CLOSE_BRACKET = ']',
TOKEN_QUESTION_MARK = '?',
TOKEN_OPEN_PARENTHESIS = '(',
TOKEN_CLOSE_PARENTHESIS = ')',
TOKEN_WHITESPACE = /^(?:(?:\s+$)|(?:\/(?:\/|\*)))/,
TOKEN_NUMBER = /^[+-]?\d+(([.]\d+)*([eE][+-]?\d+))?$/,
TOKEN_IDENTIFIER = /^[a-zA-Z_$](\w|$)*$/;
function IS_WORD (token) {
return /^\w+$/.test(token);
}
function Lexer(/*String*/ aString)
{
this._index = -1;
// FIXME: Used fixed regex
this._tokens = (aString + '\n').match(/\/\/.*(\r|\n)?|\/\*(?:.|\n|\r)*?\*\/|\w+\b|[+-]?\d+(([.]\d+)*([eE][+-]?\d+))?|"[^"\\]*(\\[\s\S][^"\\]*)*"|'[^'\\]*(\\[\s\S][^'\\]*)*'|\s+|./g);
this._context = [];
return this;
}
Lexer.prototype.push = function()
{
this._context.push(this._index);
}
Lexer.prototype.pop = function()
{
this._index = this._context.pop();
}
Lexer.prototype.peek = function(shouldSkipWhitespace)
{
if (shouldSkipWhitespace)
{
this.push();
var token = this.skip_whitespace();
this.pop();
return token;
}
return this._tokens[this._index + 1];
}
Lexer.prototype.next = function()
{
return this._tokens[++this._index];
}
Lexer.prototype.previous = function()
{
return this._tokens[--this._index];
}
Lexer.prototype.last = function()
{
if (this._index < 0)
return null;
return this._tokens[this._index - 1];
}
Lexer.prototype.skip_whitespace = function(shouldMoveBackwards)
{
var token;
if (shouldMoveBackwards)
while ((token = this.previous()) && TOKEN_WHITESPACE.test(token)) ;
else
while ((token = this.next()) && TOKEN_WHITESPACE.test(token)) ;
return token;
}
exports.Lexer = Lexer;
function IS_NOT_EMPTY (buffer) {
return buffer.atoms.length !== 0;
}
function CONCAT (buffer, atom) {
return buffer.atoms[buffer.atoms.length] = atom;
}
function StringBuffer()
{
this.atoms = [];
}
StringBuffer.prototype.toString = function()
{
return this.atoms.join("");
}
exports.preprocess = function(/*String*/ aString, /*CFURL*/ aURL, /*unsigned*/ flags)
{
return new Preprocessor(aString, aURL, flags).executable();
}
exports.eval = function(/*String*/ aString)
{
return eval(exports.preprocess(aString).code());
}
var Preprocessor = function(/*String*/ aString, /*CFURL|String*/ aURL, /*unsigned*/ flags)
{
this._URL = aURL;
// Remove the shebang.
aString = aString.replace(/^#[^\n]+\n/, "\n");
this._currentSelector = "";
this._currentClass = "";
this._currentSuperClass = "";
this._currentSuperMetaClass = "";
this._buffer = new StringBuffer();
this._preprocessed = null;
this._dependencies = [];
this._tokens = new Lexer(aString);
this._flags = flags;
this._classMethod = false;
this._executable = null;
this._classLookupTable = {};
this._classVars = {};
CONCAT(this._buffer, 'require("NodObjC/global");\n\n');
/*var classObject = new objj_class();
for (var i in classObject)
this._classVars[i] = 1;*/
this.preprocess(this._tokens, this._buffer);
}
Preprocessor.prototype.setClassInfo = function(className, superClassName, ivars)
{
this._classLookupTable[className] = { superClassName:superClassName, ivars:ivars };
}
Preprocessor.prototype.getClassInfo = function(className)
{
return this._classLookupTable[className];
}
Preprocessor.prototype.allIvarNamesForClassName = function(className)
{
var names = {},
classInfo = this.getClassInfo(className);
while (classInfo)
{
for (var i in classInfo.ivars)
names[i] = 1;
classInfo = this.getClassInfo(classInfo.superClassName);
}
return names;
}
exports.Preprocessor = Preprocessor;
Preprocessor.Flags = { };
Preprocessor.Flags.IncludeDebugSymbols = 1 << 0;
Preprocessor.Flags.IncludeTypeSignatures = 1 << 1;
Preprocessor.prototype.executable = function()
{
if (!this._executable)
this._executable = new Executable(this._buffer.toString(), this._dependencies, this._URL);
return this._executable;
}
Preprocessor.prototype.accessors = function(tokens)
{
var token = tokens.skip_whitespace(),
attributes = {};
if (token != TOKEN_OPEN_PARENTHESIS)
{
tokens.previous();
return attributes;
}
while ((token = tokens.skip_whitespace()) != TOKEN_CLOSE_PARENTHESIS)
{
var name = token,
value = true;
if (!IS_WORD(name))
throw new SyntaxError(this.error_message("*** @accessors attribute name not valid."));
if ((token = tokens.skip_whitespace()) == TOKEN_EQUAL)
{
value = tokens.skip_whitespace();
if (!IS_WORD(value))
throw new SyntaxError(this.error_message("*** @accessors attribute value not valid."));
if (name == "setter")
{
if ((token = tokens.next()) != TOKEN_COLON)
throw new SyntaxError(this.error_message("*** @accessors setter attribute requires argument with \":\" at end of selector name."));
value += ":";
}
token = tokens.skip_whitespace();
}
attributes[name] = value;
if (token == TOKEN_CLOSE_PARENTHESIS)
break;
if (token != TOKEN_COMMA)
throw new SyntaxError(this.error_message("*** Expected ',' or ')' in @accessors attribute list."));
}
return attributes;
}
Preprocessor.prototype.brackets = function(/*Lexer*/ tokens, /*StringBuffer*/ aStringBuffer)
{
var tuples = [];
while (this.preprocess(tokens, null, null, null, tuples[tuples.length] = [])) ;
if (tuples[0].length === 1)
{
CONCAT(aStringBuffer, '[');
// When we have an empty array literal ([]), tuples[0][0] will be an empty StringBuffer
CONCAT(aStringBuffer, tuples[0][0]);
CONCAT(aStringBuffer, ']');
}
else
{
// The first two arguments are always the receiver and the selector.
if (tuples[0][0].atoms[0] == TOKEN_SUPER)
{
CONCAT(aStringBuffer, 'self.super(');
}
else
{
CONCAT(aStringBuffer, tuples[0][0]);
CONCAT(aStringBuffer, '(');
}
// add the first (maybe only) piece of the selector
var firstSelector = tuples[0][1];
if (firstSelector[firstSelector.length - 1] === TOKEN_COLON) {
firstSelector = firstSelector.substring(0, firstSelector.length - 1);
}
CONCAT(aStringBuffer, '"');
CONCAT(aStringBuffer, firstSelector);
CONCAT(aStringBuffer, '"');
var index = 1,
count = tuples.length;
for(; index < count; ++index)
{
var pair = tuples[index];
var sel = pair[1];
var arg = pair[0];
// add the arg
CONCAT(aStringBuffer, ", ");
CONCAT(aStringBuffer, arg);
// if there's another piece of the selector to add, then add it
if (sel) {
CONCAT(aStringBuffer, ", \"");
if (sel[sel.length - 1] === TOKEN_COLON) {
sel = sel.substring(0, sel.length - 1);
}
CONCAT(aStringBuffer, sel);
CONCAT(aStringBuffer, '\"');
}
}
CONCAT(aStringBuffer, ')');
}
}
Preprocessor.prototype.directive = function(tokens, aStringBuffer, allowedDirectivesFlags)
{
// Grab the next token, preprocessor directives follow '@' immediately.
var buffer = aStringBuffer ? aStringBuffer : new StringBuffer(),
token = tokens.next();
// Convert @"strings" to NSString instances
if (token.charAt(0) === TOKEN_DOUBLE_QUOTE) {
CONCAT(buffer, 'NSString("stringWithUTF8String", ');
CONCAT(buffer, token);
CONCAT(buffer, ')');
}
// Currently we simply swallow forward declarations and only provide them to allow
// compatibility with Objective-C files.
else if (token === TOKEN_CLASS)
{
tokens.skip_whitespace();
return;
}
// @implementation Class implementations
else if (token === TOKEN_IMPLEMENTATION)
this.implementation(tokens, buffer);
// @import
else if (token === TOKEN_IMPORT)
this._import(tokens);
// @selector
else if (token === TOKEN_SELECTOR)
this.selector(tokens, buffer);
if (!aStringBuffer)
return buffer;
}
Preprocessor.prototype.hash = function(tokens, aStringBuffer)
{
// Grab the next token, C preprocessor directives follow '#' immediately.
var buffer = aStringBuffer ? aStringBuffer : new StringBuffer(),
token = tokens.next();
// #pragma (C Preprocessor directive)
if (token === TOKEN_PRAGMA)
{
token = tokens.skip_whitespace();
// '#pragma mark' directive is used in Xcode editor for creating labels,
// which is irrelevant to Cappuccino - just swallow this line
if (token === TOKEN_MARK)
{
while ((token = tokens.next()).indexOf("\n") < 0);
}
}
// if not a #pragma directive, it should not be processed here
else
throw new SyntaxError(this.error_message("*** Expected \"pragma\" to follow # but instead saw \"" + token + "\"."));
}
// preprocessor compat
var NO = false;
var MAX = Math.max;
Preprocessor.prototype.implementation = function(tokens, /*StringBuffer*/ aStringBuffer)
{
var buffer = aStringBuffer,
token = "",
category = NO,
class_name = tokens.skip_whitespace(),
superclass_name = "Nil";
if (!(/^\w/).test(class_name))
throw new Error(this.error_message("*** Expected class name, found \"" + class_name + "\"."));
this._currentSuperClass = "objj_getClass(\"" + class_name + "\").super_class";
this._currentSuperMetaClass = "objj_getMetaClass(\"" + class_name + "\").super_class";
this._currentClass = class_name;
this._currentSelector = "";
// If we reach an open parenthesis, we are declaring a category.
if ((token = tokens.skip_whitespace()) == TOKEN_OPEN_PARENTHESIS)
{
token = tokens.skip_whitespace();
if (token == TOKEN_CLOSE_PARENTHESIS)
throw new SyntaxError(this.error_message("*** Can't Have Empty Category Name for class \"" + class_name + "\"."));
if (tokens.skip_whitespace() != TOKEN_CLOSE_PARENTHESIS)
throw new SyntaxError(this.error_message("*** Improper Category Definition for class \"" + class_name + "\"."));
CONCAT(buffer, "{\nvar the_class = objj_getClass(\"" + class_name + "\")\n");
CONCAT(buffer, "if(!the_class) throw new SyntaxError(\"*** Could not find definition for class \\\"" + class_name + "\\\"\");\n");
CONCAT(buffer, "var meta_class = the_class.isa;");
}
else
{
// If we reach a colon (':'), then a superclass is being declared.
if(token == TOKEN_COLON)
{
token = tokens.skip_whitespace();
if (!TOKEN_IDENTIFIER.test(token))
throw new SyntaxError(this.error_message("*** Expected class name, found \"" + token + "\"."));
superclass_name = token;
token = tokens.skip_whitespace();
}
CONCAT(buffer, ";(function () {\n var the_class = ");
CONCAT(buffer, superclass_name);
CONCAT(buffer, ".extend('");
CONCAT(buffer, class_name);
CONCAT(buffer, "');\n");
CONCAT(buffer, " var meta_class = the_class.getClass();\n");
// If we are at an opening curly brace ('{'), then we have an ivar declaration.
if (token == TOKEN_OPEN_BRACE)
{
var ivar_names = {},
ivar_count = 0,
declaration = [],
attributes,
accessors = {},
types = [];
while((token = tokens.skip_whitespace()) && token != TOKEN_CLOSE_BRACE)
{
if (token === TOKEN_PREPROCESSOR)
{
token = tokens.next();
if (token === TOKEN_ACCESSORS)
attributes = this.accessors(tokens);
else if (token !== TOKEN_OUTLET)
throw new SyntaxError(this.error_message("*** Unexpected '@' token in ivar declaration ('@"+token+"')."));
else
types.push("@" + token);
}
else if (token == TOKEN_SEMICOLON)
{
var name = declaration[declaration.length - 1];
var type = declaration[declaration.length - 2];
CONCAT(buffer, " the_class.addIvar('");
CONCAT(buffer, name);
CONCAT(buffer, "', ");
CONCAT(buffer, map_type(type));
CONCAT(buffer, ");\n");
ivar_names[name] = 1;
declaration = [];
types = [];
if (attributes)
{
accessors[name] = attributes;
attributes = null;
}
}
else
{
declaration.push(token);
types.push(token);
}
}
// If we have objects in our declaration, the user forgot a ';'.
if (declaration.length)
throw new SyntaxError(this.error_message("*** Expected ';' in ivar declaration, found '}'."));
if (ivar_count)
CONCAT(buffer, "]);\n");
if (!token)
throw new SyntaxError(this.error_message("*** Expected '}'"));
this.setClassInfo(class_name, superclass_name === "Nil" ? null : superclass_name, ivar_names);
// build up the list of illegal method param names
var ivar_names = this.allIvarNamesForClassName(class_name);
for (ivar_name in accessors)
{
var accessor = accessors[ivar_name],
property = accessor["property"] || ivar_name;
// getter
var getterName = accessor["getter"] || property,
getterCode = "(id)" + getterName + "\n{\nreturn " + ivar_name + ";\n}";
CONCAT(buffer, this.method(new Lexer(getterCode), ivar_names));
// setter
if (accessor["readonly"])
continue;
var setterName = accessor["setter"];
if (!setterName)
{
var start = property.charAt(0) == '_' ? 1 : 0;
setterName = (start ? "_" : "") + "set" + property.substr(start, 1).toUpperCase() + property.substring(start + 1) + ":";
}
var setterCode = "(void)" + setterName + "(id)newValue\n{\n";
if (accessor["copy"])
setterCode += "if (" + ivar_name + " !== newValue)\n" + ivar_name + " = [newValue copy];\n}";
else
setterCode += ivar_name + " = newValue;\n}";
CONCAT(buffer, this.method(new Lexer(setterCode), ivar_names));
}
}
else
tokens.previous();
// We must make a new class object for our class definition.
CONCAT(buffer, " the_class.register();\n");
}
if (!ivar_names)
var ivar_names = this.allIvarNamesForClassName(class_name);
while ((token = tokens.skip_whitespace()))
{
if (token == TOKEN_PLUS)
{
this._classMethod = true;
CONCAT(buffer, this.method(tokens, this._classVars));
CONCAT(buffer, '\n\n');
}
else if (token == TOKEN_MINUS)
{
this._classMethod = false;
CONCAT(buffer, this.method(tokens, ivar_names));
CONCAT(buffer, '\n\n');
}
// If we reach a # symbol, we may be at a C preprocessor directive.
else if (token == TOKEN_HASH)
{
this.hash(tokens, buffer);
}
// Check if we've reached @end...
else if (token == TOKEN_PREPROCESSOR)
{
// The only preprocessor directive we should ever encounter at this point is @end.
if ((token = tokens.next()) == TOKEN_END)
break;
else
throw new SyntaxError(this.error_message("*** Expected \"@end\", found \"@" + token + "\"."));
}
//else
// throw new SyntaxError(this.error_message("*** Expected a method declaration, or \"@end\", found \"" + token + "\"."));
}
// finish the function statement and execute it
CONCAT(buffer, '})();\n');
this._currentClass = "";
}
Preprocessor.prototype._import = function(tokens)
{
var URLString = "",
token = tokens.skip_whitespace(),
isQuoted = (token !== TOKEN_LESS_THAN);
if (token === TOKEN_LESS_THAN)
{
while ((token = tokens.next()) && token !== TOKEN_GREATER_THAN)
URLString += token;
if (!token)
throw new SyntaxError(this.error_message("*** Unterminated import statement."));
}
else if (token.charAt(0) === TOKEN_DOUBLE_QUOTE)
URLString = token.substr(1, token.length - 2);
else
throw new SyntaxError(this.error_message("*** Expecting '<' or '\"', found \"" + token + "\"."));
CONCAT(this._buffer, "framework(\'");
CONCAT(this._buffer, URLString);
CONCAT(this._buffer, "\');\n");
//CONCAT(this._buffer, isQuoted ? "\", YES);" : "\", NO);");
this._dependencies.push({ path: URLString, isQuoted: isQuoted });
}
function map_type (type) {
if (type === 'void') {
return '"v"';
} else if (type === 'id') {
return '"@"';
}
return '"@"';
}
Preprocessor.prototype.method = function(/*Lexer*/ tokens, ivar_names)
{
var buffer = new StringBuffer(),
token,
selector = "",
parameters = [],
types = [null];
ivar_names = ivar_names || {};
while ((token = tokens.skip_whitespace()) && token !== TOKEN_OPEN_BRACE && token !== TOKEN_SEMICOLON)
{
if (token == TOKEN_COLON)
{
var type = "";
// Colons are part of the selector name
selector += token;
token = tokens.skip_whitespace();
if (token == TOKEN_OPEN_PARENTHESIS)
{
// Swallow parameter/return type. Perhaps later we can use this for debugging?
while ((token = tokens.skip_whitespace()) && token != TOKEN_CLOSE_PARENTHESIS)
type += token;
token = tokens.skip_whitespace();
}
// Add the type. If it's empty, add null instead.
types[parameters.length + 1] = type || null;
// Since this follows a colon, this must be the parameter name.
parameters[parameters.length] = token;
if (token in ivar_names)
CPLog.warn(this.error_message("*** Warning: Method ( "+selector+" ) uses a parameter name that is already in use ( "+token+" )"));
}
else if (token == TOKEN_OPEN_PARENTHESIS)
{
var type = "";
// Since :( is handled above, this must be the return type, just swallow it.
while ((token = tokens.skip_whitespace()) && token != TOKEN_CLOSE_PARENTHESIS)
type += token;
// types[0] is the return argument
types[0] = type || null;
}
// Argument list ", ..."
else if (token == TOKEN_COMMA)
{
// At this point, "..." MUST follow.
if ((token = tokens.skip_whitespace()) != TOKEN_PERIOD || tokens.next() != TOKEN_PERIOD || tokens.next() != TOKEN_PERIOD)
throw new SyntaxError(this.error_message("*** Argument list expected after ','."));
// FIXME: Shouldn't allow any more after this.
}
// Build selector name.
else
selector += token;
}
if (token === TOKEN_SEMICOLON)
{
token = tokens.skip_whitespace();
if (token !== TOKEN_OPEN_BRACE)
{
throw new SyntaxError(this.error_message("Invalid semi-colon in method declaration. "+
"Semi-colons are allowed only to terminate the method signature, before the open brace."));
}
}
var index = 0,
count = parameters.length;
if (this._classMethod) {
CONCAT(buffer, " meta_class.");
} else {
CONCAT(buffer, " the_class.");
}
CONCAT(buffer, "addMethod(\"");
// first the selector
CONCAT(buffer, selector);
// then the types
CONCAT(buffer, "\", { retval: ");
CONCAT(buffer, map_type(types[0]));
CONCAT(buffer, ", args: [ \"@\", \":\", ");
for (var i = 1; i < types.length; i++) {
CONCAT(buffer, map_type(types[1]));
if (i+1 != types.length) {
CONCAT(buffer, ', ');
}
}
CONCAT(buffer, " ]");
CONCAT(buffer, " }, ");
this._currentSelector = selector;
//if (this._flags & Preprocessor.Flags.IncludeDebugSymbols)
// CONCAT(buffer, " $" + this._currentClass + "__" + selector.replace(/:/g, "_"));
// finally the function implementation
CONCAT(buffer, "function (self, _cmd");
for (; index < count; ++index)
{
CONCAT(buffer, ", ");
CONCAT(buffer, parameters[index]);
}
CONCAT(buffer, ") {\n with (self) {\n");
CONCAT(buffer, "// BEGIN METHOD IMPLEMENTATION\n");
console.error(tokens)
CONCAT(buffer, this.preprocess(tokens, null, TOKEN_CLOSE_BRACE, TOKEN_OPEN_BRACE));
CONCAT(buffer, "// END METHOD IMPLEMENTATION\n");
CONCAT(buffer, " }\n }");
// TODO: actually use Flags.IncludeTypeSignatures flag instead of tying to Flags.IncludeDebugSymbols
//if (this._flags & Preprocessor.Flags.IncludeDebugSymbols) //flags.IncludeTypeSignatures)
// CONCAT(buffer, ","+JSON.stringify(types));
CONCAT(buffer, ")");
this._currentSelector = "";
return buffer;
}
Preprocessor.prototype.preprocess = function(tokens, /*StringBuffer*/ aStringBuffer, terminator, instigator, tuple)
{
var buffer = aStringBuffer ? aStringBuffer : new StringBuffer(),
count = 0,
token = "";
if (tuple)
{
tuple[0] = buffer;
var bracket = false,
closures = [0, 0, 0];
}
while ((token = tokens.next()) && ((token !== terminator) || count))
{
if (tuple)
{
// Ignore :'s the belong to ternary operators (?:)
if (token === TOKEN_QUESTION_MARK)
++closures[2];
// Ingore anything between { } and ()
else if (token === TOKEN_OPEN_BRACE)
++closures[0];
else if (token === TOKEN_CLOSE_BRACE)
--closures[0];
else if (token === TOKEN_OPEN_PARENTHESIS)
++closures[1];
else if (token === TOKEN_CLOSE_PARENTHESIS)
--closures[1];
// If not in {} and not in () and this is a colon and we don't belong to a tertiary operator OR this is a closing bracket...
else if ((token === TOKEN_COLON && closures[2]-- === 0 ||
(bracket = (token === TOKEN_CLOSE_BRACKET))) &&
closures[0] === 0 && closures[1] === 0)
{
tokens.push(); // 1
// If a bracket made us enter, go backwards skipping whitespace ([a b ] allowed),
// if not grab token immediately behind us ([a b : c] not allowed
var label = bracket ? tokens.skip_whitespace(true) : tokens.previous(),
isEmptyLabel = TOKEN_WHITESPACE.test(label);
// The label must be an identifier, and preceded by whitespace, or whitespace itself (the "empty label")
if (isEmptyLabel || TOKEN_IDENTIFIER.test(label) && TOKEN_WHITESPACE.test(tokens.previous()))
{
tokens.push(); // 2
var last = tokens.skip_whitespace(true),
operatorCheck = true,
isDoubleOperator = false;
// unary or binary, still disables.
// + - + x is bad because it could be (+ - + x) or (a + - + x)
// the only good is unbroken chain
if (last === '+' || last === '-'){//alert(tokens.last())
if (tokens.previous() !== last)
operatorCheck = false;
else
{
last = tokens.skip_whitespace(true);
isDoubleOperator = true;
}}
tokens.pop(); // 2
tokens.pop(); // 1
//alert(operatorCheck + "operatorCheck for " + label + " and " + last);
if (operatorCheck && (
// <)> <label><:|]>
// <}> <label><:|]>
// <]> <label><:|]>
(!isDoubleOperator && (last === TOKEN_CLOSE_BRACE)) ||
last === TOKEN_CLOSE_PARENTHESIS || last === TOKEN_CLOSE_BRACKET ||
// <.> label<:|]> --> 5. label
// <5> label<:|]>
last === TOKEN_PERIOD || TOKEN_NUMBER.test(last) ||
// <string> <label><:\]>/
last.charAt(last.length - 1) === '\"' || last.charAt(last.length - 1) === '\'' ||
// <identifier-not> <label><:|]>
TOKEN_IDENTIFIER.test(last) && !/^(new|return|case|var)$/.test(last)))
{
if (isEmptyLabel)
tuple[1] = ':';
else
{
tuple[1] = label;
if (!bracket)
tuple[1] += ':';
var count = buffer.atoms.length;
while (buffer.atoms[count--] !== label) ;
buffer.atoms.length = count;
}
return !bracket;
}
if (bracket)
return NO;
}
tokens.pop(); // 2
if (bracket)
return NO;
}
closures[2] = MAX(closures[2], 0);
}
if (instigator)
{
if (token === instigator)
++count;
else if (token === terminator)
--count;
}
// Safari can't handle function declarations of the form function [name]([arguments]) { }
// in evals. It requires them to be in the form [name] = function([arguments]) { }. So we
// need to find these and fix them.
if (token === TOKEN_FUNCTION)
{//if (window.p) alert("function");
var accumulator = "";
// Following the function identifier we can either have an open parenthesis or an identifier:
while ((token = tokens.next()) && token !== TOKEN_OPEN_PARENTHESIS && !(/^\w/).test(token))
accumulator += token;
// If the next token is an open parenthesis, we have a standard function and we don't have to
// change it:
if (token === TOKEN_OPEN_PARENTHESIS)
{
if (instigator === TOKEN_OPEN_PARENTHESIS)
++count;
CONCAT(buffer, "function" + accumulator + '(');
if (tuple)
++closures[1];
}
// If it's not a parenthesis, we know we have a non-supported function declaration, so fix it:
else
{
CONCAT(buffer, token + " = function");
/*#if FIREBUG
var functionName = token;
// Skip everything until the next close parenthesis.
while ((token = tokens.next()) && token != TOKEN_CLOSE_PARENTHESIS)
CONCAT(buffer, token);
// Don't forget the last token!
CONCAT(buffer, token);
// Skip everything until the next open curly brace.
while ((token = tokens.next()) && token != TOKEN_OPEN_BRACE)
CONCAT(buffer, token);
if (tuple)
++closures[2];
// Place the open curly brace as well, and the function name
CONCAT(buffer, token + "\n \"__FIREBUG_FNAME__" + functionName + "\".length;\n");
#endif*/
}
}
// If we reach an @ symbol, we are at a preprocessor directive.
else if (token == TOKEN_PREPROCESSOR)
this.directive(tokens, buffer);
// If we reach a # symbol, we may be at a C preprocessor directive.
else if (token == TOKEN_HASH)
this.hash(tokens, buffer);
// If we reach a bracket, we will either be preprocessing a message send, a literal
// array, or an array index.
else if (token == TOKEN_OPEN_BRACKET)
this.brackets(tokens, buffer);
// If not simply append the token.
else
CONCAT(buffer, token);
}
// If we get this far and we're parsing an objj_msgSend (or array), then we have a problem.
if (tuple)
throw new SyntaxError(this.error_message("*** Expected ']' - Unterminated message send or array."));
if (!aStringBuffer)