Back to index

moin  1.9.0~rc2
_luabuiltins.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     pygments.lexers._luabuiltins
00004     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00005 
00006     This file contains the names and modules of lua functions
00007     It is able to re-generate itself, but for adding new functions you
00008     probably have to add some callbacks (see function module_callbacks).
00009 
00010     Do not edit the MODULES dict by hand.
00011 
00012     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00013     :license: BSD, see LICENSE for details.
00014 """
00015 
00016 MODULES = {'basic': ['_G',
00017            '_VERSION',
00018            'assert',
00019            'collectgarbage',
00020            'dofile',
00021            'error',
00022            'getfenv',
00023            'getmetatable',
00024            'ipairs',
00025            'load',
00026            'loadfile',
00027            'loadstring',
00028            'next',
00029            'pairs',
00030            'pcall',
00031            'print',
00032            'rawequal',
00033            'rawget',
00034            'rawset',
00035            'select',
00036            'setfenv',
00037            'setmetatable',
00038            'tonumber',
00039            'tostring',
00040            'type',
00041            'unpack',
00042            'xpcall'],
00043  'coroutine': ['coroutine.create',
00044                'coroutine.resume',
00045                'coroutine.running',
00046                'coroutine.status',
00047                'coroutine.wrap',
00048                'coroutine.yield'],
00049  'debug': ['debug.debug',
00050            'debug.getfenv',
00051            'debug.gethook',
00052            'debug.getinfo',
00053            'debug.getlocal',
00054            'debug.getmetatable',
00055            'debug.getregistry',
00056            'debug.getupvalue',
00057            'debug.setfenv',
00058            'debug.sethook',
00059            'debug.setlocal',
00060            'debug.setmetatable',
00061            'debug.setupvalue',
00062            'debug.traceback'],
00063  'io': ['file:close',
00064         'file:flush',
00065         'file:lines',
00066         'file:read',
00067         'file:seek',
00068         'file:setvbuf',
00069         'file:write',
00070         'io.close',
00071         'io.flush',
00072         'io.input',
00073         'io.lines',
00074         'io.open',
00075         'io.output',
00076         'io.popen',
00077         'io.read',
00078         'io.tmpfile',
00079         'io.type',
00080         'io.write'],
00081  'math': ['math.abs',
00082           'math.acos',
00083           'math.asin',
00084           'math.atan2',
00085           'math.atan',
00086           'math.ceil',
00087           'math.cosh',
00088           'math.cos',
00089           'math.deg',
00090           'math.exp',
00091           'math.floor',
00092           'math.fmod',
00093           'math.frexp',
00094           'math.huge',
00095           'math.ldexp',
00096           'math.log10',
00097           'math.log',
00098           'math.max',
00099           'math.min',
00100           'math.modf',
00101           'math.pi',
00102           'math.pow',
00103           'math.rad',
00104           'math.random',
00105           'math.randomseed',
00106           'math.sinh',
00107           'math.sin',
00108           'math.sqrt',
00109           'math.tanh',
00110           'math.tan'],
00111  'modules': ['module',
00112              'require',
00113              'package.cpath',
00114              'package.loaded',
00115              'package.loadlib',
00116              'package.path',
00117              'package.preload',
00118              'package.seeall'],
00119  'os': ['os.clock',
00120         'os.date',
00121         'os.difftime',
00122         'os.execute',
00123         'os.exit',
00124         'os.getenv',
00125         'os.remove',
00126         'os.rename',
00127         'os.setlocale',
00128         'os.time',
00129         'os.tmpname'],
00130  'string': ['string.byte',
00131             'string.char',
00132             'string.dump',
00133             'string.find',
00134             'string.format',
00135             'string.gmatch',
00136             'string.gsub',
00137             'string.len',
00138             'string.lower',
00139             'string.match',
00140             'string.rep',
00141             'string.reverse',
00142             'string.sub',
00143             'string.upper'],
00144  'table': ['table.concat',
00145            'table.insert',
00146            'table.maxn',
00147            'table.remove',
00148            'table.sort']}
00149 
00150 if __name__ == '__main__':
00151     import re
00152     import urllib
00153     import pprint
00154 
00155     # you can't generally find out what module a function belongs to if you
00156     # have only its name. Because of this, here are some callback functions
00157     # that recognize if a gioven function belongs to a specific module
00158     def module_callbacks():
00159         def is_in_coroutine_module(name):
00160             return name.startswith('coroutine.')
00161 
00162         def is_in_modules_module(name):
00163             if name in ['require', 'module'] or name.startswith('package'):
00164                 return True
00165             else:
00166                 return False
00167 
00168         def is_in_string_module(name):
00169             return name.startswith('string.')
00170 
00171         def is_in_table_module(name):
00172             return name.startswith('table.')
00173 
00174         def is_in_math_module(name):
00175             return name.startswith('math')
00176 
00177         def is_in_io_module(name):
00178             return name.startswith('io.') or name.startswith('file:')
00179 
00180         def is_in_os_module(name):
00181             return name.startswith('os.')
00182 
00183         def is_in_debug_module(name):
00184             return name.startswith('debug.')
00185 
00186         return {'coroutine': is_in_coroutine_module,
00187                 'modules': is_in_modules_module,
00188                 'string': is_in_string_module,
00189                 'table': is_in_table_module,
00190                 'math': is_in_math_module,
00191                 'io': is_in_io_module,
00192                 'os': is_in_os_module,
00193                 'debug': is_in_debug_module}
00194 
00195 
00196 
00197     def get_newest_version():
00198         f = urllib.urlopen('http://www.lua.org/manual/')
00199         r = re.compile(r'^<A HREF="(\d\.\d)/">Lua \1</A>')
00200         for line in f:
00201             m = r.match(line)
00202             if m is not None:
00203                 return m.groups()[0]
00204 
00205     def get_lua_functions(version):
00206         f = urllib.urlopen('http://www.lua.org/manual/%s/' % version)
00207         r = re.compile(r'^<A HREF="manual.html#pdf-(.+)">\1</A>')
00208         functions = []
00209         for line in f:
00210             m = r.match(line)
00211             if m is not None:
00212                 functions.append(m.groups()[0])
00213         return functions
00214 
00215     def get_function_module(name):
00216         for mod, cb in module_callbacks().iteritems():
00217             if cb(name):
00218                 return mod
00219         if '.' in name:
00220             return name.split('.')[0]
00221         else:
00222             return 'basic'
00223 
00224     def regenerate(filename, modules):
00225         f = open(filename)
00226         try:
00227             content = f.read()
00228         finally:
00229             f.close()
00230 
00231         header = content[:content.find('MODULES = {')]
00232         footer = content[content.find("if __name__ == '__main__':"):]
00233 
00234 
00235         f = open(filename, 'w')
00236         f.write(header)
00237         f.write('MODULES = %s\n\n' % pprint.pformat(modules))
00238         f.write(footer)
00239         f.close()
00240 
00241     def run():
00242         version = get_newest_version()
00243         print '> Downloading function index for Lua %s' % version
00244         functions = get_lua_functions(version)
00245         print '> %d functions found:' % len(functions)
00246 
00247         modules = {}
00248         for full_function_name in functions:
00249             print '>> %s' % full_function_name
00250             m = get_function_module(full_function_name)
00251             modules.setdefault(m, []).append(full_function_name)
00252 
00253         regenerate(__file__, modules)
00254 
00255 
00256     run()