-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_regular_expression.py
executable file
·187 lines (147 loc) · 5.36 KB
/
gen_regular_expression.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
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
#!/bin/python3
import argparse
#------------------------------------------------------------------------------
# Global Variables
#------------------------------------------------------------------------------
# Input regular expression string
orig_regexp_str = ""
# Input regular expression priority
orig_regexp_priority = ""
# All elements of input regular expression.
# The regular expression part will updated in loop.
regexp_elements = []
# Attribute of each regular expression part:
# [ 0: index of regexp_elements,
# 1: start value,
# 2: end value,
# 3: current value ]
regexp_attribute = []
# Priority of each regular expression part.
# Index of this list: specify regular expression
# Value of this list: specify priority of each regular expression, for instance: 0, 1, 2
regexp_priority = []
# File name
file_name = ""
# Prefix and suffix string
prefix_str = ""
suffix_str = ""
#------------------------------------------------------------------------------
# Functions
#------------------------------------------------------------------------------
# Parse input arguments
def parse_arguments():
# Parse arguments
parser = argparse.ArgumentParser(description='')
parser.add_argument("-r", "--regexp", help="Regular expression")
parser.add_argument("-p", "--priority", help="Priority")
parser.add_argument("-f", "--file", help="File name")
parser.add_argument("-x", "--prefix", help="Prefix string")
parser.add_argument("-y", "--suffix", help="Suffix string")
args = parser.parse_args()
# -r, --regexp
global orig_regexp_str
if args.regexp:
orig_regexp_str = args.regexp
# -p, --priority
global orig_regexp_priority
if args.priority:
orig_regexp_priority = args.priority
# -f, --file
global file_name
if args.file:
file_name = args.file
# -x, --prefix
global prefix_str
if args.prefix:
prefix_str = args.prefix
# -y, --suffix
global suffix_str
if args.suffix:
suffix_str = args.suffix
# Parse input regular expression string
def parse_regexp_string():
regexp_element_idx = 0
start_idx = 0
if len(prefix_str) > 0:
regexp_elements.append(prefix_str)
regexp_element_idx += 1
while True:
left_bracket_idx = orig_regexp_str.find('[', start_idx)
if left_bracket_idx != -1:
right_bracket_idx = orig_regexp_str.find(']', left_bracket_idx)
if right_bracket_idx != -1:
regexp_elements.append(orig_regexp_str[start_idx:left_bracket_idx])
regexp_element_idx += 1
regexp_elements.append(orig_regexp_str[left_bracket_idx:right_bracket_idx+1])
regexp_attribute.append([regexp_element_idx, '', '', ''])
regexp_element_idx += 1
start_idx = right_bracket_idx + 1
else:
print("ERROR: Cannot find right square bracket corresponding to left one at ", left_bracket_idx)
exit()
else:
if start_idx < len(orig_regexp_str):
regexp_elements.append(orig_regexp_str[start_idx:])
break
if len(suffix_str) > 0:
regexp_elements.append(suffix_str)
def parse_regexp_attribute():
for attr in regexp_attribute:
regexp_idx = attr[0]
regexp_str = regexp_elements[regexp_idx]
minus_idx = regexp_str.find('-')
if minus_idx != -1:
attr[1] = int(regexp_str[1:minus_idx])
attr[2] = int(regexp_str[minus_idx+1:-1])
attr[3] = attr[1]
print(regexp_attribute)
def parse_regexp_priority():
global orig_regexp_priority
global regexp_priority
if len(orig_regexp_priority) > 0:
for element in orig_regexp_priority.split(' '):
regexp_priority.append(int(element))
else:
for element in reversed(range(0, len(regexp_attribute))):
regexp_priority.append(element)
regexp_attribute[regexp_priority[0]][3] = regexp_attribute[regexp_priority[0]][1] - 1
print(regexp_priority)
def translation_done():
for attr in regexp_attribute:
if attr[3] != attr[2]:
return False
return True
def update_regexp_attribute():
step_forward = True
for idx in regexp_priority:
if step_forward:
if regexp_attribute[idx][3] < regexp_attribute[idx][2]:
regexp_attribute[idx][3] += 1
step_forward = False
elif regexp_attribute[idx][3] == regexp_attribute[idx][2]:
regexp_attribute[idx][3] = regexp_attribute[idx][1]
step_forward = True
regexp_elements[regexp_attribute[idx][0]] = str(regexp_attribute[idx][3])
def print_regexp_string():
file_content = ""
while True:
if translation_done():
break
update_regexp_attribute()
if len(file_name) > 0:
file_content += ''.join(regexp_elements) + '\n'
else:
print(''.join(regexp_elements))
if len(file_name) > 0:
with open(file_name, 'w') as f:
f.write(file_content)
f.close()
def translate_reg_expression():
parse_regexp_string()
parse_regexp_attribute()
parse_regexp_priority()
print_regexp_string()
# Entry point
if __name__ == "__main__":
parse_arguments()
translate_reg_expression()