22
22
import re
23
23
from enum import Enum
24
24
25
+
25
26
## @brief print a sequence of lines
26
- def print_lines (lines , hint = None ):
27
+ def print_lines (lines , hint = None ):
27
28
counter = 1
28
29
for l in lines :
29
30
hint_char = " "
@@ -34,7 +35,9 @@ def print_lines(lines, hint = None):
34
35
35
36
36
37
## @brief print the whole content of input and match files
37
- def print_content (input_lines , match_lines , ignored_lines , hint_input = None , hint_match = None ):
38
+ def print_content (
39
+ input_lines , match_lines , ignored_lines , hint_input = None , hint_match = None
40
+ ):
38
41
print ("------ Input Lines " + "-" * 61 )
39
42
print_lines (input_lines , hint_input )
40
43
print ("------ Match Lines " + "-" * 61 )
@@ -91,10 +94,10 @@ def check_status(input_lines, match_lines):
91
94
## @brief pattern matching tags.
92
95
## Tags are expected to be at the start of the line.
93
96
class Tag (Enum ):
94
- OPT = "{{OPT}}" # makes the line optional
95
- IGNORE = "{{IGNORE}}" # ignores all input until next match or end of input file
96
- NONDETERMINISTIC = "{{NONDETERMINISTIC}}" # switches on "deterministic mode"
97
- COMMENT = "#" # comment - line ignored
97
+ OPT = "{{OPT}}" # makes the line optional
98
+ IGNORE = "{{IGNORE}}" # ignores all input until next match or end of input file
99
+ NONDETERMINISTIC = "{{NONDETERMINISTIC}}" # switches on "deterministic mode"
100
+ COMMENT = "#" # comment - line ignored
98
101
99
102
100
103
## @brief main function for the match file processing script
@@ -106,7 +109,7 @@ def main():
106
109
input_file = sys .argv [1 ]
107
110
match_file = sys .argv [2 ]
108
111
109
- with open (input_file , 'r' ) as input , open (match_file , 'r' ) as match :
112
+ with open (input_file , "r" ) as input , open (match_file , "r" ) as match :
110
113
input_lines = input .readlines ()
111
114
# Filter out empty lines and comments (lines beginning with the comment
112
115
# character, ignoring leading whitespace)
@@ -134,7 +137,9 @@ def main():
134
137
remaining_matches = set (range (len (match_lines ))) - matched_lines
135
138
for m in remaining_matches :
136
139
line = match_lines [m ]
137
- if line .startswith (Tag .OPT .value ) or line .startswith (Tag .NONDETERMINISTIC .value ):
140
+ if line .startswith (Tag .OPT .value ) or line .startswith (
141
+ Tag .NONDETERMINISTIC .value
142
+ ):
138
143
continue
139
144
print_match_not_found (m + 1 , match_lines [m ])
140
145
print_content (input_lines , match_lines , ignored_lines , hint_match = m )
@@ -143,38 +148,55 @@ def main():
143
148
sys .exit (0 )
144
149
elif status == Status .MATCH_END :
145
150
print_input_not_found (input_idx + 1 , input_lines [input_idx ])
146
- print_content (input_lines , match_lines , ignored_lines , hint_input = input_idx )
151
+ print_content (
152
+ input_lines , match_lines , ignored_lines , hint_input = input_idx
153
+ )
147
154
sys .exit (1 )
148
155
else :
149
- if (status == Status .INPUT_AND_MATCH_END ) or (status == Status .MATCH_END and Tag .IGNORE in tags_in_effect ):
156
+ if (status == Status .INPUT_AND_MATCH_END ) or (
157
+ status == Status .MATCH_END and Tag .IGNORE in tags_in_effect
158
+ ):
150
159
# all lines matched or the last line in match file is an ignore tag
151
160
sys .exit (0 )
152
161
elif status == Status .MATCH_END :
153
162
print_incorrect_match (input_idx + 1 , input_lines [input_idx ].strip (), "" )
154
- print_content (input_lines , match_lines , ignored_lines , hint_input = input_idx )
163
+ print_content (
164
+ input_lines , match_lines , ignored_lines , hint_input = input_idx
165
+ )
155
166
sys .exit (1 )
156
167
elif status == Status .INPUT_END :
157
- # If we get to the end of the input, but still have pending matches,
158
- # then that's a failure unless all pending matches are optional -
159
- # otherwise we're done
168
+ # If we get to the end of the input, but still have pending matches,
169
+ # then that's a failure unless all pending matches are optional -
170
+ # otherwise we're done
160
171
while match_idx < len (match_lines ):
161
- if not (match_lines [match_idx ].startswith (Tag .OPT .value ) or
162
- match_lines [match_idx ].startswith (Tag .IGNORE .value ) or
163
- match_lines [match_idx ].startswith (Tag .NONDETERMINISTIC .value )):
172
+ if not (
173
+ match_lines [match_idx ].startswith (Tag .OPT .value )
174
+ or match_lines [match_idx ].startswith (Tag .IGNORE .value )
175
+ or match_lines [match_idx ].startswith (Tag .NONDETERMINISTIC .value )
176
+ ):
164
177
print_incorrect_match (match_idx + 1 , "" , match_lines [match_idx ])
165
- print_content (input_lines , match_lines , ignored_lines , hint_match = match_idx )
178
+ print_content (
179
+ input_lines ,
180
+ match_lines ,
181
+ ignored_lines ,
182
+ hint_match = match_idx ,
183
+ )
166
184
sys .exit (1 )
167
185
match_idx += 1
168
186
sys .exit (0 )
169
187
170
- input_line = input_lines [input_idx ].strip () if input_idx < len (input_lines ) else ""
188
+ input_line = (
189
+ input_lines [input_idx ].strip () if input_idx < len (input_lines ) else ""
190
+ )
171
191
match_line = match_lines [match_idx ]
172
192
173
193
# check for tags
174
194
if match_line .startswith (Tag .OPT .value ):
175
195
tags_in_effect .append (Tag .OPT )
176
- match_line = match_line [len (Tag .OPT .value ):]
177
- elif match_line .startswith (Tag .NONDETERMINISTIC .value ) and not deterministic_mode :
196
+ match_line = match_line [len (Tag .OPT .value ) :]
197
+ elif (
198
+ match_line .startswith (Tag .NONDETERMINISTIC .value ) and not deterministic_mode
199
+ ):
178
200
deterministic_mode = True
179
201
match_idx = 0
180
202
input_idx = 0
@@ -185,10 +207,10 @@ def main():
185
207
sys .exit (2 )
186
208
tags_in_effect .append (Tag .IGNORE )
187
209
match_idx += 1
188
- continue # line with ignore tag should be skipped
210
+ continue # line with ignore tag should be skipped
189
211
190
212
# split into parts at {{ }}
191
- match_parts = re .split (r' \{{(.*?)\}}' , match_line .strip ())
213
+ match_parts = re .split (r" \{{(.*?)\}}" , match_line .strip ())
192
214
pattern = ""
193
215
for j , part in enumerate (match_parts ):
194
216
if j % 2 == 0 :
@@ -218,7 +240,13 @@ def main():
218
240
input_idx += 1
219
241
else :
220
242
print_incorrect_match (match_idx + 1 , input_line , match_line .strip ())
221
- print_content (input_lines , match_lines , ignored_lines , hint_match = match_idx , hint_input = input_idx )
243
+ print_content (
244
+ input_lines ,
245
+ match_lines ,
246
+ ignored_lines ,
247
+ hint_match = match_idx ,
248
+ hint_input = input_idx ,
249
+ )
222
250
sys .exit (1 )
223
251
224
252
0 commit comments