Example #1
0
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 lines.
    """
    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
Example #2
0
def location_from_stack(level):
    """
        level = 0: our caller
        level = 1: our caller's caller
    """
    from inspect import currentframe

    cf = currentframe()

    if level == 0:
        cf = cf.f_back
    elif level == 1:
        cf = cf.f_back.f_back
    elif level == 2:
        cf = cf.f_back.f_back.f_back
    elif level == 3:
        cf = cf.f_back.f_back.f_back.f_back
    else:
        raise NotImplementedError(level)

    assert cf is not None, level

    filename = inspect.getfile(cf)
    if not os.path.exists(filename):
        msg = 'Could not read %r' % filename
        raise NotImplementedError(msg)

    lineno = cf.f_lineno - 1
    string = open(filename).read()
    if not string:
        raise Exception(filename)

    character = location(lineno, 0, string)
    character_end = location(lineno + 1, 0, string) - 1
    where = Where(string, character, character_end)

    lf = LocalFile(filename)
    res = LocationInString(where, lf)
    return res
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)