-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathscriptparse.py
More file actions
1870 lines (1472 loc) · 52.8 KB
/
scriptparse.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
from ply import *
import traceback
import scriptlex
from block_types.advancement_definition_block import advancement_definition_block
from block_types.array_definition_block import array_definition_block
from block_types.block_definition_block import block_definition_block
from block_types.block_switch_block import block_switch_block
from block_types.block_id_switch_block import block_id_switch_block
from block_types.block_tag_block import block_tag_block
from block_types.clock_section import clock_section
from block_types.command_block import command_block
from block_types.comment_block import comment_block
from block_types.create_block import create_block
from block_types.define_name_block import define_name_block
from block_types.entity_tag_block import entity_tag_block
from block_types.execute_block import execute_block
from block_types.for_index_block import for_index_block
from block_types.for_selector_block import for_selector_block
from block_types.function_call_block import function_call_block
from block_types.function_section import function_section
from block_types.import_block import import_block
from block_types.item_tag_block import item_tag_block
from block_types.loot_table_definition_block import loot_table_definition_block
from block_types.macro_call_block import macro_call_block
from block_types.macro_section import macro_section
from block_types.method_call_block import method_call_block
from block_types.move_block import move_block
from block_types.nbt_data_block import nbt_data_block
from block_types.nbt_remove_block import nbt_remove_block
from block_types.pop_block import pop_block
from block_types.pointer_decl_block import pointer_decl_block
from block_types.predicate_definition_block import predicate_definition_block
from block_types.item_modifier_definition_block import item_modifier_definition_block
from block_types.print_block import print_block
from block_types.push_block import push_block
from block_types.python_assignment_block import python_assignment_block
from block_types.python_for_block import python_for_block
from block_types.python_if_block import python_if_block
from block_types.python_import_block import python_import_block
from block_types.python_tuple_assignment_block import python_tuple_assignment_block
from block_types.reset_section import reset_section
from block_types.scoreboard_assignment_block import scoreboard_assignment_block
from block_types.selector_assignment_block import selector_assignment_block
from block_types.selector_definition_block import selector_definition_block
from block_types.shaped_recipe_block import shaped_recipe_block
from block_types.switch_block import switch_block
from block_types.tell_block import tell_block
from block_types.template_function_call_block import template_function_call_block
from block_types.template_function_section import template_function_section
from block_types.title_block import title_block
from block_types.vector_assignment_block import vector_assignment_block
from block_types.vector_assignment_scalar_block import vector_assignment_scalar_block
from block_types.while_block import while_block
from block_types.with_anonymous_block import with_anonymous_block
from block_types.with_items import with_items
from data_types.block_case import block_case
from data_types.const_number import const_number
from data_types.const_string import const_string
from data_types.number_macro_path import number_macro_path
from data_types.python_identifier import python_identifier
from data_types.interpreted_python import interpreted_python
from data_types.relcoord_vector import relcoord_vector
from data_types.relcoord import relcoord
from data_types.relcoords import relcoords
from nbt_types.block_nbt_path import block_nbt_path
from nbt_types.entity_nbt_path import entity_nbt_path
from nbt_types.nbt_json import nbt_json
from nbt_types.storage_nbt_path import storage_nbt_path
from scalar_expressions.binop_expr import binop_expr
from scalar_expressions.create_expr import create_expr
from scalar_expressions.dot_expr import dot_expr
from scalar_expressions.func_expr import func_expr
from scalar_expressions.method_expr import method_expr
from scalar_expressions.unary_expr import unary_expr
from variable_types.array_const_var import array_const_var
from variable_types.array_expr_var import array_expr_var
from variable_types.block_path_var import block_path_var
from variable_types.command_var import command_var
from variable_types.scale_var import scale_var
from variable_types.scoreboard_var import scoreboard_var
from variable_types.selector_id_var import selector_id_var
from variable_types.storage_path_var import storage_path_var
from variable_types.virtualint_var import virtualint_var
from vector_expressions.sel_vector_var_expr import sel_vector_var_expr
from vector_expressions.vector_binop_scalar_expr import vector_binop_scalar_expr
from vector_expressions.vector_binop_vector_expr import vector_binop_vector_expr
from vector_expressions.vector_expr import vector_expr
from vector_expressions.vector_here_expr import vector_here_expr
from vector_expressions.vector_var_const_vector import vector_var_const_vector
from vector_expressions.vector_var_expr import vector_var_expr
import mcfunction
tokens = scriptlex.tokens
precedence = (
('left', 'PLUS','MINUS'),
('left', 'TIMES','DIVIDE','MOD'),
('left', 'POWER'),
('right','UMINUS'),
('left', 'VECTOR'),
('left', 'COMP'),
)
#### Parsed
def p_parsed_assignment(p):
'''parsed : program'''
p[0] = ('program', p[1])
def p_parsed_lib(p):
'''parsed : import lib'''
p[0] = ('lib', p[2])
def p_parsed_expr(p):
'''parsed : expr'''
p[0] = ('expr', p[1])
#### Program
def p_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]
for param_name in p[6]:
p[0][param_name] = p[6][param_name]
p[0]['lines'] = p[7]
### Lib
def p_lib(p):
'''lib : top_level_blocks'''
p[0] = {}
p[0]['lines'] = p[1]
#### Optdesc
def p_optdesc(p):
'''optdesc : desc string newlines
| empty'''
if len(p) < 4:
p[0] = 'No Description'
else:
p[0] = p[2]
#### File Params
def p_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]
def p_file_params_none(p):
'''file_params : empty'''
p[0] = {}
#### File Param
def p_file_param(p):
'''file_param : ID integer newlines'''
if p[1] not in ['scale']:
raise SyntaxError(f'Unknown file parameter: "{p[1]}" at line {p.lineno(1)}')
p[0] = (p[1], int(p[2]))
#### Sections
def p_section_commented(p):
'''section_commented : optcomments section'''
# TODO: Put the comments into the sections
p[0] = p[2]
def p_optcomments(p):
'''optcomments : COMMENT optnewlines optcomments'''
p[0] = [p[1]] + p[3]
def p_optcomments_empty(p):
'''optcomments : empty'''
p[0] = []
def p_section(p):
'''section : clocksection
| functionsection
| resetsection
| macrosection
| template_function_section'''
p[0] = p[1]
#### Reset Section
def p_resetsection(p):
'''resetsection : reset newlines blocklist end'''
p[0] = reset_section(p.lineno(1), p[3])
def validate_mcfunction_name(str):
for ch in str:
if ch.isupper():
print(f'"{str}" is not a valid mcfunction name.')
raise SyntaxError()
#### Clock Section
def p_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
def p_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
def p_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
def p_macrosection(p):
'''macrosection : macro DOLLAR FUNCTIONID macro_args newlines blocklist end'''
p[0] = macro_section(p.lineno(1), p[3], p[4], p[6])
def p_macro_args(p):
'''macro_args : macro_params RPAREN'''
p[0] = p[1]
def p_macro_args_empty(p):
'''macro_args : empty'''
p[0] = []
def p_macro_params(p):
'''macro_params : DOLLAR ID COMMA macro_params'''
p[0] = [p[2]] + p[4]
def p_macro_params_one(p):
'''macro_params : DOLLAR ID'''
p[0] = [p[2]]
def p_macro_params_empty(p):
'''macro_params : empty'''
p[0] = []
#### Macro decorators
#### With macro items list
def p_with_macro_item_expr(p):
'''with_macro_item : DOLLAR LPAREN ID RPAREN EQUALS expr newlines'''
p[0] = ('expr', p[3], p[6])
def p_with_macro_item_str(p):
'''with_macro_item : DOLLAR LPAREN ID RPAREN EQUALS NORMSTRING newlines'''
p[0] = ('string', p[3], p[6])
def p_with_macro_items_one(p):
'''with_macro_items : with_macro_item'''
p[0] = [p[1]]
def p_with_macro_items(p):
'''with_macro_items : with_macro_item with_macro_items'''
p[0] = [p[1]] + p[2]
def p_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
def p_opt_with_macros_true(p):
'''opt_with_macros : with macros'''
p[0] = with_items(p.lineno(1), [])
def p_opt_with_macros_false(p):
'''opt_with_macros : empty'''
p[0] = None
#### Expression list
def p_exprlist_multiple(p):
'''exprlist : exprlist COMMA expr'''
p[0] = p[1]
p[0].append(p[3])
def p_exprlist_single(p):
'''exprlist : expr'''
p[0] = [p[1]]
def p_exprlist_empty(p):
'''exprlist : empty'''
p[0] = []
#### With Anonymouse
def p_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
def p_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])
def p_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])
def p_function_call_block(p):
'''function_call_block : function_call'''
p[0] = p[1]
def p_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
def p_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])
def p_method_call_block(p):
'''method_call_block : method_call'''
p[0] = p[1]
def p_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
def p_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])
def p_template_function_call_block(p):
'''template_function_call_block : template_function_call'''
p[0] = p[1]
def p_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
def p_macro_call(p):
'''macro_call : DOLLAR FUNCTIONID macro_call_args'''
p[0] = macro_call_block(p.lineno(1), p[2], p[3])
def p_macro_call_args(p):
'''macro_call_args : macro_call_params RPAREN'''
p[0] = p[1]
def p_macro_call_args_empty(p):
'''macro_call_args : empty'''
p[0] = []
def p_macro_call_params(p):
'''macro_call_params : macro_call_param COMMA macro_call_params'''
p[0] = [p[1]] + p[3]
def p_macro_call_params_one(p):
'''macro_call_params : macro_call_param'''
p[0] = [p[1]]
def p_macro_call_params_empty(p):
'''macro_call_params : empty'''
p[0] = []
def p_macro_call_param_number(p):
'''macro_call_param : const_value'''
p[0] = p[1]
#### Newlines
def p_newlines(p):
'''newlines : NEWLINE newlines
| NEWLINE'''
p[0] = None
def p_optnewlines(p):
'''optnewlines : newlines
| empty'''
#### Identifier List
def p_id_list(p):
'''id_list : ID COMMA id_list'''
p[0] = [p[1]] + p[3]
def p_id_list_one(p):
'''id_list : ID'''
p[0] = [p[1]]
def p_id_list_empty(p):
'''id_list : empty'''
p[0] = []
#### Top Level Blocks
def p_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]
def p_top_level_blocks_comment(p):
'''top_level_blocks : COMMENT newlines top_level_blocks'''
p[0] = p[3]
def p_top_level_blocks_empty(p):
'''top_level_blocks : empty'''
p[0] = []
#### Import
def p_import_statement(p):
'''import_statement : import ID'''
p[0] = import_block(p.lineno(1), p[2])
#### Import
def p_python_import_statement(p):
'''python_import_statement : import ID DOT ID'''
if p[4] == 'py':
p[0] = python_import_block(p.lineno(1), p[2])
elif p[4] == 'cblib':
p[0] = import_block(p.lineno(1), p[2])
else:
raise SyntaxError(f'Unknown import file type: "{p[2]}.{p[4]}"')
#### Variable
def p_variable_selector(p):
'''variable : fullselector DOT ID
| ID DOT ID'''
p[0] = scoreboard_var(p[1], p[3])
def p_variable_global(p):
'''variable : ID'''
if p[1] == 'scale':
p[0] = scale_var()
else:
p[0] = scoreboard_var('Global', p[1])
def p_variable_array_const(p):
'''variable : ID LBRACK virtualinteger RBRACK'''
p[0] = array_const_var('Global', p[1], p[3])
def p_variable_array_expr(p):
'''variable : ID LBRACK expr RBRACK'''
p[0] = array_expr_var('Global', p[1], p[3])
def p_variable_array_const_selector(p):
'''variable : fullselector DOT ID LBRACK virtualinteger RBRACK'''
p[0] = array_const_var(p[1], p[3], p[5])
def p_variable_array_expr_selector(p):
'''variable : fullselector DOT ID LBRACK expr RBRACK'''
p[0] = array_expr_var(p[1], p[3], p[5])
def p_variable_block_path_nocoords(p):
'''variable : LBRACK ID RBRACK DOT ID'''
p[0] = block_path_var(p[2], p[5], None, [])
def p_variable_block_path_coords(p):
'''variable : LBRACK ID at relcoords RBRACK DOT ID'''
p[0] = block_path_var(p[2], p[7], p[4], [])
def p_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])
def p_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])
def p_variable_virtualint(p):
'''variable : virtualinteger'''
p[0] = virtualint_var(p[1])
def p_variable_selector_ref(p):
'''variable : REF fullselector'''
p[0] = selector_id_var(p[2])
def p_variable_command(p):
'''variable : success NEWLINE COMMAND
| result NEWLINE COMMAND'''
p[0] = command_var(p[1], p[3])
def p_variable_storage(p):
'''variable : COLON data_path'''
p[0] = storage_path_var(None, p[2])
def p_variable_storage_target(p):
'''variable : ID COLON data_path'''
p[0] = storage_path_var(p[1], p[3])
#### Blocklist
def p_optcomment(p):
'''optcomment : COMMENT'''
p[0] = [comment_block(p.lineno(1), p[1])]
def p_optcomment_empty(p):
'''optcomment : empty'''
p[0] = []
def p_blocklist_multiple(p):
'''blocklist : codeblock optcomment newlines blocklist'''
p[0] = p[2] + [p[1]] + p[4]
def p_blocklist_empty(p):
'''blocklist : empty'''
p[0] = []
#### Block
def p_block_comment(p):
'''codeblock : COMMENT'''
p[0] = comment_block(p.lineno(1), p[1])
def p_block_command(p):
'''codeblock : COMMAND'''
p[0] = command_block(p.lineno(1), p[1])
def p_block_move(p):
'''codeblock : move fullselector relcoords'''
p[0] = move_block(p.lineno(1), p[2], p[3])
def p_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])
def p_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])
def p_block_print(p):
'''codeblock : print_block'''
p[0] = p[1]
def p_print_block(p):
'''print_block : PRINT const_value RPAREN'''
p[0] = print_block(p.lineno(1), p[2])
#### Execute
def p_else_list_empty(p):
'''else_list : empty'''
p[0] = []
def p_else_list(p):
'''else_list : else_item else_list'''
p[0] = [p[1]] + p[2]
def p_else_item_execute(p):
'''else_item : else execute_items newlines blocklist'''
p[0] = (p[2], p[4])
def p_else_item_empty(p):
'''else_item : else newlines blocklist'''
p[0] = (None, p[3])
def p_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])
def p_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])
def p_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])
def p_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]])
def p_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]])
def p_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])
def p_execute_as_create_do(p):
'''codeblock : as create_block do codeblock'''
p[0] = execute_block(p.lineno(1), [('AsCreate', p[2])], [p[4]])
def p_execute_chain(p):
'''codeblock : execute_items newlines blocklist else_list end'''
p[0] = execute_block(p.lineno(1), p[1], p[3], p[4])
def p_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
def p_execute_items_one(p):
'''execute_items : execute_item'''
p[0] = [p[1]]
def p_execute_items(p):
'''execute_items : execute_item execute_items'''
p[0] = [p[1]] + p[2]
def p_execute_if_condition(p):
'''execute_item : if conditions'''
p[0] = ('If', p[2])
def p_execute_unless_condition(p):
'''execute_item : unless conditions'''
p[0] = ('Unless', p[2])
def p_execute_as(p):
'''execute_item : as fullselector'''
p[0] = ('As', p[2])
def p_execute_on(p):
'''execute_item : on ID'''
if p[2] not in [
'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])
def p_execute_rotated(p):
'''execute_item : rotated fullselector'''
p[0] = ('Rotated', p[2])
def p_execute_facing_coords(p):
'''execute_item : facing relcoords'''
p[0] = ('FacingCoords', p[2])
def p_execute_facing_entity(p):
'''execute_item : facing fullselector'''
p[0] = ('FacingEntity', p[2])
def p_execute_align(p):
'''execute_item : align ID'''
if p[2] not in ['x','y','z','xy','xz','yz','xyz']:
raise SyntaxError(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])
def p_opt_anchor(p):
'''opt_anchor : eyes
| feet'''
p[0] = p[1]
def p_opt_anchor_empty(p):
'''opt_anchor : empty'''
p[0] = None
def p_execute_at_selector(p):
'''execute_item : at fullselector opt_anchor'''
p[0] = ('At', (p[2], None, p[3]))
def p_execute_at_relcoords(p):
'''execute_item : at opt_anchor relcoords'''
p[0] = ('At', (None, p[3], p[2]))
def p_execute_at_selector_relcoords(p):
'''execute_item : at fullselector opt_anchor relcoords'''
p[0] = ('At', (p[2], p[4], p[3]))
def p_execute_at_vector(p):
'''execute_item : at vector_expr'''
p[0] = ('AtVector', (None, p[2]))
def p_execute_at_vector_scale(p):
'''execute_item : at LPAREN const_value RPAREN vector_expr'''
p[0] = ('AtVector', (p[3], p[5]))
def p_execute_in_dimension(p):
'''execute_item : in overworld
| in the_end
| in the_nether'''
p[0] = ('In', p[2])
#### For selector
def p_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
def p_conditions_one(p):
'''conditions : condition'''
p[0] = [p[1]]
def p_conditions(p):
'''conditions : condition and conditions'''
p[0] = [p[1]] + p[3]
def p_condition_bool_predicate(p):
'''condition : predicate ID'''
p[0] = ('predicate', p[2])
def p_condition_fullselector(p):
'''condition : fullselector'''
p[0] = ('selector', p[1])
def p_condition_score(p):
'''condition : expr LT expr
| expr LEQ expr
| expr EQUALEQUAL expr
| expr GT expr
| expr GEQ expr'''
op = p[2]
if op == '==':
op = '='
p[0] = ('score', (p[1], op, p[3]))
def p_condition_vector_equality(p):
'''condition : vector_var EQUALEQUAL vector_var'''
p[0] = ('vector_equality', (p[1], p[3]))
def p_condition_bool(p):
'''condition : expr'''
p[0] = ('score', (p[1], '>', virtualint_var('0')))
def p_condition_not_bool(p):
'''condition : not expr'''
p[0] = ('score', (p[2], '<=', virtualint_var(0)))
def p_opt_block_state(p):
'''opt_block_state : LBRACK block_states RBRACK'''
p[0] = p[1] + p[2] + p[3]
def p_opt_block_state_none(p):
'''opt_block_state : empty'''
p[0] = ''
def p_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]
def p_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]
def p_opt_tile_data(p):
'''opt_tile_data : json_object'''
p[0] = p[1]
def p_opt_tile_data_empty(p):
'''opt_tile_data : empty'''
p[0] = ''
def p_condition_block(p):
'''condition : block relcoords ID opt_block_state opt_tile_data'''
p[0] = ('block', (p[2], p[3] + p[4] + p[5]))
def p_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]))
def p_condition_block_nocoords(p):
'''condition : block ID opt_block_state opt_tile_data'''
p[0] = ('block', (relcoords(), p[2] + p[3] + p[4]))
def p_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]))
def p_condition_nbt_path(p):
'''condition : nbt_object
| nbt_list'''
p[0] = ('nbt_path', p[1])
#### If python
def p_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
def p_block_while(p):
'''codeblock : while conditions newlines blocklist end'''
p[0] = while_block(p.lineno(1), [('If', p[2])], p[4])
def p_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
def p_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])
def p_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
def p_switch(p):
'''codeblock : switch expr newlines cases end'''
p[0] = switch_block(p.lineno(1), p[2], p[4])
def p_cases_one(p):
'''cases : switch_case newlines'''
p[0] = [p[1]]
def p_cases_multiple(p):
'''cases : switch_case newlines cases'''
p[0] = [p[1]] + p[3]
def p_switch_case_single(p):
'''switch_case : case const_value newlines blocklist end'''
p[0] = ('range', (p[2], p[2], p[4]), p.lineno(1))
def p_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))
def p_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
def p_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)
def p_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)
def p_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)
def p_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)
def p_opt_coords(p):
'''opt_coords : at relcoords'''
p[0] = p[2]
def p_opt_coords_empty(p):
'''opt_coords : empty'''
p[0] = relcoords()
def p_block_cases_empty(p):
'''block_cases : empty'''
p[0] = []
def p_block_cases_multi(p):
'''block_cases : block_case newlines block_cases'''
p[0] = [p[1]] + p[3]
def p_block_case_default(p):
'''block_case : default newlines blocklist end'''
p[0] = block_case(None, None, p[3], True)
def p_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)
def p_opt_block_properties_empty(p):
'''opt_block_properties : empty'''
p[0] = []
def p_opt_block_properties(p):
'''opt_block_properties : LBRACK block_properties RBRACK'''
p[0] = p[2]
def p_block_property(p):
'''block_property : ID EQUALS ID
| ID EQUALS virtualinteger
| facing EQUALS ID
| facing EQUALS virtualinteger'''
p[0] = (p[1], p[3])
def p_block_properties_one(p):
'''block_properties : block_property'''
p[0] = [p[1]]
def p_block_properties_multi(p):
'''block_properties : block_property COMMA block_properties'''
p[0] = [p[1]] + p[3]
#### Tell/Title
def p_block_tell(p):
'''codeblock : tell fullselector string'''
p[0] = tell_block(p.lineno(1), p[2], p[3])
def p_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])
def p_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])
def p_block_selector_assignment(p):
'''codeblock : selector_assignment
| selector_define_block'''
p[0] = p[1]
def p_block_with_anonymous_block(p):
'''codeblock : with_anonymous_block'''
p[0] = p[1]
def p_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
def p_block_push(p):
'''codeblock : push exprlist'''
p[0] = push_block(p.lineno(1), p[2])
def p_var_list_one(p):
'''var_list : variable'''
p[0] = [p[1]]
def p_var_list_empty(p):
'''var_list : empty'''
p[0] = []
def p_var_list(p):
'''var_list : variable COMMA var_list'''
p[0] = [p[1]] + p[3]
def p_block_pop(p):
'''codeblock : pop var_list'''
p[0] = pop_block(p.lineno(1), p[2])
#### String
def p_string(p):
'''string : NORMSTRING'''
p[0] = p[1][1:-1]
#### Number
def p_integer(p):
'''integer : DECIMAL
| HEX
| BINARY'''
p[0] = p[1]
def p_integer_minus(p):
'''integer : MINUS DECIMAL
| MINUS HEX
| MINUS BINARY'''
p[0] = str(-int(p[2]))
def p_number_integer(p):
'''number : integer
| float_val'''
p[0] = p[1]
def p_float_val(p):
'''float_val : FLOAT'''
p[0] = p[1]
def p_float_val_minus(p):
'''float_val : MINUS FLOAT'''
p[0] = str(-float(p[2]))
#### Qualifier list
def p_qualifiers_multiple(p):