Esempio n. 1
0
 def parse(self):
     """Parse cases, within each of which there is a pair of input and output.
     :return (object, object, [str]): input, output, comments
     """
     cases = self.PAT_IO_HEADERS.findall(self.text + 'Input:')
     try:
         # TODO how about cmts?
         cases = [(case, []) for case in cases] if cases else self._parse_cases()
         return [(self.params.parse(i), self.rets.parse(o), cmts) for (i, o), cmts in cases]
     except ValueError as e:
         raise type(e)('{} in \n{}'.format(e.args[0], to_readable(self.text))) from e
Esempio n. 2
0
 def _tokens(self, text):
     try:
         return self._run(text)
     except SyntaxError as e:
         MARKER = chr(0)
         i, text = self.i, self.text
         ch = text[i]
         text = text[:i] + MARKER + text[i+1:]
         text = to_readable(text)
         i = text.index(MARKER)
         text = text[:i] + ch + text[i+1:]
         e.args = ('{} at index {} in \n{}'.format(e.args[0], i, text),)
         raise
Esempio n. 3
0
    def parse(self, text):
        """
        :return object:
        """
        stack = []
        curr = []
        local_depth = 0
        stypes = []
        for token in Tokenizer(text).line():
            if isinstance(token, Tokenizer.OpeningBracket):
                stack.append(curr)
                curr = []
                local_depth = max(len(stack), local_depth)
            elif isinstance(token, Tokenizer.ClosingBracket):
                if not stack:
                    raise SyntaxError('brackets do not pair')
                last_curr = stack.pop()
                last_curr.append(curr)
                curr = last_curr
                if len(stack) == 0:
                    stypes.append(self.GENERIC + self._LIST * local_depth)
                    local_depth = 0
            elif isinstance(token, Tokenizer.Token):
                pass
            else:
                curr.append(token)
                if len(stack) == 0:
                    stypes.append(type(token).__name__)
        if stack:
            raise SyntaxError('missing closing brackets')

        # print(curr)
        data = curr
        if self.types:
            try:
                data = self.cast(data)
            except TypeError as e:
                ts_expected = ' * '.join(t + self._LIST * d for t, d in self.types)
                ts_actual = ' * '.join(stypes)
                e.args = ('{}: type ( {} ) is required, but type ( {} ) is given in \n{}'.format(
                    e.args[0], ts_expected, ts_actual, to_readable(text)),)
                raise
        # print(data)
        return data