def transform(x, parents): # @UnusedVariable w0 = x.where s0 = w0.string def translate(line, col): # add initial white space on first line if line == 0: col += len(extra_before) # add the initial empty lines line += num_empty_lines_start return line, col # these are now in the original string transform_original_s line, col = translate(*line_and_col(w0.character, s0)) character = location(line, col, transform_original_s) if w0.character_end is None: character_end = None else: line, col = translate(*line_and_col(w0.character_end, s0)) character_end = location(line, col, transform_original_s) where = Where(string=transform_original_s, character=character, character_end=character_end) return get_copy_with_where(x, where)
def translate_where(where0, string): """ Take the first where; compute line, col according to where0.string, and find out the corresponding chars in the second string. This assumes that string and where0.string have the same number of lines. """ nlines = len(string.split('\n')) nlines0 = len(where0.string.split('\n')) if nlines != nlines0: msg = 'I expected they have the same lines.' msg += '\n string (%d lines): %r' % (nlines, string) msg += '\n where0.string (%d lines): %r' % (nlines0, where0.string) raise_desc(DPInternalError, msg) string0 = where0.string line, col = line_and_col(where0.character, string0) character2 = location(line, col, string) if where0.character_end is None: character_end2 = None else: line, col = line_and_col(where0.character_end, string0) character_end2 = location(line, col, string) where = Where(string=string, character=character2, character_end=character_end2) return where
def check_good_use_of_special_paragraphs(md, filename): lines = md.split('\n') for i in range(1, len(lines)): line = lines[i] prev = lines[i - 1] prefix = has_special_line_prefix(line) if prefix: if prev.strip(): msg = ('Wrong use of special paragraph indicator. You have ' 'to leave an empty line before the special paragraph.') c = location(i, 1, md) c_end = c + len(prefix) where = Where(md, c, c_end).with_filename(filename) raise DPSyntaxError(msg, where=where) if False: def looks_like_list_item(s): if s.startswith('--'): return False if s.startswith('**'): return False return s.startswith('-') or s.startswith('*') if looks_like_list_item(line): if prev.strip() and not looks_like_list_item(prev): msg = ('Wrong use of list indicator. You have ' 'to leave an empty line before the list.') c = location(i, 1, md) c_end = c + 1 where = Where(md, c, c_end).with_filename(filename) raise DPSyntaxError(msg, where=where)
def _invalid(self, mo): i = mo.start('invalid') lines = self.template[:i].splitlines(True) if not lines: colno = 1 lineno = 1 else: colno = i - len(''.join(lines[:-1])) lineno = len(lines) char = location(lineno - 1, colno - 1, s) w = Where(s, char) raise DPSyntaxError('Invalid placeholder', where=w)
def parsing_error_recov09(): """ Invalid HTML produced """ s="""# a! a""" ast_to_html_(s) s="""mcdp { a }kk""" for c in range(len(s)+1): line, col = line_and_col(c, s) c2 = location(line, col, s) print('c = %2d line = %2d col = %s c2 = %s' % (c, line, col ,c2)) assert c == c2, (c, line, col, c2)
def check_parsable(s): from xml.etree import ElementTree as ET # parser = ET.XMLParser() # parser.entity["nbsp"] = unichr(160) s = '<add-wrap-for-xml-parser>' + s + '</add-wrap-for-xml-parser>' # print indent(s, ' for xml') # with open('rtmp.xml', 'w') as f: # f.write(s) try: _ = ET.fromstring(s) except Exception as e: line1, col1 = e.position line = line1 - 1 col = col1 - 1 character = location(line, col, s) msg = 'Invalid XML: %s' % e where = Where(s, character) logger.error('line %s col %s' % (where.line, where.col)) logger.error(where) raise DPSyntaxError(msg, where=where)