blob: 4f7db810d011c54d5fc447ecdb160b15c55a1963 [file] [log] [blame]
Harald Weltee56a12f2017-01-22 23:36:09 +01001#!/usr/bin/env python
2
3from optparse import OptionParser
4from pyparsing import *
5from 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)
9macroDef = "#define" + Word(alphas+"_",alphanums+"_").setResultsName("macro") + \
10 empty + restOfLine.setResultsName("value")
11
12NL = Suppress( LineEnd() )
13restOfLineNL = restOfLine + NL
14
15LineComment = Literal('//') + restOfLineNL
16Comment = cStyleComment | LineComment
17
18# doesn't work :/
19macroDef.ignore(Comment)
20
21parser = OptionParser()
22parser.add_option("-n", "--name", dest="name",
23 help="Name of the value_string symbol to create")
24parser.add_option("-f", "--flavor", dest="flavor", default='osmocom',
25 help="Flavor of generated C (osmocom, wireshark)")
26parser.add_option("-w", "--weak-symbol", dest="weak", default=True,
27 help="Generate weak symbols")
28(options, args) = parser.parse_args()
29filename = args[0]
30
31wr = ValueStringWriter(flavor=options.flavor, weak=options.weak, includes=[filename])
32
33with 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)