forked from SethBling/cbscript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptparse.py
More file actions
Latest commit
1870 lines (1472 loc) · 52.8 KB
/
Copy pathscriptparse.py
File metadata and controls
1870 lines (1472 loc) · 52.8 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
fromplyimport*
importtraceback
importscriptlex
fromblock_types.advancement_definition_blockimportadvancement_definition_block
fromblock_types.array_definition_blockimportarray_definition_block
fromblock_types.block_definition_blockimportblock_definition_block
fromblock_types.block_switch_blockimportblock_switch_block
fromblock_types.block_id_switch_blockimportblock_id_switch_block
fromblock_types.block_tag_blockimportblock_tag_block
fromblock_types.clock_sectionimportclock_section
fromblock_types.command_blockimportcommand_block
fromblock_types.comment_blockimportcomment_block
fromblock_types.create_blockimportcreate_block
fromblock_types.define_name_blockimportdefine_name_block
fromblock_types.entity_tag_blockimportentity_tag_block
fromblock_types.execute_blockimportexecute_block
fromblock_types.for_index_blockimportfor_index_block
fromblock_types.for_selector_blockimportfor_selector_block
fromblock_types.function_call_blockimportfunction_call_block
fromblock_types.function_sectionimportfunction_section
fromblock_types.import_blockimportimport_block
fromblock_types.item_tag_blockimportitem_tag_block
fromblock_types.loot_table_definition_blockimportloot_table_definition_block
fromblock_types.macro_call_blockimportmacro_call_block
fromblock_types.macro_sectionimportmacro_section
fromblock_types.method_call_blockimportmethod_call_block
fromblock_types.move_blockimportmove_block
fromblock_types.nbt_data_blockimportnbt_data_block
fromblock_types.nbt_remove_blockimportnbt_remove_block
fromblock_types.pop_blockimportpop_block
fromblock_types.pointer_decl_blockimportpointer_decl_block
fromblock_types.predicate_definition_blockimportpredicate_definition_block
fromblock_types.item_modifier_definition_blockimportitem_modifier_definition_block
fromblock_types.print_blockimportprint_block
fromblock_types.push_blockimportpush_block
fromblock_types.python_assignment_blockimportpython_assignment_block
fromblock_types.python_for_blockimportpython_for_block
fromblock_types.python_if_blockimportpython_if_block
fromblock_types.python_import_blockimportpython_import_block
fromblock_types.python_tuple_assignment_blockimportpython_tuple_assignment_block
fromblock_types.reset_sectionimportreset_section
fromblock_types.scoreboard_assignment_blockimportscoreboard_assignment_block
fromblock_types.selector_assignment_blockimportselector_assignment_block
fromblock_types.selector_definition_blockimportselector_definition_block
fromblock_types.shaped_recipe_blockimportshaped_recipe_block
fromblock_types.switch_blockimportswitch_block
fromblock_types.tell_blockimporttell_block
fromblock_types.template_function_call_blockimporttemplate_function_call_block
fromblock_types.template_function_sectionimporttemplate_function_section
fromblock_types.title_blockimporttitle_block
fromblock_types.vector_assignment_blockimportvector_assignment_block
fromblock_types.vector_assignment_scalar_blockimportvector_assignment_scalar_block
fromblock_types.while_blockimportwhile_block
fromblock_types.with_anonymous_blockimportwith_anonymous_block
fromblock_types.with_itemsimportwith_items
fromdata_types.block_caseimportblock_case
fromdata_types.const_numberimportconst_number
fromdata_types.const_stringimportconst_string
fromdata_types.number_macro_pathimportnumber_macro_path
fromdata_types.python_identifierimportpython_identifier
fromdata_types.interpreted_pythonimportinterpreted_python
fromdata_types.relcoord_vectorimportrelcoord_vector
fromdata_types.relcoordimportrelcoord
fromdata_types.relcoordsimportrelcoords
fromnbt_types.block_nbt_pathimportblock_nbt_path
fromnbt_types.entity_nbt_pathimportentity_nbt_path
fromnbt_types.nbt_jsonimportnbt_json
fromnbt_types.storage_nbt_pathimportstorage_nbt_path
fromscalar_expressions.binop_exprimportbinop_expr
fromscalar_expressions.create_exprimportcreate_expr
fromscalar_expressions.dot_exprimportdot_expr
fromscalar_expressions.func_exprimportfunc_expr
fromscalar_expressions.method_exprimportmethod_expr
fromscalar_expressions.unary_exprimportunary_expr
fromvariable_types.array_const_varimportarray_const_var
fromvariable_types.array_expr_varimportarray_expr_var
fromvariable_types.block_path_varimportblock_path_var
fromvariable_types.command_varimportcommand_var
fromvariable_types.scale_varimportscale_var
fromvariable_types.scoreboard_varimportscoreboard_var
fromvariable_types.selector_id_varimportselector_id_var
fromvariable_types.storage_path_varimportstorage_path_var
fromvariable_types.virtualint_varimportvirtualint_var
fromvector_expressions.sel_vector_var_exprimportsel_vector_var_expr
fromvector_expressions.vector_binop_scalar_exprimportvector_binop_scalar_expr
fromvector_expressions.vector_binop_vector_exprimportvector_binop_vector_expr
fromvector_expressions.vector_exprimportvector_expr
fromvector_expressions.vector_here_exprimportvector_here_expr
fromvector_expressions.vector_var_const_vectorimportvector_var_const_vector
fromvector_expressions.vector_var_exprimportvector_var_expr
importmcfunction
tokens=scriptlex.tokens
precedence= (
('left', 'PLUS','MINUS'),
('left', 'TIMES','DIVIDE','MOD'),
('left', 'POWER'),
('right','UMINUS'),
('left', 'VECTOR'),
('left', 'COMP'),
)
#### Parsed
defp_parsed_assignment(p):
'''parsed : program'''
p[0] = ('program', p[1])
defp_parsed_lib(p):
'''parsed : import lib'''
p[0] = ('lib', p[2])
defp_parsed_expr(p):
'''parsed : expr'''
p[0] = ('expr', p[1])
#### Program
defp_program(p):
'''program : optcomments dir string newlines optdesc file_params top_level_blocks'''
p[0] = {}
p[0]['dir'] =p[3]
p[0]['desc'] =p[5]
forparam_nameinp[6]:
p[0][param_name] =p[6][param_name]
p[0]['lines'] =p[7]
### Lib
defp_lib(p):
'''lib : top_level_blocks'''
p[0] = {}
p[0]['lines'] =p[1]
#### Optdesc
defp_optdesc(p):
'''optdesc : desc string newlines
| empty'''
iflen(p) <4:
p[0] ='No Description'
else:
p[0] =p[2]
#### File Params
defp_file_params(p):
'''file_params : file_param file_params'''
param_name, param_value=p[1]
p[2][param_name] =param_value
p[0] =p[2]
defp_file_params_none(p):
'''file_params : empty'''
p[0] = {}
#### File Param
defp_file_param(p):
'''file_param : ID integer newlines'''
ifp[1] notin ['scale']:
raiseSyntaxError(f'Unknown file parameter: "{p[1]}" at line {p.lineno(1)}')
p[0] = (p[1], int(p[2]))
#### Sections
defp_section_commented(p):
'''section_commented : optcomments section'''
# TODO: Put the comments into the sections
p[0] =p[2]
defp_optcomments(p):
'''optcomments : COMMENT optnewlines optcomments'''
p[0] = [p[1]] +p[3]
defp_optcomments_empty(p):
'''optcomments : empty'''
p[0] = []
defp_section(p):
'''section : clocksection
| functionsection
| resetsection
| macrosection
| template_function_section'''
p[0] =p[1]
#### Reset Section
defp_resetsection(p):
'''resetsection : reset newlines blocklist end'''
p[0] =reset_section(p.lineno(1), p[3])
defvalidate_mcfunction_name(str):
forchinstr:
ifch.isupper():
print(f'"{str}" is not a valid mcfunction name.')
raiseSyntaxError()
#### Clock Section
defp_clocksection(p):
'''clocksection : clock ID newlines blocklist end'''
validate_mcfunction_name(p[2])
p[0] =clock_section(p.lineno(1), p[2], p[4])
#### Function Section
defp_functionsection(p):
'''functionsection : function FUNCTIONID id_list RPAREN newlines blocklist end'''
validate_mcfunction_name(p[2])
p[0] =function_section(p.lineno(1), p[2], p[3], p[6])
#### Template Function Section
defp_template_function_section(p):
'''template_function_section : function ID LCURLY macro_params RCURLY LPAREN id_list RPAREN newlines blocklist end'''
validate_mcfunction_name(p[2])
p[0] =template_function_section(p.lineno(1), p[2], p[4], p[7], p[10])
#### Macro section
defp_macrosection(p):
'''macrosection : macro DOLLAR FUNCTIONID macro_args newlines blocklist end'''
p[0] =macro_section(p.lineno(1), p[3], p[4], p[6])
defp_macro_args(p):
'''macro_args : macro_params RPAREN'''
p[0] =p[1]
defp_macro_args_empty(p):
'''macro_args : empty'''
p[0] = []
defp_macro_params(p):
'''macro_params : DOLLAR ID COMMA macro_params'''
p[0] = [p[2]] +p[4]
defp_macro_params_one(p):
'''macro_params : DOLLAR ID'''
p[0] = [p[2]]
defp_macro_params_empty(p):
'''macro_params : empty'''
p[0] = []
#### Macro decorators
#### With macro items list
defp_with_macro_item_expr(p):
'''with_macro_item : DOLLAR LPAREN ID RPAREN EQUALS expr newlines'''
p[0] = ('expr', p[3], p[6])
defp_with_macro_item_str(p):
'''with_macro_item : DOLLAR LPAREN ID RPAREN EQUALS NORMSTRING newlines'''
p[0] = ('string', p[3], p[6])
defp_with_macro_items_one(p):
'''with_macro_items : with_macro_item'''
p[0] = [p[1]]
defp_with_macro_items(p):
'''with_macro_items : with_macro_item with_macro_items'''
p[0] = [p[1]] +p[2]
defp_with_macro_items_prefix(p):
'''with_macro_items_prefix : with newlines with_macro_items'''
p[0] =with_items(p.lineno(1), p[3])
#### With macros suffix decorator
defp_opt_with_macros_true(p):
'''opt_with_macros : with macros'''
p[0] =with_items(p.lineno(1), [])
defp_opt_with_macros_false(p):
'''opt_with_macros : empty'''
p[0] =None
#### Expression list
defp_exprlist_multiple(p):
'''exprlist : exprlist COMMA expr'''
p[0] =p[1]
p[0].append(p[3])
defp_exprlist_single(p):
'''exprlist : expr'''
p[0] = [p[1]]
defp_exprlist_empty(p):
'''exprlist : empty'''
p[0] = []
#### With Anonymouse
defp_with_anonymous_block(p):
'''with_anonymous_block : with_macro_items_prefix do newlines blocklist end'''
p[0] =with_anonymous_block(p.lineno(1), p[1], p[4])
#### Function call
defp_function_call_namespace(p):
'''function_call : ID COLON FUNCTIONID exprlist RPAREN opt_with_macros'''
p[0] =function_call_block(p.lineno(1), p[1]+p[2]+p[3], p[4], p[6])
defp_function_call(p):
'''function_call : FUNCTIONID exprlist RPAREN opt_with_macros'''
p[0] =function_call_block(p.lineno(1), p[1], p[2], p[4])
defp_function_call_block(p):
'''function_call_block : function_call'''
p[0] =p[1]
defp_function_call_block_with_macro_items(p):
'''function_call_block : with_macro_items_prefix function_call'''
p[2].with_macro_items=p[1]
p[0] =p[2]
#### Method call
defp_method_call(p):
'''method_call : fullselector DOT FUNCTIONID exprlist RPAREN opt_with_macros'''
p[0] =method_call_block(p.lineno(1), p[1], p[3], p[4], p[6])
defp_method_call_block(p):
'''method_call_block : method_call'''
p[0] =p[1]
defp_method_call_block_with_macro_items(p):
'''method_call_block : with_macro_items_prefix method_call'''
p[2].with_macro_items=p[1]
p[0] =p[2]
#### Template Function Call
defp_template_function_call(p):
'''template_function_call : ID LCURLY macro_call_params RCURLY LPAREN exprlist RPAREN opt_with_macros'''
p[0] =template_function_call_block(p.lineno(1), p[1], p[3], p[6], p[8])
defp_template_function_call_block(p):
'''template_function_call_block : template_function_call'''
p[0] =p[1]
defp_template_function_call_block_with_macro_items(p):
'''template_function_call_block : with_macro_items_prefix template_function_call'''
p[2].with_macro_items=p[1]
p[0] =p[2]
#### Macro call
defp_macro_call(p):
'''macro_call : DOLLAR FUNCTIONID macro_call_args'''
p[0] =macro_call_block(p.lineno(1), p[2], p[3])
defp_macro_call_args(p):
'''macro_call_args : macro_call_params RPAREN'''
p[0] =p[1]
defp_macro_call_args_empty(p):
'''macro_call_args : empty'''
p[0] = []
defp_macro_call_params(p):
'''macro_call_params : macro_call_param COMMA macro_call_params'''
p[0] = [p[1]] +p[3]
defp_macro_call_params_one(p):
'''macro_call_params : macro_call_param'''
p[0] = [p[1]]
defp_macro_call_params_empty(p):
'''macro_call_params : empty'''
p[0] = []
defp_macro_call_param_number(p):
'''macro_call_param : const_value'''
p[0] =p[1]
#### Newlines
defp_newlines(p):
'''newlines : NEWLINE newlines
| NEWLINE'''
p[0] =None
defp_optnewlines(p):
'''optnewlines : newlines
| empty'''
#### Identifier List
defp_id_list(p):
'''id_list : ID COMMA id_list'''
p[0] = [p[1]] +p[3]
defp_id_list_one(p):
'''id_list : ID'''
p[0] = [p[1]]
defp_id_list_empty(p):
'''id_list : empty'''
p[0] = []
#### Top Level Blocks
defp_top_level_blocks(p):
'''top_level_blocks : pythonassignment newlines top_level_blocks
| python_tuple_assignment newlines top_level_blocks
| selector_assignment newlines top_level_blocks
| selector_define_block newlines top_level_blocks
| block_define_block newlines top_level_blocks
| blocktag newlines top_level_blocks
| entitytag newlines top_level_blocks
| itemtag newlines top_level_blocks
| array_definition newlines top_level_blocks
| import_statement newlines top_level_blocks
| python_import_statement newlines top_level_blocks
| print_block newlines top_level_blocks
| pointer_decl newlines top_level_blocks
| shaped_recipe newlines top_level_blocks
| advancement_definition newlines top_level_blocks
| loot_table_definition newlines top_level_blocks
| predicate_definition newlines top_level_blocks
| item_modifier_definition newlines top_level_blocks
| section_commented newlines top_level_blocks'''
p[0] = [p[1]] +p[3]
defp_top_level_blocks_comment(p):
'''top_level_blocks : COMMENT newlines top_level_blocks'''
p[0] =p[3]
defp_top_level_blocks_empty(p):
'''top_level_blocks : empty'''
p[0] = []
#### Import
defp_import_statement(p):
'''import_statement : import ID'''
p[0] =import_block(p.lineno(1), p[2])
#### Import
defp_python_import_statement(p):
'''python_import_statement : import ID DOT ID'''
ifp[4] =='py':
p[0] =python_import_block(p.lineno(1), p[2])
elifp[4] =='cblib':
p[0] =import_block(p.lineno(1), p[2])
else:
raiseSyntaxError(f'Unknown import file type: "{p[2]}.{p[4]}"')
#### Variable
defp_variable_selector(p):
'''variable : fullselector DOT ID
| ID DOT ID'''
p[0] =scoreboard_var(p[1], p[3])
defp_variable_global(p):
'''variable : ID'''
ifp[1] =='scale':
p[0] =scale_var()
else:
p[0] =scoreboard_var('Global', p[1])
defp_variable_array_const(p):
'''variable : ID LBRACK virtualinteger RBRACK'''
p[0] =array_const_var('Global', p[1], p[3])
defp_variable_array_expr(p):
'''variable : ID LBRACK expr RBRACK'''
p[0] =array_expr_var('Global', p[1], p[3])
defp_variable_array_const_selector(p):
'''variable : fullselector DOT ID LBRACK virtualinteger RBRACK'''
p[0] =array_const_var(p[1], p[3], p[5])
defp_variable_array_expr_selector(p):
'''variable : fullselector DOT ID LBRACK expr RBRACK'''
p[0] =array_expr_var(p[1], p[3], p[5])
defp_variable_block_path_nocoords(p):
'''variable : LBRACK ID RBRACK DOT ID'''
p[0] =block_path_var(p[2], p[5], None, [])
defp_variable_block_path_coords(p):
'''variable : LBRACK ID at relcoords RBRACK DOT ID'''
p[0] =block_path_var(p[2], p[7], p[4], [])
defp_variable_block_macro_path_nocoords(p):
'''variable : LBRACK ID RBRACK DOT ID LBRACK macro_call_params RBRACK'''
p[0] =block_path_var(p[2], p[5], None, p[7])
defp_variable_block_macro_path_coords(p):
'''variable : LBRACK ID at relcoords RBRACK DOT ID LBRACK macro_call_params RBRACK'''
p[0] =block_path_var(p[2], p[7], p[4], p[9])
defp_variable_virtualint(p):
'''variable : virtualinteger'''
p[0] =virtualint_var(p[1])
defp_variable_selector_ref(p):
'''variable : REF fullselector'''
p[0] =selector_id_var(p[2])
defp_variable_command(p):
'''variable : success NEWLINE COMMAND
| result NEWLINE COMMAND'''
p[0] =command_var(p[1], p[3])
defp_variable_storage(p):
'''variable : COLON data_path'''
p[0] =storage_path_var(None, p[2])
defp_variable_storage_target(p):
'''variable : ID COLON data_path'''
p[0] =storage_path_var(p[1], p[3])
#### Blocklist
defp_optcomment(p):
'''optcomment : COMMENT'''
p[0] = [comment_block(p.lineno(1), p[1])]
defp_optcomment_empty(p):
'''optcomment : empty'''
p[0] = []
defp_blocklist_multiple(p):
'''blocklist : codeblock optcomment newlines blocklist'''
p[0] =p[2] + [p[1]] +p[4]
defp_blocklist_empty(p):
'''blocklist : empty'''
p[0] = []
#### Block
defp_block_comment(p):
'''codeblock : COMMENT'''
p[0] =comment_block(p.lineno(1), p[1])
defp_block_command(p):
'''codeblock : COMMAND'''
p[0] =command_block(p.lineno(1), p[1])
defp_block_move(p):
'''codeblock : move fullselector relcoords'''
p[0] =move_block(p.lineno(1), p[2], p[3])
defp_block_for(p):
'''codeblock : for DOLLAR ID in const_value newlines blocklist end'''
p[0] =python_for_block(p.lineno(1), [p[3]], p[5], p[7])
defp_block_for_tuple(p):
'''codeblock : for python_tuple in const_value newlines blocklist end'''
p[0] =python_for_block(p.lineno(1), p[2], p[4], p[6])
defp_block_print(p):
'''codeblock : print_block'''
p[0] =p[1]
defp_print_block(p):
'''print_block : PRINT const_value RPAREN'''
p[0] =print_block(p.lineno(1), p[2])
#### Execute
defp_else_list_empty(p):
'''else_list : empty'''
p[0] = []
defp_else_list(p):
'''else_list : else_item else_list'''
p[0] = [p[1]] +p[2]
defp_else_item_execute(p):
'''else_item : else execute_items newlines blocklist'''
p[0] = (p[2], p[4])
defp_else_item_empty(p):
'''else_item : else newlines blocklist'''
p[0] = (None, p[3])
defp_block_if_command(p):
'''codeblock : if const_value newlines blocklist else_list end'''
p[0] =python_if_block(p.lineno(1), p[2], p[4], p[5])
defp_execute_as_id_global(p):
'''codeblock : as variable newlines blocklist else_list end'''
p[0] =execute_block(p.lineno(1), [('AsId', (p[2], None))], p[4], p[5])
defp_execute_as_id_type_global(p):
'''codeblock : as variable LPAREN ATID RPAREN newlines blocklist else_list end'''
p[0] =execute_block(p.lineno(1), [('AsId', (p[2], p[4]))], p[7], p[8])
defp_execute_as_id_do_global(p):
'''codeblock : as variable do codeblock else_list'''
p[0] =execute_block(p.lineno(1), [('AsId', (p[2], None))], [p[4]])
defp_execute_as_id_do_type_global(p):
'''codeblock : as variable LPAREN ATID RPAREN do codeblock'''
p[0] =execute_block(p.lineno(1), [('AsId', (p[2], p[4]))], [p[7]])
defp_execute_as_create(p):
'''codeblock : as create_block newlines blocklist else_list end'''
p[0] =execute_block(p.lineno(1), [('AsCreate', p[2])], p[4])
defp_execute_as_create_do(p):
'''codeblock : as create_block do codeblock'''
p[0] =execute_block(p.lineno(1), [('AsCreate', p[2])], [p[4]])
defp_execute_chain(p):
'''codeblock : execute_items newlines blocklist else_list end'''
p[0] =execute_block(p.lineno(1), p[1], p[3], p[4])
defp_execute_chain_inline(p):
'''codeblock : execute_items do codeblock
| execute_items then codeblock'''
p[0] =execute_block(p.lineno(1), p[1], [p[3]])
#### Execute Items
defp_execute_items_one(p):
'''execute_items : execute_item'''
p[0] = [p[1]]
defp_execute_items(p):
'''execute_items : execute_item execute_items'''
p[0] = [p[1]] +p[2]
defp_execute_if_condition(p):
'''execute_item : if conditions'''
p[0] = ('If', p[2])
defp_execute_unless_condition(p):
'''execute_item : unless conditions'''
p[0] = ('Unless', p[2])
defp_execute_as(p):
'''execute_item : as fullselector'''
p[0] = ('As', p[2])
defp_execute_on(p):
'''execute_item : on ID'''
ifp[2] notin [
'attacker',
'controller',
'leasher',
'origin',
'owner',
'passengers',
'target',
'vehicle',
]:
print(f'Warning: Unknown argument for execute on: "{p[2]}" at line {p.lineno(2)}')
p[0] = ('On', p[2])
defp_execute_rotated(p):
'''execute_item : rotated fullselector'''
p[0] = ('Rotated', p[2])
defp_execute_facing_coords(p):
'''execute_item : facing relcoords'''
p[0] = ('FacingCoords', p[2])
defp_execute_facing_entity(p):
'''execute_item : facing fullselector'''
p[0] = ('FacingEntity', p[2])
defp_execute_align(p):
'''execute_item : align ID'''
ifp[2] notin ['x','y','z','xy','xz','yz','xyz']:
raiseSyntaxError(f'Must align to a combination of x, y, and z axes, not "{p[2]}", at line {p.lineno(2)}')
p[0] = ('Align', p[2])
defp_opt_anchor(p):
'''opt_anchor : eyes
| feet'''
p[0] =p[1]
defp_opt_anchor_empty(p):
'''opt_anchor : empty'''
p[0] =None
defp_execute_at_selector(p):
'''execute_item : at fullselector opt_anchor'''
p[0] = ('At', (p[2], None, p[3]))
defp_execute_at_relcoords(p):
'''execute_item : at opt_anchor relcoords'''
p[0] = ('At', (None, p[3], p[2]))
defp_execute_at_selector_relcoords(p):
'''execute_item : at fullselector opt_anchor relcoords'''
p[0] = ('At', (p[2], p[4], p[3]))
defp_execute_at_vector(p):
'''execute_item : at vector_expr'''
p[0] = ('AtVector', (None, p[2]))
defp_execute_at_vector_scale(p):
'''execute_item : at LPAREN const_value RPAREN vector_expr'''
p[0] = ('AtVector', (p[3], p[5]))
defp_execute_in_dimension(p):
'''execute_item : in overworld
| in the_end
| in the_nether'''
p[0] = ('In', p[2])
#### For selector
defp_for_selector(p):
'''codeblock : for ATID in fullselector newlines blocklist end'''
p[0] =for_selector_block(p.lineno(1), p[2], p[4], p[6])
#### Conditions
defp_conditions_one(p):
'''conditions : condition'''
p[0] = [p[1]]
defp_conditions(p):
'''conditions : condition and conditions'''
p[0] = [p[1]] +p[3]
defp_condition_bool_predicate(p):
'''condition : predicate ID'''
p[0] = ('predicate', p[2])
defp_condition_fullselector(p):
'''condition : fullselector'''
p[0] = ('selector', p[1])
defp_condition_score(p):
'''condition : expr LT expr
| expr LEQ expr
| expr EQUALEQUAL expr
| expr GT expr
| expr GEQ expr'''
op=p[2]
ifop=='==':
op='='
p[0] = ('score', (p[1], op, p[3]))
defp_condition_vector_equality(p):
'''condition : vector_var EQUALEQUAL vector_var'''
p[0] = ('vector_equality', (p[1], p[3]))
defp_condition_bool(p):
'''condition : expr'''
p[0] = ('score', (p[1], '>', virtualint_var('0')))
defp_condition_not_bool(p):
'''condition : not expr'''
p[0] = ('score', (p[2], '<=', virtualint_var(0)))
defp_opt_block_state(p):
'''opt_block_state : LBRACK block_states RBRACK'''
p[0] =p[1] +p[2] +p[3]
defp_opt_block_state_none(p):
'''opt_block_state : empty'''
p[0] =''
defp_block_states_one(p):
'''block_states : ID EQUALS ID
| ID EQUALS virtualinteger
| facing EQUALS ID
| facing EQUALS virtualinteger'''
p[0] =p[1] +p[2] +p[3]
defp_block_states_list(p):
'''block_states : ID EQUALS ID COMMA block_states
| ID EQUALS virtualinteger COMMA block_states
| facing EQUALS ID COMMA block_states
| facing EQUALS virtualinteger COMMA block_states'''
p[0] =p[1] +p[2] +p[3] +p[4] +p[5]
defp_opt_tile_data(p):
'''opt_tile_data : json_object'''
p[0] =p[1]
defp_opt_tile_data_empty(p):
'''opt_tile_data : empty'''
p[0] =''
defp_condition_block(p):
'''condition : block relcoords ID opt_block_state opt_tile_data'''
p[0] = ('block', (p[2], p[3] +p[4] +p[5]))
defp_condition_block_virtual(p):
'''condition : block relcoords DOLLAR ID opt_block_state opt_tile_data'''
p[0] = ('block', (p[2], p[3]+p[4]+p[5]+p[6]))
defp_condition_block_nocoords(p):
'''condition : block ID opt_block_state opt_tile_data'''
p[0] = ('block', (relcoords(), p[2] +p[3] +p[4]))
defp_condition_block_virtual_nocoords(p):
'''condition : block DOLLAR ID opt_block_state opt_tile_data'''
p[0] = ('block', (relcoords(), p[2]+p[3]+p[4]+p[5]))
defp_condition_nbt_path(p):
'''condition : nbt_object
| nbt_list'''
p[0] = ('nbt_path', p[1])
#### If python
defp_block_ifelse_command(p):
'''codeblock : if const_value newlines blocklist else newlines blocklist end'''
p[0] =python_if_block(p.lineno(1), p[2], p[4], p[7])
#### While
defp_block_while(p):
'''codeblock : while conditions newlines blocklist end'''
p[0] =while_block(p.lineno(1), [('If', p[2])], p[4])
defp_block_while_execute(p):
'''codeblock : while conditions execute_items newlines blocklist end'''
p[0] =while_block(p.lineno(1), [('If', p[2])] +p[3], p[5])
#### For
defp_block_for_index_by(p):
'''codeblock : for variable EQUALS expr to expr by expr newlines blocklist end'''
p[0] =for_index_block(p.lineno(1), p[2], p[4], p[6], p[8], p[10])
defp_block_for_index(p):
'''codeblock : for variable EQUALS expr to expr newlines blocklist end'''
p[0] =for_index_block(p.lineno(1), p[2], p[4], p[6], None, p[8])
#### Switch
defp_switch(p):
'''codeblock : switch expr newlines cases end'''
p[0] =switch_block(p.lineno(1), p[2], p[4])
defp_cases_one(p):
'''cases : switch_case newlines'''
p[0] = [p[1]]
defp_cases_multiple(p):
'''cases : switch_case newlines cases'''
p[0] = [p[1]] +p[3]
defp_switch_case_single(p):
'''switch_case : case const_value newlines blocklist end'''
p[0] = ('range', (p[2], p[2], p[4]), p.lineno(1))
defp_switch_case_range(p):
'''switch_case : case const_value to const_value newlines blocklist end'''
p[0] = ('range', (p[2], p[4], p[6]), p.lineno(1))
defp_switch_case_python(p):
'''switch_case : case DOLLAR ID in const_value newlines blocklist end'''
p[0] = ('python', (p[3], p[5], p[7]), p.lineno(1))
#### Block Switch
defp_block_switch(p):
'''codeblock : switch block opt_coords newlines block_cases end'''
p[0] =block_switch_block(p.lineno(1), p[3], p[5], False)
defp_block_id_switch(p):
'''codeblock : switch block expr newlines block_cases end'''
p[0] =block_id_switch_block(p.lineno(1), p[3], p[5], False)
defp_block_data_switch(p):
'''codeblock : switch block_data opt_coords newlines block_cases end'''
p[0] =block_switch_block(p.lineno(1), p[3], p[5], True)
defp_block_data_id_switch(p):
'''codeblock : switch block_data expr newlines block_cases end'''
p[0] =block_id_switch_block(p.lineno(1), p[3], p[5], True)
defp_opt_coords(p):
'''opt_coords : at relcoords'''
p[0] =p[2]
defp_opt_coords_empty(p):
'''opt_coords : empty'''
p[0] =relcoords()
defp_block_cases_empty(p):
'''block_cases : empty'''
p[0] = []
defp_block_cases_multi(p):
'''block_cases : block_case newlines block_cases'''
p[0] = [p[1]] +p[3]
defp_block_case_default(p):
'''block_case : default newlines blocklist end'''
p[0] =block_case(None, None, p[3], True)
defp_block_case_specific(p):
'''block_case : case ID opt_block_properties newlines blocklist end
| case TIMES opt_block_properties newlines blocklist end'''
p[0] =block_case(p[2], p[3], p[5], False)
defp_opt_block_properties_empty(p):
'''opt_block_properties : empty'''
p[0] = []
defp_opt_block_properties(p):
'''opt_block_properties : LBRACK block_properties RBRACK'''
p[0] =p[2]
defp_block_property(p):
'''block_property : ID EQUALS ID
| ID EQUALS virtualinteger
| facing EQUALS ID
| facing EQUALS virtualinteger'''
p[0] = (p[1], p[3])
defp_block_properties_one(p):
'''block_properties : block_property'''
p[0] = [p[1]]
defp_block_properties_multi(p):
'''block_properties : block_property COMMA block_properties'''
p[0] = [p[1]] +p[3]
#### Tell/Title
defp_block_tell(p):
'''codeblock : tell fullselector string'''
p[0] =tell_block(p.lineno(1), p[2], p[3])
defp_block_title(p):
'''codeblock : title fullselector string
| subtitle fullselector string
| actionbar fullselector string'''
p[0] =title_block(p.lineno(1), p[1], p[2], None, p[3])
defp_block_title_times(p):
'''codeblock : title fullselector const_value const_value const_value string
| subtitle fullselector const_value const_value const_value string
| actionbar fullselector const_value const_value const_value string'''
p[0] =title_block(p.lineno(1), p[1], p[2], (p[3], p[4], p[5]), p[6])
defp_block_selector_assignment(p):
'''codeblock : selector_assignment
| selector_define_block'''
p[0] =p[1]
defp_block_with_anonymous_block(p):
'''codeblock : with_anonymous_block'''
p[0] =p[1]
defp_block_function_call(p):
'''codeblock : function_call_block
| method_call_block
| template_function_call_block
| macro_call
| pythonassignment
| python_tuple_assignment'''
p[0] =p[1]
#### Push/pop
defp_block_push(p):
'''codeblock : push exprlist'''
p[0] =push_block(p.lineno(1), p[2])
defp_var_list_one(p):
'''var_list : variable'''
p[0] = [p[1]]
defp_var_list_empty(p):
'''var_list : empty'''
p[0] = []
defp_var_list(p):
'''var_list : variable COMMA var_list'''
p[0] = [p[1]] +p[3]
defp_block_pop(p):
'''codeblock : pop var_list'''
p[0] =pop_block(p.lineno(1), p[2])
#### String
defp_string(p):
'''string : NORMSTRING'''
p[0] =p[1][1:-1]
#### Number
defp_integer(p):
'''integer : DECIMAL
| HEX
| BINARY'''
p[0] =p[1]
defp_integer_minus(p):
'''integer : MINUS DECIMAL
| MINUS HEX
| MINUS BINARY'''
p[0] =str(-int(p[2]))
defp_number_integer(p):
'''number : integer
| float_val'''
p[0] =p[1]
defp_float_val(p):
'''float_val : FLOAT'''
p[0] =p[1]
defp_float_val_minus(p):
'''float_val : MINUS FLOAT'''
p[0] =str(-float(p[2]))
#### Qualifier list
defp_qualifiers_multiple(p):