Python pyparsing 模块,infixNotation() 实例源码

我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用pyparsing.infixNotation()

项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def expression_parser(self):
        """A function returning a (pyparsing) parser for parsing C expressions.

        Returns:
            a (pyparsing) parser for parsing C expressions.
        """
        precedence = (self._build_precedence(_UNARY_MACROS) +
                      self._build_precedence(_PRECEDENCE))

        self.expression = pyparsing.Forward()

        # pylint: disable=expression-not-assigned
        self.expression << (
            pyparsing.infixNotation(
                baseExpr=self._base_or_array_expression(),
                opList=precedence,
            )
        )

        return self.expression
项目:orangengine    作者:lampwins    | 项目源码 | 文件源码
def tag_delta(expression, tag_list):
        """Take in a tag expression and a list of tags and give the delta of tags to meet the expression

        :return tuple( list( "required tags" ),  list( "tuple of options" ) )
        """

        if tag_list is None:
            tag_list = []

        required_tags = []
        optional_tags = []

        def parse_and(tokens):
            args = tokens[0][0::2]
            extend_list = filter(lambda x: isinstance(x, str) and x not in tag_list, args)
            required_tags.extend(extend_list)

        def parse_or(tokens):
            args = tokens[0][0::2]
            append_list = filter(lambda x: isinstance(x, str) and x not in tag_list, args)
            if append_list == args:
                optional_tags.append(tuple(append_list))

        identifier = pp.Word(pp.alphanums + "_" + "-" + "'")

        expr = pp.infixNotation(identifier, [
            ("AND", 2, pp.opAssoc.LEFT, parse_and),
            ("OR", 2, pp.opAssoc.LEFT, parse_or),
            ("and", 2, pp.opAssoc.LEFT, parse_and),
            ("or", 2, pp.opAssoc.LEFT, parse_or),
        ])

        expr.parseString(expression)

        if expression and not required_tags and expression not in tag_list:
            # single tag in the expression
            required_tags.append(expression)

        return required_tags, optional_tags
项目:monasca-analytics    作者:openstack    | 项目源码 | 文件源码
def __init__(self):
        """
        Create a parser that parse arithmetic expressions. They can
        contains variable identifiers or raw numbers. The meaning
        for the identifiers is left to the
        """
        number = p.Regex(r'\d+(\.\d*)?([eE]\d+)?')
        identifier = p.Word(p.alphas)
        terminal = identifier | number
        self._expr = p.infixNotation(terminal, [
            (p.oneOf('* /'), 2, p.opAssoc.LEFT),
            (p.oneOf('+ -'), 2, p.opAssoc.LEFT)
        ]) + p.stringEnd()