-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathAstList.java
More file actions
656 lines (543 loc) · 16.5 KB
/
AstList.java
File metadata and controls
656 lines (543 loc) · 16.5 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
/*
* I don't like that this is in org.python.core, but PySequence has package
* private methods that I want to override. Hopefully we will clean up the
* PyList hierarchy in the future, and then maybe we will find a more sensible
* place for this class.
*/
package org.python.core;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.python.antlr.adapter.AstAdapter;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedType;
import org.python.expose.MethodType;
@ExposedType(name = "_ast.astlist", base = PyList.class)
public class AstList extends PySequence implements Cloneable, List, Traverseproc {
public static final PyType TYPE = PyType.fromClass(AstList.class);
private final static PyString[] fields = new PyString[0];
@ExposedGet(name = "_fields")
public PyString[] get_fields() { return fields; }
/** The underlying Java List. */
private List data;
private AstAdapter adapter;
public AstList() {
this(TYPE, new ArrayList(), null);
}
public AstList(List data) {
this(TYPE, data, null);
}
public AstList(List data, AstAdapter adapter) {
this(TYPE, data, adapter);
}
public AstList(PyType type, List data, AstAdapter adapter) {
super(TYPE);
if (data == null) {
data = new ArrayList();
}
this.data = data;
this.adapter = adapter;
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___ne__(PyObject o) {
return seq___ne__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___eq__(PyObject o) {
return seq___eq__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___lt__(PyObject o) {
return seq___lt__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___le__(PyObject o) {
return seq___le__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___gt__(PyObject o) {
return seq___gt__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___ge__(PyObject o) {
return seq___ge__(o);
}
@ExposedMethod
final boolean astlist___contains__(PyObject o) {
return object___contains__(o);
}
@ExposedMethod
final void astlist___delitem__(PyObject index) {
seq___delitem__(index);
}
@ExposedMethod
final void astlist___setitem__(PyObject o, PyObject def) {
seq___setitem__(o, def);
}
@ExposedMethod
final PyObject astlist___getitem__(PyObject o) {
PyObject ret = seq___finditem__(o);
if(ret == null) {
throw Py.IndexError("index out of range: " + o);
}
return ret;
}
@ExposedMethod
final boolean astlist___nonzero__() {
return seq___nonzero__();
}
@ExposedMethod
public PyObject astlist___iter__() {
return seq___iter__();
}
@ExposedMethod(defaults = "null")
final PyObject astlist___getslice__(PyObject start, PyObject stop, PyObject step) {
return seq___getslice__(start, stop, step);
}
@ExposedMethod(defaults = "null")
final void astlist___setslice__(PyObject start, PyObject stop, PyObject step, PyObject value) {
if(value == null) {
value = step;
step = null;
}
seq___setslice__(start, stop, step, value);
}
@ExposedMethod(defaults = "null")
final void astlist___delslice__(PyObject start, PyObject stop, PyObject step) {
seq___delslice__(start, stop, step);
}
public PyObject __imul__(PyObject o) {
return astlist___imul__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___imul__(PyObject o) {
if (!o.isIndex()) {
return null;
}
int count = o.asIndex(Py.OverflowError);
int size = size();
if (size == 0 || count == 1) {
return this;
}
if (count < 1) {
clear();
return this;
}
if (size > Integer.MAX_VALUE / count) {
throw Py.MemoryError("");
}
int oldsize = data.size();
for (int i = 1; i < count; i++) {
data.addAll(data.subList(0, oldsize));
}
return this;
}
public PyObject __mul__(PyObject o) {
return astlist___mul__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___mul__(PyObject o) {
if (!o.isIndex()) {
return null;
}
return repeat(o.asIndex(Py.OverflowError));
}
public PyObject __rmul__(PyObject o) {
return astlist___rmul__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___rmul__(PyObject o) {
if (!o.isIndex()) {
return null;
}
return repeat(o.asIndex(Py.OverflowError));
}
public PyObject __iadd__(PyObject other) {
return astlist___iadd__(other);
}
@ExposedMethod
final PyObject astlist___iadd__(PyObject o) {
PyType oType = o.getType();
if (oType == TYPE || oType == PyTuple.TYPE || this == o) {
extend(fastSequence(o, "argument must be iterable"));
return this;
}
PyObject it;
try {
it = o.__iter__();
} catch (PyException pye) {
if (!pye.match(Py.TypeError)) {
throw pye;
}
return null;
}
extend(it);
return this;
}
public PyObject __add__(PyObject other) {
return astlist___add__(other);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___add__(PyObject o) {
AstList sum = null;
Object oList = o.__tojava__(List.class);
if(oList != Py.NoConversion && oList != null) {
List otherList = (List) oList;
sum = new AstList();
sum.extend(this);
for(Iterator i = otherList.iterator(); i.hasNext();) {
sum.add(i.next());
}
}
return sum;
}
public PyObject __radd__(PyObject o) {
return astlist___radd__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final PyObject astlist___radd__(PyObject o) {
PyList sum = null;
Object oList = o.__tojava__(List.class);
if (oList != Py.NoConversion && oList != null) {
sum = new PyList();
sum.addAll((List) oList);
sum.extend(this);
}
return sum;
}
public int __len__() {
return data.size();
}
@Override
public String toString() {
return astlist_toString();
}
@ExposedMethod(names = "__repr__")
final String astlist_toString() {
return data.toString();
}
public void append(PyObject o) {
astlist_append(o);
}
@ExposedMethod
final void astlist_append(PyObject o) {
data.add(o);
}
public Object clone() {
return new AstList(this);
}
@ExposedMethod
final int astlist_count(PyObject value) {
int count = 0;
for(Object o : data) {
if(o.equals(value)) {
count++;
}
}
return count;
}
public int count(PyObject value) {
return astlist_count(value);
}
public int index(PyObject o) {
return astlist_index(o, 0, size());
}
public int index(PyObject o, int start) {
return astlist_index(o, start, size());
}
public int index(PyObject o, int start, int stop) {
return astlist_index(o, start, stop);
}
@ExposedMethod(defaults = {"null", "null"})
final int astlist_index(PyObject o, PyObject start, PyObject stop) {
int startInt = start == null ? 0 : PySlice.calculateSliceIndex(start);
int stopInt = stop == null ? size() : PySlice.calculateSliceIndex(stop);
return astlist_index(o, startInt, stopInt);
}
final int astlist_index(PyObject o, int start, int stop) {
return _index(o, "astlist.index(x): x not in list", start, stop);
}
final int astlist_index(PyObject o, int start) {
return _index(o, "astlist.index(x): x not in list", start, size());
}
final int astlist_index(PyObject o) {
return _index(o, "astlist.index(x): x not in list", 0, size());
}
private int _index(PyObject o, String message, int start, int stop) {
// Follow Python 2.3+ behavior
int validStop = boundToSequence(stop);
int validStart = boundToSequence(start);
for(int i = validStart; i < validStop && i < size(); i++) {
if(data.get(i).equals(o)) {
return i;
}
}
throw Py.ValueError(message);
}
protected void del(int i) {
data.remove(i);
}
protected void delRange(int start, int stop, int step) {
if(step >= 1) {
for(int i = start; i < stop; i += step) {
remove(i);
i--;
stop--;
}
} else if(step < 0) {
for(int i = start; i >= 0 && i >= stop; i += step) {
remove(i);
}
}
}
@ExposedMethod
final void astlist_extend(PyObject iterable){
int length = size();
setslice(length, length, 1, iterable);
}
public void extend(PyObject iterable) {
astlist_extend(iterable);
}
protected PyObject getslice(int start, int stop, int step) {
if(step > 0 && stop < start) {
stop = start;
}
int n = sliceLength(start, stop, step);
List newList = data.subList(start, stop);
if(step == 1) {
newList = data.subList(start, stop);
return new AstList(newList, adapter);
}
int j = 0;
for(int i = start; j < n; i += step) {
newList.set(j, data.get(i));
j++;
}
return new AstList(newList, adapter);
}
public void insert(int index, PyObject o) {
astlist_insert(index, o);
}
@ExposedMethod
final void astlist_insert(int index, PyObject o) {
if(index < 0) {
index = Math.max(0, size() + index);
}
if(index > size()) {
index = size();
}
data.add(index, o);
}
@ExposedMethod
final void astlist_remove(PyObject value){
del(_index(value, "astlist.remove(x): x not in list", 0, size()));
}
public void remove(PyObject value) {
astlist_remove(value);
}
public void reverse() {
astlist_reverse();
}
@ExposedMethod
final void astlist_reverse() {
Collections.reverse(data);
}
public PyObject pop() {
return pop(-1);
}
public PyObject pop(int n) {
return astlist_pop(n);
}
@ExposedMethod(defaults = "-1")
final PyObject astlist_pop(int n) {
if (adapter == null) {
return (PyObject)data.remove(n);
}
Object element = data.remove(n);
return adapter.ast2py(element);
}
protected PyObject repeat(int count) {
if (count < 0) {
count = 0;
}
int size = size();
int newSize = size * count;
if (count != 0 && newSize / count != size) {
throw Py.MemoryError("");
}
List newList = new ArrayList();
for(int i = 0; i < count; i++) {
newList.addAll(data);
}
return new AstList(newList);
}
protected void setslice(int start, int stop, int step, PyObject value) {
if(stop < start) {
stop = start;
}
if (value instanceof PySequence) {
PySequence sequence = (PySequence) value;
setslicePySequence(start, stop, step, sequence);
} else if (value instanceof List) {
List list = (List)value.__tojava__(List.class);
if(list != null && list != Py.NoConversion) {
setsliceList(start, stop, step, list);
}
} else {
setsliceIterable(start, stop, step, value);
}
}
protected void setslicePySequence(int start, int stop, int step, PySequence value) {
if (step != 0) {
if(value == this) {
PyList newseq = new PyList();
PyObject iter = value.__iter__();
for(PyObject item = null; (item = iter.__iternext__()) != null;) {
newseq.append(item);
}
value = newseq;
}
int n = value.__len__();
for (int i = 0, j = start; i < n; i++, j += step) {
pyset(j, value.pyget(i));
}
}
}
protected void setsliceList(int start, int stop, int step, List value) {
if(step != 1) {
throw Py.TypeError("setslice with java.util.List and step != 1 not supported yet");
}
int n = value.size();
for(int i = 0; i < n; i++) {
data.add(i + start, value.get(i));
}
}
protected void setsliceIterable(int start, int stop, int step, PyObject value) {
PyObject[] seq;
try {
seq = Py.make_array(value);
} catch (PyException pye) {
if (pye.match(Py.TypeError)) {
throw Py.TypeError("can only assign an iterable");
}
throw pye;
}
setslicePySequence(start, stop, step, new PyList(seq));
}
public void add(int index, Object element) {
data.add(index, element);
}
public boolean add(Object o) {
return data.add(o);
}
public boolean addAll(int index, Collection c) {
return data.addAll(index, c);
}
public boolean addAll(Collection c) {
return data.addAll(c);
}
public void clear() {
data.clear();
}
public boolean contains(Object o) {
return data.contains(o);
}
public boolean containsAll(Collection c) {
return data.containsAll(c);
}
public Object get(int index) {
return data.get(index);
}
public int indexOf(Object o) {
return data.indexOf(o);
}
public boolean isEmpty() {
return data.isEmpty();
}
public Iterator iterator() {
return data.iterator();
}
public int lastIndexOf(Object o) {
return data.lastIndexOf(o);
}
public ListIterator listIterator() {
return data.listIterator();
}
public ListIterator listIterator(int index) {
return data.listIterator(index);
}
public boolean pyadd(PyObject o) {
data.add(o);
return true;
}
public void pyadd(int index, PyObject element) {
data.add(index, element);
}
public PyObject pyget(int index) {
if (adapter == null) {
return (PyObject)data.get(index);
}
return adapter.ast2py(data.get(index));
}
public void pyset(int index, PyObject element) {
if (adapter == null) {
data.set(index, element);
} else {
Object o = adapter.py2ast(element);
data.set(index, o);
}
}
public Object remove(int index) {
return data.remove(index);
}
public boolean remove(Object o) {
return data.remove(o);
}
public boolean removeAll(Collection c) {
return data.removeAll(c);
}
public boolean retainAll(Collection c) {
return data.retainAll(c);
}
public Object set(int index, Object element) {
return data.set(index, element);
}
public int size() {
return data.size();
}
public List subList(int fromIndex, int toIndex) {
return data.subList(fromIndex, toIndex);
}
public Object[] toArray() {
return data.toArray();
}
public Object[] toArray(Object[] a) {
return data.toArray(a);
}
public Object __tojava__(Class c) {
if(c.isInstance(this)) {
return this;
}
return Py.NoConversion;
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retVal;
for (Object ob: data) {
if (ob instanceof PyObject) {
retVal = visit.visit((PyObject) ob, arg);
if (retVal != 0) return retVal;
}
}
return 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return data.contains(ob);
}
}