_lua_builtins.py 7.9 KB

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