_lua_builtins.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers._lua_builtins
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. This file contains the names and modules of lua functions
  6. It is able to re-generate itself, but for adding new functions you
  7. probably have to add some callbacks (see function module_callbacks).
  8. Do not edit the MODULES dict by hand.
  9. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  10. :license: BSD, see LICENSE for details.
  11. """
  12. from __future__ import print_function
  13. MODULES = {'basic': ('_G',
  14. '_VERSION',
  15. 'assert',
  16. 'collectgarbage',
  17. 'dofile',
  18. 'error',
  19. 'getmetatable',
  20. 'ipairs',
  21. 'load',
  22. 'loadfile',
  23. 'next',
  24. 'pairs',
  25. 'pcall',
  26. 'print',
  27. 'rawequal',
  28. 'rawget',
  29. 'rawlen',
  30. 'rawset',
  31. 'select',
  32. 'setmetatable',
  33. 'tonumber',
  34. 'tostring',
  35. 'type',
  36. 'xpcall'),
  37. 'bit32': ('bit32.arshift',
  38. 'bit32.band',
  39. 'bit32.bnot',
  40. 'bit32.bor',
  41. 'bit32.btest',
  42. 'bit32.bxor',
  43. 'bit32.extract',
  44. 'bit32.lrotate',
  45. 'bit32.lshift',
  46. 'bit32.replace',
  47. 'bit32.rrotate',
  48. 'bit32.rshift'),
  49. 'coroutine': ('coroutine.create',
  50. 'coroutine.isyieldable',
  51. 'coroutine.resume',
  52. 'coroutine.running',
  53. 'coroutine.status',
  54. 'coroutine.wrap',
  55. 'coroutine.yield'),
  56. 'debug': ('debug.debug',
  57. 'debug.gethook',
  58. 'debug.getinfo',
  59. 'debug.getlocal',
  60. 'debug.getmetatable',
  61. 'debug.getregistry',
  62. 'debug.getupvalue',
  63. 'debug.getuservalue',
  64. 'debug.sethook',
  65. 'debug.setlocal',
  66. 'debug.setmetatable',
  67. 'debug.setupvalue',
  68. 'debug.setuservalue',
  69. 'debug.traceback',
  70. 'debug.upvalueid',
  71. 'debug.upvaluejoin'),
  72. 'io': ('io.close',
  73. 'io.flush',
  74. 'io.input',
  75. 'io.lines',
  76. 'io.open',
  77. 'io.output',
  78. 'io.popen',
  79. 'io.read',
  80. 'io.stderr',
  81. 'io.stdin',
  82. 'io.stdout',
  83. 'io.tmpfile',
  84. 'io.type',
  85. 'io.write'),
  86. 'math': ('math.abs',
  87. 'math.acos',
  88. 'math.asin',
  89. 'math.atan',
  90. 'math.atan2',
  91. 'math.ceil',
  92. 'math.cos',
  93. 'math.cosh',
  94. 'math.deg',
  95. 'math.exp',
  96. 'math.floor',
  97. 'math.fmod',
  98. 'math.frexp',
  99. 'math.huge',
  100. 'math.ldexp',
  101. 'math.log',
  102. 'math.max',
  103. 'math.maxinteger',
  104. 'math.min',
  105. 'math.mininteger',
  106. 'math.modf',
  107. 'math.pi',
  108. 'math.pow',
  109. 'math.rad',
  110. 'math.random',
  111. 'math.randomseed',
  112. 'math.sin',
  113. 'math.sinh',
  114. 'math.sqrt',
  115. 'math.tan',
  116. 'math.tanh',
  117. 'math.tointeger',
  118. 'math.type',
  119. 'math.ult'),
  120. 'modules': ('package.config',
  121. 'package.cpath',
  122. 'package.loaded',
  123. 'package.loadlib',
  124. 'package.path',
  125. 'package.preload',
  126. 'package.searchers',
  127. 'package.searchpath',
  128. 'require'),
  129. 'os': ('os.clock',
  130. 'os.date',
  131. 'os.difftime',
  132. 'os.execute',
  133. 'os.exit',
  134. 'os.getenv',
  135. 'os.remove',
  136. 'os.rename',
  137. 'os.setlocale',
  138. 'os.time',
  139. 'os.tmpname'),
  140. 'string': ('string.byte',
  141. 'string.char',
  142. 'string.dump',
  143. 'string.find',
  144. 'string.format',
  145. 'string.gmatch',
  146. 'string.gsub',
  147. 'string.len',
  148. 'string.lower',
  149. 'string.match',
  150. 'string.pack',
  151. 'string.packsize',
  152. 'string.rep',
  153. 'string.reverse',
  154. 'string.sub',
  155. 'string.unpack',
  156. 'string.upper'),
  157. 'table': ('table.concat',
  158. 'table.insert',
  159. 'table.move',
  160. 'table.pack',
  161. 'table.remove',
  162. 'table.sort',
  163. 'table.unpack'),
  164. 'utf8': ('utf8.char',
  165. 'utf8.charpattern',
  166. 'utf8.codepoint',
  167. 'utf8.codes',
  168. 'utf8.len',
  169. 'utf8.offset')}
  170. if __name__ == '__main__': # pragma: no cover
  171. import re
  172. import sys
  173. # urllib ends up wanting to import a module called 'math' -- if
  174. # pygments/lexers is in the path, this ends badly.
  175. for i in range(len(sys.path)-1, -1, -1):
  176. if sys.path[i].endswith('/lexers'):
  177. del sys.path[i]
  178. try:
  179. from urllib import urlopen
  180. except ImportError:
  181. from urllib.request import urlopen
  182. import pprint
  183. # you can't generally find out what module a function belongs to if you
  184. # have only its name. Because of this, here are some callback functions
  185. # that recognize if a gioven function belongs to a specific module
  186. def module_callbacks():
  187. def is_in_coroutine_module(name):
  188. return name.startswith('coroutine.')
  189. def is_in_modules_module(name):
  190. if name in ['require', 'module'] or name.startswith('package'):
  191. return True
  192. else:
  193. return False
  194. def is_in_string_module(name):
  195. return name.startswith('string.')
  196. def is_in_table_module(name):
  197. return name.startswith('table.')
  198. def is_in_math_module(name):
  199. return name.startswith('math')
  200. def is_in_io_module(name):
  201. return name.startswith('io.')
  202. def is_in_os_module(name):
  203. return name.startswith('os.')
  204. def is_in_debug_module(name):
  205. return name.startswith('debug.')
  206. return {'coroutine': is_in_coroutine_module,
  207. 'modules': is_in_modules_module,
  208. 'string': is_in_string_module,
  209. 'table': is_in_table_module,
  210. 'math': is_in_math_module,
  211. 'io': is_in_io_module,
  212. 'os': is_in_os_module,
  213. 'debug': is_in_debug_module}
  214. def get_newest_version():
  215. f = urlopen('http://www.lua.org/manual/')
  216. r = re.compile(r'^<A HREF="(\d\.\d)/">(Lua )?\1</A>')
  217. for line in f:
  218. m = r.match(line)
  219. if m is not None:
  220. return m.groups()[0]
  221. def get_lua_functions(version):
  222. f = urlopen('http://www.lua.org/manual/%s/' % version)
  223. r = re.compile(r'^<A HREF="manual.html#pdf-(?!lua|LUA)([^:]+)">\1</A>')
  224. functions = []
  225. for line in f:
  226. m = r.match(line)
  227. if m is not None:
  228. functions.append(m.groups()[0])
  229. return functions
  230. def get_function_module(name):
  231. for mod, cb in module_callbacks().items():
  232. if cb(name):
  233. return mod
  234. if '.' in name:
  235. return name.split('.')[0]
  236. else:
  237. return 'basic'
  238. def regenerate(filename, modules):
  239. with open(filename) as fp:
  240. content = fp.read()
  241. header = content[:content.find('MODULES = {')]
  242. footer = content[content.find("if __name__ == '__main__':"):]
  243. with open(filename, 'w') as fp:
  244. fp.write(header)
  245. fp.write('MODULES = %s\n\n' % pprint.pformat(modules))
  246. fp.write(footer)
  247. def run():
  248. version = get_newest_version()
  249. functions = set()
  250. for v in ('5.2', version):
  251. print('> Downloading function index for Lua %s' % v)
  252. f = get_lua_functions(v)
  253. print('> %d functions found, %d new:' %
  254. (len(f), len(set(f) - functions)))
  255. functions |= set(f)
  256. functions = sorted(functions)
  257. modules = {}
  258. for full_function_name in functions:
  259. print('>> %s' % full_function_name)
  260. m = get_function_module(full_function_name)
  261. modules.setdefault(m, []).append(full_function_name)
  262. modules = {k: tuple(v) for k, v in modules.iteritems()}
  263. regenerate(__file__, modules)
  264. run()