-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertSliceOperator.py
executable file
·46 lines (40 loc) · 1.98 KB
/
convertSliceOperator.py
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
import re
import tokenize
import convertToken
__author__ = 'Alexis Shaw'
def convertSliceOperator(token,line,t,v,i,understood,variables):
sliceOperators = []
count = 0
i+=1
sliceOperators.append('')
while len(token)-i > 0 :
(t, v, _, _,_) = token[i]
if t == tokenize.NAME:
sliceOperators[count],i,understood,variables = convertToken.convertToken(token, sliceOperators[count],t,v,i,understood,variables,'')
elif t == tokenize.OP and re.match(r'^[([<>&^|~=+,*%-]$|^\*\*$|<<|>>|>=|<=|!=|==',v):
sliceOperators[count],i,understood,variables = convertToken.convertToken(token, sliceOperators[count],t,v,i,understood,variables,'')
elif t == tokenize.NL or t == tokenize.NUMBER:
sliceOperators[count],i,understood,variables = convertToken.convertToken(token,sliceOperators[count],t,v,i,understood,variables,'')
elif t == tokenize.STRING:
sliceOperators[count],i,understood,variables = convertToken.convertToken(token,sliceOperators[count],t,v,i,understood,variables,'')
elif t == tokenize.COMMENT and token[i+1][0] == tokenize.NL:
sliceOperators[count],i,understood,variables = convertToken.convertToken(token,sliceOperators[count],t,v,i,understood,variables,'')
elif t == tokenize.OP and v == ']':
break
elif t == tokenize.OP and v == ':':
sliceOperators.append('')
count+= 1
else: understood = False
i += 1
if len(sliceOperators) == 1:
line += '['+sliceOperators[0]+'] '
elif len(sliceOperators) == 2:
if sliceOperators[0] == '':
sliceOperators[0] = '0'
if sliceOperators[1] == '':
sliceOperators[1] = '-1'
line += '['+sliceOperators[0]+'..'+sliceOperators[1]+']'
elif len(sliceOperators) == 3:
line += '['+sliceOperators[0]+'..'+sliceOperators[1]+'..'+sliceOperators[2]+']'
understood == False
return line, i, understood, variables,count