root/trunk/ProjectFortress/astgen/lookup.py

Revision 2255, 4.0 KB (checked in by skilpat, 17 months ago)

[utility] More info in astgen/lookup.py help output.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2"""
3*******************************************************************************
4Copyright 2008 Sun Microsystems, Inc.,
54150 Network Circle, Santa Clara, California 95054, U.S.A.
6All rights reserved.
7
8U.S. Government Rights - Commercial software.
9Government users are subject to the Sun Microsystems, Inc. standard
10license agreement and applicable provisions of the FAR and its supplements.
11
12Use is subject to license terms.
13
14This distribution may include materials developed by third parties.
15
16Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
17trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
18*******************************************************************************
19
20This script simply looks up the definitions and comments for some Fortress AST nodes.
21"""
22import sys, re, os, optparse
23
24# The relative path to the file containing node definitions.
25FORTRESS_AST_PATH = './Fortress.ast'
26
27def node_definition(names, options):
28    """Return the given node's full definition, or None if it could not be found."""
29   
30    # Make the names unique.
31    names = set([name.replace('*', '[a-zA-Z]*') for name in names])
32    must_find = [name for name in names if name.isalpha()]
33   
34    # Keep a history of lines so we can backtrack to get comments.
35    lines = []
36    index = 0
37   
38    # Regex to check for node definitions.
39    regex_flags = 0
40    if options.I: regex_flags |= re.I
41    regex = re.compile(r"^(\s+)(?:abstract )?(%s)\(" % '|'.join(names), regex_flags)
42    match = None
43   
44    # Index of the line that starts a definition.
45    start_def = None
46   
47    # Index of the line that starts the most recent comment.
48    start_comment = None
49   
50    # Open file and loop over it line by line.
51    f = open(FORTRESS_AST_PATH, 'r+')
52    for line in f:
53        lines.append(line)
54       
55        # If beginning a comment, save this index.
56        if line.lstrip().startswith('/*'):
57            start_comment = index
58       
59        # If looking for a definition.
60        if start_def is None:
61            match = regex.match(line)
62            if match: start_def = index # Found node; save the index.
63       
64        # If reached end of definition.
65        if start_def is not None and ';' in line:
66            num_spaces = len(match.group(1))
67            name = match.group(2)
68           
69            # Start reading from the definition's line, or its comment's line.
70            read_from = start_def
71            if lines[start_def-1].strip() == '*/':
72                read_from = start_comment
73               
74            # Yield and reset for next node.
75            yield ("".join(s[num_spaces-2:] for s in lines[read_from:]), True)
76            start_def = None
77           
78            # Remove it from the list of names to find.
79            if name in must_find: must_find.remove(name)
80       
81        index += 1
82   
83    # Yield the ones not found.
84    for name in must_find:
85        yield ("error: Node %s was not found in %s." % (name, FORTRESS_AST_PATH), False)
86
87
88if __name__ == '__main__':
89    # Change into the script's dir.
90    os.chdir(os.path.dirname(sys.argv[0]))
91   
92    # Setup the options parser and help text.
93    usage = "usage: %prog [options] node [...]"
94    description = "Prints the definition and comments for each of the given nodes. Each node is " \
95                  "a simple string. Asterisk (*) may be used as a wildcard, e.g. *Id* *Name Trait*"
96    optparser = optparse.OptionParser(usage=usage, description=description)
97    optparser.add_option("-i",
98                         action="store_true", dest="I", default=False,
99                         help="case insensitive search")
100    (options, args) = optparser.parse_args()
101    if not args:
102        optparser.print_help()
103        sys.exit()
104   
105    # Print the definition for each node given.
106    print ""
107    count = 0
108    for definition, found in node_definition(args, options):
109        print definition
110        if found: count += 1
111    print "notice: found %d match%s" % (count, count != 1 and 'es' or ''
Note: See TracBrowser for help on using the browser.