-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRules.pm
1399 lines (1104 loc) · 42.6 KB
/
Rules.pm
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
package Ferret::Lexer::Rules;
use warnings;
use strict;
use 5.010;
use Ferret::Lexer::RuleFunctions;
use Ferret::Lexer::RuleSet;
use List::Util qw(any);
our %token_rules = (
KEYWORD_END => [
# the current node must be somewhere inside a Document or Class.
upper_nodes_must_have => [ # KEYWORD_END[0]
'Document Class',
'End keyword must terminate a class or package declaration',
0
],
# $c->{end_cap} (the class or package to capture 'end') must exist
current_must_have => [ # KEYWORD_END[1]
'end_cap',
'Class or package must capture end keyword',
1
],
# the current node at this time must be a Document or Class.
current_node_must_be => [ # KEYWORD_END[2]
'Document Class',
'Package or class must be the current node to capture end keyword',
2
]
],
CLOSURE_S => [
# $c->{clos_cap} (a node/structure to capture the closure)
# must be set for an opening curly bracket.
current_must_have => [ # CLOSURE_S[0]
'clos_cap',
'No preceding structure to capture the closure',
0
]
],
CLOSURE_E => [
# $c->{closure} (the current closure) must be set
# for a closing curly bracket.
current_must_have => [ # CLOSURE_E[0]
'closure',
'Attempted to terminate a closure, but no closure is open',
0
]
],
PAREN_E => [
# $c->{list} (the current list) must be set for a closing parenthesis.
current_must_have => [ # PAREN_E[0]
'list',
'Attempted to close a structural list, but no list is open',
0
]
],
BRACKET_E => [
# $c->{list} (the current list) must be set for a closing bracket.
current_must_have => [ # BRACKET_E[0]
'list',
'Attempted to close a value list or hash, but no list is open',
0
]
],
ANGLE_E => [
current_node_must_be => [ # ANGLE_E[0]
'TypedClass',
'Attempted to terminate a <Type>, but one is not open',
0
]
],
# OP_SEMI => [
#
# # $c->{instruction} (the current instruction) must be set in order
# # for a semicolon to terminate it.
# current_must_have => [ # OP_SEMI[0]
# 'instruction',
# 'Attempted to terminate an instruction, but no instruction is open',
# 0
# ]
#
# ],
OP_ELLIP => [
current_node_must_be => [ # OP_ELLIP[0]
'WantNeed WantNeedType WantNeedValue',
'Ellipsis can only follow an argument declaration',
0
]
],
OP_PROP => [
next_token_must_be => [ # OP_PROP[0]
'BRACKET_S',
'Property operator (.) must be followed by either a bareword '.
'property or a bracket-delimited expression',
0
]
],
KEYWORD_CATCH => [
current_must_have => [ # KEYWORD_CATCH[0]
'instruction',
'Attempted to catch an error on a failable instruction, but no '.
"instruction is open. Make sure the 'catch' keyword comes before ".
"instruction terminator (semicolon or newline)",
0
]
],
KEYWORD_IN => [
current_node_must_satisfy => [ # KEYWORD_IN[0]
sub {
my $node = shift;
$node->{parameter_for} && $node->{parameter_for} eq 'for';
},
"Could not find the corresponding 'for' keyword",
0
]
],
KEYWORD_CONTINUE => [
last_element_must_be => [ # KEYWORD_CONTINUE[0]
'For',
"Could not find the corresponding 'for' block",
0
]
]
);
our %element_rules = (
Document => {
# parent of package declaration must be a document.
parent_must_be => [ # Document[0]
'Main',
'Package declaration must be in the global scope',
0
]
},
Instruction => {
# there has to be at least one child, the statement.
# I don't think this can ever actually happen.
min_children => [ # Instruction[0]
1,
undef,
0
],
# we could have up to two children. the second could be a catch.
max_children => [ # Instruction[1]
2,
'Seems likely that you forgot a semicolon',
1
],
# if a second child does exist, it MUST be a catch.
child_1_must_be => [ # Instruction[2]
'Catch',
'Seems likely that you forgot a semicolon',
2
]
},
Class => {
# parent of package declaration must be a document.
parent_must_be => [ # Class[0]
'Document',
'Class declaration must be at package level',
0
],
# classes can currently contain only methods and variable declarations.
children_must_be => [ # Class[1]
'Function Instruction Type',
'Class must only contain class functions, instance methods, '.
'and variable/type declarations',
1
],
# if it's an instruction, it must satisfy these rules.
children_must_satisfy => [ # Class[2]
sub {
my $el = shift;
return 1 if $el->type ne 'Instruction';
my $child = $el->first_child;
# these are ayy-ok.
return 1 if $child->type eq 'Load';
return 1 if $child->type eq 'Alias';
return 1 if $child->type eq 'SharedDeclaration';
# if it's an assignment, it must be a lexical variable.
if ($child->type eq 'Assignment') {
return $child->assign_to->type eq 'LexicalVariable';
}
return;
},
'Class-level instructions can only include lexical variable '.
'assignments and load statements',
2
]
},
WantNeed => {
# # WantNeed must always be a direct child of an instruction.
# parent_must_be => [ # WantNeed[0]
# 'Instruction',
# undef,
# 0
# ],
# WantNeed must always be inside one of these.
must_be_somewhere_inside => [ # WantNeed[1]
'Function',
'Argument declaration must be within a function or method',
1
],
children_must_be => [ # WantNeed[2]
'InstanceVariable LexicalVariable WantNeedType WantNeedValue',
'Argument declaration can only contain lexical variables, '.
'instance variables, their bareword types, and their fallback '.
'value expressions',
2
],
min_children => [ # WantNeed[3]
1,
undef,
3
],
max_children => [ # WantNeed[4]
3,
undef,
4
]
},
WantNeedType => {
parent_must_be => [ # WantNeedType[0]
'WantNeed',
undef,
0
],
children_must_be => [ # WantNeedType[1]
'Bareword', # TODO: TypedClass
'Argument declaration type following colon (:) must be a bareword',
1
],
must_come_after => [ # WantNeedType[2]
'InstanceVariable LexicalVariable',
'Argument declaration type must follow argument variable',
2
],
min_children => [ # WantNeedType[3]
1,
undef,
3
]
},
WantNeedValue => {
parent_must_be => [ # WantNeedValue[0]
'WantNeed',
undef,
0
],
children_must_be => [ # WantNeedValue[1]
'@Expression',
'Argument declaration fallback value must be an expression '.
'of sorts',
1
],
must_come_after => [ # WantNeedValue[2]
'InstanceVariable LexicalVariable WantNeedType',
'Argument declaration type must follow argument variable or type',
2
],
num_children => [ # WantNeedValue[3]
1,
undef,
3
]
},
PropertyModifier => {
# must be a direct child an instruction.
parent_must_be => [ # PropertyModifier[0]
'Instruction',
undef,
0
],
# it can be variables or properties.
# however, it cannot be a special variable.
children_must_be => [ # PropertyModifier[1]
'@PropertyOwner Index',
'Property modifier can only capture non-special variables '.
'or properties',
1
],
# if it's a property, it cannot be a special one.
children_must_satisfy => [ # PropertyModifier[2]
sub {
my $el = shift;
return 1 if $el->type ne 'Property';
return !$el->is_special;
},
'Property modifier cannot capture special properties',
2
],
# there can only be one child.
num_children => [ # PropertyModifier[3]
1,
undef,
3
]
},
InstanceVariable => {
# instance variables only make sense inside of classes.
must_be_somewhere_inside => [ # InstanceVariable[0]
'Function',
'Instance variables must be inside a class instance method',
0
],
parent_must_satisfy => [ # InstanceVariable[1] TODO
sub {
my @upper_funcs = shift->filter_ancestors(type => 'Function');
return any { $_->is_method } @upper_funcs;
},
'Instance variables must be inside a class instance method',
1
]
},
ThisVariable => {
# instance variables only make sense inside of classes.
must_be_somewhere_inside => [ # ThisVariable[0]
'Function',
'Instance variables must be inside a class function or method',
0
]
},
NamedPair => {
# pairs can only be inside of lists (specifically, hashes or objects).
must_be_somewhere_inside => [ # NamedPair[0]
'List',
'Pair must be inside a list',
0
],
# each pair must be a direct child of a list item.
parent_must_be => [ # NamedPair[1]
'ListItem',
'Pair must be a direct child of a list item',
1
],
num_children => [ # NamedPair[2]
1,
undef,
2
],
children_must_be => [ # NamedPair[3]
'@Expression',
undef,
3
]
},
Pair => {
# pairs can only be inside of lists (specifically, hashes or objects).
must_be_somewhere_inside => [ # Pair[0]
'List',
'Pair must be inside a list',
0
],
# each pair must be a direct child of a list item.
parent_must_be => [ # Pair[1]
'ListItem',
'Pair must be a direct child of a list item',
1
],
num_children => [ # Pair[2]
2,
undef,
2
],
children_must_be => [ # Pair[3]
'@Expression',
undef,
3
]
},
List => {
children_must_be => [ # List[0]
'ListItem',
'Lists can only directly contain list items',
0
]
},
ListItem => {
# list items can only contain expressions and pairs.
children_must_be => [ # ListItem[0]
'@Expression NamedPair Pair',
'Lists can only contain expressions of sorts',
0
],
must_come_after => [ # ListItem[1]
'NONE ListItem',
'List items can only follow other list items',
1
],
must_come_before => [ # ListItem[2]
'NONE ListItem',
'List items can only follow other list items',
2
],
max_children => [ # ListItem[3]
1,
undef,
3
]
# Rule implemented in F/List.pm:
# Pairs and non-pairs cannot be mixed in a list unless it is the
# argument list of a call.
},
# Method => {
#
# # direct parent must be a class.
# parent_must_be => [ # Method[0] FIXME
# 'Class',
# 'Methods and computed properties must be directly inside a class',
# 0
# ]
#
# },
OnParameter => {
# children can only be non-special variables or properties.
children_must_be => [ # OnParameter[0]
'Property LexicalVariable InstanceVariable ThisVariable '.
'PropertyVariable Bareword',
"'On' parameter can only be a non-special variable or property",
0
],
# if it's a property, it cannot be a special one.
children_must_satisfy => [ # OnParameter[1]
sub {
my $el = shift;
return 1 if $el->type ne 'Property';
return !$el->is_special;
},
"'On' parameter cannot be a special property",
1
],
# it can only contain one property or variable.
num_children => [ # OnParameter[2]
1,
undef,
2
]
},
SharedDeclaration => {
# must be a direct child an instruction.
parent_must_be => [ # SharedDeclaration[0]
'Instruction',
undef,
0
],
# rules for the parent instruction
parent_rules => {
# the instruction's parent must be a class or document.
parent_must_be => [ # SharedDeclaration[4]
'Class Document',
'Shared variable declaration can only exist at class or '.
'package level',
4
]
},
# it can be lexical variables or an assignment of a lexical variable.
children_must_be => [ # SharedDeclaration[1]
'LexicalVariable Assignment',
'Shared variable declaration can only capture a lexical variable '.
'or a lexical variable assignment',
1
],
# if it's an assignment, it must be of a lexical variable.
children_must_satisfy => [ # SharedDeclaration[2]
sub {
my $el = shift;
return 1 if $el->type ne 'Assignment';
return $el->assign_to->type eq 'LexicalVariable';
},
'Shared variable declaration can capture an assignment only '.
'of a lexical variable',
2
],
# there can only be one child.
num_children => [ # SharedDeclaration[3]
1,
undef,
3
]
},
LocalDeclaration => {
# # must be a direct child an instruction.
# parent_must_be => [ # LocalDeclaration[0]
# 'Instruction',
# undef,
# 0
# ],
# it can be lexical variables or an assignment of a lexical variable.
children_must_be => [ # LocalDeclaration[1]
'LexicalVariable Assignment',
'Local variable declaration can only capture a lexical variable '.
'or a lexical variable assignment',
1
],
# if it's an assignment, it must be of a lexical variable.
children_must_satisfy => [ # LocalDeclaration[2]
sub {
my $el = shift;
my $parent = shift;
return 1 if $el->type ne 'Assignment';
return $el->assign_to->type eq 'LexicalVariable';
},
'Local variable declaration can capture an assignment only '.
'of a lexical variable',
2
],
# there can only be one child.
num_children => [ # LocalDeclaration[3]
1,
undef,
0
]
},
Load => {
# the parent must be an instruction.
parent_must_be => [ # Load[0]
'Instruction',
"Load statement must be a direct child of an instruction",
0
],
# rules for the parent instruction
parent_rules => {
# the instruction's parent must be a class or document.
parent_must_be => [ # Load[1]
'Class Document',
'Load statement can only exist at class or package level',
1
]
},
children_must_be => [ # Load[2]
'Bareword',
'Load statement can only contain a bareword package name',
2
],
# there can only be one child.
num_children => [ # Load[3]
1,
undef,
3
]
},
Stop => {
parent_must_be => [ # Stop[0]
'Instruction',
'Stop statement must be a direct child of an instruction',
0
],
must_be_somewhere_inside => [ # Stop[1]
'Function',
'Stop statement must be inside a function, method, or callback',
1
]
},
LoopStatement => {
parent_must_be => [ # LoopStatement[0]
'Instruction',
'Loop control statement (next/last/redo) must be a direct child '.
'of an instruction',
0
],
must_be_somewhere_inside => [ # LoopStatement[1]
'ForBody',
'Loop control statement (next/last/redo) must be inside a for '.
'loop body',
1
]
},
PropertyVariable => {
must_be_somewhere_inside => [ # PropertyVariable[0]
'InsideBody TypeBody FunctionBody',
"Property variable (standalone .property) can only exist within ".
"'inside', 'type', or 'function' block",
0
],
# make sure we're in the BODY of the Inside, not the param_exp.
parent_must_satisfy => [ # PropertyVariable[1]
sub {
my $el = shift;
# if it's somewhere in one of these, we're ok
return 1 if $el->first_self_or_parent('InsideBody');
return 1 if $el->first_self_or_parent('Type');
# otherwise, it's a function
my $func = $el->first_self_or_parent('Function') or return;
return $func->anonymous && !$func->arguments;
},
'Property variable (standalone .property) is only valid within '.
'a function if it is anonymous and has no additional argument '.
'requirements',
1
]
},
TypeBody => {
children_must_be => [ # TypeBody[0]
'Instruction',
'Type definitions can only contain instructions',
0
],
children_must_satisfy => [ # TypeBody[1]
sub {
my $el = shift;
# not instruction, pass on top children_must_be.
return 1 if $el->type ne 'Instruction';
my $child = $el->first_child;
# first of all, it has to be an expression,
# unless it's a type requirement.
return if
!$child or
!$child->is_type('Expression')
&& $child->type ne 'TypeRequirement';
return 1;
},
'Type definitions can only contain test values or method '.
'requirements',
1
]
},
Defer => {
must_be_somewhere_inside => [ # Defer[0]
'Function',
"'Defer' can only exist within a function or method",
0
]
},
Assignment => {
# consider: since assignments are expressions now, this can probably be removed?
# parent_must_be => [ # Assignment[0]
# 'Instruction IfParameter Alias SharedDeclaration LocalDeclaration '.
# 'Assignment',
# "Assignment must be direct child of an instruction, 'if' ".
# "parameter, alias, or variable declaration",
# 0
# ],
# left side and right side
num_children => 2, # Assignment[1]
# the first child is the left side of the assignment.
# it has be assignable.
child_0_must_be => [ # Assignment[2]
'@Assignable Bareword',
'Left side of assignment must be an assignable expression',
2
],
# if the left side is a bareword, this has to be an alias
child_0_must_satisfy => [ # Assignment[7]
sub {
my ($left, $as) = @_;
return 1 if $left->type ne 'Bareword';
return $as->parent->type eq 'Alias';
},
'Cannot assign to bareword. Try: alias newName = oldName',
7
],
# the right side of the assignment has to be an expression.
child_1_must_be => [ # Assignment[3]
'@Expression',
'Right side of assignment must be an expression',
3
],
directly_inside_rules => {
# with Alias as parent...
Alias => {
# both sides have to be a bareword.
children_must_be => [ # Assignment[4]
'Bareword',
'Both sides of assignment must be a bareword function or '.
'type name inside "alias" declaration',
4
]
}
}
},
Alias => {
parent_must_be => [ # Alias[0]
'Instruction',
undef,
0
],
children_must_be => [ # Alias[1]
'Assignment',
'Alias can only contain an assignment for a bareword function '.
'or type name',
1
],
num_children => [ # Alias[2]
1,
undef,
2
]
# note the rules within assignment for while inside Alias.
},
IfParameter => {
children_must_satisfy => [ # IfParameter[0]
sub {
my $el = shift;
# lazy assignments cannot act as expressions because
# they return a non-Ferret value (CODE reference)
return !$el->{lazy} if $el->is_type('Assignment');
return $el->is_type('Expression');
},
'If parameter must be an expression',
0
],
num_children => [ # IfParameter[1]
1,
undef,
1
]
},
TypeRequirement => {
parent_must_be => [ # TypeRequirement[0]
'Instruction',
'Type requirement (can/isa/satisfies/transform) must be a direct '.
'child of an instruction',
0
],
must_be_somewhere_inside => [ # TypeRequirement[1]
'Type',
'Type requirement (can/isa/satisfies/transform) can only exist '.
"somewhere inside of a 'type' construct",
1
],
child_0_must_be => [ # TypeRequirement[2] TODO
'@Expression',
'Type requirement (can/isa/satisfies/transform) must contain '.
'an expression of some sort',
2
],
child_1_must_be => [ # TypeRequirement[3] TODO
'Bareword List',
'Return type in method requirement must be a bareword or a '.
'list in the form (name: Type, other: OtherType)',
3
],
child_1_must_satisfy => [ # TypeRequirement[4]
sub {
my ($child, $type) = @_;
# first of all, if this child even exists, it has to have been
# marked as having a return type (which means -> was found).
return if !$type->{has_return};
# next, the list can only contain bareword return pair names
# and their bareword types.
return interface_method_list(@_) if $child->type eq 'List';
return 1;
},
'Type requirement can only capture one element unless it is a '.
'method requirement with a return type or list of return types',
4
],
min_children => [ # TypeRequirement[5] TODO
1,
undef,
5
],
max_children => [ # TypeRequirement[6] TODO
2,
undef,
6
],
# Rule implemented in F/TypeRequirement.pm:
# If the type requirement is a 'can' statement,
# its interface method, represented by a subtype of Call,
# must have a 'function' of type PropertyVariable.
# e.g. .someMethod()
# NOT someMethod()
# Checked on ->close of the TypeRequirement.
},
Call => {
# first child better be an expression
child_0_must_be => [ # Call[0]
'@Expression',
"Cannot call non-expression",
0
],
# second child better be an argument list
child_1_must_be => [ # Call[1]
'List',
"Second child of call, if any, must be its argument list",
1
],
# disallows call operator to omit the argument list
min_children => [ # Call[2]
1,
undef,
2
]
},
InterfaceMethod => {
# first child better be a .property
child_0_must_be => [ # InterfaceMethod[0]
'PropertyVariable',
"'can' method requirement must look like: can .methodName(...)",
0
],
# second child better be an argument list