Index: net/tools/dafsa/make_dafsa.py |
diff --git a/net/tools/dafsa/make_dafsa.py b/net/tools/dafsa/make_dafsa.py |
index 5c9082d372c18007b853a9e9599c57b5e253f736..358be16ee89f0845e4685f0a415c8dcb091d2039 100755 |
--- a/net/tools/dafsa/make_dafsa.py |
+++ b/net/tools/dafsa/make_dafsa.py |
@@ -192,8 +192,19 @@ The bytes in the generated array has the following meaning: |
10: 0x82 <return_value> 0x82 & 0x0F -> return 2 |
""" |
+import argparse |
import sys |
+ |
+parser = argparse.ArgumentParser() |
+parser.add_argument('infile', nargs='?', type=argparse.FileType('r')) |
+parser.add_argument('outfile', nargs='?', type=argparse.FileType('w')) |
+parser.add_argument('--reverse_inputs', action='store_true', |
+ help='Generates a DAFSA on reversed input strings; this is ' |
+ 'useful for suffix queries') |
+args = parser.parse_args() |
+ |
+ |
class InputError(Exception): |
"""Exception raised for errors in the input file.""" |
@@ -453,14 +464,17 @@ def parse_gperf(infile): |
if not line.endswith(('0', '1', '2', '3', '4', '5', '6', '7')): |
raise InputError('Expected value to be in the range of 0-7, found "%s"' % |
line[-1]) |
- return [line[:-3] + line[-1] for line in lines] |
- |
+ result = [] |
+ for line in lines: |
+ word, result_code = line[:-3], line[-1] |
+ if args.reverse_inputs: |
+ word = word[::-1] |
+ result.append(word + result_code) |
+ result.sort() |
+ return result |
def main(): |
- if len(sys.argv) != 3: |
- print('usage: %s infile outfile' % sys.argv[0]) |
- return 1 |
- with open(sys.argv[1], 'r') as infile, open(sys.argv[2], 'w') as outfile: |
+ with args.infile as infile, args.outfile as outfile: |
outfile.write(words_to_cxx(parse_gperf(infile))) |
return 0 |