File: astutils.py

package info (click to toggle)
logilab-astng 0.17.2-2.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 444 kB
  • ctags: 752
  • sloc: python: 4,843; makefile: 43; sh: 14
file content (79 lines) | stat: -rw-r--r-- 2,457 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright (c) 2003 Sylvain Thenault (thenault@nerim.net)
# Copyright (c) 2003 Logilab
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
"""Some usefull functions to manipulate ast tuples
"""

__author__ = u"Sylvain Thenault"

import symbol
import token
from types import TupleType

def debuild(ast_tuple):
    """
    reverse ast_tuple to string
    """
    if type(ast_tuple[1]) is TupleType:
        result = ''
        for child in ast_tuple[1:]: 
            result = '%s%s' % (result, debuild(child))
        return result
    else:
        return ast_tuple[1]

def clean(ast_tuple):
    """
    reverse ast tuple to a list of tokens
    merge sequences (token.NAME, token.DOT, token.NAME)
    """
    result = []
    last = None
    for couple in _clean(ast_tuple):
        if couple[0] == token.NAME and last == token.DOT:
            result[-1][1] += couple[1]
        elif couple[0] == token.DOT and last == token.NAME:
            result[-1][1] += couple[1]
        else:
            result.append(couple)
        last = couple[0]
    return result

def _clean(ast_tuple):
    """ transform the ast into as list of tokens (i.e. final elements)
    """
    if type(ast_tuple[1]) is TupleType:
        v = []
        for c in ast_tuple[1:]:
            v += _clean(c)
        return v
    else:
        return [list(ast_tuple[:2])]
    
def cvrtr(tuple):
    """debug method returning an ast string in a readable fashion"""
    if type(tuple) is TupleType:
        try:
            try:
                txt = 'token.'+token.tok_name[tuple[0]]
            except:
                txt = 'symbol.'+symbol.sym_name[tuple[0]]
        except:
            txt =  'Unknown token/symbol'
        return [txt] + map(cvrtr, tuple[1:])
    else:
        return tuple

__all__ = ('debuild', 'clean', 'cvrtr')