aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-01-22 23:36:09 +0100
committerHarald Welte <laforge@gnumonks.org>2017-01-24 17:09:26 +0100
commite56a12f2a58a167b21b429b6aa9c09f2b4d21c2c (patch)
tree8e0fd69666228b6663f785fed970b11945842900
parentd248204bb7f609d1f89ad36e70e471af78f5b2bb (diff)
Add python script to generate value_string from file with #defines
-rwxr-xr-xtools/define-create-valstr.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/tools/define-create-valstr.py b/tools/define-create-valstr.py
new file mode 100755
index 0000000..4f7db81
--- /dev/null
+++ b/tools/define-create-valstr.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+
+from optparse import OptionParser
+from pyparsing import *
+from value_string import *
+
+# define the structure of a macro definition (the empty term is used
+# to advance to the next non-whitespace character)
+macroDef = "#define" + Word(alphas+"_",alphanums+"_").setResultsName("macro") + \
+ empty + restOfLine.setResultsName("value")
+
+NL = Suppress( LineEnd() )
+restOfLineNL = restOfLine + NL
+
+LineComment = Literal('//') + restOfLineNL
+Comment = cStyleComment | LineComment
+
+# doesn't work :/
+macroDef.ignore(Comment)
+
+parser = OptionParser()
+parser.add_option("-n", "--name", dest="name",
+ help="Name of the value_string symbol to create")
+parser.add_option("-f", "--flavor", dest="flavor", default='osmocom',
+ help="Flavor of generated C (osmocom, wireshark)")
+parser.add_option("-w", "--weak-symbol", dest="weak", default=True,
+ help="Generate weak symbols")
+(options, args) = parser.parse_args()
+filename = args[0]
+
+wr = ValueStringWriter(flavor=options.flavor, weak=options.weak, includes=[filename])
+
+with open(filename, 'r') as f:
+ res = macroDef.scanString(f.read())
+ vdict = {}
+ wr.export_header()
+ for tokens, startPos, EndPos in res:
+ vdict[tokens.value] = tokens.macro
+ wr.export_value_str(options.name, vdict, sort_key=None)