forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.js
2366 lines (2230 loc) · 71.3 KB
/
ast.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
//
// This is pretty much a straight port of ast.c from CPython 2.6.5.
//
// The previous version was easier to work with and more JS-ish, but having a
// somewhat different ast structure than cpython makes testing more difficult.
//
// This way, we can use a dump from the ast module on any arbitrary python
// code and know that we're the same up to ast level, at least.
//
var SYM = Sk.ParseTables.sym;
var TOK = Sk.Tokenizer.Tokens;
var COMP_GENEXP = 0;
var COMP_SETCOMP = 1;
/** @constructor */
function Compiling (encoding, filename, c_flags) {
this.c_encoding = encoding;
this.c_filename = filename;
this.c_flags = c_flags || 0;
}
/**
* @return {number}
*/
function NCH (n) {
goog.asserts.assert(n !== undefined);
if (n.children === null) {
return 0;
}
return n.children.length;
}
function CHILD (n, i) {
goog.asserts.assert(n !== undefined);
goog.asserts.assert(i !== undefined);
return n.children[i];
}
function REQ (n, type) {
goog.asserts.assert(n.type === type, "node wasn't expected type");
}
function strobj (s) {
goog.asserts.assert(typeof s === "string", "expecting string, got " + (typeof s));
return new Sk.builtin.str(s);
}
/** @return {number} */
function numStmts (n) {
var ch;
var i;
var cnt;
switch (n.type) {
case SYM.single_input:
if (CHILD(n, 0).type === TOK.T_NEWLINE) {
return 0;
}
else {
return numStmts(CHILD(n, 0));
}
case SYM.file_input:
cnt = 0;
for (i = 0; i < NCH(n); ++i) {
ch = CHILD(n, i);
if (ch.type === SYM.stmt) {
cnt += numStmts(ch);
}
}
return cnt;
case SYM.stmt:
return numStmts(CHILD(n, 0));
case SYM.compound_stmt:
return 1;
case SYM.simple_stmt:
return Math.floor(NCH(n) / 2); // div 2 is to remove count of ;s
case SYM.suite:
if (NCH(n) === 1) {
return numStmts(CHILD(n, 0));
}
else {
cnt = 0;
for (i = 2; i < NCH(n) - 1; ++i) {
cnt += numStmts(CHILD(n, i));
}
return cnt;
}
break;
default:
goog.asserts.fail("Non-statement found");
}
return 0;
}
function forbiddenCheck (c, n, x, lineno) {
if (x === "None") {
throw new Sk.builtin.SyntaxError("assignment to None", c.c_filename, lineno);
}
if (x === "True" || x === "False") {
throw new Sk.builtin.SyntaxError("assignment to True or False is forbidden", c.c_filename, lineno);
}
}
/**
* Set the context ctx for e, recursively traversing e.
*
* Only sets context for expr kinds that can appear in assignment context as
* per the asdl file.
*/
function setContext (c, e, ctx, n) {
var i;
var exprName;
var s;
goog.asserts.assert(ctx !== AugStore && ctx !== AugLoad);
s = null;
exprName = null;
switch (e.constructor) {
case Attribute:
case Name:
if (ctx === Store) {
forbiddenCheck(c, n, e.attr, n.lineno);
}
e.ctx = ctx;
break;
case Subscript:
e.ctx = ctx;
break;
case List:
e.ctx = ctx;
s = e.elts;
break;
case Tuple:
if (e.elts.length === 0) {
throw new Sk.builtin.SyntaxError("can't assign to ()", c.c_filename, n.lineno);
}
e.ctx = ctx;
s = e.elts;
break;
case Lambda:
exprName = "lambda";
break;
case Call:
exprName = "function call";
break;
case BoolOp:
case BinOp:
case UnaryOp:
exprName = "operator";
break;
case GeneratorExp:
exprName = "generator expression";
break;
case Yield:
exprName = "yield expression";
break;
case ListComp:
exprName = "list comprehension";
break;
case SetComp:
exprName = "set comprehension";
break;
case DictComp:
exprName = "dict comprehension";
break;
case Dict:
case Set:
case Num:
case Str:
exprName = "literal";
break;
case Compare:
exprName = "comparison";
break;
case Repr:
exprName = "repr";
break;
case IfExp:
exprName = "conditional expression";
break;
default:
goog.asserts.fail("unhandled expression in assignment");
}
if (exprName) {
throw new Sk.builtin.SyntaxError("can't " + (ctx === Store ? "assign to" : "delete") + " " + exprName, c.c_filename, n.lineno);
}
if (s) {
for (i = 0; i < s.length; ++i) {
setContext(c, s[i], ctx, n);
}
}
}
var operatorMap = {};
(function () {
operatorMap[TOK.T_VBAR] = BitOr;
operatorMap[TOK.T_CIRCUMFLEX] = BitXor;
operatorMap[TOK.T_AMPER] = BitAnd;
operatorMap[TOK.T_LEFTSHIFT] = LShift;
operatorMap[TOK.T_RIGHTSHIFT] = RShift;
operatorMap[TOK.T_PLUS] = Add;
operatorMap[TOK.T_MINUS] = Sub;
operatorMap[TOK.T_STAR] = Mult;
operatorMap[TOK.T_SLASH] = Div;
operatorMap[TOK.T_DOUBLESLASH] = FloorDiv;
operatorMap[TOK.T_PERCENT] = Mod;
}());
function getOperator (n) {
goog.asserts.assert(operatorMap[n.type] !== undefined);
return operatorMap[n.type];
}
function astForCompOp (c, n) {
/* comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'
|'is' 'not'
*/
REQ(n, SYM.comp_op);
if (NCH(n) === 1) {
n = CHILD(n, 0);
switch (n.type) {
case TOK.T_LESS:
return Lt;
case TOK.T_GREATER:
return Gt;
case TOK.T_EQEQUAL:
return Eq;
case TOK.T_LESSEQUAL:
return LtE;
case TOK.T_GREATEREQUAL:
return GtE;
case TOK.T_NOTEQUAL:
return NotEq;
case TOK.T_NAME:
if (n.value === "in") {
return In_;
}
if (n.value === "is") {
return Is;
}
}
}
else if (NCH(n) === 2) {
if (CHILD(n, 0).type === TOK.T_NAME) {
if (CHILD(n, 1).value === "in") {
return NotIn;
}
if (CHILD(n, 0).value === "is") {
return IsNot;
}
}
}
goog.asserts.fail("invalid comp_op");
}
function seqForTestlist (c, n) {
/* testlist: test (',' test)* [','] */
var i;
var seq = [];
goog.asserts.assert(n.type === SYM.testlist ||
n.type === SYM.listmaker ||
n.type === SYM.testlist_comp ||
n.type === SYM.testlist_safe ||
n.type === SYM.testlist1);
for (i = 0; i < NCH(n); i += 2) {
goog.asserts.assert(CHILD(n, i).type === SYM.test || CHILD(n, i).type === SYM.old_test);
seq[i / 2] = astForExpr(c, CHILD(n, i));
}
return seq;
}
function astForSuite (c, n) {
/* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
var j;
var num;
var i;
var end;
var ch;
var pos;
var seq;
REQ(n, SYM.suite);
seq = [];
pos = 0;
if (CHILD(n, 0).type === SYM.simple_stmt) {
n = CHILD(n, 0);
/* simple_stmt always ends with an NEWLINE and may have a trailing
* SEMI. */
end = NCH(n) - 1;
if (CHILD(n, end - 1).type === TOK.T_SEMI) {
end -= 1;
}
for (i = 0; i < end; i += 2) // by 2 to skip ;
{
seq[pos++] = astForStmt(c, CHILD(n, i));
}
}
else {
for (i = 2; i < NCH(n) - 1; ++i) {
ch = CHILD(n, i);
REQ(ch, SYM.stmt);
num = numStmts(ch);
if (num === 1) {
// small_stmt or compound_stmt w/ only 1 child
seq[pos++] = astForStmt(c, ch);
}
else {
ch = CHILD(ch, 0);
REQ(ch, SYM.simple_stmt);
for (j = 0; j < NCH(ch); j += 2) {
if (NCH(CHILD(ch, j)) === 0) {
goog.asserts.assert(j + 1 === NCH(ch));
break;
}
seq[pos++] = astForStmt(c, CHILD(ch, j));
}
}
}
}
goog.asserts.assert(pos === numStmts(n));
return seq;
}
function astForExceptClause (c, exc, body) {
/* except_clause: 'except' [test [(',' | 'as') test]] */
var e;
REQ(exc, SYM.except_clause);
REQ(body, SYM.suite);
if (NCH(exc) === 1) {
return new ExceptHandler(null, null, astForSuite(c, body), exc.lineno, exc.col_offset);
}
else if (NCH(exc) === 2) {
return new ExceptHandler(astForExpr(c, CHILD(exc, 1)), null, astForSuite(c, body), exc.lineno, exc.col_offset);
}
else if (NCH(exc) === 4) {
e = astForExpr(c, CHILD(exc, 3));
setContext(c, e, Store, CHILD(exc, 3));
return new ExceptHandler(astForExpr(c, CHILD(exc, 1)), e, astForSuite(c, body), exc.lineno, exc.col_offset);
}
goog.asserts.fail("wrong number of children for except clause");
}
function astForTryStmt (c, n) {
var exceptSt;
var i;
var handlers;
var nc = NCH(n);
var nexcept = (nc - 3) / 3;
var body, orelse = [],
finally_ = null;
REQ(n, SYM.try_stmt);
body = astForSuite(c, CHILD(n, 2));
if (CHILD(n, nc - 3).type === TOK.T_NAME) {
if (CHILD(n, nc - 3).value === "finally") {
if (nc >= 9 && CHILD(n, nc - 6).type === TOK.T_NAME) {
/* we can assume it's an "else",
because nc >= 9 for try-else-finally and
it would otherwise have a type of except_clause */
orelse = astForSuite(c, CHILD(n, nc - 4));
nexcept--;
}
finally_ = astForSuite(c, CHILD(n, nc - 1));
nexcept--;
}
else {
/* we can assume it's an "else",
otherwise it would have a type of except_clause */
orelse = astForSuite(c, CHILD(n, nc - 1));
nexcept--;
}
}
else if (CHILD(n, nc - 3).type !== SYM.except_clause) {
throw new Sk.builtin.SyntaxError("malformed 'try' statement", c.c_filename, n.lineno);
}
if (nexcept > 0) {
handlers = [];
for (i = 0; i < nexcept; ++i) {
handlers[i] = astForExceptClause(c, CHILD(n, 3 + i * 3), CHILD(n, 5 + i * 3));
}
exceptSt = new TryExcept(body, handlers, orelse, n.lineno, n.col_offset);
if (!finally_) {
return exceptSt;
}
/* if a 'finally' is present too, we nest the TryExcept within a
TryFinally to emulate try ... except ... finally */
body = [exceptSt];
}
goog.asserts.assert(finally_ !== null);
return new TryFinally(body, finally_, n.lineno, n.col_offset);
}
function astForDottedName (c, n) {
var i;
var e;
var id;
var col_offset;
var lineno;
REQ(n, SYM.dotted_name);
lineno = n.lineno;
col_offset = n.col_offset;
id = strobj(CHILD(n, 0).value);
e = new Name(id, Load, lineno, col_offset);
for (i = 2; i < NCH(n); i += 2) {
id = strobj(CHILD(n, i).value);
e = new Attribute(e, id, Load, lineno, col_offset);
}
return e;
}
function astForDecorator (c, n) {
/* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
var nameExpr;
REQ(n, SYM.decorator);
REQ(CHILD(n, 0), TOK.T_AT);
REQ(CHILD(n, NCH(n) - 1), TOK.T_NEWLINE);
nameExpr = astForDottedName(c, CHILD(n, 1));
if (NCH(n) === 3) // no args
{
return nameExpr;
}
else if (NCH(n) === 5) // call with no args
{
return new Call(nameExpr, [], [], null, null, n.lineno, n.col_offset);
}
else {
return astForCall(c, CHILD(n, 3), nameExpr);
}
}
function astForDecorators (c, n) {
var i;
var decoratorSeq;
REQ(n, SYM.decorators);
decoratorSeq = [];
for (i = 0; i < NCH(n); ++i) {
decoratorSeq[i] = astForDecorator(c, CHILD(n, i));
}
return decoratorSeq;
}
function astForDecorated (c, n) {
var thing;
var decoratorSeq;
REQ(n, SYM.decorated);
decoratorSeq = astForDecorators(c, CHILD(n, 0));
goog.asserts.assert(CHILD(n, 1).type === SYM.funcdef || CHILD(n, 1).type === SYM.classdef);
thing = null;
if (CHILD(n, 1).type === SYM.funcdef) {
thing = astForFuncdef(c, CHILD(n, 1), decoratorSeq);
}
else if (CHILD(n, 1) === SYM.classdef) {
thing = astForClassdef(c, CHILD(n, 1), decoratorSeq);
}
if (thing) {
thing.lineno = n.lineno;
thing.col_offset = n.col_offset;
}
return thing;
}
/* with_item: test ['as' expr] */
function astForWithItem (c, n, content) {
var expr_ty, context_expr, optional_vars;
REQ(n, SYM.with_item);
context_expr = astForExpr(c, CHILD(n, 0));
if (NCH(n) == 3) {
optional_vars = astForExpr(c, CHILD(n, 2));
setContext(c, optional_vars, Store, n);
}
return new With_(context_expr, optional_vars, content, n.lineno, n.col_offset);
}
function astForWithStmt (c, n) {
/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
var i;
var ret
var inner;
REQ(n, SYM.with_stmt)
/* process the with items inside-out */
i = NCH(n) -1
/* the suite of the innermost with item is the suite of the with stmt */
inner = astForSuite(c, CHILD(n,i));
while (true) {
i-=2;
ret = astForWithItem(c, CHILD(n, i), inner)
/* was this the last item? */
if (i == 1) {
break;
}
inner = [ret];
}
return ret
}
function astForExecStmt (c, n) {
var expr1, globals = null, locals = null;
var nchildren = NCH(n);
goog.asserts.assert(nchildren === 2 || nchildren === 4 || nchildren === 6);
/* exec_stmt: 'exec' expr ['in' test [',' test]] */
REQ(n, SYM.exec_stmt);
expr1 = astForExpr(c, CHILD(n, 1));
if (nchildren >= 4) {
globals = astForExpr(c, CHILD(n, 3));
}
if (nchildren === 6) {
locals = astForExpr(c, CHILD(n, 5));
}
return new Exec(expr1, globals, locals, n.lineno, n.col_offset);
}
function astForIfStmt (c, n) {
/* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
['else' ':' suite]
*/
var off;
var i;
var orelse;
var hasElse;
var nElif;
var decider;
var s;
REQ(n, SYM.if_stmt);
if (NCH(n) === 4) {
return new If_(
astForExpr(c, CHILD(n, 1)),
astForSuite(c, CHILD(n, 3)),
[], n.lineno, n.col_offset);
}
s = CHILD(n, 4).value;
decider = s.charAt(2); // elSe or elIf
if (decider === "s") {
return new If_(
astForExpr(c, CHILD(n, 1)),
astForSuite(c, CHILD(n, 3)),
astForSuite(c, CHILD(n, 6)),
n.lineno, n.col_offset);
}
else if (decider === "i") {
nElif = NCH(n) - 4;
hasElse = false;
orelse = [];
/* must reference the child nElif+1 since 'else' token is third, not
* fourth child from the end. */
if (CHILD(n, nElif + 1).type === TOK.T_NAME &&
CHILD(n, nElif + 1).value.charAt(2) === "s") {
hasElse = true;
nElif -= 3;
}
nElif /= 4;
if (hasElse) {
orelse = [
new If_(
astForExpr(c, CHILD(n, NCH(n) - 6)),
astForSuite(c, CHILD(n, NCH(n) - 4)),
astForSuite(c, CHILD(n, NCH(n) - 1)),
CHILD(n, NCH(n) - 6).lineno,
CHILD(n, NCH(n) - 6).col_offset)];
nElif--;
}
for (i = 0; i < nElif; ++i) {
off = 5 + (nElif - i - 1) * 4;
orelse = [
new If_(
astForExpr(c, CHILD(n, off)),
astForSuite(c, CHILD(n, off + 2)),
orelse,
CHILD(n, off).lineno,
CHILD(n, off).col_offset)];
}
return new If_(
astForExpr(c, CHILD(n, 1)),
astForSuite(c, CHILD(n, 3)),
orelse, n.lineno, n.col_offset);
}
goog.asserts.fail("unexpected token in 'if' statement");
}
function astForExprlist (c, n, context) {
var e;
var i;
var seq;
REQ(n, SYM.exprlist);
seq = [];
for (i = 0; i < NCH(n); i += 2) {
e = astForExpr(c, CHILD(n, i));
seq[i / 2] = e;
if (context) {
setContext(c, e, context, CHILD(n, i));
}
}
return seq;
}
function astForDelStmt (c, n) {
/* del_stmt: 'del' exprlist */
REQ(n, SYM.del_stmt);
return new Delete_(astForExprlist(c, CHILD(n, 1), Del), n.lineno, n.col_offset);
}
function astForGlobalStmt (c, n) {
/* global_stmt: 'global' NAME (',' NAME)* */
var i;
var s = [];
REQ(n, SYM.global_stmt);
for (i = 1; i < NCH(n); i += 2) {
s[(i - 1) / 2] = strobj(CHILD(n, i).value);
}
return new Global(s, n.lineno, n.col_offset);
}
function astForAssertStmt (c, n) {
/* assert_stmt: 'assert' test [',' test] */
REQ(n, SYM.assert_stmt);
if (NCH(n) === 2) {
return new Assert(astForExpr(c, CHILD(n, 1)), null, n.lineno, n.col_offset);
}
else if (NCH(n) === 4) {
return new Assert(astForExpr(c, CHILD(n, 1)), astForExpr(c, CHILD(n, 3)), n.lineno, n.col_offset);
}
goog.asserts.fail("improper number of parts to assert stmt");
}
function aliasForImportName (c, n) {
/*
import_as_name: NAME ['as' NAME]
dotted_as_name: dotted_name ['as' NAME]
dotted_name: NAME ('.' NAME)*
*/
var i;
var a;
var name;
var str;
loop: while (true) {
switch (n.type) {
case SYM.import_as_name:
str = null;
name = strobj(CHILD(n, 0).value);
if (NCH(n) === 3) {
str = CHILD(n, 2).value;
}
return new alias(name, str == null ? null : strobj(str));
case SYM.dotted_as_name:
if (NCH(n) === 1) {
n = CHILD(n, 0);
continue loop;
}
else {
a = aliasForImportName(c, CHILD(n, 0));
goog.asserts.assert(!a.asname);
a.asname = strobj(CHILD(n, 2).value);
return a;
}
break;
case SYM.dotted_name:
if (NCH(n) === 1) {
return new alias(strobj(CHILD(n, 0).value), null);
}
else {
// create a string of the form a.b.c
str = "";
for (i = 0; i < NCH(n); i += 2) {
str += CHILD(n, i).value + ".";
}
return new alias(strobj(str.substr(0, str.length - 1)), null);
}
break;
case TOK.T_STAR:
return new alias(strobj("*"), null);
default:
throw new Sk.builtin.SyntaxError("unexpected import name", c.c_filename, n.lineno);
}
break;
}
}
function astForImportStmt (c, n) {
/*
import_stmt: import_name | import_from
import_name: 'import' dotted_as_names
import_from: 'from' ('.'* dotted_name | '.') 'import'
('*' | '(' import_as_names ')' | import_as_names)
*/
var modname;
var idx;
var nchildren;
var ndots;
var mod;
var i;
var aliases;
var col_offset;
var lineno;
REQ(n, SYM.import_stmt);
lineno = n.lineno;
col_offset = n.col_offset;
n = CHILD(n, 0);
if (n.type === SYM.import_name) {
n = CHILD(n, 1);
REQ(n, SYM.dotted_as_names);
aliases = [];
for (i = 0; i < NCH(n); i += 2) {
aliases[i / 2] = aliasForImportName(c, CHILD(n, i));
}
return new Import_(aliases, lineno, col_offset);
}
else if (n.type === SYM.import_from) {
mod = null;
ndots = 0;
for (idx = 1; idx < NCH(n); ++idx) {
if (CHILD(n, idx).type === SYM.dotted_name) {
mod = aliasForImportName(c, CHILD(n, idx));
idx++;
break;
}
else if (CHILD(n, idx).type !== TOK.T_DOT) {
break;
}
ndots++;
}
++idx; // skip the import keyword
switch (CHILD(n, idx).type) {
case TOK.T_STAR:
// from ... import
n = CHILD(n, idx);
nchildren = 1;
break;
case TOK.T_LPAR:
// from ... import (x, y, z)
n = CHILD(n, idx + 1);
nchildren = NCH(n);
break;
case SYM.import_as_names:
// from ... import x, y, z
n = CHILD(n, idx);
nchildren = NCH(n);
if (nchildren % 2 === 0) {
throw new Sk.builtin.SyntaxError("trailing comma not allowed without surrounding parentheses", c.c_filename, n.lineno);
}
break;
default:
throw new Sk.builtin.SyntaxError("Unexpected node-type in from-import", c.c_filename, n.lineno);
}
aliases = [];
if (n.type === TOK.T_STAR) {
aliases[0] = aliasForImportName(c, n);
}
else {
for (i = 0; i < NCH(n); i += 2) {
aliases[i / 2] = aliasForImportName(c, CHILD(n, i));
}
}
modname = mod ? mod.name.v : "";
return new ImportFrom(strobj(modname), aliases, ndots, lineno, col_offset);
}
throw new Sk.builtin.SyntaxError("unknown import statement", c.c_filename, n.lineno);
}
function astForTestlistComp(c, n) {
/* testlist_comp: test ( comp_for | (',' test)* [','] ) */
/* argument: test [comp_for] */
goog.asserts.assert(n.type === SYM.testlist_comp || n.type === SYM.argument);
if (NCH(n) > 1 && CHILD(n, 1).type === SYM.comp_for) {
return astForGenExpr(c, n);
}
return astForTestlist(c, n);
}
function astForListcomp (c, n) {
/* listmaker: test ( list_for | (',' test)* [','] )
list_for: 'for' exprlist 'in' testlist_safe [list_iter]
list_iter: list_for | list_if
list_if: 'if' test [list_iter]
testlist_safe: test [(',' test)+ [',']]
*/
function countListFors (c, n) {
var nfors = 0;
var ch = CHILD(n, 1);
count_list_for: while (true) {
nfors++;
REQ(ch, SYM.list_for);
if (NCH(ch) === 5) {
ch = CHILD(ch, 4);
}
else {
return nfors;
}
count_list_iter: while (true) {
REQ(ch, SYM.list_iter);
ch = CHILD(ch, 0);
if (ch.type === SYM.list_for) {
continue count_list_for;
}
else if (ch.type === SYM.list_if) {
if (NCH(ch) === 3) {
ch = CHILD(ch, 2);
continue count_list_iter;
}
else {
return nfors;
}
}
break;
}
break;
}
}
function countListIfs (c, n) {
var nifs = 0;
while (true) {
REQ(n, SYM.list_iter);
if (CHILD(n, 0).type === SYM.list_for) {
return nifs;
}
n = CHILD(n, 0);
REQ(n, SYM.list_if);
nifs++;
if (NCH(n) == 2) {
return nifs;
}
n = CHILD(n, 2);
}
}
var j;
var ifs;
var nifs;
var lc;
var expression;
var t;
var forch;
var i;
var ch;
var listcomps;
var nfors;
var elt;
REQ(n, SYM.listmaker);
goog.asserts.assert(NCH(n) > 1);
elt = astForExpr(c, CHILD(n, 0));
nfors = countListFors(c, n);
listcomps = [];
ch = CHILD(n, 1);
for (i = 0; i < nfors; ++i) {
REQ(ch, SYM.list_for);
forch = CHILD(ch, 1);
t = astForExprlist(c, forch, Store);
expression = astForTestlist(c, CHILD(ch, 3));
if (NCH(forch) === 1) {
lc = new comprehension(t[0], expression, []);
}
else {
lc = new comprehension(new Tuple(t, Store, ch.lineno, ch.col_offset), expression, []);
}
if (NCH(ch) === 5) {
ch = CHILD(ch, 4);
nifs = countListIfs(c, ch);
ifs = [];
for (j = 0; j < nifs; ++j) {
REQ(ch, SYM.list_iter);
ch = CHILD(ch, 0);
REQ(ch, SYM.list_if);
ifs[j] = astForExpr(c, CHILD(ch, 1));
if (NCH(ch) === 3) {
ch = CHILD(ch, 2);
}
}
if (ch.type === SYM.list_iter) {
ch = CHILD(ch, 0);
}
lc.ifs = ifs;
}
listcomps[i] = lc;
}
return new ListComp(elt, listcomps, n.lineno, n.col_offset);
}
function astForFactor (c, n) {
/* some random peephole thing that cpy does */
var expression;
var pnum;
var patom;
var ppower;
var pfactor;
if (CHILD(n, 0).type === TOK.T_MINUS && NCH(n) === 2) {
pfactor = CHILD(n, 1);
if (pfactor.type === SYM.factor && NCH(pfactor) === 1) {
ppower = CHILD(pfactor, 0);
if (ppower.type === SYM.power && NCH(ppower) === 1) {
patom = CHILD(ppower, 0);
if (patom.type === SYM.atom) {
pnum = CHILD(patom, 0);
if (pnum.type === TOK.T_NUMBER) {
pnum.value = "-" + pnum.value;
return astForAtom(c, patom);
}
}
}
}
}
expression = astForExpr(c, CHILD(n, 1));
switch (CHILD(n, 0).type) {
case TOK.T_PLUS:
return new UnaryOp(UAdd, expression, n.lineno, n.col_offset);
case TOK.T_MINUS:
return new UnaryOp(USub, expression, n.lineno, n.col_offset);
case TOK.T_TILDE:
return new UnaryOp(Invert, expression, n.lineno, n.col_offset);
}
goog.asserts.fail("unhandled factor");
}
function astForForStmt (c, n) {
/* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
var target;
var _target;
var nodeTarget;
var seq = [];
REQ(n, SYM.for_stmt);
if (NCH(n) === 9) {
seq = astForSuite(c, CHILD(n, 8));
}
nodeTarget = CHILD(n, 1);
_target = astForExprlist(c, nodeTarget, Store);
if (NCH(nodeTarget) === 1) {
target = _target[0];
}
else {
target = new Tuple(_target, Store, n.lineno, n.col_offset);
}
return new For_(target,
astForTestlist(c, CHILD(n, 3)),
astForSuite(c, CHILD(n, 5)),
seq, n.lineno, n.col_offset);
}
function astForCall (c, n, func) {
/*
arglist: (argument ',')* (argument [',']| '*' test [',' '**' test]
| '**' test)
argument: test [comp_for] | test '=' test # Really [keyword '='] test
*/
var tmp;
var k;
var key;
var e;
var kwarg;
var vararg;
var keywords;
var args;
var ch;
var i;
var ngens;
var nkeywords;
var nargs;
REQ(n, SYM.arglist);
nargs = 0;
nkeywords = 0;
ngens = 0;
for (i = 0; i < NCH(n); i++) {
ch = CHILD(n, i);
if (ch.type === SYM.argument) {
if (NCH(ch) === 1) {
nargs++;
}
else if (CHILD(ch, 1).type === SYM.comp_for) {
ngens++;
}
else {
nkeywords++;
}
}
}