def _resolve_rule_refs(self, grammar_parser, model_parser):
        """Resolves parser ParsingExpression crossrefs."""

        def _resolve_rule(rule):
            """
            Recursively resolve peg rule references.

            Args:
                rule(ParsingExpression or RuleCrossRef)
            """
            if not isinstance(rule, RuleCrossRef) and rule in resolved_rules:
                return rule
            resolved_rules.add(rule)

            if grammar_parser.debug:
                grammar_parser.dprint("Resolving rule: {}".format(rule))

            if type(rule) is RuleCrossRef:
                rule_name = rule.rule_name
                suppress = rule.suppress
                if rule_name in model_parser.metamodel:
                    rule = model_parser.metamodel[rule_name]._tx_peg_rule
                    if type(rule) is RuleCrossRef:
                        rule = _resolve_rule(rule)
                        model_parser.metamodel[rule_name]._tx_peg_rule = rule
                    if suppress:
                        # Special case. Suppression on rule reference.
                        _tx_class = rule._tx_class
                        rule = Sequence(nodes=[rule],
                                        rule_name=rule_name,
                                        suppress=suppress)
                        rule._tx_class = _tx_class
                else:
                    line, col = grammar_parser.pos_to_linecol(rule.position)
                    raise TextXSemanticError(
                        'Unexisting rule "{}" at position {}.'
                        .format(rule.rule_name,
                                (line, col)), line, col)

            assert isinstance(rule, ParsingExpression),\
                "{}:{}".format(type(rule), text(rule))

            # Recurse into subrules, and resolve rules.
            for idx, child in enumerate(rule.nodes):
                if child not in resolved_rules:
                    child = _resolve_rule(child)
                    rule.nodes[idx] = child

            return rule

        # Two pass resolving
        for i in range(2):
            if grammar_parser.debug:
                grammar_parser.dprint("RESOLVING RULE CROSS-REFS - PASS {}"
                                      .format(i + 1))

            resolved_rules = set()
            _resolve_rule(model_parser.parser_model)

            # Resolve rules of all meta-classes to handle unreferenced
            # rules also.
            for cls in model_parser.metamodel:
                cls._tx_peg_rule = _resolve_rule(cls._tx_peg_rule)
def _resolve_rule_refs ( self , grammar_parser , model_parser ) : def _resolve_rule ( rule ) : if not isinstance ( rule , RuleCrossRef ) and rule in resolved_rules : return rule resolved_rules . add ( rule ) if grammar_parser . debug : grammar_parser . dprint ( "Resolving rule: {}" . format ( rule ) ) if type ( rule ) is RuleCrossRef : rule_name = rule . rule_name suppress = rule . suppress if rule_name in model_parser . metamodel : rule = model_parser . metamodel [ rule_name ] . _tx_peg_rule if type ( rule ) is RuleCrossRef : rule = _resolve_rule ( rule ) model_parser . metamodel [ rule_name ] . _tx_peg_rule = rule if suppress : _tx_class = rule . _tx_class rule = Sequence ( nodes = [ rule ] , rule_name = rule_name , suppress = suppress ) rule . _tx_class = _tx_class else : line , col = grammar_parser . pos_to_linecol ( rule . position ) raise TextXSemanticError ( 'Unexisting rule "{}" at position {}.' . format ( rule . rule_name , ( line , col ) ) , line , col ) assert isinstance ( rule , ParsingExpression ) , "{}:{}" . format ( type ( rule ) , text ( rule ) ) for idx , child in enumerate ( rule . nodes ) : if child not in resolved_rules : child = _resolve_rule ( child ) rule . nodes [ idx ] = child return rule for i in range ( 2 ) : if grammar_parser . debug : grammar_parser . dprint ( "RESOLVING RULE CROSS-REFS - PASS {}" . format ( i + 1 ) ) resolved_rules = set ( ) _resolve_rule ( model_parser . parser_model ) for cls in model_parser . metamodel : cls . _tx_peg_rule = _resolve_rule ( cls . _tx_peg_rule )
Resolves parser ParsingExpression crossrefs.