Exemple #1
0
 def testFile(self):
     inp = parse.parse_file(
         os.path.join(os.path.dirname(__file__),
                      'namedtuple.mpy'))
     inp1 = inp.expand_defcode_blocks()
     _mpy = Builder()
     ns = dict(_mpy=_mpy)
     inp1.exec_(ns, ns)
     inp2 = parse.parse_string('$namedtuple(?Point, ?x, ?y)')
     inp3 = inp2.expand_defcode_blocks()
     inp4 = inp3.quote()
     inp4.namespace = ns
     _mpy.push()
     inp4.exec_(ns, ns)
     expanded = _mpy.pop()
     expanded.exec_(ns, ns)
     self.assertEqual(str(ns['Point'](1,2)),
                      'Point (x =1, y =2)')
Exemple #2
0
 def expand(self, fn):
     '''Token-based macro and code quoting expander'''
     inp = parse.parse_file(fn)
     # Expand the defcode blocks
     inp1 = inp.expand_defcode_blocks()
     # Quote and exec to get the macros expanded
     inp2 = inp1.quote()
     self._mpy.push()
     inp2.exec_(self.namespace, self.namespace)
     inp3 = self._mpy.pop()
     # Exec the module in the namespace
     inp3.exec_(self.namespace, self.namespace)
     try:
         first_token = iter(inp3).next()
         if first_token.match(token.STRING):
             doc = first_token.value
         else:
             doc = None
     except StopIteration, si:
         doc = None