-
Notifications
You must be signed in to change notification settings - Fork 2
/
homunculus.js
11183 lines (11054 loc) · 371 KB
/
homunculus.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.homunculus = {}));
}(this, function (exports) { 'use strict';
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
function inheritPrototype(subType, superType) {
var prototype = Object.create(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
//继承static变量
Object.keys(superType).forEach(function(k) {
subType[k] = superType[k];
});
return subType;
}
function wrap(fn) {
fn.extend = function(sub) {
inheritPrototype(sub, fn);
return wrap(sub);
};
fn.methods = function(o) {
Object.keys(o).forEach(function(k) {
fn.prototype[k] = o[k];
});
return fn;
};
fn.statics = function(o) {
Object.keys(o).forEach(function(k) {
fn[k] = o[k];
});
return fn;
};
return fn;
}
function klass(cons) {
return wrap(cons || function() {});
}
klass.extend = inheritPrototype;
var Class = klass;
var character = createCommonjsModule(function (module, exports) {
exports.LINE = '\n';
exports.ENTER = '\r';
exports.BLANK = ' ';
exports.TAB = '\t';
exports.UNDERLINE = '_';
exports.DOLLAR = '$';
exports.SHARP = '#';
exports.MINUS = '-';
exports.AT = '@';
exports.SLASH = '/';
exports.BACK_SLASH = '\\';
exports.DECIMAL = '.';
exports.LEFT_BRACKET = '[';
exports.RIGHT_BRACKET = ']';
exports.LEFT_BRACE = '{';
exports.RIGHT_BRACE = '}';
exports.STAR = '*';
exports.LEFT_PARENTHESE = '(';
exports.RIGHT_PARENTHESE = ')';
exports.COMMA = ',';
exports.SEMICOLON = ';';
exports.EQUAL = '=';
exports.GRAVE = '`';
exports.isDigit = function(c) {
return c >= '0' && c <= '9';
};
exports.isDigit16 = function(c) {
return exports.isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
};
exports.isDigit2 = function(c) {
return c == '0' || c == '1';
};
exports.isDigit8 = function(c) {
return c >= '0' && c <= '7';
};
exports.isLetter = function(c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
};
exports.count = function(s, c) {
var count = 0,
i = -1;
while((i = s.indexOf(c, i + 1)) != -1) {
count++;
}
return count;
};
exports.isUndefined = function(s) {
return typeof s == 'undefined';
};
exports.isString = function(s) {
return Object.prototype.toString.call(s) == "[object String]";
};
exports.isNumber = function(s) {
return Object.prototype.toString.call(s) == "[object Number]";
};
});
var character_1 = character.LINE;
var character_2 = character.ENTER;
var character_3 = character.BLANK;
var character_4 = character.TAB;
var character_5 = character.UNDERLINE;
var character_6 = character.DOLLAR;
var character_7 = character.SHARP;
var character_8 = character.MINUS;
var character_9 = character.AT;
var character_10 = character.SLASH;
var character_11 = character.BACK_SLASH;
var character_12 = character.DECIMAL;
var character_13 = character.LEFT_BRACKET;
var character_14 = character.RIGHT_BRACKET;
var character_15 = character.LEFT_BRACE;
var character_16 = character.RIGHT_BRACE;
var character_17 = character.STAR;
var character_18 = character.LEFT_PARENTHESE;
var character_19 = character.RIGHT_PARENTHESE;
var character_20 = character.COMMA;
var character_21 = character.SEMICOLON;
var character_22 = character.EQUAL;
var character_23 = character.GRAVE;
var character_24 = character.isDigit;
var character_25 = character.isDigit16;
var character_26 = character.isDigit2;
var character_27 = character.isDigit8;
var character_28 = character.isLetter;
var character_29 = character.count;
var character_30 = character.isUndefined;
var character_31 = character.isString;
var character_32 = character.isNumber;
var tid = 0;
var types;
var Token = Class(function(type, content, val, sIndex) {
this.t = type; //token类型
this.c = content; //token的字面内容,string包括头尾的引号
this.pr = null;
this.ne = null;
this.li = -1;
this.co = -1;
if(character.isNumber(val)) {
sIndex = val;
val = content;
}
else if(character.isUndefined(val)) {
val = content;
sIndex = -1;
}
this.v = val; //token的值,一般情况下等于content,特殊如string情况下值是不加头尾的引号
this.id = tid++; //token的索引
this.si = sIndex; //token在源码字符串中的索引
}).methods({
type: function(t) {
if(t !== void 0) {
this.t = t;
}
return this.t;
},
content: function(c) {
if(c !== void 0) {
this.c = c;
}
return this.c;
},
val: function(v) {
if(v !== void 0) {
this.v = v;
}
return this.v;
},
tag: function(t) {
if(t !== void 0) {
this.t = t;
}
return Token.type(this.t);
},
tid: function(id) {
if(id !== void 0) {
this.id = id;
}
return this.id;
},
sIndex: function(si) {
if(si !== void 0) {
this.si = si;
}
return this.si;
},
prev: function(t) {
if(t !== void 0) {
this.pr = t;
}
return this.pr;
},
next: function(t) {
if(t !== void 0) {
this.ne = t;
}
return this.ne;
},
cancel: function() {
tid--;
},
isVirtual: function() {
return this.t == Token.VIRTUAL;
},
line: function(i) {
if(i !== undefined) {
this.li = i;
}
return this.li;
},
col: function(i) {
if(i !== undefined) {
this.co = i;
}
return this.co;
}
}).statics({
//公用
IGNORE: -2,
VIRTUAL: -1,
OTHER: 0,
BLANK: 1,
TAB: 2,
LINE: 3,
NUMBER: 4,
ID: 5,
COMMENT: 6,
STRING: 7,
SIGN: 8,
KEYWORD: 10,
//js部分
REG: 9,
//es6
TEMPLATE: 13,
//仅java
ANNOT: 11,
//基本无用
ENTER: 14,
TEMPLATE_HEAD: 29,
TEMPLATE_MIDDLE: 30,
TEMPLATE_TAIL: 31,
type: function(tag) {
var self = this;
if(character.isUndefined(types)) {
types = [];
Object.keys(self).forEach(function(o) {
if(typeof self[o] == 'number') {
types[self[o]] = o;
}
});
}
return types[tag];
},
reset: function() {
tid = 0;
}
});
var Token_1 = Token;
var walk = createCommonjsModule(function (module, exports) {
var index;
function recursion(node, ignore, nodeVisitors, tokenVisitors) {
var isToken = node.isToken();
if(isToken) {
var token = node.token();
var isVirtual = token.isVirtual();
if(!isVirtual) {
var ignores = [];
while(ignore[++index]) {
ignores.push(ignore[index]);
}
if(tokenVisitors.hasOwnProperty(token.type())) {
tokenVisitors[token.type()](token, ignores);
}
}
}
else {
if(nodeVisitors.hasOwnProperty(node.name())) {
nodeVisitors[node.name()](node);
}
node.leaves().forEach(function(leaf) {
recursion(leaf, ignore, nodeVisitors, tokenVisitors);
});
}
}
exports.simple = function(node, nodeVisitors, tokenVisitors) {
exports.simpleIgnore(node, {}, nodeVisitors, tokenVisitors);
};
exports.simpleIgnore = function(node, ignore, nodeVisitors, tokenVisitors) {
index = 0;
while(ignore[index]) {
ignore[index++];
}
nodeVisitors = nodeVisitors || {};
tokenVisitors = tokenVisitors || {};
recursion(node, ignore, nodeVisitors, tokenVisitors);
};
function rcs(node, callback) {
var isToken = node.isToken();
if(isToken) {
var token = node.token();
var isVirtual = token.isVirtual();
if(!isVirtual) {
callback(token, true);
}
}
else {
callback(node);
node.leaves().forEach(function(leaf) {
rcs(leaf, callback);
});
}
}
exports.recursion = function(node, callback) {
rcs(node, callback);
};
function toPlainObject(node, res) {
var isToken = node.isToken();
if(isToken) {
var token = node.token();
var isVirtual = token.isVirtual();
if(!isVirtual) {
res.push(token.content());
}
}
else {
res.push(node.name().toUpperCase());
var childs = [];
res.push(childs);
node.leaves().forEach(function(leaf) {
toPlainObject(leaf, childs);
});
}
return res;
}
exports.plainObject = function(obj) {
//token
if(Array.isArray(obj)) {
var res = [];
obj.forEach(function(token) {
res.push(token.content());
});
return res;
}
//ast
else {
return toPlainObject(obj, []);
}
};
});
var walk_1 = walk.simple;
var walk_2 = walk.simpleIgnore;
var walk_3 = walk.recursion;
var walk_4 = walk.plainObject;
var Lexer = Class(function(rule) {
this.rule = rule; //当前语法规则
this.init();
}).methods({
init: function() {
this.code = ''; //要解析的代码
this.peek = ''; //向前看字符
this.index = 0; //向前看字符字符索引
this.isReg = Lexer.IS_REG; //当前/是否是perl风格正则表达式
this.tokenList = []; //结果的token列表
this.parentheseState = false; //(开始时标记之前终结符是否为if/for/while等关键字
this.parentheseStack = []; //圆括号深度记录当前是否为if/for/while等语句内部
this.braceState = false; //{是object还是block
this.braceStack = []; //深度记录
this.cacheLine = 0; //行缓存值
this.totalLine = 1; //总行数
this.colNum = 0; //列
this.colMax = 0; //最大列数
this.isReturn = false; //当出现return,后面有换行则自动插入;,影响{的语意
this.last = null;
},
parse: function(code) {
this.code = code || '';
var temp = [];
this.scan(temp);
return temp;
},
parseOn: function() {
var temp = [];
this.scan(temp);
return temp;
},
tokens: function(plainObject) {
if(plainObject) {
return walk.plainObject(this.tokenList);
}
return this.tokenList;
},
scan: function(temp) {
var perlReg = this.rule.perlReg();
var length = this.code.length;
var count = 0;
this.colNum = length ? 1 : 0;
outer:
while(this.index < length) {
if(this.cacheLine > 0 && count >= this.cacheLine) {
break;
}
this.readch();
//perl风格正则
if(perlReg
&& this.isReg == Lexer.IS_REG
&& this.peek == character.SLASH
&& !{ '/': true, '*': true }[this.code.charAt(this.index)]) {
this.dealReg(temp, length);
this.isReg = Lexer.NOT_REG;
}
//依次遍历匹配规则,命中则继续
else {
for(var i = 0, matches = this.rule.matches(), len = matches.length; i < len; i++) {
var match = matches[i];
if(match.match(this.peek, this.code, this.index)) {
var token = new Token_1(match.tokenType(), match.content(), match.val(), this.index - 1);
var error = match.error();
var matchLen = match.content().length;
if(error) {
this.error(error, this.code.slice(this.index - matchLen, this.index));
}
if(token.type() == Token_1.ID
&& this.rule.keyWords().hasOwnProperty(token.content())) {
token.type(Token_1.KEYWORD);
}
//回调可自定义处理匹配的token
if(match.callback) {
match.callback.call(match, token, this.tokenList);
}
//回调特殊处理忽略掉此次匹配
if(match.cancel) {
token.cancel();
continue;
}
var n = character.count(token.val(), character.LINE);
count += n;
//处理token
this.dealToken(token, matchLen, n, temp);
//支持perl正则需判断关键字、圆括号对除号语义的影响
if(perlReg && match.perlReg() != Lexer.IGNORE) {
this.stateReg(match);
}
//处理{
this.stateBrace(match.content(), token.type());
continue outer;
}
}
//如果有未匹配的,说明规则不完整,抛出错误
this.error('unknow token');
}
}
return this;
},
dealToken: function(token, matchLen, count, temp) {
if(this.last) {
token.prev(this.last);
this.last.next(token);
}
this.last = token;
temp.push(token);
this.tokenList.push(token);
this.index += matchLen - 1;
token.line(this.totalLine);
token.col(this.colNum);
this.totalLine += count;
if(count) {
var j = token.content().indexOf(character.LINE);
var k = token.content().lastIndexOf(character.LINE);
this.colMax = Math.max(this.colMax, this.colNum + j);
this.colNum = token.content().length - k;
}
else {
this.colNum += matchLen;
}
this.colMax = Math.max(this.colMax, this.colNum);
},
stateReg: function(match) {
if(match.perlReg() == Lexer.SPECIAL) {
this.isReg = match.special();
}
else {
this.isReg = match.perlReg();
}
if(this.peek == character.LEFT_PARENTHESE) {
this.parentheseStack.push(this.parentheseState);
this.parentheseState = false;
}
else if(this.peek == character.RIGHT_PARENTHESE) {
this.isReg = this.parentheseStack.pop() ? Lexer.IS_REG : Lexer.NOT_REG;
}
else {
this.parentheseState = match.parenthese();
}
},
stateBrace: function(content, type) {
if(content == '{') {
if(this.isReturn) {
this.braceState = true;
}
this.braceStack.push(this.braceState);
this.isReturn = false;
}
else if(content == '}') {
this.braceState = this.braceStack.pop();
if(this.braceState) {
this.isReg = false;
}
this.isReturn = false;
}
else if(type == Token_1.SIGN) {
//反向设置,符号大多出现expr中,后跟{表示object;某些不能跟;以下(换行)跟表示block
this.braceState = !{
'--': true,
'++': true,
'=>': true,
';': true,
')': true
}.hasOwnProperty(content);
this.isReturn = false;
}
else if(type == Token_1.KEYWORD) {
this.braceState = {
'instanceof': true,
'delete': true,
'void': true,
'typeof': true,
'return': true
}.hasOwnProperty(content);
this.isReturn = content == 'return';
}
else if([Token_1.BLANK, Token_1.TAB, Token_1.LINE, Token_1.COMMENT].indexOf(type) == -1) {
this.braceState = false;
}
else if(type == Token_1.LINE
|| type == Token_1.COMMENT
&& content.indexOf('\n') > -1) {
if(this.isReturn) {
this.braceState = false;
this.isReturn = false;
}
}
},
dealReg: function(temp, length) {
var lastIndex = this.index - 1;
var res = false;
outer:
do {
this.readch();
if(this.peek == character.LINE) {
this.error('SyntaxError: unterminated regular expression literal '
+ this.peek, this.code.slice(lastIndex, this.index));
break;
}
else if(this.peek == character.BACK_SLASH) {
this.index++;
}
else if(this.peek == character.LEFT_BRACKET) {
do {
this.readch();
if(this.peek == character.LINE) {
this.error('SyntaxError: unterminated regular expression literal '
+ this.peek, this.code.slice(lastIndex, this.index));
break outer;
}
else if(this.peek == character.BACK_SLASH) {
this.index++;
}
else if(this.peek == character.RIGHT_BRACKET) {
continue outer;
}
} while(this.index < length);
}
else if(this.peek == character.SLASH) {
res = true;
var hash = {};
var flag = {
'g': true,
'i': true,
'm': true,
'u': true,
'y': true
};
//正则的flag中有gimuy5种,大小写敏感且不能重复
do {
this.readch();
if(character.isLetter(this.peek)) {
if(hash.hasOwnProperty(this.peek) || !flag.hasOwnProperty(this.peek)) {
this.error('SyntaxError: invalid regular expression flag '
+ this.peek, this.code.slice(lastIndex, this.index));
break outer;
}
hash[this.peek] = true;
}
else {
break outer;
}
} while(this.index <= length);
}
} while(this.index < length);
if(!res) {
this.error('SyntaxError: unterminated regular expression literal',
this.code.slice(lastIndex, this.index - 1));
}
var token = new Token_1(Token_1.REG, this.code.slice(lastIndex, --this.index), lastIndex);
this.index = lastIndex + 1;
this.dealToken(token, token.content().length, 0, temp);
},
readch: function() {
this.peek = this.code.charAt(this.index++);
},
cache: function(i) {
if(!character.isUndefined(i) && i !== null) {
this.cacheLine = i;
}
return this.cacheLine;
},
finish: function() {
return this.index >= this.code.length;
},
line: function() {
return this.totalLine;
},
col: function() {
return this.colMax;
},
error: function(s, str) {
if(character.isUndefined(str)) {
str = this.code.substr(this.index - 1, 20);
}
if(Lexer.mode() === Lexer.STRICT) {
throw new Error(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str);
}
else if(Lexer.mode() === Lexer.LOOSE && typeof console !== void 0) {
console.warn(s + ', line ' + this.line() + ' col ' + this.colNum + '\n' + str);
}
return this;
}
}).statics({
IGNORE: 0,
IS_REG: 1,
NOT_REG: 2,
SPECIAL: 3,
STRICT: 0,
LOOSE: 1,
mode: function(i) {
if(!character.isUndefined(i)) {
cmode = i;
}
return cmode;
}
});
var cmode = Lexer.STRICT;
var Lexer_1 = Lexer;
var Rule = Class(function(words, pReg) {
var self = this;
self.kw = {};
words.forEach(function(o) {
self.kw[o] = true;
});
self.pReg = pReg || false;
self.matchList = [];
}).methods({
perlReg: function() {
return this.pReg;
},
addMatch: function(match) {
this.matchList.push(match);
return this;
},
matches: function() {
return this.matchList;
},
keyWords: function() {
return this.kw;
},
addKeyWord: function(v) {
this.kw[v] = true;
return this.kw;
}
});
var Rule_1 = Rule;
var CssToken = Token_1.extend(function(type, content, val, sIndex) {
Token_1.call(this, type, content, val, sIndex);
}).statics({
HEAD: 12,
PROPERTY: 15,
VARS: 16,
HACK: 17,
IMPORTANT: 18,
PSEUDO: 19,
UNITS: 20,
SELECTOR: 21,
ATTR: 22,
COLOR: 23
});
var CssToken_1 = CssToken;
var Match = Class(function(type, setPReg, special, parenthese) {
this.type = type;
if(character.isUndefined(setPReg)) {
setPReg = Lexer_1.IGNORE;
}
this.setPReg = setPReg;
this.result = null;
//忽略0,是1,否2,特殊3
if(setPReg) {
if(character.isUndefined(special)) {
special = function() {
return Lexer_1.IGNORE;
};
}
if(character.isUndefined(parenthese)) {
parenthese = function() {
return false;
};
}
}
this.special = special;
this.parenthese = parenthese;
}).methods({
tokenType: function() {
return this.type;
},
perlReg: function() {
return this.setPReg;
},
val: function() {
return this.content();
},
content: function() {
return this.result;
},
match: function(c, code, index) {
//需被实现
throw new Error('match needs to be implement');
},
error: function() {
return false;
}
});
var RegMatch = Match.extend(function(type, reg, valid, setPReg, special, parenthese) {
if(typeof valid == 'number') {
parenthese = special;
special = setPReg;
setPReg = valid;
valid = null;
}
Match.call(this, type, setPReg, special, parenthese);
this.reg = reg;
this.valid = valid;
}).methods({
match: function(c, code, index) {
var self = this,
res = self.reg.exec(code.slice(index - 1));
self.msg = null;
if(res) {
self.result = res[0];
if(self.valid) {
for(var i = 0, keys = Object.keys(self.valid), len = keys.length; i < len; i++) {
if(self.valid[keys[i]].test(self.result)) {
self.msg = keys[i];
break;
}
}
}
return true;
}
return false;
},
error: function() {
return this.msg;
}
});
var RegMatch_1 = RegMatch;
var S = {
'\n': true,
'\r': true,
' ': true,
'\t': true
};
var ADD_VALUE = new RegMatch_1(CssToken_1.ID, /^[a-z][\w\-\+\.]*/i);
var CssLexer = Lexer_1.extend(function(rule) {
Lexer_1.call(this, rule);
this.media = false;
this.impt = false;
this.value = false;
this.parenthese = false;
this.bracket = false;
this.number = false;
this.url = false;
this.kw = false;
this.sel = true;
this.va = false;
this.cvar = false;
this.page = false;
this.kf = false;
this.ns = false;
this.doc = false;
this.supports = false;
this.extend = false;
this.param = false;
this.depth = 0;
}).methods({
//@override
scan: function(temp) {
var length = this.code.length;
var count = 0;
this.colNum = length ? 1 : 0;
outer:
while(this.index < length) {
if(this.cacheLine > 0 && count >= this.cacheLine) {
break;
}
this.readch();
//(之后的字符串可省略"号
if(this.parenthese && this.url) {
if(!S.hasOwnProperty(this.peek)
&& !{
"'": true,
'"': true,
')': true,
'$': true
}.hasOwnProperty(this.peek)) {
this.dealPt(temp);
this.url = false;
continue outer;
}
//url只能省略一次,即url()中第一个出现的非空白token,多个的话不能省略
this.url = false;
}
for(var i = 0, matches = this.rule.matches(), len = matches.length; i < len; i++) {
var match = matches[i];
if(match.match(this.peek, this.code, this.index)) {
var token = new CssToken_1(match.tokenType(), match.content(), match.val(), this.index - 1);
var matchLen = match.content().length;
//回调可自定义处理匹配的token
if(match.callback) {
match.callback(token);
}
var s = token.content().toLowerCase();
switch(token.type()) {
//单位必须紧跟数字,否则便不是单位
case CssToken_1.BLANK:
case CssToken_1.TAB:
case CssToken_1.LINE:
this.number = false;
break;
//@import和@media之后进入值状态
case CssToken_1.HEAD:
s = s.replace(/^@(-moz-|-o-|-ms-|-webkit-|-vx-|-hp-|-khtml-|mso-|-prince-|-rim-|-ro-|-tc-|-wap-|-apple-|-atsc-|-ah-)/, '@');
this.sel = false;
this.kw = false;
this.value = true;
this.number = false;
this.va = false;
switch(s) {
case '@import':
this.impt = true;
break;
case '@media':
this.media = true;
break;
case '@page':
this.page = true;
break;
case '@keyframes':
this.kf = true;
break;
case '@document':
this.doc = true;
break;
case '@supports':
this.supports = true;
break;
case '@extend':
this.extend = true;
this.value = false;
this.sel = true;
break;
}
break;
//单位要跟在数字之后,否则便不是单位
case CssToken_1.UNITS:
if(!this.number) {
continue;
}
this.sel = false;
this.kw = false;
this.va = false;
this.number = false;
this.page = false;
this.kf = false;
this.ns = false;
this.doc = false;
break;
case CssToken_1.KEYWORD:
if(!this.value || this.supports) {
this.kw = true;
this.url = false;
this.va = false;
this.sel = false;
this.number = false;
this.page = false;
this.kf = false;
this.ns = false;
this.doc = false;
this.parenthese = false;
}
break;
case CssToken_1.COLOR:
if(!this.value && !this.param && !this.cvar) {
token.type(CssToken_1.SELECTOR);
}
break;
//将id区分出属性名和属性值
case CssToken_1.ID:
if(this.bracket && this.sel) {
token.type(CssToken_1.ATTR);
this.url = false;
this.va = false;
}
else if(this.extend) {
token.type(CssToken_1.SELECTOR);
}
else if(this.number) {
token.type(CssToken_1.UNITS);
this.url = false;
this.kw = false;
this.va = false;
}
else if(this.page || this.kf || this.ns) {
this.sel = true;
this.url = false;
this.kw = false;
this.va = false;
this.value = false;
}
else if(this.va) {
token.type(CssToken_1.VARS);
this.url = false;
this.va = false;
}
else if(this.supports) {
if(this.rule.keyWords().hasOwnProperty(s)) {
token.type(CssToken_1.KEYWORD);
}
else {
token.type(CssToken_1.PROPERTY);
}
}
else if(this.param || this.cvar) {
//value时id可以带+号,必须紧跟
if(this.code.charAt(this.index) == '+') {
ADD_VALUE.match(this.peek, this.code, this.index);
token = new CssToken_1(ADD_VALUE.tokenType(), ADD_VALUE.content(), ADD_VALUE.val(), this.index - 1);
matchLen = ADD_VALUE.content().length;
}
if(this.rule.keyWords().hasOwnProperty(s)) {
//前面是hack也作为关键字
if(this.tokenList[this.tokenList.length - 1].type() == CssToken_1.HACK) {
token.type(CssToken_1.KEYWORD);
this.kw = true;
}
else {
//LL2确定后面如果是:说明是关键字($var:keyword:)
for(var j = this.index + matchLen - 1; j < length; j++) {
var c = this.code.charAt(j);
if(!S.hasOwnProperty(c)) {
if(c == ':') {
token.type(CssToken_1.KEYWORD);
this.kw = true;
}
else {
token.type(CssToken_1.PROPERTY);
this.value = true;