| 1 | |
|---|
| 2 | """ |
|---|
| 3 | ******************************************************************************* |
|---|
| 4 | Copyright 2008 Sun Microsystems, Inc., |
|---|
| 5 | 4150 Network Circle, Santa Clara, California 95054, U.S.A. |
|---|
| 6 | All rights reserved. |
|---|
| 7 | |
|---|
| 8 | U.S. Government Rights - Commercial software. |
|---|
| 9 | Government users are subject to the Sun Microsystems, Inc. standard |
|---|
| 10 | license agreement and applicable provisions of the FAR and its supplements. |
|---|
| 11 | |
|---|
| 12 | Use is subject to license terms. |
|---|
| 13 | |
|---|
| 14 | This distribution may include materials developed by third parties. |
|---|
| 15 | |
|---|
| 16 | Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered |
|---|
| 17 | trademarks of Sun Microsystems, Inc. in the U.S. and other countries. |
|---|
| 18 | ******************************************************************************* |
|---|
| 19 | |
|---|
| 20 | This script simply looks up the definitions and comments for some Fortress AST nodes. |
|---|
| 21 | """ |
|---|
| 22 | import sys, re, os, optparse |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | FORTRESS_AST_PATH = './Fortress.ast' |
|---|
| 26 | |
|---|
| 27 | def node_definition(names, options): |
|---|
| 28 | """Return the given node's full definition, or None if it could not be found.""" |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 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 | |
|---|
| 35 | lines = [] |
|---|
| 36 | index = 0 |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 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 | |
|---|
| 45 | start_def = None |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | start_comment = None |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | f = open(FORTRESS_AST_PATH, 'r+') |
|---|
| 52 | for line in f: |
|---|
| 53 | lines.append(line) |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | if line.lstrip().startswith('/*'): |
|---|
| 57 | start_comment = index |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | if start_def is None: |
|---|
| 61 | match = regex.match(line) |
|---|
| 62 | if match: start_def = index # Found node; save the index. |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | if start_def is not None and ';' in line: |
|---|
| 66 | num_spaces = len(match.group(1)) |
|---|
| 67 | name = match.group(2) |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | read_from = start_def |
|---|
| 71 | if lines[start_def-1].strip() == '*/': |
|---|
| 72 | read_from = start_comment |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | yield ("".join(s[num_spaces-2:] for s in lines[read_from:]), True) |
|---|
| 76 | start_def = None |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | if name in must_find: must_find.remove(name) |
|---|
| 80 | |
|---|
| 81 | index += 1 |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | for name in must_find: |
|---|
| 85 | yield ("error: Node %s was not found in %s." % (name, FORTRESS_AST_PATH), False) |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | if __name__ == '__main__': |
|---|
| 89 | |
|---|
| 90 | os.chdir(os.path.dirname(sys.argv[0])) |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 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 | |
|---|
| 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 '') |
|---|