Exemplo n.º 1
0
 def _test_single(self, parser_element, input_output):
     p = parser.Parser(parser_element)
     print "Parser:", parser_element
     for input, output in input_output:
         print "Input:", input
         result = p.parse(input)
         print "Expected: %r" % output
         print "Result: %r" % result
         self.assertEqual(result, output)
Exemplo n.º 2
0
 def _test_multiple(self, parser_element, input_outputs, must_finish=True):
     p = parser.Parser(parser_element)
     for input, outputs in input_outputs:
         results = p.parse_multiple(input, must_finish)
         self.assertEqual(len(results), len(outputs))
         for index, result, output in zip(xrange(len(results)), results, outputs):
             if isinstance(result, list): result = tuple(result)
             if isinstance(output, list): output = tuple(output)
             self.assertEqual(result, output)
Exemplo n.º 3
0
 def _test_multiple(self, parser_element, input_outputs, must_finish=True):
     p = parser.Parser(parser_element)
     print "Parser:", parser_element
     for input, outputs in input_outputs:
         print "Input:", input
         results = p.parse_multiple(input, must_finish)
         self.assertEqual(len(results), len(outputs))
         for index, result, output in zip(xrange(len(results)), results,
                                          outputs):
             if isinstance(result, list): result = tuple(result)
             if isinstance(output, list): output = tuple(output)
             print "Expected: %r" % output
             print "Result %d: %r" % (index, result)
             self.assertEqual(result, output)
Exemplo n.º 4
0
class Compound(elements_.Alternative):

    _log = logging.getLogger("compound.parse")
    _parser = parser_.Parser(stuff, _log)

    def __init__(self,
                 spec,
                 extras=None,
                 actions=None,
                 name=None,
                 value=None,
                 value_func=None,
                 elements=None,
                 default=None):
        self._spec = spec
        self._value = value
        self._value_func = value_func

        if extras is None: extras = {}
        if actions is None: actions = {}
        if elements is None: elements = {}

        # Convert extras argument from sequence to mapping.
        if isinstance(extras, (tuple, list)):
            mapping = {}
            for element in extras:
                if not isinstance(element, elements_.ElementBase):
                    self._log.error("Invalid extras item: %s" % element)
                    raise TypeError("Invalid extras item: %s" % element)
                if not element.name:
                    self._log.error("Extras item does not have a name: %s" %
                                    element)
                    raise TypeError("Extras item does not have a name: %s" %
                                    element)
                if element.name in mapping:
                    self._log.warning(
                        "Multiple extras items with the same name: %s" %
                        element)
                mapping[element.name] = element
            extras = mapping
        elif not isinstance(extras, dict):
            self._log.error("Invalid extras argument: %s" % extras)
            raise TypeError("Invalid extras argument: %s" % extras)

        # Temporary transition code so that both "elements" and "extras"
        #  are supported as keyword arguments.
        if extras and elements:
            extras = dict(extras)
            extras.update(elements)
        elif elements:
            extras = elements
        self._extras = extras

        # This solution is non-ideal as "stuff" is a global instance.
        stuff.set_elements(extras)
        stuff.set_actions(actions)

        element = self._parser.parse(spec)
        if not element:
            self._log.error("Invalid compound spec: %r" % spec)
            raise SyntaxError("Invalid compound spec: %r" % spec)
        elements_.Alternative.__init__(self, (element, ),
                                       name=name,
                                       default=default)

    def __str__(self):
        arguments = ["%r" % self._spec]
        if self.name: arguments.append("name=%r" % self.name)
        arguments = ", ".join(arguments)
        return "%s(%s)" % (self.__class__.__name__, arguments)

    def value(self, node):
        if self._value_func is not None:
            # Prepare *extras* dict for passing to value_func().
            extras = {"_node": node}
            for name, element in self._extras.iteritems():
                extra_node = node.get_child_by_name(name, shallow=True)
                if extra_node:
                    extras[name] = extra_node.value()
                elif element.has_default():
                    extras[name] = element.default
            try:
                value = self._value_func(node, extras)
            except Exception, e:
                self._log.warning("Exception from value_func: %s" % e)
                raise
            return value
        elif self._value is not None:
            return self._value
Exemplo n.º 5
0
 def _test_single(self, parser_element, input_output):
     p = parser.Parser(parser_element)
     for input, output in input_output:
         result = p.parse(input)
         self.assertEqual(result, output)