From 603900db2e1f893de5a10d8da7d7e1d2552f991a Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 15 Mar 2010 23:20:36 +0100 Subject: tools/dfagen: limit c output using origin, closes #102 --- tools/dfagen/dfagen/output/c.py | 43 ++++++++++++++++------- tools/dfagen/examples/Makefile | 6 +++- tools/dfagen/examples/import.conf | 5 +++ tools/dfagen/examples/tpl/template_cb.txt | 3 ++ tools/dfagen/examples/tpl/template_imported12.txt | 6 ++++ 5 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 tools/dfagen/examples/import.conf create mode 100644 tools/dfagen/examples/tpl/template_cb.txt create mode 100644 tools/dfagen/examples/tpl/template_imported12.txt (limited to 'tools') diff --git a/tools/dfagen/dfagen/output/c.py b/tools/dfagen/dfagen/output/c.py index 2d45720e..e6cbca9b 100644 --- a/tools/dfagen/dfagen/output/c.py +++ b/tools/dfagen/dfagen/output/c.py @@ -169,12 +169,18 @@ class WriterData: r += ' },\n' return r - def states_template (self, template): + def states_template (self, template, **kargs): + """Call a template for every states. The first parameter is the + template file name. An optionnal 'origin' keyword parameter limits to + states from the given origin(s) (use | to separate origins).""" + origin = None + if 'origin' in kargs: + origin = kargs['origin'].split ('|') t = open (os.path.join (self.templatedir, template), 'r') tt = t.read () t.close () exp = '' - for s in self.states (): + for s in self.states (origin): for tr in s.iter_transitions (): d = WriterData (self.prefix, self.automaton, self.user) branches_to = '\n'.join ( @@ -206,27 +212,39 @@ class WriterData: return exp def __getitem__ (self, key): - preproc = lambda v: v - args = [] + # Get key and arguments. key = key.split (',') - key, args = key[0], key[1:] + key, targs = key[0], key[1:] + # Choose postprocessor. + postproc = lambda v: v if key.startswith ('*'): key = key[1:] - preproc = lambda v: ' * ' + v.replace ('\n', '\n * ') + '\n' + postproc = lambda v: ' * ' + v.replace ('\n', '\n * ') + '\n' if key.startswith ('_'): key = key[1:] - preproc = lambda v: v and v + '\n' or '' + postproc = lambda v: v and v + '\n' or '' + # Parse arguments. + args = [] + kargs = {} + for a in targs: + if '=' in a: + k, v = a.split ('=') + kargs[k] = v + else: + args.append (a) + # Find value. val = None if key in self.dict: - try: - val = self.dict[key] (*args) - except TypeError: + if callable (self.dict[key]): + val = self.dict[key] (*args, **kargs) + else: val = self.dict[key] elif key.startswith ('user.'): val = self.user[key[5:]] - val = preproc (val) if val is None: raise KeyError, key + # Postprocessing and return. + val = postproc (val) return val class Writer: @@ -260,7 +278,8 @@ class Writer: def write (prefix, automaton, user, outputdir, origin): if origin is not None: - raise NotImplementedError ("--origin is not implemented for C output") + raise NotImplementedError ("--origin is not implemented for C " + "output, use templates") templatedir = os.path.splitext (__file__)[0] if 'template-dir' in user: templatedir = os.path.join (os.path.split (user.file)[0], diff --git a/tools/dfagen/examples/Makefile b/tools/dfagen/examples/Makefile index ad97b8a4..35c3e3cd 100644 --- a/tools/dfagen/examples/Makefile +++ b/tools/dfagen/examples/Makefile @@ -1,6 +1,6 @@ CFLAGS = -O2 -Wall -all: ex1 ex2 ex1.png ex2.png ex2_tpl import.dump.check +all: ex1 ex2 ex1.png ex2.png ex2_tpl import.dump.check import_imported12.txt ex1: ex1.o ex1_cb.o @@ -32,6 +32,9 @@ ex2_cb.c: ex2_cb_skel.c ex2_cb.c.patch ex2_tpl: ex2.fsm ex2_tpl.conf python ../dfagen.py -o c -d $(filter %.fsm, $^) -c $(filter %.conf, $^) -p $@ +import_imported12.txt: import.fsm imported1.fsm imported2.fsm imported3.fsm import.conf + python ../dfagen.py -o c -d import.fsm -c import.conf -p import + %.dot: %.fsm python ../dfagen.py -o dot -d $< -p $(@:%.dot=%) @@ -53,3 +56,4 @@ clean: rm -f ex2.dot ex2.png rm -f ex2_tpl_defs.h ex2_tpl_table.h rm -f import.dump + rm -f import_imported12.txt diff --git a/tools/dfagen/examples/import.conf b/tools/dfagen/examples/import.conf new file mode 100644 index 00000000..60e2ef0f --- /dev/null +++ b/tools/dfagen/examples/import.conf @@ -0,0 +1,5 @@ +[user] +template-dir = tpl + +[templates] +template_imported12.txt = %_imported12.txt diff --git a/tools/dfagen/examples/tpl/template_cb.txt b/tools/dfagen/examples/tpl/template_cb.txt new file mode 100644 index 00000000..7495502e --- /dev/null +++ b/tools/dfagen/examples/tpl/template_cb.txt @@ -0,0 +1,3 @@ +%(state)s =%(event)s=> +%(branches_to)s + diff --git a/tools/dfagen/examples/tpl/template_imported12.txt b/tools/dfagen/examples/tpl/template_imported12.txt new file mode 100644 index 00000000..64c8e745 --- /dev/null +++ b/tools/dfagen/examples/tpl/template_imported12.txt @@ -0,0 +1,6 @@ + +Example for template origin limitation. + +Only states from Imported1 and Imported2 will be listed. + +%(states_template,template_cb.txt,origin=Imported1|Imported2)s -- cgit v1.2.3