您的位置:首页 > 理论基础 > 计算机网络

解读uglifyJS(转载:http://rapheal.sinaapp.com/2014/05/15/uglifyjs-ast-parse/)

2015-05-31 19:15 686 查看
javascript

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

//语法分析 by raphealguo
//得到一个AST树
//parse-js移植于一个Lisp的项目
//所以获得的AST树类似Lisp里边的表,数组套数组:["toplevel", ["stat", [xxxx]]]
 
//@param $TEXT 输入源代码
//@param exigent_mode 严格的模式,这个模式不自动插入分号解析
//@param embed_tokens @unknowed
function parse($TEXT, exigent_mode, embed_tokens) {
var S = {
//输入,可以自己构造一个token输入器传递进来 否则就用tokenizer返回的next_token作为输入器
input         : typeof $TEXT == "string" ? tokenizer($TEXT, true) : $TEXT,
 
//当前token
token         : null,
 
//上一个token
prev          : null,
 
//向前看一个token
peeked        : null,
 
//是不是在一个function里边
in_function   : 0,
 
//在函数里边标记"use strict"这种指示性字符串
in_directives : true,
 
//是不是在循环里边
in_loop       : 0,
 
//labels集合
labels        : []
};
 
S.token = next();//先拿到第一个token
 
/*
function is_token(token, type, val) {
return token.type == type && (val == null || token.value == val);
};
*/
//看看当前token的类型跟值对不对得上
function is(type, value) {
return is_token(S.token, type, value);
};
 
//获取当前token
function peek() { return S.peeked || (S.peeked = S.input()); };
 
//获取下一个token
function next() {
S.prev = S.token;//记录前一个token
if (S.peeked) {//看看当前已经peek过没 有的话 直接拿它做next token就好了
S.token = S.peeked;
S.peeked = null;
} else {//否则 S.input == tokenizer返回的next_token钩子
S.token = S.input();
}
 
//"use strict";这样才算是指示性字符串
S.in_directives = S.in_directives && (
S.token.type == "string" || is("punc", ";")
);
 
//返回拿到的token
return S.token;
};
 
//获取上一个token
function prev() {
return S.prev;
};
 
//辅助函数报错
function croak(msg, line, col, pos) {
var ctx = S.input.context();
js_error(msg,
line != null ? line : ctx.tokline,
col != null ? col : ctx.tokcol,
pos != null ? pos : ctx.tokpos);
};
 
function token_error(token, msg) {
croak(msg, token.line, token.col);
};
 
function unexpected(token) {
if (token == null)
token = S.token;
token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")");
};
 
//看看当前token是不是符合当前状态,是的话拿出下一个token
function expect_token(type, val) {
if (is(type, val)) {
return next();
}
token_error(S.token, "Unexpected token " + S.token.type + ", expected " + type);
};
 
function expect(punc) { return expect_token("punc", punc); };
 
//能不能插入分号
function can_insert_semicolon() {
//插入分号的条件是exigent_mode = false,严格模式:
//1. 当前token与上个token之间是否有换行
//2. 文件末尾
//3. }后边
return !exigent_mode && (
S.token.nlb || is("eof") || is("punc", "}")
);
};
 
//状态:分号
function semicolon() {
if (is("punc", ";")) next();//如果当前token是分号 则继续
else if (!can_insert_semicolon()) unexpected();//否则看看能否自动插入分号 否则报错
};
 
//前边说的 最后返回的ast树就是数组套数组
function as() {
return slice(arguments);
};
 
//状态:括号表达式
function parenthesised() {
expect("(");
var ex = expression();
expect(")");
return ex;
};
 
//@unknowed
function add_tokens(str, start, end) {
return str instanceof NodeWithToken ? str : new NodeWithToken(str, start, end);
};
 
//辅助函数 返回一个解析当前语法子AST树的钩子
function maybe_embed_tokens(parser) {
//一般embed_tokens是false
//@unknowed
if (embed_tokens) return function() {
var start = S.token;
var ast = parser.apply(this, arguments);
ast[0] = add_tokens(ast[0], start, prev());
return ast;
};
else return parser;
};
 
//状态:语句块
//这个状态略微复杂
var statement = maybe_embed_tokens(function() {
if (is("operator", "/") || is("operator", "/=")) {
//强制进入正则表达式解析
//语句的开头如果遇到/ 或者/=的时候
//不进入正则表达式的状态 会有语法错误
//而且显然此时应该是一个正则表达式的状态
S.peeked = null;
S.token = S.input(S.token.value.substr(1)); // force regexp
}
 
switch (S.token.type) {
case "string":
var dir = S.in_directives, stat = simple_statement();
 
//in_directives = true 表达了在function(){}的body里边
//此时遇到第一个字符串  看看是不是 "use strict"这样指示性的字符串
if (dir && stat[1][0] == "string" && !is("punc", ","))
return as("directive", stat[1][1]);
return stat;
 
//简单的表达式
case "num":
case "regexp":
case "operator":
case "atom":
return simple_statement();
 
case "name":
//得到一个串,往后看一个token看看是不是: 是的话 认为是一个标签语句
return is_token(peek(), "punc", ":")
//拿到当前的token.value就是标签名字,接着吃掉两个token:label跟冒号
//留意prog1的实现 会把所有参数都当做函数执行一次~
? labeled_statement(prog1(S.token.value, next, next))
 
: simple_statement();
 
//语句块的开头 符号只能是{[(;
//其他符号都是非法
case "punc":
switch (S.token.value) {
case "{":
return as("block", block_());
case "[":
case "(":
return simple_statement();
case ";":
next();
return as("block");
default:
unexpected();
}
 
//轮到关键字
case "keyword":
switch (prog1(S.token.value, next)) {
case "break":
return break_cont("break");
 
case "continue":
return break_cont("continue");
 
case "debugger":
semicolon();
return as("debugger");
 
case "do":
return (function(body){
//do{ body }while(parenthesised);
expect_token("keyword", "while");
return as("do", prog1(parenthesised, semicolon), body);
})(in_loop(statement));
 
case "for":
return for_();
 
case "function":
return function_(true);
 
case "if":
return if_();
 
case "return":
if (S.in_function == 0)//不在函数里边不能return
croak("'return' outside of function");
return as("return",
is("punc", ";")
? (next(), null)
: can_insert_semicolon()
? null
: prog1(expression, semicolon));
 
case "switch":
return as("switch", parenthesised(), switch_block_());
 
case "throw":
if (S.token.nlb)
croak("Illegal newline after 'throw'");
return as("throw", prog1(expression, semicolon));
 
case "try":
return try_();
 
case "var":
return prog1(var_, semicolon);
 
case "const":
return prog1(const_, semicolon);
 
case "while":
return as("while", parenthesised(), in_loop(statement));
 
case "with":
return as("with", parenthesised(), statement());
 
default:
unexpected();
}
}
});
 
//状态:标签
//例如 begin: var a = 1;
function labeled_statement(label) {
//深度搜索push
S.labels.push(label);
var start = S.token, stat = statement();
 
//var STATEMENTS_WITH_LABELS = array_to_hash([ "for", "do", "while", "switch" ]);
//在严格模式下 除了for do while switch外,其他语句不许使用label
if (exigent_mode && !HOP(STATEMENTS_WITH_LABELS, stat[0]))
unexpected(start);
 
//深度搜索pop
S.labels.pop();
 
//得到label树枝
return as("label", label, stat);
};
 
//状态:简单语句
//简单语句 == 表达式+分号
//返回是表达式的值
function simple_statement() {
return as("stat", prog1(expression, semicolon));
};
 
//状态:break
//@param type "continue"/"break"
function break_cont(type) {
var name;
if (!can_insert_semicolon()) {//如果当前不能插入分号 那得看看后边是不是跟一个label了
name = is("name") ? S.token.value : null;
}
if (name != null) {
next();//吃掉一个token
if (!member(name, S.labels))//如果当前label不在当前作用域嵌套的labels里边 报错
croak("Label " + name + " without matching loop or statement");
}
else if (S.in_loop == 0)//不在循环中肯定不能用break跟continue~
croak(type + " not inside a loop or switch");
 
//break continue后边带分号
semicolon();
return as(type, name);
};
 
//状态:for
function for_() {
//留意当前状态已经吃掉一个for了,所以for之后应该期待一个左括号
expect("(");
var init = null;
//for(initStat; expr; expr)
if (!is("punc", ";")) {//如果遇到有initStat的话
//拿到初始化表达式
init = is("keyword", "var")
? (next(), var_(true))
: expression(true, true);
 
//如果下一个token是in的话
if (is("operator", "in")) {
//语法是不允许:for(var i,j in arr)的
if (init[0] == "var" && init[1].length > 1)
croak("Only one variable declaration allowed in for..in loop");
 
//进入for in的状态
return for_in(init);
}
}
return regular_for(init);
};
 
//状态:for后边的语法
function regular_for(init) {
//下边映射了左括号之后的状态:for(init; test; step)
 
//已经处理过初始化表达式了
expect(";");
var test = is("punc", ";") ? null : expression();
expect(";");
var step = is("punc", ")") ? null : expression();
 
//for最后的括号
expect(")");
 
//init代表这个for的初始化表达式
//test表示for的判断表达式
//step表示下一步的表达式
//for的主体 in_loop里边还记录了当前嵌套循环的深度
return as("for", init, test, step, in_loop(statement));
};
 
//状态:for in
function for_in(init) {
//下边映射: for (init in obj)
var lhs = init[0] == "var" ? as("name", init[1][0]) : init;
next();//吃掉in
var obj = expression();
expect(")");
 
//for in的AST树枝
return as("for-in", init, lhs, obj, in_loop(statement));
};
 
//状态:函数
var function_ = function(in_statement) {
//如有name说明不是匿名函数
var name = is("name") ? prog1(S.token.value, next) : null;
 
//function没有作为表达式的时候 不允许匿名!
if (in_statement && !name)
unexpected();
expect("(");
 
//表达式函数:function
//明确function声明定义,没有赋值给变量的函数:defun
/*
以下两种属于function:
(function a(){})();
v = function (){};
以下两种属于defun:
function d(){}
c = function d(){};
*/
return as(in_statement ? "defun" : "function",
name,//函数名
 
// arguments
//函数参数
(function(first, a){
//只要没遇到右括号 就一直扫描
while (!is("punc", ")")) {
//第一个参数前边不用找都好
if (first) first = false; else expect(",");
 
//必须是变量名
if (!is("name")) unexpected();
a.push(S.token.value);//加入参数列表数组
next();
}
next();//吃掉右括号
return a;//得到参数列表
})(true, []),
 
// body
//函数体
(function(){
//嵌套的函数层+1
++S.in_function;
 
var loop = S.in_loop;
 
//进入函体,标记in_directives,为了认"use strict"这种指示性字符串
S.in_directives = true;
 
//函数在循环中的嵌套层数为0!?@unknowed
S.in_loop = 0;
 
//拿到函数体AST树枝
var a = block_();
 
//恢复状态
--S.in_function;
S.in_loop = loop;
return a;
})());
};
 
//状态:if
function if_() {
//if (cond)
var cond = parenthesised(), body = statement(), belse;
 
/*
如果是带else if的
if (cond) {
 
}else if(cond2){
 
}else{
 
}
其实可以解析成
if (cond){
 
}else{
//下边这个块可以作为当前else的statement
if (cond){
 
}else{
 
}
}
*/
//如果有else
if (is("keyword", "else")) {
next();
belse = statement();
}
return as("if", cond, body, belse);
};
 
//状态:块
function block_() {
// { block }
expect("{");
var a = [];
 
//块里边都是语句
while (!is("punc", "}")) {
if (is("eof")) unexpected();
a.push(statement());
}
next();//吃掉}这个token
return a;
};
 
//状态:switch块
 
//执行in_loop 记录嵌套深度
var switch_block_ = curry(in_loop, function(){
expect("{");
var a = [], cur = null;
while (!is("punc", "}")) {
if (is("eof")) unexpected();
//以下两种状态:
//case expression : statement
//default : statement
 
if (is("keyword", "case")) {
next();
cur = [];
a.push([ expression(), cur ]);
expect(":");
}
else if (is("keyword", "default")) {
next();
expect(":");
cur = [];
a.push([ null, cur ]);
}
else {
if (!cur) unexpected();//都没有解析过case跟default 肯定是不对的
cur.push(statement());
}
}
next();
//得到一个列表:
//[ ["case1", [stat1, stat2]], ["case2", [stat1, stat2]], [null, [stat1, stat2]] ]
return a;
});
 
//状态:try
function try_() {
// try {body} catch(name) { bcatch} finally { bfinally }
var body = block_(), bcatch, bfinally;
if (is("keyword", "catch")) {
next();
expect("(");
if (!is("name"))
croak("Name expected");
var name = S.token.value;
next();
expect(")");
bcatch = [ name, block_() ];
}
if (is("keyword", "finally")) {
next();
bfinally = block_();
}
if (!bcatch && !bfinally)//catch跟finally至少要有一个!
croak("Missing catch/finally blocks");
return as("try", body, bcatch, bfinally);
};
 
//状态:var声明list
//@param no_in 不在for in里边
function vardefs(no_in) {
//var a, b, c = 2, d = 1;
//这里的状态对应是:a, b, c = 2, d = 1;
var a = [];
for (;;) {
if (!is("name"))
unexpected();//一定要有变量名
var name = S.token.value;
next();
if (is("operator", "=")) {//如果有=
next();
//拿到表达式的值
a.push([ name, expression(false, no_in) ]);
} else {
//否则变量没有初始化值
a.push([ name ]);
}
//下一个必须是逗号 否则就结束这个var列表定义了
if (!is("punc", ","))
break;
next();
}
return a;
};
 
//状态:var
//var a,b;
function var_(no_in) {
return as("var", vardefs(no_in));
};
 
//状态:const
//const a,b;
function const_() {
return as("const", vardefs());
};
 
//状态:new
//映射成:new newexp(args)
function new_() {
var newexp = expr_atom(false), args;
if (is("punc", "(")) {
next();
args = expr_list(")");
} else {
args = [];
}
return subscripts(as("new", newexp, args), true);
};
 
//状态:元表达式 后边可能带下标
//映射成: new A()  a.b | a["b"] | a.c() | {a:1} | function x(); | /asd/gi.test("xx") |
var expr_atom = maybe_embed_tokens(function(allow_calls) {
//可以嵌套new
//var a = new new A();
if (is("operator", "new")) {
next();
return new_();
}
if (is("punc")) {
switch (S.token.value) {
//为什么以下都要加subscripts修饰呢
//因为:[1,2,3][0]   {a:1}.a  ({a:1}).a 后边还需要解析属性!
case "(":
next();
//得到一个括号表达式
return subscripts(prog1(expression, curry(expect, ")")), allow_calls);
case "[":
next();
//得到一个数组
return subscripts(array_(), allow_calls);
case "{":
next();
//得到一个对象
return subscripts(object_(), allow_calls);
}
unexpected();//除了([{都出错
}
if (is("keyword", "function")) {//匿名函数构造器
next();
return subscripts(function_(false), allow_calls);
}
if (HOP(ATOMIC_START_TOKEN, S.token.type)) {
var atom = S.token.type == "regexp"
//regexp=/abc/gi 带有两个属性:abc以及gi
? as("regexp", S.token.value[0], S.token.value[1])
: as(S.token.type, S.token.value);
return subscripts(prog1(atom, next), allow_calls);
}
unexpected();
});
 
//状态:逗号表达式
//映射成:a,b,c
//@param closing 结束token标记
//@parame allow_trailing_comma 是否允许最末尾加逗号  例如[a,b,c,]
//@param allow_empty 是否允许空值 例如[a,,v]
function expr_list(closing, allow_trailing_comma, allow_empty) {
var first = true, a = [];
//直到遇到token是closing为止
while (!is("punc", closing)) {
//如果不是第一个 就必须有逗号
if (first) first = false; else expect(",");
 
//允许末尾加逗号
if (allow_trailing_comma && is("punc", closing)) break;
if (is("punc", ",") && allow_empty) {
a.push([ "atom", "undefined" ]);
} else {
a.push(expression(false));
}
}
next();
return a;
};
 
//状态:数组
function array_() {
//结束必须是]符号
//严格模式exigent_mode下不允许最后还有逗号 例如[1,2,3,]
//允许空值 例如 [1,,2] == [1, undefined, 2]
return as("array", expr_list("]", !exigent_mode, true));
};
 
//状态:对象
function object_() {
//映射为: { "a" : c, b : 1 }
var first = true, a = [];
while (!is("punc", "}")) {//遇到}才结束
//如果不是第一个 就必须有逗号
if (first) first = false; else expect(",");
 
//允许末尾加逗号
if (!exigent_mode && is("punc", "}"))
// allow trailing comma
break;
 
var type = S.token.type;
 
//拿到属性名字token
var name = as_property_name();
 
if (type == "name" && (name == "get" || name == "set") && !is("punc", ":")) {
//看看是不是get set方法
/*
get set的语法如下:
{
get as_name(){
return this.value;
}
}
*/
a.push([ as_name(), function_(false), name ]);
} else {
expect(":");
a.push([ name, expression(false) ]);
}
}
next();
return as("object", a);
};
 
//状态:拿到一个属性名字
function as_property_name() {
switch (S.token.type) {
case "num":
case "string":
return prog1(S.token.value, next);
}
return as_name();
};
 
//状态:拿到一个名字
function as_name() {
switch (S.token.type) {
case "name":
case "operator"://为什么可以是operator呢?因为token解析出来的操作符是包含"in", "instanceof", "typeof", "new", "void", "delete"关键字的 这些也是可以作为一个名字
case "keyword":
case "atom":
return prog1(S.token.value, next);
default:
unexpected();
}
};
 
//状态:获取对象属性
//分别对应下边的状态:
//expr
//expr.as_name
//expr[expression]
//expr(expr_list)
function subscripts(expr, allow_calls) {
if (is("punc", ".")) {
next();
return subscripts(as("dot", expr, as_name()), allow_calls);
}
if (is("punc", "[")) {
next();
return subscripts(as("sub", expr, prog1(expression, curry(expect, "]"))), allow_calls);
}
if (allow_calls && is("punc", "(")) {
next();
return subscripts(as("call", expr, expr_list(")")), true);
}
return expr;
};
 
//状态:可能是一元操作符
function maybe_unary(allow_calls) {
//看看是不是一元操作符
if (is("operator") && HOP(UNARY_PREFIX, S.token.value)) {
return make_unary("unary-prefix",
prog1(S.token.value, next),
maybe_unary(allow_calls));
}
var val = expr_atom(allow_calls);
while (is("operator") && HOP(UNARY_POSTFIX, S.token.value) && !S.token.nlb) {
val = make_unary("unary-postfix", S.token.value, val);
next();
}
return val;
};
 
//状态:一元操作符
function make_unary(tag, op, expr) {
if ((op == "++" || op == "--") && !is_assignable(expr))
//看看是不是能赋值  例如:++this在严格模式下就不可取
croak("Invalid use of " + op + " operator");
return as(tag, op, expr);
};
 
//状态:二元操作符
function expr_op(left, min_prec, no_in) {
var op = is("operator") ? S.token.value : null;
 
//要兼容for in的情况 在no_in的情况不要吃掉in这个token
if (op && op == "in" && no_in) op = null;
 
//PRECEDENCE 二元操作符一个列表 包含了优先级
//prec为当前操作符的优先级
var prec = op != null ? PRECEDENCE[op] : null;
if (prec != null && prec > min_prec) {//大于当前的优先级 才能解析到当前的表达式
//expr = a+b/c;
//扫描到a 发现+ 接着向后看到了b 接着递归进去expr_op的时候 发现/优先级比+高 所以继续往后看token
next();
var right = expr_op(maybe_unary(true), prec, no_in);
return expr_op(as("binary", op, left, right), min_prec, no_in);
}
//如果是 expr = a/b+c的情况 在递归扫描大b的时候就不会进来上边那个if 直接返回b
return left;
};
 
//辅助函数 只有maybe_conditional用到 感觉应该废弃掉这个
function expr_ops(no_in) {
return expr_op(maybe_unary(true), 0, no_in);
};
 
//状态:操作数
//看看是否有三元操作符?:
function maybe_conditional(no_in) {
//映射成:expr ? yes : no_expr
var expr = expr_ops(no_in);
if (is("operator", "?")) {
next();
var yes = expression(false);
expect(":");
return as("conditional", expr, yes, expression(false, no_in));
}
return expr;
};
 
//辅助函数 看看expr能否被赋值
function is_assignable(expr) {
if (!exigent_mode) return true;
switch (expr[0]+"") {
case "dot":
case "sub":
case "new":
case "call":
return true;
case "name":
return expr[1] != "this";
}
};
 
//状态:赋值操作表达式
function maybe_assign(no_in) {
//left = right
var left = maybe_conditional(no_in), val = S.token.value;
if (is("operator") && HOP(ASSIGNMENT, val)) {//如果有赋值操作符 例如= += -=
if (is_assignable(left)) {
next();
//可以a=b=c=d=1
return as("assign", ASSIGNMENT[val], left, maybe_assign(no_in));
}
croak("Invalid assignment");
}
return left;
};
 
//状态:表达式
//为什么要commas
//有种情况 a ? b : c,d 这里应该认为是(a ? b : c), d
//所以在解析c,d的时候 不应该把c,d认为是一个表达式,这里commas应该是false
var expression = maybe_embed_tokens(function(commas, no_in) {
if (arguments.length == 0)
commas = true;
 
//拿一个表达式,可以是赋值语句
//var a = 1, b = 2, c;
//a=1匹配了这里的maybe_assign状态
var expr = maybe_assign(no_in);
//根据运算符优先级,看看要不要解析逗号
if (commas && is("punc", ",")) {
next();
return as("seq", expr, expression(true, no_in));
}
return expr;
});
 
//记录嵌套循环
function in_loop(cont) {
try {
++S.in_loop;
return cont();
} finally {
--S.in_loop;
}
};
 
//得到一个顶级的AST树枝,然后从statement状态开始递归解析子树
return as("toplevel", (function(a){
while (!is("eof"))
a.push(statement());
return a;
})([]));
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: