Exemplo n.º 1
0
    def _compile_impossible(self, element, *args, **kwargs):
        # Override this to avoid VoidRefs disabling entire rules/grammars.
        # Use a special <_impossible> private rule instead. Only add the
        # special rule if it isn't in the result grammar.
        grammar = args[0]
        if "_impossible" not in grammar.rule_names:
            # Check that the impossible literal contains only valid words.
            words = set(self.impossible_literal.split())
            valid_literal = bool(words)
            for word in words:
                if not valid_literal:
                    break
                if not self.engine.check_valid_word(word):
                    valid_literal = False

            if valid_literal:
                expansion = jsgf.Literal(self.impossible_literal)
            else:
                # Fallback on a NullRef. There are some problems with using
                # these, but they get the job done for simple rules.
                expansion = jsgf.NullRef()
            grammar.add_rule(
                jsgf.Rule(name="_impossible",
                          visible=False,
                          expansion=expansion))

        return jsgf.NamedRuleRef("_impossible")
Exemplo n.º 2
0
    def translate_rule_ref(self, state):
        """
        Translate the dragonfly RuleRef object inside the TranslationState
        object by collecting all of the rules as necessary using the
        'dependencies' list.
        :type state: TranslationState
        :return TranslationState
        """
        element = state.element
        if not isinstance(element, RuleRef):
            raise TranslationError("Cannot translate element '%s' as a "
                                   "RuleRef." % state.element)
        name = element.name
        if name not in state.rule_names:
            # Make a new equivalent JSGF rule
            state.element = element.rule.element
            self.get_jsgf_equiv(state)
            rule_expansion = state.expansion
            new_rule = jsgf.Rule(name, element.rule.exported, rule_expansion)
            state.element = element
            state.dependencies.append(new_rule)
            state.expansion = jsgf.RuleRef(new_rule)
        else:
            # Use the existing rule
            index = state.rule_names.index(name)
            state.expansion = jsgf.RuleRef(state.dependencies[index])

        return state
Exemplo n.º 3
0
 def test_using_rule_ref(self):
     rule1 = Rule("rule", Literal("hello"), exported=True)
     rule_ref = RuleRef(rule1, name="rule_ref")
     state = TranslationState(rule_ref)
     self.translator.translate_rule_ref(state)
     self.assertEqual(state.element, rule_ref)
     expected_jsgf_rule = jsgf.Rule("rule_ref", True, "hello")
     self.assertListEqual(state.dependencies, [expected_jsgf_rule])
     self.assertEqual(state.expansion, jsgf.RuleRef(expected_jsgf_rule))
Exemplo n.º 4
0
 def compile_rule(self, rule, *args, **kwargs):
     return jsgf.Rule(name=self.get_reference_name(rule),
                      visible=rule.exported,
                      expansion=self.compile_element(
                          rule.element, *args, **kwargs))
Exemplo n.º 5
0
 def translate_to_rule(self, rule_name, df_spec, visible=True):
     expansion = self.translate(df_spec).expansion
     return jsgf.Rule(rule_name, visible, expansion)