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

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

项目:learn-test    作者:hugolu    | 项目源码 | 文件源码
def __init__(self, calc = SimpleCalculator()):
        self.exprStack = []

        def pushStack(s, l, t):
            self.exprStack.append(t[0])

        integer = Word(nums).addParseAction(pushStack)
        addop = Literal('+') | Literal('-')
        mulop = Literal('*') | Literal('/')
        lpar = Literal('(')
        rpar = Literal(')')

        expr = Forward()
        atom = integer | lpar + expr + rpar
        term = atom + ZeroOrMore((mulop + atom).addParseAction(pushStack))
        expr << term + ZeroOrMore((addop + term).addParseAction(pushStack))
        self.expr = expr + StringEnd()

        self.opfun = {
                '+' : (lambda a, b: calc.add(a,b)),
                '-' : (lambda a, b: calc.sub(a,b)),
                '*' : (lambda a, b: calc.mul(a,b)),
                '/' : (lambda a, b: calc.div(a,b)) }
项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def anything_beetween(opener_and_closer):
    """Builds a (pyparsing) parser for the content inside delimiters.

    Args:
    opener_and_closer: a string containing two elements: opener and closer

    Returns:
      A (pyparsing) parser for the content inside delimiters.
    """
    opener = pyparsing.Literal(opener_and_closer[0])
    closer = pyparsing.Literal(opener_and_closer[1])
    char_removal_mapping = dict.fromkeys(map(ord, opener_and_closer))
    other_chars = unicode(string.printable).translate(char_removal_mapping)
    word_without_delimiters = pyparsing.Word(other_chars).setName(
        "other_chars")
    anything = pyparsing.Forward()
    delimited_block = opener + anything + closer
    # pylint: disable=expression-not-assigned
    anything << pyparsing.ZeroOrMore(
        word_without_delimiters.setName("word_without_delimiters")
        | delimited_block.setName("delimited_block")
    )

    # Combine all the parts into a single string.
    return pyparsing.Combine(anything)
项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def anything_beetween(opener_and_closer):
    """Builds a (pyparsing) parser for the content inside delimiters.

    Args:
    opener_and_closer: a string containing two elements: opener and closer

    Returns:
      A (pyparsing) parser for the content inside delimiters.
    """
    opener = pyparsing.Literal(opener_and_closer[0])
    closer = pyparsing.Literal(opener_and_closer[1])
    char_removal_mapping = dict.fromkeys(map(ord, opener_and_closer))
    other_chars = unicode(string.printable).translate(char_removal_mapping)
    word_without_delimiters = pyparsing.Word(other_chars).setName(
        "other_chars")
    anything = pyparsing.Forward()
    delimited_block = opener + anything + closer
    # pylint: disable=expression-not-assigned
    anything << pyparsing.ZeroOrMore(
        word_without_delimiters.setName("word_without_delimiters")
        | delimited_block.setName("delimited_block")
    )

    # Combine all the parts into a single string.
    return pyparsing.Combine(anything)
项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def expression(self):
        expression = pyparsing.Forward()

        # (1 + (2 + 3))
        nested_expression = pyparsing.nestedExpr(
            "(", ")", expression).setParseAction(self._combine_lists)

        # FOO(2 , 3)
        function_call = (
            _TOKEN().setResultsName("function")
            + _OPEN_PARENTHESIS()
            + pyparsing.delimitedList(
                pyparsing.Combine(expression, adjacent=False, joinString=" "),
                delim=",").setResultsName("func_args")
            + _CLOSE_PARENTHESIS()
        )

        expression << pyparsing.OneOrMore(
            function_call.setParseAction(self._is_known_function)
            | pyparsing.Group(nested_expression)
            | _TOKEN()
            | _NOT_TOKEN()
        )

        return pyparsing.Combine(expression, adjacent=False, joinString=" ")
项目: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
项目:coretools    作者:iotile    | 项目源码 | 文件源码
def _create_block_bnf():
    global block_bnf, time_interval, slot_id, statement, block_id, ident, stream

    if block_bnf is not None:
        return

    trigger_clause = Group(stream_trigger | Group(stream).setResultsName('stream_always') | Group(ident).setResultsName('identifier'))

    every_block_id = Group(Literal(u'every').suppress() - time_interval).setResultsName('every_block')
    when_block_id = Group(Literal(u'when').suppress() + Literal("connected").suppress() - Literal("to").suppress() - slot_id).setResultsName('when_block')
    latch_block_id = Group(Literal(u'when').suppress() - stream_trigger).setResultsName('latch_block')
    config_block_id = Group(Literal(u'config').suppress() - slot_id).setResultsName('config_block')
    on_block_id = Group(Literal(u'on').suppress() - trigger_clause.setResultsName('triggerA') - Optional((Literal("and") | Literal("or")) - trigger_clause.setResultsName('triggerB'))).setResultsName('on_block')

    block_id = every_block_id | when_block_id | latch_block_id | config_block_id | on_block_id

    block_bnf = Forward()
    statement = generic_statement | block_bnf

    block_bnf << Group(block_id + Group(Literal(u'{').suppress() + ZeroOrMore(statement) + Literal(u'}').suppress())).setResultsName('block')
项目:imcsdk    作者:CiscoUcs    | 项目源码 | 文件源码
def parse_filter_str(self, filter_str):
        """
        method to parse filter string
        """

        prop = pp.WordStart(pp.alphas) + pp.Word(pp.alphanums +
                                                 "_").setResultsName("prop")
        value = (pp.QuotedString("'") | pp.QuotedString('"') | pp.Word(
            pp.printables, excludeChars=",")).setResultsName("value")
        types_ = pp.oneOf("re eq ne gt ge lt le").setResultsName("types")
        flags = pp.oneOf("C I").setResultsName("flags")
        comma = pp.Literal(',')
        quote = (pp.Literal("'") | pp.Literal('"')).setResultsName("quote")

        type_exp = pp.Group(pp.Literal("type") + pp.Literal(
            "=") + quote + types_ + quote).setResultsName("type_exp")
        flag_exp = pp.Group(pp.Literal("flag") + pp.Literal(
            "=") + quote + flags + quote).setResultsName("flag_exp")

        semi_expression = pp.Forward()
        semi_expression << pp.Group(pp.Literal("(") +
                                    prop + comma + value +
                                    pp.Optional(comma + type_exp) +
                                    pp.Optional(comma + flag_exp) +
                                    pp.Literal(")")
                                    ).setParseAction(
            self.parse_filter_obj).setResultsName("semi_expression")

        expr = pp.Forward()
        expr << pp.operatorPrecedence(semi_expression, [
            ("not", 1, pp.opAssoc.RIGHT, self.not_operator),
            ("and", 2, pp.opAssoc.LEFT, self.and_operator),
            ("or", 2, pp.opAssoc.LEFT, self.or_operator)
        ])

        result = expr.parseString(filter_str)
        return result
项目:SlackBuilds    作者:montagdude    | 项目源码 | 文件源码
def is_ok(self):
        # pyparsing 2.0.0 bug, but it may be patched in distributions
        try:
            import pyparsing
            f = pyparsing.Forward()
            f <<= pyparsing.Literal('a')
            return f is not None
        except (ImportError, TypeError):
            return False
项目:NLPre    作者:NIHOPA    | 项目源码 | 文件源码
def __init__(self):
        nest = pypar.nestedExpr
        g = pypar.Forward()
        nestedParens = nest('(', ')')
        nestedBrackets = nest('[', ']')
        nestedCurlies = nest('{', '}')
        nest_grammar = nestedParens | nestedBrackets | nestedCurlies

        parens = "(){}[]"
        letters = ''.join([x for x in pypar.printables
                           if x not in parens])
        word = pypar.Word(letters)

        g = pypar.OneOrMore(word | nest_grammar)
        self.grammar = g
项目:NLPre    作者:NIHOPA    | 项目源码 | 文件源码
def __init__(self):
        nest = pypar.nestedExpr
        g = pypar.Forward()
        nestedParens = nest('(', ')')
        nestedBrackets = nest('[', ']')
        nestedCurlies = nest('{', '}')
        nest_grammar = nestedParens | nestedBrackets | nestedCurlies

        parens = "(){}[]"
        letters = ''.join([x for x in pypar.printables
                           if x not in parens])
        word = pypar.Word(letters)

        g = pypar.OneOrMore(word | nest_grammar)
        self.grammar = g
项目:SQLpie    作者:lessaworld    | 项目源码 | 文件源码
def build_parser(self):
        parsed_term = pyparsing.Group(pyparsing.Combine(pyparsing.Word(pyparsing.alphanums) + \
                                      pyparsing.Suppress('*'))).setResultsName('wildcard') | \
                      pyparsing.Group(pyparsing.Combine(pyparsing.Word(pyparsing.alphanums+"._") + \
                                      pyparsing.Word(':') + pyparsing.Group(pyparsing.Optional("\"") + \
                                      pyparsing.Optional("<") + pyparsing.Optional(">") + pyparsing.Optional("=") + \
                                      pyparsing.Optional("-") + pyparsing.Word(pyparsing.alphanums+"._/") + \
                                      pyparsing.Optional("&") + pyparsing.Optional("<") + pyparsing.Optional(">") + \
                                      pyparsing.Optional("=") + pyparsing.Optional("-") + \
                                      pyparsing.Optional(pyparsing.Word(pyparsing.alphanums+"._/")) + \
                                      pyparsing.Optional("\"")))).setResultsName('fields') | \
                      pyparsing.Group(pyparsing.Combine(pyparsing.Suppress('-')+ \
                                      pyparsing.Word(pyparsing.alphanums+"."))).setResultsName('not_term') | \
                      pyparsing.Group(pyparsing.Word(pyparsing.alphanums)).setResultsName('term')

        parsed_or = pyparsing.Forward()
        parsed_quote_block = pyparsing.Forward()
        parsed_quote_block << ((parsed_term + parsed_quote_block) | parsed_term )
        parsed_quote = pyparsing.Group(pyparsing.Suppress('"') + parsed_quote_block + \
                       pyparsing.Suppress('"')).setResultsName("quotes") | parsed_term
        parsed_parenthesis = pyparsing.Group((pyparsing.Suppress("(") + parsed_or + \
                             pyparsing.Suppress(")"))).setResultsName("parenthesis") | parsed_quote
        parsed_and = pyparsing.Forward()
        parsed_and << (pyparsing.Group(parsed_parenthesis + pyparsing.Suppress(pyparsing.Keyword("and")) + \
                       parsed_and).setResultsName("and") | \
                       pyparsing.Group(parsed_parenthesis + pyparsing.OneOrMore(~pyparsing.oneOf("or and") + \
                       parsed_and)).setResultsName("and") | parsed_parenthesis)
        parsed_or << (pyparsing.Group(parsed_and + pyparsing.Suppress(pyparsing.Keyword("or")) + \
                      parsed_or).setResultsName("or") | parsed_and)
        return parsed_or.parseString
项目:gdb-pretty-frame-cpp    作者:philtweir    | 项目源码 | 文件源码
def __init__(self):
        self.enclosed = pyp.Forward()
        nestedAngles = pyp.nestedExpr('<', '>', content=self.enclosed)
        self.enclosed << (pyp.Word('_' + pyp.alphanums) | '{' | '}' |
                          '(' | ')' | '*' | '&' | ',' | pyp.Word("::") |
                          nestedAngles)
项目:ucscsdk    作者:CiscoUcs    | 项目源码 | 文件源码
def parse_filter_str(self, filter_str):
        """
        method to parse filter string
        """

        prop = pp.WordStart(pp.alphas) + pp.Word(pp.alphanums +
                                                 "_").setResultsName("prop")
        value = (pp.QuotedString("'") | pp.QuotedString('"') | pp.Word(
            pp.printables, excludeChars=",")).setResultsName("value")
        types_ = pp.oneOf("re eq ne gt ge lt le").setResultsName("types")
        flags = pp.oneOf("C I").setResultsName("flags")
        comma = pp.Literal(',')
        quote = (pp.Literal("'") | pp.Literal('"')).setResultsName("quote")

        type_exp = pp.Group(pp.Literal("type") + pp.Literal(
            "=") + quote + types_ + quote).setResultsName("type_exp")
        flag_exp = pp.Group(pp.Literal("flag") + pp.Literal(
            "=") + quote + flags + quote).setResultsName("flag_exp")

        semi_expression = pp.Forward()
        semi_expression << pp.Group(pp.Literal("(") +
                                    prop + comma + value +
                                    pp.Optional(comma + type_exp) +
                                    pp.Optional(comma + flag_exp) +
                                    pp.Literal(")")
                                    ).setParseAction(
            self.parse_filter_obj).setResultsName("semi_expression")

        expr = pp.Forward()
        expr << pp.operatorPrecedence(semi_expression, [
            ("not", 1, pp.opAssoc.RIGHT, self.not_operator),
            ("and", 2, pp.opAssoc.LEFT, self.and_operator),
            ("or", 2, pp.opAssoc.LEFT, self.or_operator)
        ])

        result = expr.parseString(filter_str)
        return result
项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
def parser(self):
        # Define punctuation as suppressed literals.
        lparen, rparen, lbrack, rbrack, lbrace, rbrace, colon = \
            map(pp.Suppress, "()[]{}:")

        integer = pp.Combine(pp.Optional(pp.oneOf("+ -")) + pp.Word(pp.nums)) \
            .setName("integer") \
            .setParseAction(lambda toks: int(toks[0]))

        real = pp.Combine(pp.Optional(pp.oneOf("+ -")) + pp.Word(pp.nums) + "." +
                          pp.Optional(pp.Word(pp.nums)) +
                          pp.Optional(pp.oneOf("e E") + pp.Optional(pp.oneOf("+ -")) + pp.Word(pp.nums))) \
            .setName("real") \
            .setParseAction(lambda toks: float(toks[0]))

        _datetime_arg = (integer | real)
        datetime_args = pp.Group(pp.delimitedList(_datetime_arg))
        _datetime = pp.Suppress(pp.Literal('datetime') + pp.Literal("(")) + datetime_args + pp.Suppress(")")
        _datetime.setParseAction(lambda x: self._make_datetime(x[0]))

        tuple_str = pp.Forward()
        list_str = pp.Forward()
        dict_str = pp.Forward()

        list_item = real | integer | _datetime | pp.quotedString.setParseAction(pp.removeQuotes) | \
                    pp.Group(list_str) | tuple_str | dict_str

        tuple_str << (pp.Suppress("(") + pp.Optional(pp.delimitedList(list_item)) +
                      pp.Optional(pp.Suppress(",")) + pp.Suppress(")"))
        tuple_str.setParseAction(lambda toks : tuple(toks.asList()))
        list_str << (lbrack + pp.Optional(pp.delimitedList(list_item) +
                                          pp.Optional(pp.Suppress(","))) + rbrack)

        dict_entry = pp.Group(list_item + colon + list_item)
        dict_str << (lbrace + pp.Optional(pp.delimitedList(dict_entry) +
                                          pp.Optional(pp.Suppress(","))) + rbrace)
        dict_str.setParseAction(lambda toks: dict(toks.asList()))
        return list_item
项目:qmflows-namd    作者:SCM-NV    | 项目源码 | 文件源码
def parse_list_of_lists(xs):
    """
    Parse a list of list of integers using pyparsing
    """
    enclosed = pa.Forward()  # Parser to be defined later
    natural = pa.Word(pa.nums)  # Natural Number
    # Nested Grammar
    nestedBrackets = pa.nestedExpr(pa.Suppress('['), pa.Suppress(']'), content=enclosed)
    enclosed << (natural | pa.Suppress(',') | nestedBrackets)
    try:
        rs = enclosed.parseString(xs).asList()[0]
        return list(map(lambda x: list(map(int, x)), rs))
    except pa.ParseException:
        raise RuntimeError("Invalid Macro states Specification")
项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def XXXX_cast_expression(self):
        """A function returning a parser for parsing cast expressions.

        Args:
            expression: a pyparsing parser for parsing an expression to be cast.

        Returns:
            A (pyparsing) parser for parsing cast expressions.
        """
        word = pyparsing.Word(pyparsing.alphanums + '_*[]')
        nested = pyparsing.Forward().setName("nested")
        nested << pyparsing.Combine(
            pyparsing.Literal('(').suppress()
            + pyparsing.Combine(
                pyparsing.ZeroOrMore(self._integer() | word | nested))
            + pyparsing.Literal(')').suppress()
        )
        typeof_expression = (
            _OPEN_PARENTHESIS
            + pyparsing.Keyword('typeof')
            + nested("typeof_arg")
            + _CLOSE_PARENTHESIS
        )

        type_expression = (
            typeof_expression
            | nested("simple_type")
        )
        return (
            type_expression
            + ~(_PLUS | _MINUS)
            + self.expression("expression")
        ).setParseAction(self._create_cast_expression)
项目:cumin    作者:wikimedia    | 项目源码 | 文件源码
def grammar():
    """Define the query grammar.

    Backus-Naur form (BNF) of the grammar::

        <grammar> ::= <item> | <item> <boolean> <grammar>
           <item> ::= <hosts> | "(" <grammar> ")"
        <boolean> ::= "and not" | "and" | "xor" | "or"

    Given that the pyparsing library defines the grammar in a BNF-like style, for the details of the tokens not
    specified above check directly the source code.

    Returns:
        pyparsing.ParserElement: the grammar parser.

    """
    # Boolean operators
    boolean = (pp.CaselessKeyword('and not').leaveWhitespace() | pp.CaselessKeyword('and') |
               pp.CaselessKeyword('xor') | pp.CaselessKeyword('or'))('bool')

    # Parentheses
    lpar = pp.Literal('(')('open_subgroup')
    rpar = pp.Literal(')')('close_subgroup')

    # Hosts selection: clustershell (,!&^[]) syntax is allowed: host10[10-42].domain
    hosts = (~(boolean) + pp.Word(pp.alphanums + '-_.,!&^[]'))('hosts')

    # Final grammar, see the docstring for its BNF based on the tokens defined above
    # Groups are used to split the parsed results for an easy access
    full_grammar = pp.Forward()
    item = hosts | lpar + full_grammar + rpar
    full_grammar << pp.Group(item) + pp.ZeroOrMore(pp.Group(boolean + item))  # pylint: disable=expression-not-assigned

    return full_grammar
项目:cumin    作者:wikimedia    | 项目源码 | 文件源码
def grammar():
    """Define the query grammar for the external backend used for testing."""
    # Hosts selection: clustershell (,!&^[]) syntax is allowed: host10[10-42].domain
    hosts = pp.Word(pp.alphanums + '-_.,!&^[]')('hosts')

    # Final grammar, see the docstring for its BNF based on the tokens defined above
    # Groups are used to split the parsed results for an easy access
    full_grammar = pp.Forward()
    full_grammar << pp.Group(hosts) + pp.ZeroOrMore(pp.Group(hosts))  # pylint: disable=expression-not-assigned

    return full_grammar
项目:Taigabot    作者:FrozenPigs    | 项目源码 | 文件源码
def parseTerms():
    """
    expop   :: '^'
    multop  :: '*' | '/'
    addop   :: '+' | '-'
    integer :: ['+' | '-'] '0'..'9'+
    atom    :: PI | E | real | fn '(' expr ')' | '(' expr ')'
    factor  :: atom [ expop factor ]*
    term    :: factor [ multop factor ]*
    expr    :: term [ addop term ]*
    """
    global terms
    if not terms:
        point = Literal( "." )
        e     = CaselessLiteral( "E" )
        fnumber = Combine( Word( "+-"+nums, nums ) +
                           Optional( point + Optional( Word( nums ) ) ) +
                           Optional( e + Word( "+-"+nums, nums ) ) )
        ident = Word(alphas, alphas+nums+"_$")

        plus  = Literal( "+" )
        minus = Literal( "-" )
        mult  = Literal( "*" )
        div   = Literal( "/" )
        lpar  = Literal( "(" ).suppress()
        rpar  = Literal( ")" ).suppress()
        addop  = plus | minus
        multop = mult | div
        expop = Literal( "^" )
        pi    = CaselessLiteral( "PI" )

        expr = Forward()
        atom = (Optional("-") + ( pi | e | fnumber | ident + lpar + expr + rpar ).setParseAction( pushFirst ) | ( lpar + expr.suppress() + rpar )).setParseAction(pushUMinus)

        # by defining exponentiation as "atom [ ^ factor ]..." instead of "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-righ
        # that is, 2^3^2 = 2^(3^2), not (2^3)^2.
        factor = Forward()
        factor << atom + ZeroOrMore( ( expop + factor ).setParseAction( pushFirst ) )

        term = factor + ZeroOrMore( ( multop + factor ).setParseAction( pushFirst ) )
        expr << term + ZeroOrMore( ( addop + term ).setParseAction( pushFirst ) )
        terms = expr
    return terms
项目:cumin    作者:wikimedia    | 项目源码 | 文件源码
def grammar():
    """Define the query grammar.

    Backus-Naur form (BNF) of the grammar::

            <grammar> ::= <item> | <item> <and_or> <grammar>
               <item> ::= [<neg>] <query-token> | [<neg>] "(" <grammar> ")"
        <query-token> ::= <token> | <hosts>
              <token> ::= <category>:<key> [<operator> <value>]

    Given that the pyparsing library defines the grammar in a BNF-like style, for the details of the tokens not
    specified above check directly the source code.

    Returns:
        pyparsing.ParserElement: the grammar parser.

    """
    # Boolean operators
    and_or = (pp.CaselessKeyword('and') | pp.CaselessKeyword('or'))('bool')
    # 'neg' is used as label to allow the use of dot notation, 'not' is a reserved word in Python
    neg = pp.CaselessKeyword('not')('neg')

    operator = pp.oneOf(OPERATORS, caseless=True)('operator')  # Comparison operators
    quoted_string = pp.quotedString.copy().addParseAction(pp.removeQuotes)  # Both single and double quotes are allowed

    # Parentheses
    lpar = pp.Literal('(')('open_subgroup')
    rpar = pp.Literal(')')('close_subgroup')

    # Hosts selection: glob (*) and clustershell (,!&^[]) syntaxes are allowed:
    # i.e. host10[10-42].*.domain
    hosts = quoted_string | (~(and_or | neg) + pp.Word(pp.alphanums + '-_.*,!&^[]'))

    # Key-value token for allowed categories using the available comparison operators
    # i.e. F:key = value
    category = pp.oneOf(CATEGORIES, caseless=True)('category')
    key = pp.Word(pp.alphanums + '-_.%@:')('key')
    selector = pp.Combine(category + ':' + key)  # i.e. F:key
    # All printables characters except the parentheses that are part of this or the global grammar
    all_but_par = ''.join([c for c in pp.printables if c not in ('(', ')', '{', '}')])
    value = (quoted_string | pp.Word(all_but_par))('value')
    token = selector + pp.Optional(operator + value)

    # Final grammar, see the docstring for its BNF based on the tokens defined above
    # Groups are used to split the parsed results for an easy access
    full_grammar = pp.Forward()
    item = pp.Group(pp.Optional(neg) + (token | hosts('hosts'))) | pp.Group(
        pp.Optional(neg) + lpar + full_grammar + rpar)
    full_grammar << item + pp.ZeroOrMore(pp.Group(and_or) + full_grammar)  # pylint: disable=expression-not-assigned

    return full_grammar
项目:cumin    作者:wikimedia    | 项目源码 | 文件源码
def grammar(backend_keys):
    """Define the main multi-query grammar.

    Cumin provides a user-friendly generic query language that allows to combine the results of subqueries for multiple
    backends:

    * Each query part can be composed with the others using boolean operators ``and``, ``or``, ``and not``, ``xor``.
    * Multiple query parts can be grouped together with parentheses ``(``, ``)``.
    * Specific backend query ``I{backend-specific query syntax}``, where ``I`` is an identifier for the specific
      backend.
    * Alias replacement, according to aliases defined in the configuration file ``A:group1``.
    * The identifier ``A`` is reserved for the aliases replacement and cannot be used to identify a backend.
    * A complex query example: ``(D{host1 or host2} and (P{R:Class = Role::MyClass} and not A:group1)) or D{host3}``

    Backus-Naur form (BNF) of the grammar::

              <grammar> ::= <item> | <item> <boolean> <grammar>
                 <item> ::= <backend_query> | <alias> | "(" <grammar> ")"
        <backend_query> ::= <backend> "{" <query> "}"
                <alias> ::= A:<alias_name>
              <boolean> ::= "and not" | "and" | "xor" | "or"

    Given that the pyparsing library defines the grammar in a BNF-like style, for the details of the tokens not
    specified above check directly the source code.

    Arguments:
        backend_keys (list): list of the GRAMMAR_PREFIX for each registered backend.

    Returns:
        pyparsing.ParserElement: the grammar parser.

    """
    # Boolean operators
    boolean = (pp.CaselessKeyword('and not').leaveWhitespace() | pp.CaselessKeyword('and') |
               pp.CaselessKeyword('xor') | pp.CaselessKeyword('or'))('bool')

    # Parentheses
    lpar = pp.Literal('(')('open_subgroup')
    rpar = pp.Literal(')')('close_subgroup')

    # Backend query: P{PuppetDB specific query}
    query_start = pp.Combine(pp.oneOf(backend_keys, caseless=True)('backend') + pp.Literal('{'))
    query_end = pp.Literal('}')
    # Allow the backend specific query to use the end_query token as well, as long as it's in a quoted string
    # and fail if there is a query_start token before the first query_end is reached
    query = pp.SkipTo(query_end, ignore=pp.quotedString, failOn=query_start)('query')
    backend_query = pp.Combine(query_start + query + query_end)

    # Alias
    alias = pp.Combine(pp.CaselessKeyword('A') + ':' + pp.Word(pp.alphanums + '-_.+')('alias'))

    # Final grammar, see the docstring for its BNF based on the tokens defined above
    # Group are used to have an easy dictionary access to the parsed results
    full_grammar = pp.Forward()
    item = backend_query | alias | lpar + full_grammar + rpar
    full_grammar << pp.Group(item) + pp.ZeroOrMore(pp.Group(boolean + item))  # pylint: disable=expression-not-assigned

    return full_grammar
项目:CAEML    作者:Renumics    | 项目源码 | 文件源码
def __init__(self, comm_file_path):
        expression_spaced = Forward()
        expression = Forward()
        args_spaced = Forward()

        cb = Optional(',') + ')'  # closing_brackets might include a ','
        ob = Optional(' ') + '(' + Optional(' ')  # closing_brackets might include a ' '

        value = (Or([pyparsing_common.identifier.copy().setResultsName('id'),
                     pyparsing_common.number.copy().setResultsName('number'),
                     QuotedString("'").setResultsName('string')])).setParseAction(Value).setResultsName('value')

        values = (ZeroOrMore(value.setResultsName('valueList', listAllMatches=True) + Optional(','))).setParseAction(
            Values)

        keyword = pyparsing_common.identifier.copy()

        keyword_argument = (
            keyword.setResultsName('keyword') + '=' + expression_spaced.setResultsName('expression')
        ).setParseAction(Keyword_argument)

        keyword_arguments = (
            keyword_argument.setResultsName('keyword_argument', listAllMatches=True) +
            ZeroOrMore(',' + keyword_argument.setResultsName('keyword_argument', listAllMatches=True))
        ).setParseAction(Keyword_arguments)

        expression << (Or([
            value, (ob + values.setResultsName('values') + cb),
            '_F' + ob + keyword_arguments.setResultsName('keyword_arguments') + cb,
            ob + expression.setResultsName('expression') + cb
        ])).setParseAction(Expression)

        expression_spaced << (Or([expression, ob + expression_spaced + cb]))

        left_side = pyparsing_common.identifier.setResultsName('left_side')
        operator_name = pyparsing_common.identifier.setResultsName('operator_name')
        paragraph = (Optional(left_side + "=") + operator_name + ob + Optional(keyword_arguments
            .setResultsName(
            'keyword_arguments')) + cb + Optional(';')).setParseAction(Paragraph)

        file = OneOrMore(paragraph).setResultsName('paragraphs').setParseAction(File)

        self.beam_data_model = file.parseFile(comm_file_path)