_lua_builtins.py 8.1 KB

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