MCPcopy
hub / github.com/rocky/python-uncompyle6 / customize_grammar_rules

Method customize_grammar_rules

uncompyle6/parsers/parse2.py:238–679  ·  view source on GitHub ↗

The base grammar we start out for a Python version even with the subclassing is, well, is pretty base. And we want it that way: lean and mean so that parsing will go faster. Here, we add additional grammar rules based on specific instructions that are in the instruc

(self, tokens, customize)

Source from the content-addressed store, hash-verified

236 """
237
238 def customize_grammar_rules(self, tokens, customize):
239 """The base grammar we start out for a Python version even with the
240 subclassing is, well, is pretty base. And we want it that way: lean and
241 mean so that parsing will go faster.
242
243 Here, we add additional grammar rules based on specific instructions
244 that are in the instruction/token stream. In classes that
245 inherit from from here and other versions, grammar rules may
246 also be removed.
247
248 For example if we see a pretty rare JUMP_IF_NOT_DEBUG
249 instruction we'll add the grammar for that.
250
251 More importantly, here we add grammar rules for instructions
252 that may access a variable number of stack items. CALL_FUNCTION,
253 BUILD_LIST and so on are like this.
254
255 Without custom rules, there can be an super-exponential number of
256 derivations. See the deparsing paper for an elaboration of
257 this.
258 """
259
260 if "PyPy" in customize:
261 # PyPy-specific customizations
262 self.addRule(
263 """
264 stmt ::= assign3_pypy
265 stmt ::= assign2_pypy
266 assign3_pypy ::= expr expr expr store store store
267 assign2_pypy ::= expr expr store store
268 list_comp ::= expr BUILD_LIST_FROM_ARG for_iter store list_iter
269 JUMP_BACK
270 """,
271 nop_func,
272 )
273
274 # For a rough break out on the first word. This may
275 # include instructions that don't need customization,
276 # but we'll do a finer check after the rough breakout.
277 customize_instruction_basenames = frozenset(
278 (
279 "BUILD",
280 "CALL",
281 "CONTINUE",
282 "DELETE",
283 "DUP",
284 "EXEC",
285 "GET",
286 "JUMP",
287 "LOAD",
288 "LOOKUP",
289 "MAKE",
290 "SETUP",
291 "RAISE",
292 "UNPACK",
293 )
294 )
295

Callers 1

parseFunction · 0.45

Calls 4

add_unique_ruleMethod · 0.80
add_unique_rulesMethod · 0.80
get_pos_kwMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected