Beispiel #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")
Beispiel #2
0
 def _compile_list_ref(self, element, *args, **kwargs):
     name = element.list.name.replace(" ", "_")
     return jsgf.NamedRuleRef(name)