Game Development Community

ctags for TorqueScript

by Eric Roberts · 10/31/2009 (7:22 pm) · 2 comments

The following python script will recursively generate a 'tags' file through your Torquescript files. This is according to the standard published here. Programs like Vim have support for editing Torquescript and using the tags file format.

import os

def generate_tags():
    script_files = []
    tags = []

    import re
    regular_expressions = (
        (re.compile(r'^s*functions+w+::(?P<tag>w+)s*(.*$'), 'm'), #method
        (re.compile(r'^s*functions+(?P<tag>w+)s*(.*$'), 'f'), #function
        (re.compile(r'^s*$(w+::)*(?P<tag>w+)s*=s.*;$'), 'g'), #global
        (re.compile(r'^s*news+w+s*(s*(?P<tag>w+)s*).*$'), 'o'), #object instance
        (re.compile(r'^s*(super)?classs*=s*"(?P<tag>w+)"s*;$', re.IGNORECASE), 'c'), #class type
    )
    for root, dirs, files in os.walk('.'):
        script_files.extend([root + '/' + x for x in files if valid_fileformat(x)])

    for filename in script_files:
        file = open(filename)
        for line in file:
            for re in regular_expressions:
                re_obj = re[0].match(line)
                if re_obj:
                    tags.append(
                        (re_obj.group('tag'), filename, '/^%s$/;"t%s'
                            % (line[:-1], re[1])
                        )
                    )
                    break

    tags.sort()
    tag_file = open('tags', 'w')
    tag_file.writelines(
        ['%st%st%sn' % (tag[0], tag[1], tag[2]) for tag in tags]
    )
    tag_file.close()


def valid_fileformat(filename):
    file_formats = ('gui', 'cs', 'mis')

    for file_format in file_formats:
        if filename.endswith('.%s' % file_format):
            return True

    return False

if __name__ == "__main__":
    generate_tags()

#1
10/31/2009 (7:26 pm)
+1 for regular expression wizardry keeping your utility scripts manageable :)

Nice job!
#2
10/31/2009 (8:55 pm)
@Josh: Thanks!

I just noticed as well the '\' for the '\s' portion of the regular expressions didn't show up. If anyone is interested in using this, it should be pretty obvious which 's' is missing a preceeding '\' (generally it's an 's' before a '+' or a '*').