Exemple #1
0
def _build_parse_error(data, target_symbol, chart):
    # Find the last `i` that had some Earley items --
    # that is, the last `i` where we could still make sense of the input data.
    i, items = [(i, items)
                for (i, (items, _, _)) in enumerate(chart)
                if len(items) > 0][-1]
    found = data[i : i + 1]

    # What terminal symbols did we expect at that `i`?
    expected = OrderedDict()
    for (symbol, rule, pos, start) in items:
        next_symbol = rule.xsymbols[pos]
        if isinstance(next_symbol, Terminal):
            chars = format_chars(next_symbol.chars())
            # And why did we expect it? As part of what nonterminals?
            expected.setdefault(chars, set()).update(
                _find_pivots(chart, symbol, start))

        if symbol is target_symbol and next_symbol is None:
            # This item indicates a complete parse of `target_symbol`,
            # so if the input data just stopped there, that would work, too,
            expected[u'end of data'] = None

    return ParseError(name=None, position=i,
                      expected=list(expected.items()), found=found)
Exemple #2
0
def _build_parse_error(data, target_symbol, chart):
    # Find the last `i` that had some Earley items --
    # that is, the last `i` where we could still make sense of the input data.
    i, items = [(i, items)
                for (i, (items, _, _)) in enumerate(chart)
                if len(items) > 0][-1]
    found = data[i : i + 1]

    # What terminal symbols did we expect at that `i`?
    expected = OrderedDict()
    for (symbol, rule, pos, start) in items:
        next_symbol = rule.xsymbols[pos]
        if isinstance(next_symbol, Terminal):
            chars = format_chars(next_symbol.chars())
            # And why did we expect it? As part of what nonterminals?
            expected.setdefault(chars, set()).update(
                _find_pivots(chart, symbol, start))

        if symbol is target_symbol and next_symbol is None:
            # This item indicates a complete parse of `target_symbol`,
            # so if the input data just stopped there, that would work, too,
            expected[u'end of data'] = None

    return ParseError(name=None, position=i,
                      expected=list(expected.items()), found=found)
Exemple #3
0
 def url_encoded_data(self):
     if self.headers.content_type == \
             media.application_x_www_form_urlencoded and \
             okay(self.decoded_body) and self.content_is_full:
         for char in iterbytes(self.decoded_body):
             if not URL_ENCODED_GOOD_CHARS[ord(char)]:
                 self.complain(1040, char=format_chars([char]))
                 return Unavailable(self.decoded_body)
         # pylint: disable=no-member
         return parse_qs(self.decoded_body.decode('ascii'))
     return None
Exemple #4
0
 def url_encoded_data(self):
     if self.headers.content_type == \
             media.application_x_www_form_urlencoded and \
             okay(self.decoded_body) and self.content_is_full:
         for byte in six.iterbytes(self.decoded_body):
             if not URL_ENCODED_GOOD_BYTES[byte]:
                 char = six.int2byte(byte)
                 self.complain(1040, char=format_chars([char]))
                 return Unavailable
         # pylint: disable=no-member
         return parse_qs(self.decoded_body.decode('ascii'))
     else:
         return None
Exemple #5
0
 def url_encoded_data(self):
     if self.headers.content_type == \
             media.application_x_www_form_urlencoded and \
             okay(self.decoded_body) and self.content_is_full:
         for byte in six.iterbytes(self.decoded_body):
             if not URL_ENCODED_GOOD_BYTES[byte]:
                 char = six.int2byte(byte)
                 self.complain(1040, char=format_chars([char]))
                 return Unavailable
         # pylint: disable=no-member
         return parse_qs(self.decoded_body.decode('ascii'))
     else:
         return None
Exemple #6
0
def expand_parse_error(error):
    paras = [[error.name]] if error.name else []
    paras.append([u'Parse error at offset %d.' % error.position])
    if error.found == b'':
        paras.append([u'Found end of data.'])
    elif error.found is not None:
        paras.append([u'Found: %s' % format_chars([error.found])])

    paras.append([u'Expected:'])
    for i, (option, symbols) in enumerate(error.expected):
        para = []
        if option:
            para.extend([option] if i == 0 else [u'or ', option])
            if symbols:
                para.append(u' as part of ')
        for j, symbol in enumerate(symbols or []):
            para.extend([symbol] if j == 0 else [u' or ', symbol])
        paras.append(para)

    return paras
Exemple #7
0
def expand_parse_error(error):
    paras = [[error.name]] if error.name else []
    paras.append([u'Parse error at offset %d.' % error.position])
    if error.found == b'':
        paras.append([u'Found end of data.'])
    elif error.found is not None:
        paras.append([u'Found: %s' % format_chars([error.found])])

    paras.append([u'Expected:'])
    for i, (option, symbols) in enumerate(error.expected):
        para = []
        if option:
            para.extend([option] if i == 0 else [u'or ', option])
            if symbols:
                para.append(u' as part of ')
        for j, symbol in enumerate(symbols or []):
            para.extend([symbol] if j == 0 else [u' or ', symbol])
        paras.append(para)

    return paras
Exemple #8
0
def expand_parse_error(error):
    paras = [[u'Parse error at offset %d.' % error.point]]
    if error.found == b'':
        paras.append([u'Found end of data.'])
    elif error.found is not None:
        paras.append([u'Found: %s' % format_chars([error.found])])

    for i, (option, as_part_of) in enumerate(error.expected):
        if i == 0:
            paras.append([u'Expected:'])
            para = [option]
        else:
            para = [u'or ', option]
        if as_part_of:
            para.append(u' as part of ')
            for j, parent in enumerate(as_part_of):
                para.extend([u' or ', parent] if j > 0 else [parent])
        paras.append(para)

    return paras
Exemple #9
0
def expand_parse_error(error):
    paras = [[error.name]] if error.name else []
    paras.append([u'Parse error at offset %d.' % error.point])
    if error.found == b'':
        paras.append([u'Found end of data.'])
    elif error.found is not None:
        paras.append([u'Found: %s' % format_chars([error.found])])

    for i, (option, as_part_of) in enumerate(error.expected):
        if i == 0:
            paras.append([u'Expected:'])
            para = [option]
        else:
            para = [u'or ', option]
        if as_part_of:
            para.append(u' as part of ')
            for j, parent in enumerate(as_part_of):
                para.extend([u' or ', parent] if j > 0 else [parent])
        paras.append(para)

    return paras