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