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

Method customize_grammar_rules

uncompyle6/parsers/parse3.py:654–1648  ·  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

652 self.add_unique_rule(new_rule, opname, attr, customize)
653
654 def customize_grammar_rules(self, tokens, customize):
655 """The base grammar we start out for a Python version even with the
656 subclassing is, well, is pretty base. And we want it that way: lean and
657 mean so that parsing will go faster.
658
659 Here, we add additional grammar rules based on specific instructions
660 that are in the instruction/token stream. In classes that
661 inherit from from here and other versions, grammar rules may
662 also be removed.
663
664 For example if we see a pretty rare DELETE_DEREF instruction we'll
665 add the grammar for that.
666
667 More importantly, here we add grammar rules for instructions
668 that may access a variable number of stack items. CALL_FUNCTION,
669 BUILD_LIST and so on are like this.
670
671 Without custom rules, there can be an super-exponential number of
672 derivations. See the deparsing paper for an elaboration of
673 this.
674
675 """
676
677 self.is_pypy = False
678
679 # For a rough break out on the first word. This may
680 # include instructions that don't need customization,
681 # but we'll do a finer check after the rough breakout.
682 customize_instruction_basenames = frozenset(
683 (
684 "BUILD",
685 "CALL",
686 "CONTINUE",
687 "DELETE",
688 "GET",
689 "JUMP",
690 "LOAD",
691 "LOOKUP",
692 "MAKE",
693 "RETURN",
694 "RAISE",
695 "SETUP",
696 "UNPACK",
697 "WITH",
698 )
699 )
700
701 # Opcode names in the custom_ops_processed set have rules that get added
702 # unconditionally and the rules are constant. So they need to be done
703 # only once and if we see the opcode a second we don't have to consider
704 # adding more rules.
705 #
706 # Note: BUILD_TUPLE_UNPACK_WITH_CALL gets considered by
707 # default because it starts with BUILD. So we'll set to ignore it from
708 # the start.
709 custom_ops_processed = {"BUILD_TUPLE_UNPACK_WITH_CALL"}
710
711 # A set of instruction operation names that exist in the token stream.

Callers

nothing calls this directly

Calls 8

call_fn_nameMethod · 0.95
custom_classfunc_ruleMethod · 0.95
add_unique_ruleMethod · 0.80
add_unique_rulesMethod · 0.80
get_pos_kwMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected