def __init__(self, name=None, element=None, exported=None, context=None): if name is None: name = self.__class__.__name__ if context is None: context = self.context # Complex handling of exported, because of clashing use of the # exported name at the class level: property & class-value. if exported is not None: pass elif (hasattr(self.__class__, "exported") and not isinstance(self.__class__.exported, property)): exported = self.__class__.exported else: exported = self._default_exported # Similar complex handling of element. if element is not None: pass elif (hasattr(self.__class__, "element") and not isinstance(self.__class__.element, property)): element = self.__class__.element # Type checking of initialization values. assert isinstance(name, string_types) assert isinstance(element, ElementBase) self._name = name Rule.__init__(self, self._name, element, exported=exported, context=context)
def __init__(self, name=None, mapping=None, extras=None, defaults=None, exported=None, context=None): # pylint: disable=too-many-branches if name is None: name = self.__class__.__name__ if mapping is None: mapping = self.mapping if extras is None: extras = self.extras if defaults is None: defaults = self.defaults if context is None: context = self.context # Complex handling of exported, because of clashing use of the # exported name at the class level: property & class-value. if exported is not None: pass elif (hasattr(self.__class__, "exported") and not isinstance(self.__class__.exported, property)): exported = self.__class__.exported else: exported = self._default_exported # Type checking of initialization values. assert isinstance(name, string_types) assert isinstance(mapping, dict) for key, value in mapping.items(): assert isinstance(key, string_types) assert isinstance(extras, (list, tuple)) for item in extras: assert isinstance(item, ElementBase) assert exported in (True, False) self._name = name self._mapping = mapping self._extras = {element.name: element for element in extras} self._defaults = defaults children = [] for spec, value in self._mapping.items(): c = Compound(spec, elements=self._extras, value=value) children.append(c) if children: element = Alternative(children) else: element = None Rule.__init__(self, self._name, element, exported=exported, context=context)
def __init__(self, manager): self._manager = manager name = "_recobs_grammar" Grammar.__init__(self, name, description=None, context=None) rule = Rule(element=Impossible(), exported=True) self.add_rule(rule)
def test_reference_names_with_spaces(self): """ Verify that reference names with spaces are accepted. """ lst = List("my list", ["test list"]) grammar = Grammar("My dragonfly grammar") grammar.add_rule(CompoundRule(name="my rule", spec="test rule")) grammar.add_rule(Rule(element=ListRef("my list", lst), exported=True)) try: grammar.load() self.assert_mimic_success("test rule") self.assert_mimic_success("test list") finally: grammar.unload()
def compile_grammar(self, grammar, context): self._log.debug("%s: Compiling grammar %s." % (self, grammar.name)) grammar_handle = context.CreateGrammar() fake_rule = Rule(name="_FakeRule", element=Literal("impossible " * 20), exported=True) self._compile_rule(fake_rule, grammar, grammar_handle) for rule in grammar.rules: self._compile_rule(rule, grammar, grammar_handle) grammar_handle.Rules.Commit() return grammar_handle
def __init__(self, name=None, spec=None, extras=None, defaults=None, exported=None, context=None): if name is None: name = self._name or self.__class__.__name__ if spec is None: spec = self.spec if extras is None: extras = self.extras if defaults is None: defaults = self.defaults if context is None: context = self.context # Complex handling of exported, because of clashing use of the # exported name at the class level: property & class-value. if exported is not None: pass elif (hasattr(self.__class__, "exported") and not isinstance(self.__class__.exported, property)): exported = self.__class__.exported else: exported = self._default_exported assert isinstance(name, string_types) assert isinstance(spec, string_types) assert isinstance(extras, (list, tuple)) for item in extras: assert isinstance(item, ElementBase) assert exported in (True, False) self._name = name self._spec = spec self.spec = spec self._extras = dict((element.name, element) for element in extras) self._defaults = dict(defaults) child = Compound(spec, extras=self._extras) Rule.__init__(self, name, child, exported=exported, context=context)
def __init__(self, name, element, default=None): rule_name = "_%s_%02d" % (self.__class__.__name__, RuleWrap._next_id) RuleWrap._next_id += 1 rule = Rule(name=rule_name, element=element, exported=False) RuleRef.__init__(self, rule=rule, name=name, default=default)