File: ast.py

package info (click to toggle)
python-gendoc 0.73-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 312 kB
  • ctags: 845
  • sloc: python: 2,610; makefile: 124; sh: 26
file content (284 lines) | stat: -rw-r--r-- 7,735 bytes parent folder | download | duplicates (5)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#############################################################################
# Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1993, 1994, 1995.
# Unpublished work.  All Rights Reserved.
#
# The software contained on this media is the property of the
# DSTC Pty Ltd.  Use of this software is strictly in accordance
# with the license agreement in the accompanying LICENSE.DOC file.
# If your distribution of this software does not contain a
# LICENSE.DOC file then you have no rights to use this software
# in any manner and should contact DSTC at the address below
# to determine an appropriate licensing arrangement.
# 
#      DSTC Pty Ltd
#      Level 7, Gehrmann Labs
#      University of Queensland
#      St Lucia, 4072
#      Australia
#      Tel: +61 7 3365 4310
#      Fax: +61 7 3365 4311
#      Email: enquiries@dstc.edu.au
# 
# This software is being provided "AS IS" without warranty of
# any kind.  In no event shall DSTC Pty Ltd be liable for
# damage of any kind arising out of or in connection with
# the use or performance of this software.
#
# Project:  Python Tools
#
# File:     ast.py
#
# Description:
#           Functions to traverse and inspect Python ASTs.
#
# History:
#           19-Dec-1995  Martin Chilvers (martin) : created
#
# "@(#)$RCSfile: ast.py,v $ $Revision: 1 $"
#
#$Log: /Gendoc/ast.py $
# 
# 1     98-04-01 13:15 Daniel
# Revision 1.2  1996/07/08  05:34:28  omfadmin
# Incorporated Fred Drake's changes (Python 1.4 support).
#
# Revision 1.1  1996/06/13  17:57:56  omfadmin
# Initial revision
#
#Revision 1.4  1995/12/19 01:43:40  martin
#Added DSTC header.
#
#
#############################################################################

""" Functions to traverse and inspect Python abstract syntax trees (ASTs). 

The ASTs must be in tuple form as produced by the function 'ast2tuple' in the
parser module.

The functions intended for public consumption are:-

    'map_fn'        map a function to every node in an AST.
    'search'        search an AST for nodes of particular types.
    'match'         compare two ASTs using some simple pattern matching.
    'terminals'     return a list of all terminals in an AST.
    'nonterminals'  return a list of all non-terminals in an AST.
    'str'           return a string that is the concatenation of all the
                    terminals in an AST.
    'node_type'     return the type of an AST node.
    'node_body'     return the body of an AST node (ie. the node minus its
                    type).

All other functions are intended for private use only (but you can use them
if you *really* want to ;^)

"""

# Built-in/standard modules.
import types
import string

# Local modules.
import token
import symbol


# Prefix to identify pattern matching variables.
AST_PATTERN = '$$$$'


def map_fn(node, fn):
    """ Map a function to every node in an AST (depth-first).

        'node'  is the root of the AST.
        'fn'    is the function to map to every node.

    """

    if len(node) > 0:
	apply(fn, (node,))

	if token.ISNONTERMINAL(node_type(node)):
	    for child in node_body(node):
		map_fn(child, fn)

    return


def search(node, lst_tokens, lst_found, n = 0):
    """ Search an AST for nodes of particular types.

    'node'        is the root of the AST.
    'lst_tokens'  is the list of tokens to search for eg:-
                  [token.INDENT, token.DEDENT, symbol.fpdef]
    'lst_found'   is the list of matching nodes (output).
    'n'           is the number of nodes to match. If 'n' is zero (the
                  default) then ALL matching nodes are listed.

    Returns (a reference to) the list of matching nodes.
    
    """

    if len(node) > 0:
	if node_type(node) in lst_tokens:
	    lst_found.append(node)

	if token.ISNONTERMINAL(node_type(node)):
	    for child in node_body(node):
		search(child, lst_tokens, lst_found, n)
		if n != 0 and len(lst_found) >= n:
		    break

    return lst_found


def match(x, y, d_matches):
    """ Compare two ASTs using some simple(!!) pattern matching.

    'x'          is the AST to match (cannot contain patterns!).
    'y'          is the AST to match WITH (CAN contain patterns!).
    'd_matches'  is the dictionary of matched patterns (output).

    Returns true if the ASTs match, otherwise false.

    Patterns are specified in 'y' as strings prefixed with 'AST_PATTERN' (eg.
    my_pattern = AST_PATTERN + 'FOOBAR'). If a pattern match is found then the
    element of 'x' that matches the pattern is added to the dictionary
    'd_matches' with 'my_pattern' as the key. Terminals can be matched on both
    their type AND their value, whereas non-terminals can only be matched at
    the node level.

    eg. 1

    (token.STRING, 'Value') matches (AST_PATTERN + 'Type', AST_PATTERN + 'Doc')

    where d_matches[AST_PATTERN + 'Type'] = token.STRING
    and   d_matches[AST_PATTERN + 'Doc']  = 'Value'

    eg. 2

    (symbol.fpdef, ..rest of node..) matches (AST_PATTERN + 'ANode')

    where d_matches[AST_PATTERN + 'Anode'] = (symbol.fpdef, ..rest of node..)

    """

    if len(x) > 0:
	if token.ISTERMINAL(node_type(x)):
	    result = match_terminal(x, y, d_matches)

	else:
	    result = match_nonterminal(x, y, d_matches)

    else:
	result = len(y) == 0

    return result


def terminals(node, lst_found):
    """ Return a list of all terminals in an AST.

    'node'       is the root of the AST.
    'lst_found'  is the list of all terminals in the AST (output).

    Returns (a reference to) the list of terminals found.

    """

    if len(node) > 0:
	if token.ISTERMINAL(node_type(node)):
	    lst_found.append(node)
	else:
	    for child in node_body(node):
		terminals(child, lst_found)

    return lst_found


def nonterminals(node, lst_found):
    """ Return a list of all non-terminals in an AST.

    'node'       is the root of the AST.
    'lst_found'  is the list of all non-terminals in the AST (output).

    Returns (a reference to) the list of terminals found.

    """

    if len(node) > 0:
	if token.ISNONTERMINAL(node_type(node)):
	    lst_found.append(node)

	    for child in node_body(node):
		nonterminals(child, lst_found)

    return lst_found


def str(node, sep = ''):
    """ Return the string concatenation of all the terminals in an AST.

    'node'  is root of the AST.
    'sep'   is the string used to separate each terminal.

    Returns a string containing all of the terminals separated by 'sep'.

    """

    lst_terminals = terminals(node, [])
    return string.joinfields(map(lambda (a, b): b, lst_terminals), sep)


def node_type(node):
    """ Returns the type of an AST node (ie. its token/symbol value). """

    return node[0]


def node_body(node):
    """ Returns the body of an AST node (ie. the node minus its type!). """

    return node[1:]


def is_pattern(x):
    """ Determine whether or not an item is a pattern matching variable. """

    return type(x) == types.StringType and x[:len(AST_PATTERN)] == AST_PATTERN


def match_item(x, y, d_matches):
    """ Compare two node items using some simple pattern matching! """

    if is_pattern(y):
	d_matches[y] = x
	result = 1

    else:
	result = x == y

    return result


def match_terminal((x_type, x_value), (y_type, y_value), d_matches):
    """ Compare two terminals. """

    return (match_item(x_type, y_type, d_matches)
	    and match_item(x_value, y_value, d_matches))


def match_nonterminal(x, y, d_matches):
    """ Compare two nonterminals. """

    result = (match_item(x, y, d_matches)
	      or match_item(node_type(x), node_type(y), d_matches))

    if result:
	for i in range (len(node_body(x))):
	    result = match(node_body(x)[i],
			   node_body(y)[i], d_matches)
	    if not result:
		break

    return result