-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionArgumentFailure.wl
192 lines (120 loc) · 5.85 KB
/
functionArgumentFailure.wl
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
(* ::Package:: *)
(* ::Chapter:: *)
(*Begin*)
BeginPackage["functionArgumentFailure`"];
(* usages for public functions *)
functionArgumentFailure::usage = "When argument patterns are not matched an Failure object is produced";
resetFailurelist::usage = "reset the global failurelist";
setFailure::usage = "generates a Failure object and appends it to the failurelist";
(* ::Chapter:: *)
(*Private*)
Begin["`Private`"];
(* ::Subsection:: *)
(*functionArgumentFailure*)
functionArgumentFailure[function_Symbol, args___] :=
Module[{givennumberofarguments, expectednumberofarguments, argumentslist, patterntests,
failure, failureinfo, failurebasicinfo},
argumentslist = {args};
patterntests = findPatternTests[function];
givennumberofarguments = Length[argumentslist];
expectednumberofarguments = Length[patterntests];
If[expectednumberofarguments == givennumberofarguments,
failureinfo = wrongArgumentTypes[argumentslist, patterntests, function];
,
If[expectednumberofarguments < givennumberofarguments,
failureinfo = tooManyArguments[argumentslist, patterntests, function];
,
failureinfo = tooFewArguments[argumentslist, patterntests, function];
]
];
failurebasicinfo = <|
"number of given arguments" -> givennumberofarguments,
"expected number of arguments" -> expectednumberofarguments,
"Heads of given arguments" -> Head /@ argumentslist,
"Perfomed tests" -> patterntests|>;
failure = setFailure[function, Prepend[failureinfo, failurebasicinfo ] ];
Return[failure]
];
functionArgumentFailure::wrongargs = "in `1` :wrong arguments";
functionArgumentFailure::toomanyargs = "in `1` :too many arguments";
functionArgumentFailure::toofewargs = "in `1` :not enough arguments";
(* ::Subsection:: *)
(*wrongArgumentTypes*)
wrongArgumentTypes[argumentslist_List, patterntests_List, function_Symbol] :=
Module[{failedpos, likelyreason, failedargument, messageid, argumentnamelist},
argumentnamelist = getArgumentnamelist[patterntests];
failedpos = getFailedPositionOfPatterntest[argumentslist, patterntests];
likelyreason = "argument(s) which failed the test: " <> Riffle[Extract[argumentnamelist, failedpos], ", "] ;
failedargument = Short[#, 3] & /@ Extract[argumentslist, failedpos];
Message[functionArgumentFailure::wrongargs, function];
Return[genFailureInfo[likelyreason, failedpos, failedargument]]
]
(* ::Subsection:: *)
(*tooManyArguments*)
tooManyArguments[argumentslist_List, patterntests_List, function_Symbol] := Module[{failedpos, likelyreason,
failedargument, givennumberofarguments, expectednumberofarguments},
givennumberofarguments = Length[argumentslist];
expectednumberofarguments = Length[patterntests];
failedpos = Range[expectednumberofarguments + 1, givennumberofarguments];
likelyreason = "too many arguments";
failedargument = argumentslist[[expectednumberofarguments + 1 ;; -1]];
Message[functionArgumentFailure::toomanyargs, function];
Return[genFailureInfo[likelyreason, failedpos, failedargument]]
]
(* ::Subsection:: *)
(*tooFewArguments*)
tooFewArguments[argumentslist_List, patterntests_List, function_Symbol] := Module[{failedpos, likelyreason,
failedargument},
failedpos = Missing[];
likelyreason = "not enough arguments";
failedargument = Missing[];
Message[functionArgumentFailure::toofewargs, function];
Return[genFailureInfo[likelyreason, failedpos, failedargument]]
];
(* ::Subsection:: *)
(*genFailureInfo*)
genFailureInfo[likelyreason_, failedpos_, failedargument_ ] := <|"reason of failure"-> likelyreason,
"Position of failed argument(s)" -> failedpos, "List of failed argument(s)" -> failedargument|>
(* ::Subsection:: *)
(*getArgumentnamelist*)
getArgumentnamelist[patterntests_List] := Flatten[StringTrim[#, (WhitespaceCharacter | "_")] & /@
StringCases[StringReplace[patterntests, {"(" -> "", ")" -> ""}], __ ~~ "_"]]
(* ::Subsection:: *)
(*getFailedPositionOfPatterntest*)
getFailedPositionOfPatterntest[argumentslist_List, patterntests_List]:= Position[checkFunctionArgumentpattern /@
Thread[{argumentslist, patterntests}], False];
(* ::Subsection:: *)
(*checkFunctionArgumentpattern*)
(* Applying and check the pattern tests found with findPatternTests. *)
checkFunctionArgumentpattern[{argument_, test_String}] := MatchQ[argument, ToExpression[test]]
(* ::Subsection:: *)
(*findPatternTests*)
(* Returns the Pattern tests of the "function" as string used.*)
findPatternTests[function_Symbol] := Module[{patternsstringlist, functiondefinition, functiondefinitionlist,
patternsstringlistwithoutoptions, s},
functiondefinition = ToString[Definition[function]];
functiondefinitionlist = First@StringSplit[First[StringSplit[functiondefinition, "\n"]], ":="];
patternsstringlist =
StringSplit[StringDrop[StringDrop[First[StringCases[functiondefinitionlist, "[" ~~ Longest[__] ~~ "]"]],
{1}], {-1}], ","];
patternsstringlistwithoutoptions = DeleteCases[patternsstringlist,
s_String /; StringMatchQ[s, ___ ~~ "OptionsPattern[]" ~~ ___]];
Return[patternsstringlistwithoutoptions]
];
(* ::Subsection:: *)
(*setFailure*)
(* Define the Failure Object and add it to the failurelist*)
setFailure[function_Symbol, failures_?AssociationQ] := Module[{failure},
failure = Failure[function, failures];
If[ListQ@ Global`failurelist == False, resetFailurelist[]];
AppendTo[Global`failurelist, failure];
Return[failure]
];
setFailure[args___]:= exception[setFailure, args];
(* ::Subsection:: *)
(*resetFailurlist*)
resetFailurelist[] := Module[{}, Global`failurelist = {};];
(* ::Chapter:: *)
(*End of package*)
End[];
EndPackage[ ];