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