Example #1
0
    def _create_repeat_rule(self, merge_rule):
        merge_rule = merge_rule.prepare_for_merger()
        alts = [RuleRef(rule=merge_rule)
                ]  # +[RuleRef(rule=sm) for sm in selfmod]
        single_action = Alternative(alts)
        sequence = Repetition(single_action,
                              min=1,
                              max=self._max_repetitions,
                              name=CCRMerger2._SEQ)
        original = Alternative(alts, name=CCRMerger2._ORIGINAL)
        terminal = Alternative(alts, name=CCRMerger2._TERMINAL)

        class RepeatRule(CompoundRule):
            spec = "[<" + CCRMerger2._ORIGINAL + "> original] " + \
                   "[<" + CCRMerger2._SEQ + ">] " + \
                   "[terminal <" + CCRMerger2._TERMINAL + ">]"
            extras = [sequence, original, terminal]

            def _process_recognition(self, node, extras):
                _original = extras[
                    CCRMerger2.
                    _ORIGINAL] if CCRMerger2._ORIGINAL in extras else None
                _sequence = extras[
                    CCRMerger2._SEQ] if CCRMerger2._SEQ in extras else None
                _terminal = extras[
                    CCRMerger2.
                    _TERMINAL] if CCRMerger2._TERMINAL in extras else None
                if _original is not None: _original.execute()
                if _sequence is not None:
                    for action in _sequence:
                        action.execute()
                if _terminal is not None: _terminal.execute()

        return RepeatRule(name=self._get_new_rule_name())
Example #2
0
        class RepeatRule(CompoundRule):
            spec = "<%s>" % SEQ
            extras = [Repetition(single_action, min=1, max=max, name=SEQ)]

            def _process_recognition(self, node, extras):
                for action in extras[SEQ]:
                    action.execute()
Example #3
0
    def __init__(self, name=None, min=1, max=12, as_int=False):
        self._as_int = as_int
        if self._as_int: self._base = len(self._digits) - 1

        pairs = []
        for value, word in enumerate(self._digits):
            if isinstance(word, str):
                pairs.append((word, value))
            elif isinstance(word, (tuple, list)):
                pairs.extend([(w, value) for w in word])
            else:
                raise ValueError("Invalid type in digit list: %r" % word)

        alternatives = [Compound(w, value=v, name=self._digit_name)
                        for w, v in pairs]
        child = Alternative(alternatives)
        Repetition.__init__(self, child, min, max, name=name)
Example #4
0
    def __init__(self, name=None, min=1, max=12, as_int=False):
        self._as_int = as_int
        if self._as_int: self._base = len(self._digits) - 1

        pairs = []
        for value, word in enumerate(self._digits):
            if isinstance(word, str):
                pairs.append((word, value))
            elif isinstance(word, (tuple, list)):
                pairs.extend([(w, value) for w in word])
            else:
                raise ValueError("Invalid type in digit list: %r" % word)

        alternatives = [Compound(w, value=v, name=self._digit_name)
                        for w, v in pairs]
        child = Alternative(alternatives)
        Repetition.__init__(self, child, min, max, name=name)
Example #5
0
    def _create_nested_rule(self, rule):
        alts = [RuleRef(rule=rule)]  #+[RuleRef(rule=sm) for sm in selfmod]
        single_action = Alternative(alts)
        bef = Repetition(single_action, min=1, max=8, name="before")
        aft = Repetition(single_action, min=1, max=8, name="after")
        seq1 = Repetition(single_action, min=1, max=6, name="sequence1")
        seq2 = Repetition(single_action, min=1, max=6, name="sequence2")
        sing1 = Alternative(alts, name="singleton1")
        sing2 = Alternative(alts, name="singleton2")

        if rule.nested.extras is None:
            rule.nested.extras = [bef, aft, seq1, seq2, sing1, sing2]
        else:
            rule.nested.extras.extend([bef, aft, seq1, seq2, sing1, sing2])

        nested = rule.nested()
        return nested
Example #6
0
    def __init__(self, name=None, zero=False, default=None):
        name = str(name)
        int_name = "_Number_int_" + name
        if zero: int_min = 0
        else: int_min = 1
        single = Integer(None, int_min, self._int_max)

        ser_name = "_Number_ser_" + name
        item = Integer(None, 0, 100)
        if zero:
            series = Repetition(item, 1, self._ser_len)
        else:
            first = Integer(None, 1, 100)
            repetition = Repetition(item, 0, self._ser_len - 1)
            series = Sequence([first, repetition])

        children = [single, series]
        Alternative.__init__(self, children, name=name, default=default)
Example #7
0
    def _create_repeat_rule(self, rule):
        ORIGINAL, SEQ, TERMINAL = "original", "caster_base_sequence", "terminal"
        alts = [RuleRef(rule=rule)]  #+[RuleRef(rule=sm) for sm in selfmod]
        single_action = Alternative(alts)
        max = SETTINGS["max_ccr_repetitions"]
        sequence = Repetition(single_action, min=1, max=max, name=SEQ)
        original = Alternative(alts, name=ORIGINAL)
        terminal = Alternative(alts, name=TERMINAL)

        class RepeatRule(CompoundRule):
            spec = "[<" + ORIGINAL + "> original] [<" + SEQ + ">] [terminal <" + TERMINAL + ">]"
            extras = [sequence, original, terminal]

            def _process_recognition(self, node, extras):
                original = extras[ORIGINAL] if ORIGINAL in extras else None
                sequence = extras[SEQ] if SEQ in extras else None
                terminal = extras[TERMINAL] if TERMINAL in extras else None
                if original is not None: original.execute()
                if sequence is not None:
                    for action in sequence:
                        action.execute()
                if terminal is not None: terminal.execute()

        rules = []
        rules.append(RepeatRule(name="Repeater" + MergeRule.get_merge_name()))

        if rule.nested is not None:
            bef = Repetition(single_action, min=1, max=8, name="before")
            aft = Repetition(single_action, min=1, max=8, name="after")
            seq1 = Repetition(single_action, min=1, max=6, name="sequence1")
            seq2 = Repetition(single_action, min=1, max=6, name="sequence2")
            sing1 = Alternative(alts, name="singleton1")
            sing2 = Alternative(alts, name="singleton2")

            if rule.nested.extras is None:
                rule.nested.extras = [bef, aft, seq1, seq2, sing1, sing2]
            else:
                rule.nested.extras.extend([bef, aft, seq1, seq2, sing1, sing2])
            nested = rule.nested()
            rules.append(nested)

        return rules
Example #8
0
 def _create_repeat_rule(self, rule):
     ORIGINAL, SEQ, TERMINAL = "original", "caster_base_sequence", "terminal"
     alts = [RuleRef(rule=rule)]#+[RuleRef(rule=sm) for sm in selfmod]
     single_action = Alternative(alts)
     max = settings.SETTINGS["miscellaneous"]["max_ccr_repetitions"]
     sequence = Repetition(single_action, min=1, max=max, name=SEQ)
     original = Alternative(alts, name=ORIGINAL)
     terminal = Alternative(alts, name=TERMINAL)
     class RepeatRule(CompoundRule):
         spec = "[<"+ORIGINAL+"> original] [<" + SEQ + ">] [terminal <"+TERMINAL+">]"
         extras = [ sequence, original, terminal ] 
         def _process_recognition(self, node, extras):
             original = extras[ORIGINAL] if ORIGINAL in extras else None
             sequence = extras[SEQ] if SEQ in extras else None
             terminal = extras[TERMINAL] if TERMINAL in extras else None
             if original is not None: original.execute()
             if sequence is not None:
                 for action in sequence:
                     action.execute()
             if terminal is not None: terminal.execute()
     return RepeatRule(name="Repeater"+MergeRule.get_merge_name())