-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython.jison
More file actions
1082 lines (947 loc) · 34.4 KB
/
python.jison
File metadata and controls
1082 lines (947 loc) · 34.4 KB
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
/* Python Parser for Jison */
/* https://docs.python.org/2.7/reference/lexical_analysis.html */
/* https://docs.python.org/2.7/reference/grammar.html */
/* lexical gammar */
%{ var indents = [0], indent = 0, dedents = 0 %}
%lex
/** identifiers **/
identifier ("_"|{letter})({letter}|{digit}|"_")*
letter {lowercase}|{uppercase}
lowercase [a-z]
uppercase [A-Z]
digit [0-9]
/** strings **/
longstring {longstring_double}|{longstring_single}
longstring_double '"""'{longstringitem}*'"""'
longstring_single "'''"{longstringitem}*"'''"
longstringitem {longstringchar}|{escapeseq}
longstringchar [^\\]
shortstring {shortstring_double}|{shortstring_single}
shortstring_double '"'{shortstringitem_double}*'"'
shortstring_single "'"{shortstringitem_single}*"'"
shortstringitem_double {shortstringchar_double}|{escapeseq}
shortstringitem_single {shortstringchar_single}|{escapeseq}
shortstringchar_single [^\\\n\']
shortstringchar_double [^\\\n\"]
escapeseq \\.
/** numbers **/
integer {decinteger}|{hexinteger}|{octinteger}
decinteger ([1-9]{digit}*)
hexinteger "0"[x|X]{hexdigit}+
octinteger "0"[o|O]?{octdigit}+
bininteger "0"[b|B]({bindigit}+)
hexdigit {digit}|[a-fA-F]
octdigit [0-7]
bindigit [0|1]
/** floats **/
floatnumber {exponentfloat}|{pointfloat}
exponentfloat ({digit}+|{pointfloat}){exponent}
pointfloat ({digit}*{fraction})|({digit}+".")
fraction "."{digit}+
exponent [e|E][\+|\-]{digit}+
/** reserved **/
reserved {keywords}|{operators}
keywords "continue"|"finally"|"return"|"global"|
"assert"|"except"|"import"|"lambda"|
"raise"|"class"|"print"|"break"|"while"|
"yield"|"from"|"elif"|"else"|"with"|
"pass"|"exec"|"and"|"del"|"not"|"def"|
"for"|"try"|"as"|"or"|"if"|"in"|"is"
operators ">>="|"<<="|"**="|"//="|"+="|"-="|"*="|
"/="|"%="|"&="|"|="|"^="|"**"|"//"|"<<"|
">>"|"<="|">="|"=="|"!="|"<>"|"+"|"-"|
"*"|"/"|"%"|"&"|"|"|"^"|"~"|"<"|">"|"("|
")"|"["|"]"|"{"|"}"|"@"|","|":"|"."|"`"|
"="|";"|"'"|"""|"#"|"\"
%s INITIAL DEDENTS INLINE
%%
<<EOF>> return 'EOF'
<INITIAL>\ %{ indent += 1 %}
<INITIAL>\t %{ indent = ( indent + 8 ) & -7 %}
<INITIAL>\n %{ indent = 0 // blank line %}
<INITIAL>. %{
this.unput( yytext )
var last = indents[ indents.length - 1 ]
if ( indent > last ) {
this.begin( 'INLINE' )
indents.push( indent )
return 'INDENT'
} else if ( indent < last ) {
this.begin( 'DEDENTS' )
dedents = 0 // how many dedents occured
while( last = indents.pop() ) {
if ( last == indent ) break
dedents += 1
}
} else {
this.begin( 'INLINE' )
}
%}
<DEDENTS>. %{
this.unput( yytext )
if ( dedents-- > 0 ) {
dedents -= 1
return 'DEDENT'
} else {
this.begin( 'INLINE' )
}
%}
<INLINE>\n+ %{
indent = 0;
this.begin( 'INITIAL' )
return 'NEWLINE'
%}
<INLINE>[\ \t\f]+ /* skip whitespace, separate tokens */
<INLINE>{reserved} %{ return yytext %}
<INLINE>{floatnumber} return 'NUMBER'
<INLINE>{bininteger} %{
// parseInt to convert to base-10
var i = yytext.substr(2); // binary val
yytext = 'parseInt("'+i+'",2)'
return 'NUMBER'
%}
<INLINE>{integer} return 'NUMBER'
<INLINE>{longstring} %{
// escape string and convert to double quotes
// http://stackoverflow.com/questions/770523/escaping-strings-in-javascript
var str = yytext.substr(3, yytext.length-6)
.replace( /[\\"']/g, '\\$&' )
.replace(/\u0000/g, '\\0');
yytext = '"' + str + '"'
return 'STRING'
%}
<INLINE>{shortstring} return 'STRING'
<INLINE>{identifier} return 'NAME'
/lex
%start expressions
%%
/** grammar **/
expressions
: file_input { console.log( $1 ) }
;
// (NEWLINE | stmt)* ENDMARKER
file_input
: EOF
| file_input_head EOF
;
file_input_head
: NEWLINE
| stmt
;
// 'def' NAME parameters ':' suite
funcdef
: 'def' NAME parameters ':' suite
{
var params = []
$3.forEach(function(p, i) {
if ( p.args ) {
$5 = '\nvar ' + p.param + ' = [].slice.call(arguments,' + i + ');\n' + $5;
return
} else if ( p.kargs ) {
$5 = '\nvar ' + p.param + ' = arguments[arguments.length-1]; (typeof ' + p.param + '!="object") && (' + p.param + '={});\n' + $5;
return
}
var code = '(typeof ' + p.param + ' == "undefined") && ';
if ( p.default )
code += '(' + p.param + ' = ' + p.default + ')'
else
code += '(throw new Error("Missing argument: ' + p.param + '"))'
$5 = code + ';\n' + $5;
params.push( p.param )
})
$$ = 'var ' + $2 + '=function(' + params.join( ',' ) + '){' + $5 + '}'
}
;
parameters
: '(' ')' { $$ = '' }
| '(' varargslist ')' { $$ = $2 }
;
// ( (fpdef ['=' test] ',')*
// ('*' NAME [',' '**' NAME] | '**' NAME) |
// fpdef ['=' test] (',' fpdef ['=' test])* [','])
varargslist
: fpdef { $$ = [{ param: $1 }] }
| fpdef '=' test { $$ = [{ param: $1, default: $3 }] }
| fpdef varargslist_tail { $$ = [{ param: $1 }].concat( $2 ) }
| fpdef '=' test varargslist_tail { $$ = [{ param: $1, default: $3 }].concat( $4 ) }
| '*' NAME { $$ = [{ param: $2, args: true }] }
| '**' NAME { $$ = [{ param: $2, kargs: true }] }
| '*' NAME ',' '**' NAME { $$ = [{ param: $2, args: true },{ param: $5, kargs: true }] }
| varargslist_head '*' NAME { $$ = $1.concat( [{ param: $3, args: true }] ) }
| varargslist_head '**' NAME { $$ = $1.concat( [{ param: $3, kargs: true }] ) }
| varargslist_head '*' NAME ',' '**' NAME
{ $$ = $1.concat( [{ param: $3, args: true }, { param: $6, kargs: true }] ) }
;
varargslist_head
: fpdef ',' { $$ = [{ param: $1 }] }
| fpdef '=' test ',' { $$ = [{ param: $1, default: $3 }] }
| fpdef ',' varargslist_head { $$ = [{ param: $1 }].concat( $3 ) }
| fpdef '=' test ',' varargslist_head
{ $$ = [{ param: $1, default: $3 }].concat( $5 ) }
;
varargslist_tail
: ',' fpdef { $$ = [{ param: $2 }] }
| ',' fpdef ',' { $$ = [{ param: $2 }] }
| ',' fpdef '=' test { $$ = [{ param: $2, default: $4 }] }
| ',' fpdef '=' test ',' { $$ = [{ param: $2, default: $4 }] }
| ',' fpdef varargslist_tail { $$ = [{ param: $2 }].concat( $3 ) }
| ',' fpdef '=' test varargslist_tail
{ $$ = [{ param: $2, default: $4 }].concat( $5 ) }
;
// NAME | '(' fplist ')'
fpdef
: NAME
| '(' fplist ')' // todo
;
// fpdef (',' fpdef)* [',']
fplist
: fpdef
| fpdef ','
| fpdef fplist_tail { $$ = $1 + ',' + $2 }
;
fplist_tail
: ',' fpdef { $$ = $2 }
| ',' fpdef ',' { $$ = $2 }
| ',' fpdef fplist_tail { $$ = $2 + ',' + $3 }
;
// simple_stmt | compound_stmt
stmt
: simple_stmt
| compound_stmt
;
// small_stmt (';' small_stmt)* [';'] NEWLINE
simple_stmt
/*: small_stmt NEWLINE*/
: small_stmt EOF
| small_stmt ';' NEWLINE
| small_stmt ';' EOF
| small_stmt simple_stmt_tail NEWLINE
{ $$ = $1 + $2 }
| small_stmt simple_stmt_tail EOF { $$ = $1 + $2 }
;
simple_stmt_tail
: ';' small_stmt { $$ = $1 + $2 }
| ';' small_stmt ';' { $$ = $1 + $2 }
| ';' small_stmt simple_stmt_tail { $$ = $1 + $2 + $3 }
;
// (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
// import_stmt | global_stmt | exec_stmt | assert_stmt)
small_stmt
: expr_stmt
| print_stmt // todo
| del_stmt // todo
| pass_stmt // todo
| flow_stmt // todo
| import_stmt // todo
| global_stmt // todo
| exec_stmt // todo
| assert_stmt // todo
;
// expr_stmt: testlist (augassign (yield_expr|testlist) |
// ('=' (yield_expr|testlist))*)
expr_stmt
: testlist augassign testlist
{
if ( typeof $2 == "function" ) {
$$ = $1 + $2( $1, $3 )
} else {
$$ = $1 + $2 + $3
}
}
| testlist augassign yield_expr // todo
| testlist expr_stmt_tail { $$ = $1 + $2 }
;
expr_stmt_tail
: '=' testlist { $$ = $1 + $2 }
| '=' testlist expr_stmt_tail { $$ = $1 + $2 + $3 }
| '=' yield_expr // todo
| '=' yield_expr expr_stmt_tail // todo
;
// ('+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' |
// '<<=' | '>>=' | '**=' | '//=')
augassign
: '+=' | '-=' | '*=' | '/=' | '%='
| '&=' | '|=' | '^=' | '<<=' | '>>='
| '**='
{
$$ = function(x, y) { return '=Math.pow(' + x + ',' + y + ')' }
}
| '//='
{
$$ = function(x,y) { return '=' + x + '/' + y }
}
;
// 'print' ( [ test (',' test)* [','] ] |
// '>>' test [ (',' test)+ [','] ] )
print_stmt
: 'print' { $$ = 'console.log("")' }
| 'print' test { $$ = 'console.log(' + $2 + ')' }
| 'print' test ',' { $$ = 'console.log(' + $2 + ')' }
| 'print' test print_stmt_tail { $$ = 'console.log(' + $2 + $3 + ')' }
;
print_stmt_tail
: ',' test { $$ = $1 + $2 }
| ',' test ',' { $$ = $1 + $2 }
| ',' test print_stmt_tail { $$ = $1 + $2 + $3 }
;
// 'del' exprlist
del_stmt
: 'del' exprlist { $$ = 'delete ' + $2 }
;
// 'pass'
pass_stmt: 'pass' { $$ = '' };
// break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
flow_stmt
: break_stmt
| continue_stmt
| return_stmt
| raise_stmt
| yield_stmt
;
// 'break'
break_stmt: 'break';
// 'continue'
continue_stmt: 'continue';
// 'return' [testlist]
return_stmt
: 'return'
| 'return' testlist { $$ = $1 + ' ' + $2 }
;
// 'raise' [test [',' test [',' test]]]
raise_stmt
: 'raise' { $$ = 'throw new Error()' } // todo, support raising the current exception
| 'raise' test { $$ = 'throw ' + $2 }
| 'raise' test ',' test { $$ = 'throw new ' + $2 + '(' + $4 + ')' }
| 'raise' test ',' test ',' test { $$ = 'throw new ' + $2 + '(' + $4 + ')' } // todo (add traceback)
;
// yield_expr
yield_stmt: yield_expr;
// import_name | import_from
import_stmt: import_name | import_from;
// 'import' dotted_as_names
import_name
: 'import' dotted_as_names
{
$$ = $2.map(function(i) {
var path = i.path.replace( '.', '/' )
return 'var ' + i.name + '=require("' + path + '");'
}).join( '\n' )
}
;
// ('from' ('.'* dotted_name | '.'+)
// 'import' ('*' | '(' import_as_names ')' | import_as_names))
import_from
: 'from' dotted_name import_from_tail
{
$$ = $3.map(function(i){
var path = ( $2 + '/' + i.path ).replace( '.', '/' )
return 'var ' + i.name + '=require("' + path + '");'
}).join( '\n' )
}
| 'from' dots dotted_name import_from_tail
{
var base = $2.replace( /\.\./g, '../' )
if ( base[ base.length - 1 ] != '/' ) base += '/'
$$ = $4.map(function(i){
var path = ( $3 + '/' + i.path ).replace( /\./g, '/' )
path = base + path
return 'var ' + i.name + '=require("' + path + '");'
}).join( '\n' )
}
| 'from' dots import_from_tail
{
var base = $2.replace( /\.\./g, '../' )
if ( base[ base.length - 1 ] != '/' ) base += '/'
$$ = $3.map(function(i){
var path = i.path.replace( /\./g, '/' )
path = base + path
return 'var ' + i.name + '=require("' + path + '");'
}).join( '\n' )
}
;
import_from_tail
: 'import' '*' // todo
| 'import' import_as_names { $$ = $2 }
| 'import' '(' import_as_names ')' { $$ = $3 }
;
dots
: '.'
| '.' dots { $$ = $1 + $2 }
;
// NAME ['as' NAME]
import_as_name
: NAME { $$ = [{ path: $1, name: $1 }] }
| NAME 'as' NAME { $$ = [{ path: $1, name: $3 }] }
;
// import_as_name (',' import_as_name)* [',']
import_as_names
: import_as_name
| import_as_name ','
| import_as_name import_as_names_tail
{ $$ = $1.concat( $2 ) }
;
import_as_names_tail
: ',' import_as_name { $$ = $2 }
| ',' import_as_name ',' { $$ = $2 }
| ',' import_as_name import_as_names_tail
{ $$ = $2.concat( $3 ) }
;
// dotted_name ['as' NAME]
dotted_as_name
: dotted_name { $$ = [{ path: $1, name: $1 }] }
| dotted_name 'as' NAME { $$ = [{ path: $1, name: $3 }] }
;
// dotted_as_name (',' dotted_as_name)*
dotted_as_names
: dotted_as_name
| dotted_as_name dotted_as_names_tail
{ $$ = $1.concat( $2 ) }
;
dotted_as_names_tail
: ',' dotted_as_name { $$ = $2 }
| ',' dotted_as_name dotted_as_names_tail
{ $$ = $2.concat( $3 ) }
;
// NAME ('.' NAME)*
dotted_name
: NAME
| NAME dotted_name_tail { $$ = $1 + $2 }
;
dotted_name_tail
: '.' NAME { $$ = $1 + $2 }
| '.' NAME dotted_name_tail { $$ = $1 + $2 + $3 }
;
// 'global' NAME (',' NAME)*
// todo
global_stmt
: 'global' NAME
| 'global' global_stmt_tail
;
global_stmt_tail
: ',' NAME
| ',' NAME global_stmt_tail
;
// 'exec' expr ['in' test [',' test]]
// todo
exec_stmt
: 'exec' expr
| 'exec' expr 'in' test
| 'exec' expr 'in' test ',' test
;
// 'assert' test [',' test]
assert_stmt
: 'assert' test
{
$$ = 'if (!(' + $2 + ')) throw new Error("Assertion Error")'
}
| 'assert' test ',' test
{
$$ = 'if (!(' + $2 + ')) throw new Error(' + $4 + ')'
}
;
// if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef |
// decorated
compound_stmt
: if_stmt
| while_stmt
| for_stmt
| try_stmt
| with_stmt
| funcdef
| classdef
| decorated
;
// 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
if_stmt
: 'if' test ':' suite { $$ = 'if(' + $2 + '){' + $4 + '}\n' }
| 'if' test ':' suite if_stmt_tail { $$ = 'if(' + $2 + '){' + $4 + '}' + $5 }
;
if_stmt_tail
: 'else' ':' suite { $$ = 'else{' + $3 + '}' }
| if_stmt_elif 'else' ':' suite { $$ = $1 + 'else{' + $4 + '}' }
| if_stmt_elif
;
if_stmt_elif
: 'elif' test ':' suite
{ $$ = 'else if(' + $2 + '){' + $4 + '}' }
| 'elif' test ':' suite if_stmt_elif
{ $$ = 'else if(' + $2 + '){' + $4 + '}' + $5 }
;
// 'while' test ':' suite ['else' ':' suite]
while_stmt
: 'while' test ':' suite
{ $$ = 'while(' + $2 + '){' + $4 + '}' }
| 'while' test ':' suite 'else' ':' suite
{ $$ = 'while(true){if(!(' + $2 + ')){' + $7 + ';break}' + $4 + '}' }
;
// 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
for_stmt
: 'for' exprlist 'in' testlist ':' suite
{ $$ = $4 + '.forEach(function(' + $2 + '){' + $6 + '}' }
| 'for' exprlist 'in' testlist ':' suite 'else' ':' suite // todo
{ $$ = $4 + '.forEach(function(' + $2 + '){' + $6 + '}' }
;
// ('try' ':' suite
// ((except_clause ':' suite)+
// ['else' ':' suite]
// ['finally' ':' suite] |
// 'finally' ':' suite))
try_stmt
: 'try' ':' suite try_except
{
var rethrow = true
$$ = 'try{' + $3 + '}catch(___py_exc){' + $4.map(function( e, i ) {
var name = e.name, suite = e.suite, type = e.type;
if ( name ) suite = 'var ' + name + '=___py_exc;' + suite
if ( !type ) {
rethrow = false
if ( i != $4.length - 1 ) // last except-block
throw new Error( "SyntaxError: default 'except:' must be last" )
return ( i != 0 ) ? 'else{' + suite + '}' : suite;
}
suite = 'if(___py_exc instanceof ' + type + '){' + suite + '}'
return ( ( i == 0 ) ? '' : 'else ' ) + suite
}).join( '' )
+ ( ( rethrow ) ? 'else{throw ___py_exc}' : '' )
+ '}'
+ ( ( $4.finally ) ? 'finally{' + $4.finally + '}' : '' )
}
| 'try' ':' suite 'finally' ':' suite
{ $$ = 'try{' + $3 + '}finally{' + $6 + '}' }
;
try_except
: try_except_head
| try_except_head 'else' ':' suite
{ $1.else = $4; $$ = $1 } // todo else
| try_except_head 'finally' ':' suite
{ $1.finally = $4; $$ = $1 }
| try_except_head 'else' ':' suite 'finally' ':' suite
{ $1.else = $4; $1.finally = $7; $$ = $1 } // todo else
;
try_except_head
: except_clause ':' suite { $1.suite = $3; $$ = [ $1 ] }
| except_clause ':' suite try_except_head
{ $1.suite = $3; $$ = [ $1 ].concat( $4 ) }
;
// 'except' [test [('as' | ',') test]]
except_clause
: 'except' { $$ = {} }
| 'except' test { $$ = { type: $2 } }
| 'except' test 'as' test { $$ = { type: $2, name: $4 } }
| 'except' test ',' test { $$ = { type: $2, name: $4 } }
;
// 'with' with_item (',' with_item)* ':' suite
with_stmt // todo: need to reconsider the entire semantics here
: 'with' with_item ':' suite
{
$$ = '(function(' + $2.as + '){' + $4 + '})(' + $2.name + ')'
}
| 'with' with_item with_stmt_tail ':' suite
{
$2 = [ $2 ].concat( $3 )
var as = $2.map(function(i){ return i.as }).join( ',' )
var name = $2.map(function(i){ return i.name }).join( ',' )
$$ = '(function(' + as + '){' + $5 + '})(' + name + ')'
}
;
with_stmt_tail
: ',' with_item { $$ = [ $2 ] }
| ',' with_item with_stmt_tail { $$ = [ $2 ].concat( $3 ) }
;
// test ['as' expr]
with_item
: test { $$ = { name: $1, as: $1 } }
| test 'as' expr { $$ = { name: $1, as: $2 } }
;
// simple_stmt | NEWLINE INDENT stmt+ DEDENT
suite
/*: simple_stmt*/
: NEWLINE INDENT { $$ = 'cookies'}
/*| NEWLINE INDENT stmts DEDENT { $$ = $3 }
| NEWLINE INDENT stmts '1' { $$ = $3 }*/
;
stmts
: stmt
| stmt stmts { $$ = $1 + ';\n' + $2 }
;
// Backward compatibility cruft to support:
// [ x for x in lambda: True, lambda: False if x() ]
// even while also allowing:
// lambda x: 5 if x else 2
// (But not a mix of the two)
// old_test [(',' old_test)+ [',']]
testlist_safe
: old_test
| old_test testlist_safe_tail { $$ = $1 + $2 }
;
testlist_safe_tail
: ',' old_test { $$ = $1 + $2 }
| ',' old_test ',' { $$ = $1 + $2 }
| ',' old_test testlist_safe_tail { $$ = $1 + $2 + $3 }
;
// or_test | old_lambdef
old_test
: or_test | old_lambdef ;
// 'lambda' [varargslist] ':' old_test
old_lambdef
: 'lambda' ':' old_test { $$ = 'function(){return ' + $3 + '};' }
| 'lambda' varargslist ':' old_test { $$ = 'function(' + $2 + '){return ' + $4 + '};' }
;
// if
// or_test ['if' or_test 'else' test] | lambdef
test
: or_test
| or_test 'if' or_test 'else' test { $$ = '(('+$3+')?'+$1+':'+$5 + ')' }
;
// logical of ||
// and_test ('or' and_test)*
or_test
: and_test
| and_test or_test_tail { $$ = $1 + $2 }
;
or_test_tail
: 'or' and_test { $$ = $1 + $2 }
| 'or' and_test or_test_tail { $$ = $1 + $2 + $3 }
;
// logical and &&
// not_test ('and' not_test)*
and_test
: not_test
| not_test and_test_tail { $$ = $1 + $2 }
;
and_test_tail
: 'and' not_test { $$ = '&&' + $2 }
| 'and' not_test and_test_tail { $$ = '&&' + $2 + $3 }
;
// logical not !
// 'not' not_test | comparison
not_test
: 'not' not_test { $$ = '!' + $2 }
| comparison
;
// comparisons
// expr (comp_op expr)*
comparison
: expr
| expr comparison_tail { $$ = $1 + $2 }
;
comparison_tail
: comp_op expr { $$ = $1 + $2 }
| comp_op expr comparison_tail { $$ = $1 + $2 + $3 }
;
// comparison operators
// '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
comp_op
: '<' | '>' | '==' | '>=' | '<='
| '!=' | '<>' { $$ = '!=' }
| 'in' /* todo */
| 'not in' /* todo */
| 'is' /* todo */
| 'is not' /* todo */
;
// binary or |
// xor_expr ('|' xor_expr)*
expr
: xor_expr
| xor_expr expr_tail { $$ = $1 + $2 }
;
expr_tail
: '|' xor_expr { $$ = $1 + $2 }
| '|' xor_expr expr_tail { $$ = $1 + $2 + $3 }
;
// binary xor ^
// and_expr ('^' and_expr)*
xor_expr
: and_expr
| and_expr xor_expr_tail { $$ = $1 + $2 }
;
xor_expr_tail
: '^' and_expr { $$ = $1 + $2 }
| '^' and_expr xor_expr_tail { $$ = $1 + $2 + $3 }
;
// binary and &
// shift_expr ('&' shift_expr)*
and_expr
: shift_expr
| shift_expr and_expr_tail { $$ = $1 + $2 }
;
and_expr_tail
: '&' shift_expr { $$ = $1 + $2 }
| '&' shift_expr and_expr_tail { $$ = $1 + $2 + $3 }
;
// binary shift << and >>
// arith_expr (('<<'|'>>') arith_expr)*
shift_expr
: arith_expr
| arith_expr shift_expr_tail { $$ = $1 + $2 }
;
shift_expr_tail
: '<<' arith_expr { $$ = $1 + $2 }
| '>>' arith_expr { $$ = $1 + $2 }
| '<<' arith_expr shift_expr_tail { $$ = $1 + $2 + $3 }
| '>>' arith_expr shift_expr_tail { $$ = $1 + $2 + $3 }
;
// arithmatic addition + and subtraction -
// term (('+'|'-') term)*
arith_expr
: term
| term arith_expr_tail { $$ = $1 + $2 }
;
arith_expr_tail
: '+' term { $$ = $1 + $2 }
| '-' term { $$ = $1 + $2 }
| '+' term arith_expr_tail { $$ = $1 + $2 + $3 }
| '-' term arith_expr_tail { $$ = $1 + $2 + $3 }
;
// arithmatic term multiplication * division / remainder % and //
// factor (('*'|'/'|'%'|'//') factor)*
term
: factor
| factor term_tail { $$ = $1 + $2 }
;
term_tail
: '*' factor { $$ = $1 + $2 }
| '/' factor { $$ = $1 + $2 }
| '%' factor { $$ = $1 + $2 }
| '//' factor { $$ = '/' + $2 }
| '*' factor term_tail { $$ = $1 + $2 + $3 }
| '/' factor term_tail { $$ = $1 + $2 + $3 }
| '%' factor term_tail { $$ = $1 + $2 + $3 }
| '//' factor term_tail { $$ = $1 + $2 + $3 }
;
// sign +, -, ~
// ('+'|'-'|'~') factor | power
factor
: power
| "+" factor { $$ = $1 + $2 }
| "-" factor { $$ = $1 + $2 }
| "~" factor { $$ = $1 + $2 }
;
// power **
// atom trailer* ['**' factor]
power
: atom
| atom trailers { $$ = $1 + $2 }
| atom '**' factor { $$ = 'Math.pow(' + $1 + ',' + $3 + ')' }
| atom trailers '**' factor { $$ = 'Math.pow(' + $1 + $2 + ',' + $4 + ')' }
;
// literal
// ('(' [yield_expr|testlist_comp] ')' |
// '[' [listmaker] ']' |
// '{' [dictorsetmaker] '}' |
// '`' testlist1 '`' |
// NAME | NUMBER | STRING+)
atom
: NAME | NUMBER | STRING
| '[' ']' { $$ = $1 + $2 }
| '[' listmaker ']' { $$ = $2 }
| '{' '}' { $$ = $1 + $2 }
| '{' dictorsetmaker '}' { $$ = $2 }
;
// list contents
// test ( list_for | (',' test)* [','] )
listmaker
: test { $$ = '[' + $1 + ']' }
| test listmaker_tail { $$ = '[' + $1 + $2 + ']' }
| test list_for
{
$$ = '(function(){var ___py_results=[];'
+ $2.before
+ '___py_results.push(' + $1 + ')'
+ $2.after
+ 'return ___py_results})();'
}
;
listmaker_tail
: ',' test { $$ = $1 + $2 }
| ',' test ',' { $$ = $1 + $2 }
| ',' test listmaker_tail { $$ = $1 + $2 + $3 }
;
// trailers to access data
// '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
trailer
: '.' NAME { $$ = $1 + $2 }
;
trailers
: trailer
| trailer trailers { $$ = $1 + $2 }
;
// comma-separated list of expressions
// expr (',' expr)* [',']
exprlist
: expr
| expr exprlist_tail { $$ = $1 + $2 }
;
exprlist_tail
: ',' expr { $$ = $1 + $2 }
| ',' expr ',' { $$ = $1 + $2 }
| ',' expr exprlist_tail { $$ = $1 + $2 + $3 }
;
// dict or set
// ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
// (test (comp_for | (',' test)* [','])) )
// python dictionaries doesn't cast the keys to string automatically
// like javascript does, but evaluates them. the logic here will implement
// the same behavior for javascript (long format) if the dictionary contains
// non-string non-digit keys
dictorsetmaker
// dict
: test ':' test
{
var items = [{k:$1,v:$3}]
var long = items.some(function(i){
return (!i.k || !i.k.match( /['"\d]/) )
});
$$ = ( long )
? '[' + items.map(function(pair){
return '[' + pair.k + ',' + pair.v + ']'
}) + ']'
+ '.reduce('
+ 'function(memo,i){'
+ 'memo[i[0]] = i[1];'
+ 'return memo'
+ '},{})'
: '{' + $1 + $2 + $3 + '}'
}
| test ':' test dict_tail
{
var items = [{k:$1,v:$3}].concat($4)
var long = items.some(function(i){
return (!i.k || !i.k.match( /['"\d]/) )
});
$$ = ( long )
? '[' + items.map(function(pair){
return '[' + pair.k + ',' + pair.v + ']'
}) + ']'
+ '.reduce('
+ 'function(memo,i){'
+ 'memo[i[0]] = i[1];'
+ 'return memo'
+ '},{})'
: '{' + $1 + $2 + $3 + $4 + '}'
}
// set
| test { $$ = '[' + $1 + ']' }
| test set_tail
{ // http://stackoverflow.com/questions/13486479/how-to-get-an-array-of-unique-values-from-an-array-containing-duplicates-in-java
$$ = '[' + $1 + $2 + '].filter(function(e,i,arr){'
+ 'return arr.lastIndexOf(e) === i'
+ '})'
}
| test comp_for
{
$$ = '(function(){var ___py_results=[];'
+ $2.before
+ '___py_results.push(' + $1 + ')'
+ $2.after
+ 'return ___py_results})()'
+ '.filter(function(e,i,arr){'
+ 'return arr.lastIndexOf(e) === i'
+ '})'
}
;
dict_tail
: ',' test ':' test { $$ = [ { k: $2, v: $4 } ] }
| ',' test ':' test ',' { $$ = [ {k: $2, v: $4 } ] }
| ',' test ':' test dict_tail { $$ = [ { k: $2, v: $4 }].concat( $5 ) }
;
set_tail
: ',' test { $$ = $1 + $2 }
| ',' test ',' { $$ = $1 + $2 }
| ',' test set_tail { $$ = $1 + $2 + $3 }
;
// 'class' NAME ['(' [testlist] ')'] ':' suite
classdef
: 'class' NAME ':' suite
{
$$ = 'var ' + $2 + '=function ' + $2 + '(){}'
}
| 'class' NAME '(' ')' ':' suite
| 'class' NAME '(' testlist ')' ':' suite
;
// test (',' test)* [',']
testlist
: test
| test testlist_tail { $$ = $1 + $2 }
;
testlist_tail
: ',' test { $$ = $1 + $2 }
| ',' test ',' { $$ = $1 + $2 }
| ',' test testlist_tail { $$ = $1 + $2 + $3 }
;