Harald Welte | e56a12f | 2017-01-22 23:36:09 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | from optparse import OptionParser |
| 4 | from pyparsing import * |
| 5 | from value_string import * |
| 6 | |
| 7 | # define the structure of a macro definition (the empty term is used |
| 8 | # to advance to the next non-whitespace character) |
| 9 | macroDef = "#define" + Word(alphas+"_",alphanums+"_").setResultsName("macro") + \ |
| 10 | empty + restOfLine.setResultsName("value") |
| 11 | |
| 12 | NL = Suppress( LineEnd() ) |
| 13 | restOfLineNL = restOfLine + NL |
| 14 | |
| 15 | LineComment = Literal('//') + restOfLineNL |
| 16 | Comment = cStyleComment | LineComment |
| 17 | |
| 18 | # doesn't work :/ |
| 19 | macroDef.ignore(Comment) |
| 20 | |
| 21 | parser = OptionParser() |
| 22 | parser.add_option("-n", "--name", dest="name", |
| 23 | help="Name of the value_string symbol to create") |
| 24 | parser.add_option("-f", "--flavor", dest="flavor", default='osmocom', |
| 25 | help="Flavor of generated C (osmocom, wireshark)") |
| 26 | parser.add_option("-w", "--weak-symbol", dest="weak", default=True, |
| 27 | help="Generate weak symbols") |
| 28 | (options, args) = parser.parse_args() |
| 29 | filename = args[0] |
| 30 | |
| 31 | wr = ValueStringWriter(flavor=options.flavor, weak=options.weak, includes=[filename]) |
| 32 | |
| 33 | with open(filename, 'r') as f: |
| 34 | res = macroDef.scanString(f.read()) |
| 35 | vdict = {} |
| 36 | wr.export_header() |
| 37 | for tokens, startPos, EndPos in res: |
| 38 | vdict[tokens.value] = tokens.macro |
| 39 | wr.export_value_str(options.name, vdict, sort_key=None) |