-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathArgParser.java
More file actions
378 lines (340 loc) · 12.1 KB
/
ArgParser.java
File metadata and controls
378 lines (340 loc) · 12.1 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
package org.python.core;
import org.python.antlr.AST;
import java.util.HashSet;
import java.util.Set;
/**
* A utility class for handling mixed positional and keyword arguments.
*
* Typical usage:
*
* <pre>
* public MatchObject search(PyObject[] args, String[] kws) {
* ArgParser ap = new ArgParser("search", args, kws,
* "pattern", "pos", "endpos");
* String string = ap.getString(0);
* int start = ap.getInt(1, 0);
* int end = ap.getInt(2, string.length());
* ...
* </pre>
*/
public class ArgParser {
// The name of the function. Used in exception messages
private String funcname;
// The actual argument values.
private PyObject[] args;
// The list of actual keyword names.
private String[] kws;
// The list of allowed and expected keyword names.
private String[] params = null;
// A marker.
private static Object required = new Object();
private static String[] emptyKws = new String[0];
// private PyBuiltinFunction.Info info;
private ArgParser(String funcname, PyObject[] args, String[] kws) {
this.funcname = funcname;
this.args = args;
if (kws == null) {
kws = emptyKws;
}
this.kws = kws;
}
/**
* Create an ArgParser for a one-argument function.
*
* @param funcname Name of the function. Used in error messages.
* @param args The actual call arguments supplied in the call.
* @param kws The actual keyword names supplied in the call.
* @param p0 The expected argument in the function definition.
*/
public ArgParser(String funcname, PyObject[] args, String[] kws, String p0) {
this(funcname, args, kws);
this.params = new String[] { p0 };
check();
}
/**
* Create an ArgParser for a two-argument function.
*
* @param funcname Name of the function. Used in error messages.
* @param args The actual call arguments supplied in the call.
* @param kws The actual keyword names supplied in the call.
* @param p0 The first expected argument in the function definition.
* @param p1 The second expected argument in the function definition.
*/
public ArgParser(String funcname, PyObject[] args, String[] kws, String p0,
String p1) {
this(funcname, args, kws);
this.params = new String[] { p0, p1 };
check();
}
/**
* Create an ArgParser for a three-argument function.
*
* @param funcname Name of the function. Used in error messages.
* @param args The actual call arguments supplied in the call.
* @param kws The actual keyword names supplied in the call.
* @param p0 The first expected argument in the function definition.
* @param p1 The second expected argument in the function definition.
* @param p2 The third expected argument in the function definition.
*/
public ArgParser(String funcname, PyObject[] args, String[] kws, String p0,
String p1, String p2) {
this(funcname, args, kws);
this.params = new String[] { p0, p1, p2 };
check();
}
/**
* Create an ArgParser for a multi-argument function.
*
* @param funcname Name of the function. Used in error messages.
* @param args The actual call arguments supplied in the call.
* @param kws The actual keyword names supplied in the call.
* @param paramnames The list of expected argument in the function definition.
*/
public ArgParser(String funcname, PyObject[] args, String[] kws,
String[] paramnames) {
this(funcname, args, kws);
this.params = paramnames;
check();
}
public ArgParser(String funcname, PyObject[] args, String[] kws,
String[] paramnames, int minargs) {
this(funcname, args, kws);
this.params = paramnames;
check();
if (!PyBuiltinCallable.DefaultInfo.check(args.length, minargs,
this.params.length)) {
throw PyBuiltinCallable.DefaultInfo.unexpectedCall(args.length,
false, funcname, minargs, this.params.length);
}
}
public ArgParser(String funcname, PyObject[] args, String[] kws,
String[] paramnames, int minargs, boolean takesZeroArgs) {
this(funcname, args, kws);
this.params = paramnames;
check();
if (!AST.check(args.length - kws.length, minargs, takesZeroArgs)) {
throw AST.unexpectedCall(minargs, funcname);
}
}
/**
* Return a required argument as a String.
*
* @param pos The position of the .. First argument is numbered 0.
*/
public String getString(int pos) {
return (String) getArg(pos, String.class, "string");
}
/**
* Return an optional argument as a String.
*
* @param pos The position of the argument. First argument is numbered 0.
*/
public String getString(int pos, String def) {
return (String) getArg(pos, String.class, "string", def);
}
/**
* Return a required argument as an int.
*
* @param pos The position of the argument. First argument is numbered 0.
*/
public int getInt(int pos) {
return asInt(getRequiredArg(pos));
}
/**
* Return an optional argument as an int.
*
* @param pos The position of the argument. First argument is numbered 0.
*/
public int getInt(int pos, int def) {
PyObject value = getOptionalArg(pos);
if (value == null) {
return def;
}
return asInt(value);
}
/**
* Convert a PyObject to a Java integer.
*
* @param value a PyObject
* @return value as an int
*/
private int asInt(PyObject value) {
if (value instanceof PyFloat) {
Py.warning(Py.DeprecationWarning, "integer argument expected, got float");
value = value.__int__();
}
return value.asInt();
}
/**
* Return an required argument as an index.
*
* @param pos The position of the argument. First argument is numbered 0.
*/
public int getIndex(int pos) {
PyObject value = getRequiredArg(pos);
return value.asIndex();
}
/**
* Return an optional argument as an index.
*
* @param pos The position of the argument. First argument is numbered 0.
*/
public int getIndex(int pos, int def) {
PyObject value = getOptionalArg(pos);
if (value == null) {
return def;
}
return value.asIndex();
}
/**
* Return a required argument as a PyObject.
*
* @param pos The position of the argument. First argument is numbered 0.
*/
public PyObject getPyObject(int pos) {
return getRequiredArg(pos);
}
/**
* Return an optional argument as a PyObject.
*
* @param pos The position of the argument. First argument is numbered 0.
*/
public PyObject getPyObject(int pos, PyObject def) {
PyObject value = getOptionalArg(pos);
if (value == null) {
value = def;
}
return value;
}
/**
* Return a required argument as a PyObject, ensuring the object is of the specified type.
*
* @param pos the position of the argument. First argument is numbered 0
* @param type the desired PyType of the argument
* @return the PyObject of PyType type
*/
public PyObject getPyObjectByType(int pos, PyType type) {
PyObject arg = getRequiredArg(pos); // != null
return checkedForType(arg, pos, type);
}
/**
* Return an optional argument as a PyObject, or return the default value provided, which may
* be <code>null</code>. If the returned value is not <code>null</code>, it must be of the
* specified type.
*
* @param pos the position of the argument. First argument is numbered 0
* @param type the desired PyType of the argument
* @param def to return if the argument at pos was not given (null allowed)
* @return the PyObject of PyType type
*/
public PyObject getPyObjectByType(int pos, PyType type, PyObject def) {
PyObject arg = getOptionalArg(pos);
return checkedForType((arg != null ? arg : def), pos, type);
}
// Common code for getObjectByType: don't check null!
private static PyObject checkedForType(PyObject arg, int pos, PyType type) {
if (arg == null || Py.isInstance(arg, type)) return arg;
throw Py.TypeError(String.format("argument %d must be %s, not %s", pos + 1,
type.fastGetName(), arg.getType().fastGetName()));
}
/**
* Return the remaining arguments as a tuple.
*
* @param pos The position of the argument. First argument is numbered 0.
*/
public PyObject getList(int pos) {
int kws_start = this.args.length - this.kws.length;
if (pos < kws_start) {
PyObject[] ret = new PyObject[kws_start - pos];
System.arraycopy(this.args, pos, ret, 0, kws_start - pos);
return new PyTuple(ret);
}
return Py.EmptyTuple;
}
/**
* Ensure no keyword arguments were passed, raising a TypeError if
* so.
*
*/
public void noKeywords() {
if (kws.length > 0) {
throw Py.TypeError(String.format("%s does not take keyword arguments", funcname));
}
}
private void check() {
Set<Integer> usedKws = new HashSet<Integer>();
int nargs = args.length - kws.length;
l1: for (int i = 0; i < kws.length; i++) {
for (int j = 0; j < params.length; j++) {
if (kws[i].equals(params[j])) {
if (j < nargs) {
throw Py.TypeError("keyword parameter '"
+ params[j]
+ "' was given by position and by name");
}
if (usedKws.contains(j)) {
throw Py.TypeError(String.format(
"%s got multiple values for keyword argument '%s'",
funcname, params[j]));
}
usedKws.add(j);
continue l1;
}
}
throw Py.TypeError("'" + kws[i] + "' is an invalid keyword "
+ "argument for this function");
}
}
private PyObject getRequiredArg(int pos) {
PyObject ret = getOptionalArg(pos);
if (ret == null) {
throw Py.TypeError(this.funcname + ": The " + ordinal(pos)
+ " argument is required");
}
return ret;
}
private PyObject getOptionalArg(int pos) {
int kws_start = this.args.length - this.kws.length;
if (pos < kws_start) {
return this.args[pos];
}
for (int i = 0; i < this.kws.length; i++) {
if (this.kws[i].equals(this.params[pos])) {
return this.args[kws_start + i];
}
}
return null;
}
private Object getArg(int pos, Class clss, String classname) {
return getArg(pos, clss, classname, required);
}
private Object getArg(int pos, Class clss, String classname, Object def) {
PyObject value = null;
if (def == required) {
value = getRequiredArg(pos);
} else {
value = getOptionalArg(pos);
if (value == null) {
return def;
}
}
Object ret = value.__tojava__(clss);
if (ret == Py.NoConversion) {
throw Py.TypeError("argument " + (pos + 1) + ": expected "
+ classname + ", " + value.getType().fastGetName() + " found");
}
return ret;
}
private static String ordinal(int n) {
switch (n + 1) {
case 1:
return "1st";
case 2:
return "2nd";
case 3:
return "3rd";
default:
return Integer.toString(n + 1) + "th";
}
}
}