Python pyparsing.And() Examples

The following are 6 code examples of pyparsing.And(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyparsing , or try the search function .
Example #1
Source File: rule.py    From CWR-DataApi with MIT License 6 votes vote down vote up
def _build_rule(self, rule_id):
        rule_config = self._record_configs[rule_id]

        rule_type = rule_config.rule_type

        if rule_config.rules:
            rule = self._process_rules(rule_config.rules, pp.And)
        else:
            rule = self._build_terminal_rule(rule_config)

        if rule_type in self._decorators:
            rule = self._decorators[rule_type].decorate(rule, rule_config)

        if 'results_name' in rule_config:
            rule = rule.setResultsName(rule_config['results_name'])
        else:
            rule = rule.setResultsName(rule_id)

        rule.setName(rule_id)

        if self._debug:
            rule.setDebug()
        return rule 
Example #2
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def _make_matcher_element(self):
        # Wrap the parser element for the referenced rule's root expansion so that
        # the current match value for the NamedRuleRef is also set.
        return self._set_matcher_element_attributes(pyparsing.And([
            self.referenced_rule.expansion.matcher_element
        ])) 
Example #3
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def _make_matcher_element(self):
        # Return an And element using each child's matcher element.
        return self._set_matcher_element_attributes(pyparsing.And([
            child.matcher_element for child in self.children
        ])) 
Example #4
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def _make_matcher_element(self):
        # Define an extra parse action for the child's matcher element.
        def f(tokens):
            if tokens.asList():
                # Add current match values to the _repetitions_matched list.
                self._repetitions_matched.append(save_current_matches(self.child))

                # Wipe current match values for the next repetition (if any).
                self.child.reset_for_new_match()
            return tokens

        # Get the child's matcher element and add the extra parse action.
        child_element = self.child.matcher_element.addParseAction(f)

        # Determine the parser element type to use.
        type_ = pyparsing.ZeroOrMore if self.is_optional else pyparsing.OneOrMore

        # Handle the special case of a repetition ancestor, e.g. ((a b)+)+
        rep = self.repetition_ancestor
        if rep:
            # Check if there are no other branches.
            c = rep.child
            only_branch = True
            while c is not self:
                if len(c.children) > 1:
                    only_branch = False
                    break
                else:
                    c = c.children[0]

            # Use an And element instead if self is the only branch because
            # it makes no sense to repeat a repeat like this!
            if only_branch:
                type_ = pyparsing.And
                child_element = [child_element]

        return self._set_matcher_element_attributes(type_(child_element)) 
Example #5
Source File: fragment.py    From pybel with MIT License 5 votes vote down vote up
def get_fragment_language() -> ParserElement:
    """Build a protein fragment parser."""
    _fragment_value_inner = fragment_range | missing_fragment(FRAGMENT_MISSING)
    _fragment_value = _fragment_value_inner | And([Suppress('"'), _fragment_value_inner, Suppress('"')])
    parser_element = fragment_tag + nest(_fragment_value + Optional(WCW + quote(FRAGMENT_DESCRIPTION)))
    return parser_element 
Example #6
Source File: rule.py    From CWR-DataApi with MIT License 5 votes vote down vote up
def _process_rules_group(self, rules):
        group = None

        group_type = rules.list_type
        data = rules.rules

        if group_type == 'sequence':
            group = self._process_rules(data, pp.And)
        elif group_type == 'option':
            group = self._process_rules(data, pp.MatchFirst)
        elif group_type == 'optional':
            group = pp.Optional(self._process_rules(data, pp.And))

        return group