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 parsing_error_recov03(): s = "msg\na" c = 4 line, col = line_and_col(c, s) assert s[c] == 'a' assert line == 1 assert col == 0
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)