-
Notifications
You must be signed in to change notification settings - Fork 4
/
pscript.leo
6851 lines (5810 loc) · 232 KB
/
pscript.leo
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
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1" >
<leo_header file_format="2"/>
<globals/>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="ekr.20181108111559.1"><vh>pscript study outline</vh></v>
<v t="ekr.20181108111640.313"><vh>@@clean __init__.py</vh>
<v t="ekr.20181108111640.314"><vh><< docstring >></vh></v>
</v>
<v t="ekr.20181108111640.2"><vh>@@clean base.py</vh>
<v t="ekr.20181108111640.4"><vh>class BasicParser(Parser2)</vh></v>
<v t="ekr.20181108111640.5"><vh>class Parser(Parser3)</vh></v>
<v t="ekr.20181108111640.6"><vh>this_is_js</vh></v>
</v>
<v t="ekr.20181108111640.7"><vh>@@clean commonast.py</vh>
<v t="ekr.20181108111640.9"><vh>parse</vh></v>
<v t="ekr.20181108111640.10"><vh>class Node(object)</vh>
<v t="ekr.20181108111640.11"><vh>class OPS</vh></v>
<v t="ekr.20181108111640.12"><vh>class COMP</vh></v>
<v t="ekr.20181108111640.13"><vh>__init__</vh></v>
<v t="ekr.20181108111640.14"><vh>tojson</vh></v>
<v t="ekr.20181108111640.15"><vh>fromjson</vh></v>
<v t="ekr.20181108111640.16"><vh>_fromdict</vh></v>
<v t="ekr.20181108111640.17"><vh>_todict</vh></v>
<v t="ekr.20181108111640.18"><vh>__eq__</vh></v>
<v t="ekr.20181108111640.19"><vh>__repr__</vh></v>
<v t="ekr.20181108111640.20"><vh>__str__</vh></v>
</v>
<v t="ekr.20181108111640.21"><vh>class Comment(Node)</vh></v>
<v t="ekr.20181108111640.22"><vh>class Module(Node)</vh></v>
<v t="ekr.20181108111640.23"><vh>class Num(Node)</vh></v>
<v t="ekr.20181108111640.24"><vh>class Str(Node)</vh></v>
<v t="ekr.20181108111640.25"><vh>class FormattedValue(Node)</vh></v>
<v t="ekr.20181108111640.26"><vh>class JoinedStr(Node)</vh></v>
<v t="ekr.20181108111640.27"><vh>class Bytes(Node)</vh></v>
<v t="ekr.20181108111640.28"><vh>class List(Node)</vh></v>
<v t="ekr.20181108111640.29"><vh>class Tuple(Node)</vh></v>
<v t="ekr.20181108111640.30"><vh>class Set(Node)</vh></v>
<v t="ekr.20181108111640.31"><vh>class Dict(Node)</vh></v>
<v t="ekr.20181108111640.32"><vh>class Ellipsis(Node)</vh></v>
<v t="ekr.20181108111640.33"><vh>class NameConstant(Node)</vh></v>
<v t="ekr.20181108111640.34"><vh>class Name(Node)</vh></v>
<v t="ekr.20181108111640.35"><vh>class Starred(Node)</vh></v>
<v t="ekr.20181108111640.36"><vh>class Attribute(Node)</vh></v>
<v t="ekr.20181108111640.37"><vh>class Subscript(Node)</vh></v>
<v t="ekr.20181108111640.38"><vh>class Index(Node)</vh></v>
<v t="ekr.20181108111640.39"><vh>class Slice(Node)</vh></v>
<v t="ekr.20181108111640.40"><vh>class ExtSlice(Node)</vh></v>
<v t="ekr.20181108111640.41"><vh>class Expr(Node)</vh></v>
<v t="ekr.20181108111640.42"><vh>class UnaryOp(Node)</vh></v>
<v t="ekr.20181108111640.43"><vh>class BinOp(Node)</vh></v>
<v t="ekr.20181108111640.44"><vh>class BoolOp(Node)</vh></v>
<v t="ekr.20181108111640.45"><vh>class Compare(Node)</vh></v>
<v t="ekr.20181108111640.46"><vh>class Call(Node)</vh></v>
<v t="ekr.20181108111640.47"><vh>class Keyword(Node)</vh></v>
<v t="ekr.20181108111640.48"><vh>class IfExp(Node)</vh></v>
<v t="ekr.20181108111640.49"><vh>class ListComp(Node)</vh></v>
<v t="ekr.20181108111640.50"><vh>class SetComp(Node)</vh></v>
<v t="ekr.20181108111640.51"><vh>class GeneratorExp(Node)</vh></v>
<v t="ekr.20181108111640.52"><vh>class DictComp(Node)</vh></v>
<v t="ekr.20181108111640.53"><vh>class Comprehension(Node)</vh></v>
<v t="ekr.20181108111640.54"><vh>class Assign(Node)</vh></v>
<v t="ekr.20181108111640.55"><vh>class AugAssign(Node)</vh></v>
<v t="ekr.20181108111640.56"><vh>class Raise(Node)</vh></v>
<v t="ekr.20181108111640.57"><vh>class Assert(Node)</vh></v>
<v t="ekr.20181108111640.58"><vh>class Delete(Node)</vh></v>
<v t="ekr.20181108111640.59"><vh>class Pass(Node)</vh></v>
<v t="ekr.20181108111640.60"><vh>class Import(Node)</vh></v>
<v t="ekr.20181108111640.61"><vh>class If(Node)</vh></v>
<v t="ekr.20181108111640.62"><vh>class For(Node)</vh></v>
<v t="ekr.20181108111640.63"><vh>class While(Node)</vh></v>
<v t="ekr.20181108111640.64"><vh>class Break(Node)</vh></v>
<v t="ekr.20181108111640.65"><vh>class Continue(Node)</vh></v>
<v t="ekr.20181108111640.66"><vh>class Try(Node)</vh></v>
<v t="ekr.20181108111640.67"><vh>class ExceptHandler(Node)</vh></v>
<v t="ekr.20181108111640.68"><vh>class With(Node)</vh></v>
<v t="ekr.20181108111640.69"><vh>class WithItem(Node)</vh></v>
<v t="ekr.20181108111640.70"><vh>class FunctionDef(Node)</vh></v>
<v t="ekr.20181108111640.71"><vh>class Lambda(Node)</vh></v>
<v t="ekr.20181108111640.72"><vh>class AsyncFunctionDef(Node)</vh></v>
<v t="ekr.20181108111640.73"><vh>class Arg(Node)</vh></v>
<v t="ekr.20181108111640.74"><vh>class Return(Node)</vh></v>
<v t="ekr.20181108111640.75"><vh>class Yield(Node)</vh></v>
<v t="ekr.20181108111640.76"><vh>class YieldFrom(Node)</vh></v>
<v t="ekr.20181108111640.77"><vh>class Await(Node)</vh></v>
<v t="ekr.20181108111640.78"><vh>class Global(Node)</vh></v>
<v t="ekr.20181108111640.79"><vh>class Nonlocal(Node)</vh></v>
<v t="ekr.20181108111640.80"><vh>class ClassDef(Node)</vh></v>
<v t="ekr.20181108111640.81"><vh>class NativeAstConverter</vh>
<v t="ekr.20181108111640.82"><vh>NativeAstConverter.__init__</vh></v>
<v t="ekr.20181108111640.83"><vh>NativeAstConverter._add_comments</vh></v>
<v t="ekr.20181108111640.84"><vh>NativeAstConverter.convert</vh></v>
<v t="ekr.20181108111640.85"><vh>NativeAstConverter._convert</vh></v>
<v t="ekr.20181108111640.86"><vh>NativeAstConverter._convert_Module</vh></v>
<v t="ekr.20181108111640.87"><vh>NativeAstConverter._convert_Num</vh></v>
<v t="ekr.20181108111640.88"><vh>NativeAstConverter._convert_Str</vh></v>
<v t="ekr.20181108111640.89"><vh>NativeAstConverter._convert_JoinedStr</vh></v>
<v t="ekr.20181108111640.90"><vh>NativeAstConverter._convert_FormattedValue</vh></v>
<v t="ekr.20181108111640.91"><vh>NativeAstConverter._convert_Bytes</vh></v>
<v t="ekr.20181108111640.92"><vh>NativeAstConverter._convert_List</vh></v>
<v t="ekr.20181108111640.93"><vh>NativeAstConverter._convert_Tuple</vh></v>
<v t="ekr.20181108111640.94"><vh>NativeAstConverter._convert_Set</vh></v>
<v t="ekr.20181108111640.95"><vh>NativeAstConverter._convert_Dict</vh></v>
<v t="ekr.20181108111640.96"><vh>NativeAstConverter._convert_Ellipsis</vh></v>
<v t="ekr.20181108111640.97"><vh>NativeAstConverter._convert_NameConstant</vh></v>
<v t="ekr.20181108111640.98"><vh>NativeAstConverter._convert_Name</vh></v>
<v t="ekr.20181108111640.99"><vh>NativeAstConverter._convert_Starred</vh></v>
<v t="ekr.20181108111640.100"><vh>NativeAstConverter._convert_Attribute</vh></v>
<v t="ekr.20181108111640.101"><vh>NativeAstConverter._convert_Subscript</vh></v>
<v t="ekr.20181108111640.102"><vh>NativeAstConverter._convert_Index</vh></v>
<v t="ekr.20181108111640.103"><vh>NativeAstConverter._convert_Slice</vh></v>
<v t="ekr.20181108111640.104"><vh>NativeAstConverter._convert_ExtSlice</vh></v>
<v t="ekr.20181108111640.105"><vh>NativeAstConverter._convert_Expr</vh></v>
<v t="ekr.20181108111640.106"><vh>NativeAstConverter._convert_UnaryOp</vh></v>
<v t="ekr.20181108111640.107"><vh>NativeAstConverter._convert_BinOp</vh></v>
<v t="ekr.20181108111640.108"><vh>NativeAstConverter._convert_BoolOp</vh></v>
<v t="ekr.20181108111640.109"><vh>NativeAstConverter._convert_Compare</vh></v>
<v t="ekr.20181108111640.110"><vh>NativeAstConverter._convert_Call</vh></v>
<v t="ekr.20181108111640.111"><vh>NativeAstConverter._convert_keyword</vh></v>
<v t="ekr.20181108111640.112"><vh>NativeAstConverter._convert_IfExp</vh></v>
<v t="ekr.20181108111640.113"><vh>NativeAstConverter._convert_ListComp</vh></v>
<v t="ekr.20181108111640.114"><vh>NativeAstConverter._convert_SetComp</vh></v>
<v t="ekr.20181108111640.115"><vh>NativeAstConverter._convert_GeneratorExp</vh></v>
<v t="ekr.20181108111640.116"><vh>NativeAstConverter._convert_DictComp</vh></v>
<v t="ekr.20181108111640.117"><vh>NativeAstConverter._convert_comprehension</vh></v>
<v t="ekr.20181108111640.118"><vh>NativeAstConverter._convert_Assign</vh></v>
<v t="ekr.20181108111640.119"><vh>NativeAstConverter._convert_AugAssign</vh></v>
<v t="ekr.20181108111640.120"><vh>NativeAstConverter._convert_Print</vh></v>
<v t="ekr.20181108111640.121"><vh>NativeAstConverter._convert_Exec</vh></v>
<v t="ekr.20181108111640.122"><vh>NativeAstConverter._convert_Repr</vh></v>
<v t="ekr.20181108111640.123"><vh>NativeAstConverter._convert_Raise</vh></v>
<v t="ekr.20181108111640.124"><vh>NativeAstConverter._convert_Assert</vh></v>
<v t="ekr.20181108111640.125"><vh>NativeAstConverter._convert_Delete</vh></v>
<v t="ekr.20181108111640.126"><vh>NativeAstConverter._convert_Pass</vh></v>
<v t="ekr.20181108111640.127"><vh>NativeAstConverter._convert_Import</vh></v>
<v t="ekr.20181108111640.128"><vh>NativeAstConverter._convert_ImportFrom</vh></v>
<v t="ekr.20181108111640.129"><vh>NativeAstConverter._convert_If</vh></v>
<v t="ekr.20181108111640.130"><vh>NativeAstConverter._convert_For</vh></v>
<v t="ekr.20181108111640.131"><vh>NativeAstConverter._convert_While</vh></v>
<v t="ekr.20181108111640.132"><vh>NativeAstConverter._convert_Break</vh></v>
<v t="ekr.20181108111640.133"><vh>NativeAstConverter._convert_Continue</vh></v>
<v t="ekr.20181108111640.134"><vh>NativeAstConverter._convert_Try</vh></v>
<v t="ekr.20181108111640.135"><vh>NativeAstConverter._convert_TryFinally</vh></v>
<v t="ekr.20181108111640.136"><vh>NativeAstConverter._convert_TryExcept</vh></v>
<v t="ekr.20181108111640.137"><vh>NativeAstConverter._convert_ExceptHandler</vh></v>
<v t="ekr.20181108111640.138"><vh>NativeAstConverter._convert_With</vh></v>
<v t="ekr.20181108111640.139"><vh>NativeAstConverter._convert_withitem</vh></v>
<v t="ekr.20181108111640.140"><vh>NativeAstConverter._convert_functiondefs</vh></v>
<v t="ekr.20181108111640.141"><vh>NativeAstConverter._convert_FunctionDef</vh></v>
<v t="ekr.20181108111640.142"><vh>NativeAstConverter._convert_Lambda</vh></v>
<v t="ekr.20181108111640.143"><vh>NativeAstConverter._convert_AsyncFunctionDef</vh></v>
<v t="ekr.20181108111640.144"><vh>NativeAstConverter._convert_arg</vh></v>
<v t="ekr.20181108111640.145"><vh>NativeAstConverter._convert_Return</vh></v>
<v t="ekr.20181108111640.146"><vh>NativeAstConverter._convert_Yield</vh></v>
<v t="ekr.20181108111640.147"><vh>NativeAstConverter._convert_YieldFrom</vh></v>
<v t="ekr.20181108111640.148"><vh>NativeAstConverter._convert_Await</vh></v>
<v t="ekr.20181108111640.149"><vh>NativeAstConverter._convert_Global</vh></v>
<v t="ekr.20181108111640.150"><vh>NativeAstConverter._convert_Nonlocal</vh></v>
<v t="ekr.20181108111640.151"><vh>NativeAstConverter._convert_ClassDef</vh></v>
</v>
</v>
<v t="ekr.20181108111640.152"><vh>@@clean functions.py</vh>
<v t="ekr.20181108111640.154"><vh>class JSString(str)</vh></v>
<v t="ekr.20181108111640.155"><vh>py2js</vh>
<v t="ekr.20181108210630.1"><vh><< py2js docstring >></vh></v>
</v>
<v t="ekr.20181108111640.156"><vh>js_rename</vh></v>
<v t="ekr.20181108111640.157"><vh>get_node_exe</vh></v>
<v t="ekr.20181108111640.158"><vh>evaljs</vh></v>
<v t="ekr.20181108111640.159"><vh>evalpy</vh></v>
<v t="ekr.20181108111640.160"><vh>script2js</vh></v>
</v>
<v t="ekr.20181108111640.161"><vh>@@clean modules.py</vh>
<v t="ekr.20181108111640.162"><vh><< code snippets >></vh></v>
<v t="ekr.20181108111640.163"><vh>isidentifier</vh></v>
<v t="ekr.20181108111640.164"><vh>create_js_module</vh></v>
</v>
<v t="ekr.20181108111640.165"><vh>@@clean parser0.py</vh>
<v t="ekr.20181108111640.167"><vh>class JSError(Exception)</vh></v>
<v t="ekr.20181108111640.168"><vh>unify</vh></v>
<v t="ekr.20181108111640.169"><vh>class NameSpace(dict)</vh>
<v t="ekr.20181108111640.170"><vh>NameSpace.set_nonlocal</vh></v>
<v t="ekr.20181108111640.171"><vh>NameSpace.set_global</vh></v>
<v t="ekr.20181108111640.172"><vh>NameSpace.use</vh></v>
<v t="ekr.20181108111640.173"><vh>NameSpace.add</vh></v>
<v t="ekr.20181108111640.174"><vh>NameSpace.discard</vh></v>
<v t="ekr.20181108111640.175"><vh>NameSpace.leak_stack</vh></v>
<v t="ekr.20181108111640.176"><vh>NameSpace.is_known</vh></v>
<v t="ekr.20181108111640.177"><vh>NameSpace.get_defined</vh></v>
<v t="ekr.20181108111640.178"><vh>NameSpace.get_globals</vh></v>
<v t="ekr.20181108111640.179"><vh>NameSpace.get_undefined</vh></v>
</v>
<v t="ekr.20181108111640.180"><vh>class Parser0</vh>
<v t="ekr.20181108111640.181"><vh>Parser0.__init__</vh></v>
<v t="ekr.20181108111640.182"><vh>Parser0.dump</vh></v>
<v t="ekr.20181108111640.183"><vh>Parser0._better_js_error</vh></v>
<v t="ekr.20181108111640.184"><vh>Parser0.push_stack</vh></v>
<v t="ekr.20181108111640.185"><vh>Parser0.pop_stack</vh></v>
<v t="ekr.20181108111640.186"><vh>Parser0.get_declarations</vh></v>
<v t="ekr.20181108111640.187"><vh>Parser0.with_prefix</vh></v>
<v t="ekr.20181108111640.188"><vh>Parser0.vars</vh></v>
<v t="ekr.20181108111640.189"><vh>Parser0.lf</vh></v>
<v t="ekr.20181108111640.190"><vh>Parser0.dummy</vh></v>
<v t="ekr.20181108111640.191"><vh>Parser0._handle_std_deps</vh></v>
<v t="ekr.20181108111640.192"><vh>Parser0.use_std_function</vh></v>
<v t="ekr.20181108111640.193"><vh>Parser0.use_std_method</vh></v>
<v t="ekr.20181108111640.194"><vh>Parser0.pop_docstring</vh></v>
<v t="ekr.20181108111640.195"><vh>Parser0.parse</vh></v>
</v>
</v>
<v t="ekr.20181108111640.196"><vh>@@clean parser1.py</vh>
<v t="ekr.20181108111640.197"><vh><< parser1 docstring >></vh></v>
<v t="ekr.20181108211821.1"><vh><< parser1 data >></vh></v>
<v t="ekr.20181108111640.198"><vh>class Parser1(Parser0)</vh>
<v t="ekr.20181108111640.199"><vh>Parser1.parse_Num</vh></v>
<v t="ekr.20181108111640.200"><vh>Parser1.parse_Str</vh></v>
<v t="ekr.20181108111640.201"><vh>Parser1.parse_JoinedStr</vh></v>
<v t="ekr.20181108111640.202"><vh>Parser1.parse_FormattedValue</vh></v>
<v t="ekr.20181108111640.203"><vh>Parser1._parse_FormattedValue_fmt</vh></v>
<v t="ekr.20181108111640.204"><vh>Parser1.parse_Bytes</vh></v>
<v t="ekr.20181108111640.205"><vh>Parser1.parse_NameConstant</vh></v>
<v t="ekr.20181108111640.206"><vh>Parser1.parse_List</vh></v>
<v t="ekr.20181108111640.207"><vh>Parser1.parse_Tuple</vh></v>
<v t="ekr.20181108111640.208"><vh>Parser1.parse_Dict</vh></v>
<v t="ekr.20181108111640.209"><vh>Parser1.parse_Set</vh></v>
<v t="ekr.20181108111640.210"><vh>Parser1.push_scope_prefix</vh></v>
<v t="ekr.20181108111640.211"><vh>Parser1.pop_scope_prefix</vh></v>
<v t="ekr.20181108111640.212"><vh>Parser1.parse_Name</vh></v>
<v t="ekr.20181108111640.213"><vh>Parser1.parse_Starred</vh></v>
<v t="ekr.20181108111640.214"><vh>Parser1.parse_Expr</vh></v>
<v t="ekr.20181108111640.215"><vh>Parser1.parse_UnaryOp</vh></v>
<v t="ekr.20181108111640.216"><vh>Parser1.parse_BinOp</vh></v>
<v t="ekr.20181108111640.217"><vh>Parser1._format_string</vh></v>
<v t="ekr.20181108111640.218"><vh>Parser1._wrap_truthy</vh></v>
<v t="ekr.20181108111640.219"><vh>Parser1.parse_BoolOp</vh></v>
<v t="ekr.20181108111640.220"><vh>Parser1.parse_Compare</vh></v>
<v t="ekr.20181108111640.221"><vh>Parser1.parse_Call</vh></v>
<v t="ekr.20181108111640.222"><vh>Parser1._get_args</vh></v>
<v t="ekr.20181108111640.223"><vh>Parser1._get_positional_args</vh></v>
<v t="ekr.20181108111640.224"><vh>Parser1._get_keyword_args</vh></v>
<v t="ekr.20181108111640.225"><vh>Parser1.parse_Attribute</vh></v>
<v t="ekr.20181108111640.226"><vh>Parser1.parse_Assign</vh></v>
<v t="ekr.20181108111640.227"><vh>Parser1.parse_AugAssign</vh></v>
<v t="ekr.20181108111640.228"><vh>Parser1.parse_Delete</vh></v>
<v t="ekr.20181108111640.229"><vh>Parser1.parse_Pass</vh></v>
<v t="ekr.20181108111640.230"><vh>Parser1.parse_Subscript</vh></v>
<v t="ekr.20181108111640.231"><vh>Parser1.parse_Index</vh></v>
<v t="ekr.20181108111640.232"><vh>Parser1.parse_Slice</vh></v>
<v t="ekr.20181108111640.233"><vh>Parser1.parse_ExtSlice</vh></v>
<v t="ekr.20181108111640.234"><vh>Parser1.parse_Import</vh></v>
<v t="ekr.20181108111640.235"><vh>Parser1.parse_Module</vh></v>
</v>
</v>
<v t="ekr.20181108111640.236"><vh>@@clean parser2.py</vh>
<v t="ekr.20181108111640.237"><vh><< parser2 docstring >></vh></v>
<v t="ekr.20181108212134.1"><vh><< parser2 data >></vh></v>
<v t="ekr.20181108111640.238"><vh>class Parser2(Parser1)</vh>
<v t="ekr.20181108111640.239"><vh>Parser2.parse_Raise</vh></v>
<v t="ekr.20181108111640.240"><vh>Parser2.parse_Assert</vh></v>
<v t="ekr.20181108111640.241"><vh>Parser2.parse_Try</vh></v>
<v t="ekr.20181108111640.242"><vh>Parser2.parse_ExceptHandler</vh></v>
<v t="ekr.20181108111640.243"><vh>Parser2.parse_With</vh></v>
<v t="ekr.20181108111640.244"><vh>Parser2.parse_IfExp</vh></v>
<v t="ekr.20181108111640.245"><vh>Parser2.parse_If</vh></v>
<v t="ekr.20181108111640.246"><vh>Parser2.parse_For</vh></v>
<v t="ekr.20181108111640.247"><vh>Parser2._make_iterable</vh></v>
<v t="ekr.20181108111640.248"><vh>Parser2.parse_While</vh></v>
<v t="ekr.20181108111640.249"><vh>Parser2.parse_Break</vh></v>
<v t="ekr.20181108111640.250"><vh>Parser2.parse_Continue</vh></v>
<v t="ekr.20181108111640.251"><vh>Parser2.parse_ListComp_funtionless</vh></v>
<v t="ekr.20181108111640.252"><vh>Parser2.parse_ListComp</vh></v>
<v t="ekr.20181108111640.253"><vh>Parser2._iterator_assign</vh></v>
<v t="ekr.20181108111640.254"><vh>Parser2.parse_FunctionDef</vh></v>
<v t="ekr.20181108111640.255"><vh>Parser2.parse_Lambda</vh></v>
<v t="ekr.20181108111640.256"><vh>Parser2.parse_AsyncFunctionDef</vh></v>
<v t="ekr.20181108111640.257"><vh>Parser2.parse_Return</vh></v>
<v t="ekr.20181108111640.258"><vh>Parser2.parse_ClassDef</vh></v>
<v t="ekr.20181108111640.259"><vh>Parser2.function_super</vh></v>
<v t="ekr.20181108111640.260"><vh>Parser2.parse_Await</vh></v>
<v t="ekr.20181108111640.261"><vh>Parser2.parse_Global</vh></v>
<v t="ekr.20181108111640.262"><vh>Parser2.parse_Nonlocal</vh></v>
</v>
<v t="ekr.20181108111640.263"><vh>get_class_definition</vh></v>
</v>
<v t="ekr.20181108111640.264"><vh>@@clean parser3.py</vh>
<v t="ekr.20181108111640.265"><vh><< parser3 docstring >></vh></v>
<v t="ekr.20181108111640.266"><vh>class Parser3(Parser2)</vh>
<v t="ekr.20181108111640.267"><vh>Parser3.function_this_is_js</vh></v>
<v t="ekr.20181108111640.268"><vh>Parser3.function_RawJS</vh></v>
<v t="ekr.20181108111640.269"><vh>Parser3.function_isinstance</vh></v>
<v t="ekr.20181108111640.270"><vh>Parser3.function_issubclass</vh></v>
<v t="ekr.20181108111640.271"><vh>Parser3.function_print</vh></v>
<v t="ekr.20181108111640.272"><vh>Parser3.function_len</vh></v>
<v t="ekr.20181108111640.273"><vh>Parser3.function_max</vh></v>
<v t="ekr.20181108111640.274"><vh>Parser3.function_min</vh></v>
<v t="ekr.20181108111640.275"><vh>Parser3.function_callable</vh></v>
<v t="ekr.20181108111640.276"><vh>Parser3.function_chr</vh></v>
<v t="ekr.20181108111640.277"><vh>Parser3.function_ord</vh></v>
<v t="ekr.20181108111640.278"><vh>Parser3.function_dict</vh></v>
<v t="ekr.20181108111640.279"><vh>Parser3.function_list</vh></v>
<v t="ekr.20181108111640.280"><vh>Parser3.function_tuple</vh></v>
<v t="ekr.20181108111640.281"><vh>Parser3.function_range</vh></v>
<v t="ekr.20181108111640.282"><vh>Parser3.function_sorted</vh></v>
<v t="ekr.20181108111640.283"><vh>Parser3.method_sort</vh></v>
<v t="ekr.20181108111640.284"><vh>Parser3.method_format</vh></v>
</v>
<v t="ekr.20181108111640.285"><vh>make_function</vh></v>
<v t="ekr.20181108111640.286"><vh>make_method</vh></v>
</v>
<v t="ekr.20181108111640.287"><vh>@@clean stdlib.py</vh>
<v t="ekr.20181108111640.289"><vh>get_std_info</vh></v>
<v t="ekr.20181108111640.290"><vh>_update_deps</vh></v>
<v t="ekr.20181108111640.291"><vh>get_partial_std_lib</vh></v>
<v t="ekr.20181108111640.292"><vh>get_full_std_lib</vh></v>
<v t="ekr.20181108111640.293"><vh>get_all_std_names</vh></v>
<v t="ekr.20181108212755.1"><vh><< define FUNCTIONS >></vh></v>
<v t="ekr.20181108212823.1"><vh><< define METHODS >></vh></v>
</v>
<v t="ekr.20181108111640.294"><vh>@@clean stubs.py</vh>
<v t="ekr.20181108111640.296"><vh>class RawJS</vh>
<v t="ekr.20181108111640.297"><vh>RawJS.__init__</vh></v>
<v t="ekr.20181108111640.298"><vh>RawJS.__repr__</vh></v>
<v t="ekr.20181108111640.299"><vh>RawJS.__str__</vh></v>
<v t="ekr.20181108111640.300"><vh>RawJS._str2lines</vh></v>
<v t="ekr.20181108111640.301"><vh>RawJS.get_defined_name</vh></v>
<v t="ekr.20181108111640.302"><vh>RawJS.get_code</vh></v>
</v>
<v t="ekr.20181108111640.303"><vh>class JSConstant</vh>
<v t="ekr.20181108111640.304"><vh>JSConstant.__init__</vh></v>
<v t="ekr.20181108111640.305"><vh>JSConstant.__repr__</vh></v>
</v>
<v t="ekr.20181108111640.306"><vh>class Stubs</vh></v>
</v>
<v t="ekr.20181108111640.308"><vh>@@clean testing.py</vh>
<v t="ekr.20181108111640.309"><vh><< testing data >></vh></v>
<v t="ekr.20181108111640.310"><vh>run_tests_if_main</vh></v>
<v t="ekr.20181108111640.311"><vh>_enable_faulthandler</vh></v>
<v t="ekr.20181108111640.312"><vh>_clear_our_modules</vh></v>
</v>
<v t="ekr.20181108212755.1"></v>
<v t="ekr.20181108212823.1"></v>
<v t="ekr.20181108111640.162"></v>
</vnodes>
<tnodes>
<t tx="ekr.20181108111559.1"># Note: this code has not been tested with perfect import.</t>
<t tx="ekr.20181108111640.10">class Node(object):
""" Abstract base class for all Nodes.
"""
__slots__ = ['lineno', 'col_offset']
@others
try:
Node.OPS.__doc__ += ', '.join([x for x in sorted(Node.OPS.__dict__)
if not x.startswith('_')])
Node.COMP.__doc__ += ', '.join([x for x in sorted(Node.COMP.__dict__)
if not x.startswith('_')])
except AttributeError: # pragma: no cover
pass # Py < 3.3
## -- (start marker for doc generator)
## General
</t>
<t tx="ekr.20181108111640.100">def _convert_Attribute(self, n):
return Attribute(self._convert(n.value), n.attr)
</t>
<t tx="ekr.20181108111640.101">def _convert_Subscript(self, n):
return Subscript(self._convert(n.value), self._convert(n.slice))
</t>
<t tx="ekr.20181108111640.102">def _convert_Index(self, n):
return Index(self._convert(n.value))
</t>
<t tx="ekr.20181108111640.103">def _convert_Slice(self, n):
c = self._convert
step = c(n.step)
if pyversion < (3, ) and isinstance(step, NameConstant) and step.value is None:
if not self._lines[n.step.lineno-1][n.step.col_offset:].startswith('None'):
step = None # silly Python 2 turns a[::] into a[::None]
return Slice(c(n.lower), c(n.upper), step)
</t>
<t tx="ekr.20181108111640.104">def _convert_ExtSlice(self, n):
c = self._convert
return ExtSlice([c(x) for x in n.dims])
## Expressions
</t>
<t tx="ekr.20181108111640.105">def _convert_Expr(self, n):
return Expr(self._convert(n.value))
</t>
<t tx="ekr.20181108111640.106">def _convert_UnaryOp(self, n):
op = n.op.__class__.__name__
return UnaryOp(op, self._convert(n.operand))
</t>
<t tx="ekr.20181108111640.107">def _convert_BinOp(self, n):
op = n.op.__class__.__name__
return BinOp(op, self._convert(n.left), self._convert(n.right))
</t>
<t tx="ekr.20181108111640.108">def _convert_BoolOp(self, n):
c = self._convert
op = n.op.__class__.__name__
return BoolOp(op, [c(x) for x in n.values]) # list of value_nodes
</t>
<t tx="ekr.20181108111640.109">def _convert_Compare(self, n):
c = self._convert
# Get compares and ops
comps = [c(x) for x in ([n.left] + n.comparators)]
ops = [op.__class__.__name__ for op in n.ops]
assert len(ops) == (len(comps) - 1)
# Create our comparison operators
compares = []
for i in range(len(ops)):
co = Compare(ops[i], comps[i], comps[i+1])
compares.append(co)
# Return single or wrapped in an AND
assert compares
if len(compares) == 1:
return compares[0]
else:
return BoolOp(Node.OPS.And, compares)
</t>
<t tx="ekr.20181108111640.11">class OPS:
""" Operator enums: """
# Unary
UAdd = 'UAdd'
USub = 'USub'
Not = 'Not'
Invert = 'Invert'
# Binary
Add = 'Add'
Sub = 'Sub'
Mult = 'Mult'
Div = 'Div'
FloorDiv = 'FloorDiv'
Mod = 'Mod'
Pow = 'Pow'
LShift = 'LShift'
RShift = 'RShift'
BitOr = 'BitOr'
BitXor = 'BitXor'
BitAnd = 'BitAnd'
# Boolean
And = 'And'
Or = 'Or'
</t>
<t tx="ekr.20181108111640.110">def _convert_Call(self, n):
c = self._convert
arg_nodes = [c(a) for a in n.args]
kwarg_nodes = [c(a) for a in n.keywords]
if pyversion < (3, 5):
if n.starargs:
arg_nodes.append(Starred(c(n.starargs)))
if n.kwargs:
kwarg_nodes.append(Keyword(None, c(n.kwargs)))
return Call(c(n.func), arg_nodes, kwarg_nodes)
</t>
<t tx="ekr.20181108111640.111">def _convert_keyword(self, n):
return Keyword(n.arg, self._convert(n.value or None))
</t>
<t tx="ekr.20181108111640.112">def _convert_IfExp(self, n):
c = self._convert
return IfExp(c(n.test), c(n.body), c(n.orelse))
</t>
<t tx="ekr.20181108111640.113">def _convert_ListComp(self, n):
c = self._convert
return ListComp(c(n.elt), [c(x) for x in n.generators])
</t>
<t tx="ekr.20181108111640.114">def _convert_SetComp(self, n):
c = self._convert
return SetComp(c(n.elt), [c(x) for x in n.generators])
</t>
<t tx="ekr.20181108111640.115">def _convert_GeneratorExp(self, n):
c = self._convert
return GeneratorExp(c(n.elt), [c(x) for x in n.generators])
</t>
<t tx="ekr.20181108111640.116">def _convert_DictComp(self, n):
c = self._convert
return DictComp(c(n.key), c(n.value), [c(x) for x in n.generators])
</t>
<t tx="ekr.20181108111640.117">def _convert_comprehension(self, n):
c = self._convert
return Comprehension(c(n.target), c(n.iter), [c(x) for x in n.ifs])
## Statements
</t>
<t tx="ekr.20181108111640.118">def _convert_Assign(self, n):
c = self._convert
return Assign([c(x) for x in n.targets], c(n.value))
</t>
<t tx="ekr.20181108111640.119">def _convert_AugAssign(self, n):
op = n.op.__class__.__name__
return AugAssign(self._convert(n.target), op, self._convert(n.value))
</t>
<t tx="ekr.20181108111640.12">class COMP:
""" Comparison enums: """
Eq = 'Eq'
NotEq = 'NotEq'
Lt = 'Lt'
LtE = 'LtE'
Gt = 'Gt'
GtE = 'GtE'
Is = 'Is'
IsNot = 'IsNot'
In = 'In'
NotIn = 'NotIn'
</t>
<t tx="ekr.20181108111640.120">def _convert_Print(self, n): # pragma: no cover - Python 2.x compat
c = self._convert
if len(n.values) == 1 and isinstance(n.values[0], ast.Tuple):
arg_nodes = [c(x) for x in n.values[0].elts]
else:
arg_nodes = [c(x) for x in n.values]
kwarg_nodes = []
if n.dest is not None:
kwarg_nodes.append(Keyword('dest', c(n.dest)))
if not n.nl:
kwarg_nodes.append(Keyword('end', Str('')))
return Expr(Call(Name('print'), arg_nodes, kwarg_nodes))
</t>
<t tx="ekr.20181108111640.121">def _convert_Exec(self, n): # pragma: no cover - Python 2.x compat
c = self._convert
arg_nodes = [c(n.body)]
arg_nodes.append(c(n.globals) or NameConstant(None))
arg_nodes.append(c(n.locals) or NameConstant(None))
return Expr(Call(Name('exec'), arg_nodes, []))
</t>
<t tx="ekr.20181108111640.122">def _convert_Repr(self, n): # pragma: no cover - Python 2.x compat
c = self._convert
return Call(Name('repr'), [c(n.value)], [])
</t>
<t tx="ekr.20181108111640.123">def _convert_Raise(self, n):
if pyversion < (3, ):
if n.inst or n.tback:
raise RuntimeError('Commonast does not support old raise syntax')
return Raise(self._convert(n.type), None)
return Raise(self._convert(n.exc), self._convert(n.cause))
</t>
<t tx="ekr.20181108111640.124">def _convert_Assert(self, n):
return Assert(self._convert(n.test), self._convert(n.msg))
</t>
<t tx="ekr.20181108111640.125">def _convert_Delete(self, n):
c = self._convert
return Delete([c(x) for x in n.targets])
</t>
<t tx="ekr.20181108111640.126">def _convert_Pass(self, n):
return Pass()
</t>
<t tx="ekr.20181108111640.127">def _convert_Import(self, n):
return Import(None, [(x.name, x.asname) for x in n.names], 0)
</t>
<t tx="ekr.20181108111640.128">def _convert_ImportFrom(self, n):
names = [(x.name, x.asname) for x in n.names]
return Import(n.module, names, n.level)
## Control flow
</t>
<t tx="ekr.20181108111640.129">def _convert_If(self, n):
c = self._convert
node = If(c(n.test), [], [])
self._stack.append((node.body_nodes, n.body))
self._stack.append((node.else_nodes, n.orelse))
return node
</t>
<t tx="ekr.20181108111640.13">def __init__(self, *args):
names = self.__slots__
# Checks
assert len(args) == len(names) # check this always
if docheck:
assert not hasattr(self, '__dict__'), 'Nodes must have __slots__'
assert self.__class__ is not Node, 'Node is an abstract class'
for name, val in zip(names, args):
assert not isinstance(val, ast.AST)
if name == 'name':
assert isinstance(val, (basestring, NoneType)), 'name not a string'
elif name == 'op':
assert val in Node.OPS.__dict__ or val in Node.COMP.__dict__
elif name.endswith('_node'):
assert isinstance(val, (Node, NoneType)), '%r is not a Node' % name
elif name.endswith('_nodes'):
islistofnodes = (isinstance(val, list) and
all(isinstance(n, Node) for n in val))
assert islistofnodes, '%r is not a list of nodes' % name
else:
assert not isinstance(val, Node), '%r should not be a Node' % name
assert not (isinstance(val, list) and
all(isinstance(n, Node) for n in val))
# Assign
for name, val in zip(names, args):
setattr(self, name, val)
</t>
<t tx="ekr.20181108111640.130">def _convert_For(self, n):
c = self._convert
node = For(c(n.target), c(n.iter), [], [])
self._stack.append((node.body_nodes, n.body))
self._stack.append((node.else_nodes, n.orelse))
return node
</t>
<t tx="ekr.20181108111640.131">def _convert_While(self, n):
c = self._convert
node = While(c(n.test), [], [])
self._stack.append((node.body_nodes, n.body))
self._stack.append((node.else_nodes, n.orelse))
return node
</t>
<t tx="ekr.20181108111640.132">def _convert_Break(self, n):
return Break()
</t>
<t tx="ekr.20181108111640.133">def _convert_Continue(self, n):
return Continue()
</t>
<t tx="ekr.20181108111640.134">def _convert_Try(self, n):
c = self._convert
node = Try([], [c(x) for x in n.handlers], [], [])
self._stack.append((node.body_nodes, n.body))
self._stack.append((node.else_nodes, n.orelse))
self._stack.append((node.finally_nodes, n.finalbody))
return node
</t>
<t tx="ekr.20181108111640.135">def _convert_TryFinally(self, n): # pragma: no cover - Py <= 3.2
c = self._convert
if (len(n.body) == 1) and n.body[0].__class__.__name__ == 'TryExcept':
# un-nesting for try-except-finally
n2 = n.body[0]
node = Try([], [c(x) for x in n2.handlers], [], [])
self._stack.append((node.body_nodes, n2.body))
self._stack.append((node.else_nodes, n2.orelse))
self._stack.append((node.finally_nodes, n.finalbody))
else:
node = Try([], [], [], [])
self._stack.append((node.body_nodes, n.body))
self._stack.append((node.finally_nodes, n.finalbody))
return node
</t>
<t tx="ekr.20181108111640.136">def _convert_TryExcept(self, n): # pragma: no cover - Py <= 3.2
c = self._convert
node = Try([], [c(x) for x in n.handlers], [], [])
self._stack.append((node.body_nodes, n.body))
self._stack.append((node.else_nodes, n.orelse))
return node
</t>
<t tx="ekr.20181108111640.137">def _convert_ExceptHandler(self, n):
c = self._convert
name = n.name.id if isinstance(n.name, ast.Name) else n.name
node = ExceptHandler(c(n.type), name, [])
self._stack.append((node.body_nodes, n.body))
return node
</t>
<t tx="ekr.20181108111640.138">def _convert_With(self, n):
c = self._convert
if hasattr(n, 'items'):
node = With([c(x) for x in n.items], [])
else: # pragma: no cover - Py < 3.3
items = [WithItem(c(n.context_expr), c(n.optional_vars))]
while (len(n.body) == 1) and isinstance(n.body[0], n.__class__):
n = n.body[0]
items.append(WithItem(c(n.context_expr), c(n.optional_vars)))
node = With(items, [])
self._stack.append((node.body_nodes, n.body))
return node
</t>
<t tx="ekr.20181108111640.139">def _convert_withitem(self, n):
return WithItem(self._convert(n.context_expr), self._convert(n.optional_vars))
## Function and class definitions
</t>
<t tx="ekr.20181108111640.14">def tojson(self, indent=2):
""" Return a string with the JSON representatiom of this AST.
Set indent to None for a more compact representation.
"""
return json.dumps(self._todict(), indent=indent, sort_keys=True)
</t>
<t tx="ekr.20181108111640.140">def _convert_functiondefs(self, n, cls):
c = self._convert
args = n.args
# Parse arg_nodes and kwarg_nodes
arg_nodes = [c(x) for x in args.args]
for i, default in enumerate(reversed(args.defaults)):
arg_node = arg_nodes[-1-i]
if isinstance(arg_node, Tuple):
raise RuntimeError('Tuple arguments in function def not supported.')
arg_node.value_node = c(default)
if pyversion < (3, ):
kwarg_nodes = []
else:
kwarg_nodes = [c(x) for x in args.kwonlyargs]
for i, default in enumerate(reversed(args.kw_defaults)):
kwarg_nodes[-1-i].value_node = c(default)
# Parse args_node and kwargs_node
if pyversion < (3, ):
args_node = Arg(args.vararg, None, None) if args.vararg else None
kwargs_node = Arg(args.kwarg, None, None) if args.kwarg else None
elif pyversion < (3, 4):
args_node = kwargs_node = None
if args.vararg:
args_node = Arg(args.vararg, None, c(args.varargannotation))
if args.kwarg:
kwargs_node = Arg(args.kwarg, None, c(args.kwargannotation))
else:
args_node = c(args.vararg)
kwargs_node = c(args.kwarg)
returns = None if pyversion < (3, ) else c(n.returns)
Cls = cls # noqa
node = Cls(n.name, [c(x) for x in n.decorator_list], returns,
arg_nodes, kwarg_nodes, args_node, kwargs_node, [])
if docheck:
assert isinstance(node.args_node, (NoneType, Arg))
assert isinstance(node.kwargs_node, (NoneType, Arg))
for x in node.arg_nodes + node.kwarg_nodes:
assert isinstance(x, Arg)
self._stack.append((node.body_nodes, n.body))
return node
</t>
<t tx="ekr.20181108111640.141">def _convert_FunctionDef(self, n):
return self._convert_functiondefs(n, FunctionDef)
</t>
<t tx="ekr.20181108111640.142">def _convert_Lambda(self, n):
c = self._convert
args = n.args
arg_nodes = [c(x) for x in args.args]
for i, default in enumerate(reversed(args.defaults)):
arg_nodes[-1-i].value_node = c(default)
if pyversion < (3, ):
kwarg_nodes = []
else:
kwarg_nodes = [c(x) for x in args.kwonlyargs]
for i, default in enumerate(reversed(args.kw_defaults)):
kwarg_nodes[-1-i].value_node = c(default)
return Lambda(arg_nodes, kwarg_nodes,
c(args.vararg), c(args.kwarg), c(n.body))
</t>
<t tx="ekr.20181108111640.143">def _convert_AsyncFunctionDef(self, n):
return self._convert_functiondefs(n, AsyncFunctionDef)
</t>
<t tx="ekr.20181108111640.144">def _convert_arg(self, n):
# Value is initially None
return Arg(n.arg or None, None, self._convert(n.annotation))
</t>
<t tx="ekr.20181108111640.145">def _convert_Return(self, n):
return Return(self._convert(n.value))
</t>
<t tx="ekr.20181108111640.146">def _convert_Yield(self, n):
return Yield(self._convert(n.value))
</t>
<t tx="ekr.20181108111640.147">def _convert_YieldFrom(self, n):
return YieldFrom(self._convert(n.value))
</t>
<t tx="ekr.20181108111640.148">def _convert_Await(self, n):
return Await(self._convert(n.value))
</t>
<t tx="ekr.20181108111640.149">def _convert_Global(self, n):
return Global(n.names)
</t>
<t tx="ekr.20181108111640.15">@classmethod
def fromjson(cls, text):
""" Classmethod to create an AST tree from JSON.
"""
return Node._fromdict(json.loads(text))
</t>
<t tx="ekr.20181108111640.150">def _convert_Nonlocal(self, n):
return Nonlocal(n.names)
</t>
<t tx="ekr.20181108111640.151">def _convert_ClassDef(self, n):
c = self._convert
arg_nodes = [c(a) for a in n.bases]
kwarg_nodes = [] if pyversion < (3, ) else [c(a) for a in n.keywords]
if getattr(n, 'starargs', None):
arg_nodes.append(Starred(self._convert(n.starargs)))
if getattr(n, 'kwargs', None):
kwarg_nodes.append(Keyword(None, self._convert(n.kwargs)))
node = ClassDef(n.name, [c(a) for a in n.decorator_list],
arg_nodes, kwarg_nodes, [])
self._stack.append((node.body_nodes, n.body))
return node
</t>
<t tx="ekr.20181108111640.152">import re
import os
import sys
import types
import inspect
import hashlib
import tempfile
import subprocess
from . import Parser
from .stdlib import get_full_std_lib # noqa
from .modules import create_js_module
@others
@language python
@tabwidth -4
</t>
<t tx="ekr.20181108111640.154">class JSString(str):
""" A subclass of string, so we can add attributes to JS string objects.
"""
pass
</t>
<t tx="ekr.20181108111640.155">def py2js(ob=None, new_name=None, **parser_options):
<< py2js docstring >>
def py2js_(ob):
if isinstance(ob, str):
thetype = 'str'
pycode = ob
filename = None
linenr = 0
elif isinstance(ob, types.ModuleType) and hasattr(ob, '__file__'):
thetype = 'str'
filename = inspect.getsourcefile(ob)
linenr = 0
pycode = open(filename, 'rb').read().decode()
if pycode.startswith('# -*- coding:'):
pycode = '\n' + pycode.split('\n', 1)[-1]
elif isinstance(ob, (type, types.FunctionType, types.MethodType)):
thetype = 'class' if isinstance(ob, type) else 'def'
# Get code
try:
filename = inspect.getsourcefile(ob)
lines, linenr = inspect.getsourcelines(ob)
except Exception as err:
raise ValueError('Could not get source code for object %r: %s' %
(ob, err))
if getattr(ob, '__name__', '') in ('', '<lambda>'):
raise ValueError('py2js() got anonymous function from '
'"%s", line %i, %r.' % (filename, linenr, ob))
# Normalize indentation, based on first line
indent = len(lines[0]) - len(lines[0].lstrip())
for i in range(len(lines)):
line = lines[i]
line_indent = len(line) - len(line.lstrip())
if line_indent < indent and line.strip():
assert line.lstrip().startswith('#') # only possible for comments
lines[i] = indent * ' ' + line.lstrip()
else:
lines[i] = line[indent:]
# Skip any decorators
while not lines[0].lstrip().startswith((thetype, 'async ' + thetype)):
lines.pop(0)
# join lines and rename
pycode = ''.join(lines)
else:
raise ValueError('py2js() only accepts non-builtin modules, '
'classes and functions.')
# Get hash, in case we ever want to cache JS accross sessions
h = hashlib.sha256('pscript version 1'.encode())
h.update(pycode.encode())
hash = h.digest()
# Get JS code
if filename:
p = Parser(pycode, (filename, linenr), **parser_options)
else:
p = Parser(pycode, **parser_options)
jscode = p.dump()
if new_name:
if thetype not in ('class', 'def'):
raise TypeError('py2js() can only rename functions and classes.')
jscode = js_rename(jscode, ob.__name__, new_name)
# Collect undefined variables
# vars_unknown = [name for name, s in p.vars.get_undefined()]
vars_unknown = set()
for name, usages in p.vars.get_undefined():
for usage in usages:
vars_unknown.add(usage)
# todo: now that we have so much info in the meta, maybe we should
# use use py2js everywhere where we now use Parser and move its docs here.
# Wrap in JSString
jscode = JSString(jscode)
jscode.meta = {}
jscode.meta['filename'] = filename
jscode.meta['linenr'] = linenr
jscode.meta['pycode'] = pycode
jscode.meta['pyhash'] = hash
jscode.meta['std_functions'] = p._std_functions
jscode.meta['std_methods'] = p._std_methods
jscode.meta['vars_defined'] = p.vars.get_defined()
jscode.meta['vars_global'] = p.vars.get_globals()
jscode.meta['vars_unknown'] = vars_unknown
return jscode
if ob is None:
return py2js_ # uses as a decorator with some options set
return py2js_(ob)
</t>
<t tx="ekr.20181108111640.156">re_sub1 = re.compile(r'this\.__(\w*?[a-zA-Z0-9](?!__)\W)', re.UNICODE)
def js_rename(jscode, cur_name, new_name):
""" Rename a function or class in a JavaScript code string.
The new name can be prefixed (i.e. have dots in it). Functions can be
converted to methods by prefixing with a name that starts with a capital
letter (and probably ".prototype"). Double-underscore-mangling
is taken into account.
Parameters:
jscode (str): the JavaScript source code
cur_name (str): the current name (must be an identifier, e.g. no dots).
new_name (str): the name to replace the current name with
Returns:
str: the modified JavaScript source code
"""
assert cur_name and '.' not in cur_name
isclass = cur_name[0].lower() != cur_name[0]
if isclass:
# cur_cls_name = cur_name
new_cls_name = new_name.split('.')[-1]
else:
new_cls_name = ''
parts = new_name.split('.')
prefix = '.'.join(parts[:-1])
if prefix:
maybe_cls = parts[-3] if parts[-2] == 'prototype' else parts[-2]
maybe_cls = maybe_cls.strip('$') # allow special names
if maybe_cls[0].lower() != maybe_cls[0]:
new_cls_name = maybe_cls
cur_name_short = cur_name.split('.')[-1]
new_name_short = new_name.split('.')[-1]
if isclass:
# If this is about a class ...
jscode = jscode.replace('.__name__ = "%s"' % cur_name_short,
'.__name__ = "%s"' % new_name_short)
jscode = jscode.replace('._%s__' % cur_name_short,