当前位置: 首页>>代码示例>>Python>>正文


Python pyparsing.oneOf函数代码示例

本文整理汇总了Python中pyparsing.oneOf函数的典型用法代码示例。如果您正苦于以下问题:Python oneOf函数的具体用法?Python oneOf怎么用?Python oneOf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了oneOf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_idl_line_parser

def get_idl_line_parser():
    """
    Based on http://pyparsing.wikispaces.com/file/view/parsePythonValue.py
    """
    from pyparsing import \
    Word, ZeroOrMore, OneOrMore, Optional, oneOf, StringEnd, Suppress, Group, Combine, \
    nums, dblQuotedString, removeQuotes
    
    s = Suppress
    int_number = Combine(Optional(oneOf("+ -")) + Word(nums)).setParseAction(lambda tokens: int(tokens[0])).setName("integer")
    float_number = \
        Combine(Optional(oneOf("+ -")) + Word(nums) + Optional("." +
                   Optional(Word(nums)) +
                   Optional(oneOf("e E")+Optional(oneOf("+ -")) +Word(nums)))) \
        .setName("float") \
        .setParseAction( lambda tokens: float(tokens[0]) )
               
    bounding_box = s('(') + OneOrMore( int_number | s(',') ) + s(')')
    bounding_box_with_score = Group(bounding_box + Optional( ( s(":") | s("~") ) +  float_number ))
    #filename =  s('"') + Word(alphanums + "/_.~") +  s('"')
    quoted = dblQuotedString.setParseAction(removeQuotes)
    filename = quoted
    idl_line = filename + Optional(s(':') +  ZeroOrMore(bounding_box_with_score | s(','))) + ( s(";") | s(".") ) + StringEnd() 

    #print( filename.parseString("\"left/image_00000004_0.png\"") )
    #print( bounding_box.parseString("(221, 183, 261, 289)") )
    
    return idl_line.parseString
开发者ID:Belial2010,项目名称:Pedestrian-Detection-Project,代码行数:28,代码来源:idl_parsing.py

示例2: check_in_parsing

def check_in_parsing(s):
    update =    (Literal('@') + 
                Group(OneOrMore(Word(alphas))) +  
                StringEnd()) | (Literal('@') + 
                SkipTo(oneOf("4 for For FOR")) + 
                oneOf('4 for For FOR') + 
                Word(nums))
    
    #for s in smstestlist:
    #    print s
    try:
        u = update.parseString(s)
        print u
        if len(u) == 2:
            u[1] = ' '.join(u[1])
            if u[1] in safezones:
                print "user in safezone " + u[1]
                combo = {'location':u[1],'hours':'24'}
                return combo
            else:
                print "Error: " + u[1] + " is not a safezone!"
                return "Error: " + u[1] + " is not a safezone! Syntax for checking in: @ location 4 numberofhours"
        else:
            print "user at " + u[1] + " for " + u[3] + " hours."
            combo = {'location':u[1],'hours':str(u[3])}
            return combo
    except:
        print "Error: Unable to understand!"
开发者ID:elizabethmgin,项目名称:panoptincon,代码行数:28,代码来源:utilities.py

示例3: _parse_expr

def _parse_expr(text, ldelim="(", rdelim=")"):
    """Parse mathematical expression using PyParsing."""
    var = pyparsing.Word(pyparsing.alphas + "_", pyparsing.alphanums + "_")
    point = pyparsing.Literal(".")
    exp = pyparsing.CaselessLiteral("E")
    number = pyparsing.Combine(
        pyparsing.Word("+-" + pyparsing.nums, pyparsing.nums)
        + pyparsing.Optional(point + pyparsing.Optional(pyparsing.Word(pyparsing.nums)))
        + pyparsing.Optional(
            exp + pyparsing.Word("+-" + pyparsing.nums, pyparsing.nums)
        )
    )
    atom = var | number
    oplist = [
        (pyparsing.Literal("**"), 2, pyparsing.opAssoc.RIGHT),
        (pyparsing.oneOf("+ - ~"), 1, pyparsing.opAssoc.RIGHT),
        (pyparsing.oneOf("* / // %"), 2, pyparsing.opAssoc.LEFT),
        (pyparsing.oneOf("+ -"), 2, pyparsing.opAssoc.LEFT),
        (pyparsing.oneOf("<< >>"), 2, pyparsing.opAssoc.LEFT),
        (pyparsing.Literal("&"), 2, pyparsing.opAssoc.LEFT),
        (pyparsing.Literal("^"), 2, pyparsing.opAssoc.LEFT),
        (pyparsing.Literal("|"), 2, pyparsing.opAssoc.LEFT),
    ]
    # Get functions
    expr = pyparsing.infixNotation(
        atom, oplist, lpar=pyparsing.Suppress(ldelim), rpar=pyparsing.Suppress(rdelim)
    )
    return expr.parseString(text)[0]
开发者ID:pmacosta,项目名称:peng,代码行数:28,代码来源:functions.py

示例4: _parseFeatureSignature

def _parseFeatureSignature(sig):
    """This function parses a given feature-signature."""
    mal = set()

    def _rewriteOne(p):
        return ""

    def _rewriteTwo(p):
        return ""

    def _addIdentifier2Mal(p):
        mal.add(p[0])

    operand = __string | __hexadec | __function | __integer | __identifier.setParseAction(_addIdentifier2Mal)
    compoperator = pypa.oneOf("< > <= >= == !=")
    calcoperator = pypa.oneOf("+ - * / % & | << >>")
    expr = pypa.operatorPrecedence(
        operand,
        [
            ("defined", 1, pypa.opAssoc.RIGHT, _rewriteOne),
            ("!", 1, pypa.opAssoc.RIGHT, _rewriteOne),
            (calcoperator, 2, pypa.opAssoc.LEFT, _rewriteTwo),
            (compoperator, 2, pypa.opAssoc.LEFT, _rewriteTwo),
            ("&&", 2, pypa.opAssoc.LEFT, _rewriteTwo),
            ("||", 2, pypa.opAssoc.LEFT, _rewriteTwo),
        ],
    )

    try:
        rsig = expr.parseString(sig)[0]
    except pypa.ParseException, e:
        print("ERROR (parse): cannot parse sig (%s) -- (%s)" % (sig, e.col))
        return sig
开发者ID:pombredanne,项目名称:cppstats,代码行数:33,代码来源:derivative.py

示例5: parse

def parse(formula):
    """Parse formula string and create abstract syntax tree (AST).
    """
    # LTL expression
    _ltl_expr = pp.operatorPrecedence(
        _proposition,
        [
            ("'", 1, pp.opAssoc.LEFT, ASTUnTempOp),
            ("!", 1, pp.opAssoc.RIGHT, ASTNot),
            (_UnaryTempOps, 1, pp.opAssoc.RIGHT, ASTUnTempOp),
            (pp.oneOf("& &&"), 2, pp.opAssoc.LEFT, ASTAnd),
            (pp.oneOf("| ||"), 2, pp.opAssoc.LEFT, ASTOr),
            (pp.oneOf("xor ^"), 2, pp.opAssoc.LEFT, ASTXor),
            ("->", 2, pp.opAssoc.RIGHT, ASTImp),
            ("<->", 2, pp.opAssoc.RIGHT, ASTBiImp),
            (pp.oneOf("= == !="), 2, pp.opAssoc.RIGHT, ASTComparator),
            (pp.oneOf("U V R"), 2, pp.opAssoc.RIGHT, ASTBiTempOp),
        ],
    )
    _ltl_expr.ignore(pp.LineStart() + "--" + pp.restOfLine)

    # Increase recursion limit for complex formulae
    sys.setrecursionlimit(2000)
    try:
        return _ltl_expr.parseString(formula, parseAll=True)[0]
    except RuntimeError:
        raise pp.ParseException("Maximum recursion depth exceeded," "could not parse")
开发者ID:rmattila,项目名称:tulip-control,代码行数:27,代码来源:pyparser.py

示例6: __init__

    def __init__(self):
        from pyparsing import (ParserElement, StringEnd, LineEnd, Literal,
                               pythonStyleComment, ZeroOrMore, Suppress,
                               Optional, Combine, OneOrMore, Regex, oneOf,
                               QuotedString, Group, ParseException)

        ParserElement.setDefaultWhitespaceChars("\t ")

        EOF = StringEnd()
        EOL = ~EOF + LineEnd() # EOL must not match on EOF

        escape = Literal("\\")
        comment = pythonStyleComment
        junk = ZeroOrMore(comment | EOL).suppress()

        ## word (i.e: single argument string)
        word = Suppress(escape + EOL + Optional(comment)) \
        | Combine(OneOrMore( escape.suppress() + Regex(".") |
                             QuotedString("'", escChar='\\', multiline=True) |
                             QuotedString('"', escChar='\\', multiline=True) |
                             Regex("[^ \t\r\n\f\v\\\\$&<>();\|\'\"`]+") |
                             Suppress(escape + EOL) ))

        ## redirector (aka bash file redirectors, such as "2>&1" sequences)
        fd_src = Regex("[0-2]").setParseAction(lambda t: int(t[0]))
        fd_dst = Suppress("&") + fd_src
        # "[n]<word" || "[n]<&word" || "[n]<&digit-"
        fd_redir = (Optional(fd_src, 0) + Literal("<")
                    |Optional(fd_src, 1) + Literal(">"))\
                   +(word | (fd_dst + Optional("-")))
        # "&>word" || ">&word"
        full_redir = (oneOf("&> >&") + word)\
                     .setParseAction(lambda t:("&" ,">", t[-1]))
        # "<<<word" || "<<[-]word"
        here_doc = Regex("<<(<|-?)") + word
        # "[n]>>word"
        add_to_file = Optional(fd_src | Literal("&"), 1) + \
                      Literal(">>") + word
        # "[n]<>word"
        fd_bind = Optional(fd_src, 0) + Literal("<>") + word

        redirector = (fd_redir | full_redir | here_doc
                      | add_to_file | fd_bind)\
                     .setParseAction(lambda token: tuple(token))

        ## single command (args/redir list)
        command = Group(OneOrMore(redirector | word))

        ## logical operators (section splits)
        semicolon = Suppress(";") + junk
        connector = (oneOf("&& || |") + junk) | semicolon

        ## pipeline, aka logical block of interconnected commands
        pipeline = junk + Group(command +
                                ZeroOrMore(connector + command) +
                                Optional(semicolon))

        # define object attributes
        self.LEXER = pipeline.ignore(comment) + EOF
        self.parseException = ParseException
开发者ID:0x0mar,项目名称:phpsploit,代码行数:60,代码来源:lexer.py

示例7: __init__

 def __init__(self):
     """
     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 ]*
     """
     point = Literal( "." )
     e     = CaselessLiteral( "E" )
     fnumber = Combine( Word( "+-"+alphanums+"_", alphanums+"_" ) + 
                        Optional( point + Optional( Word( alphanums+"_" ) ) ) +
                        Optional( e + Word( "+-"+alphanums+"_", alphanums+"_" ) ) )
     
     ident = Word(alphas,alphanums + "_")       
     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()
     function = ident + lpar + expr + ZeroOrMore("," + expr) + rpar
     atom = ((Optional(oneOf("- +")) +
              (pi|e|function|fnumber).setParseAction(self.pushFirst))
             | Optional(oneOf("- +")) + Group(lpar+expr+rpar)
             ).setParseAction(self.pushUMinus)       
     # by defining exponentiation as "atom [ ^ factor ]..." instead of 
     # "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-right
     # that is, 2^3^2 = 2^(3^2), not (2^3)^2.
     factor = Forward()
     factor << atom + ZeroOrMore( ( expop + factor ).setParseAction( self.pushFirst ) )
     term = factor + ZeroOrMore( ( multop + factor ).setParseAction( self.pushFirst ) )
     expr << term + ZeroOrMore( ( addop + term ).setParseAction( self.pushFirst ) )
     # addop_term = ( addop + term ).setParseAction( self.pushFirst )
     # general_term = term + ZeroOrMore( addop_term ) | OneOrMore( addop_term)
     # expr <<  general_term       
     self.bnf = expr
     # map operator symbols to corresponding arithmetic operations
     epsilon = 1e-12
     self.opn = { "+" : operator.add,
             "-" : operator.sub,
             "*" : operator.mul,
             "/" : operator.truediv,
             "^" : operator.pow }
     self.fn  = { "sin" : math.sin,
             "cos" : math.cos,
             "tan" : math.tan,
             "abs" : abs,
             "trunc" : lambda a: int(a),
             "round" : round,
             "sgn" : lambda a: abs(a)>epsilon and cmp(a,0) or 0}
开发者ID:Alwnikrotikz,项目名称:bionetgen,代码行数:60,代码来源:util.py

示例8: _parse_line

def _parse_line():
    """Parse a single data line that may contain string or numerical data.
    Float and Int 'words' are converted to their appropriate type. 
    Exponentiation is supported, as are NaN and Inf."""
        
    digits = Word(nums)
    dot = "."
    sign = oneOf("+ -")
    ee = CaselessLiteral('E') | CaselessLiteral('D')

    num_int = ToInteger(Combine( Optional(sign) + digits ))
    
    num_float = ToFloat(Combine( Optional(sign) + 
                        ((digits + dot + Optional(digits)) |
                         (dot + digits)) +
                         Optional(ee + Optional(sign) + digits)
                        ))
    
    # special case for a float written like "3e5"
    mixed_exp = ToFloat(Combine( digits + ee + Optional(sign) + digits ))
    
    nan = ToInf(oneOf("Inf -Inf")) | \
          ToNan(oneOf("NaN nan NaN%  NaNQ NaNS qNaN sNaN " + \
                        "1.#SNAN 1.#QNAN -1.#IND"))
    
    # sep = Literal(" ") | Literal("\n")
    
    data = ( OneOrMore( (nan | num_float | mixed_exp | num_int |
                         Word(printables)) ) )
    
    return data
开发者ID:OzanCKN,项目名称:OpenMDAO-Framework,代码行数:31,代码来源:filewrap.py

示例9: __init__

    def __init__(self, ffilter, queue_out):
	FuzzQueue.__init__(self, queue_out)
	Thread.__init__(self)
	self.setName('filter_thread')

	self.queue_out = queue_out

	if PYPARSING:
	    element = oneOf("c l w h")
	    digits = "XB0123456789"
	    integer = Word( digits )#.setParseAction( self.__convertIntegers )
	    elementRef = Group(element + oneOf("= != < > >= <=") + integer)
	    operator = oneOf("and or")
	    definition = elementRef + ZeroOrMore( operator + elementRef)
	    nestedformula = Group(Suppress(Optional(Literal("("))) + definition + Suppress(Optional(Literal(")"))))
	    self.finalformula = nestedformula + ZeroOrMore( operator + nestedformula)

	    elementRef.setParseAction(self.__compute_element)
	    nestedformula.setParseAction(self.__compute_formula)
	    self.finalformula.setParseAction(self.__myreduce)

	self.res = None
	self.hideparams = ffilter

	if "XXX" in self.hideparams['codes']:
	    self.hideparams['codes'].append("0")

	self.baseline = None
开发者ID:601040605,项目名称:wfuzz,代码行数:28,代码来源:filter.py

示例10: _parse_template

    def _parse_template(self, options, template):
        """Parse a template string."""
        variable_name = Word(alphas + " ")
        variable_prefix = Optional(Word(alphas) + ":")
        variable = "{" + variable_prefix + variable_name + "}"
        variable.setParseAction(self._replace_variable(options))

        block_name = oneOf("Title Description PreviousPage NextPage")
        block_start = "{block:" + block_name + "}"
        block_end = "{/block:" + block_name + "}"
        block = block_start + SkipTo(block_end) + block_end
        block.setParseAction(self._replace_block(options))

        block_type_name = oneOf("Text Photo Panorama Photoset Quote Link Chat Video Audio")
        block_type_start = "{block:" + block_type_name + "}"
        block_type_end = "{/block:" + block_type_name + "}"
        block_type = block_type_start + SkipTo(block_type_end) + block_type_end
        block_type.setParseAction(self._replace_block_type(options))

        block_cond_name = Word(alphas)
        block_cond_start = "{block:If" + Optional("Not") + block_cond_name + "}"
        block_cond_end = "{/block:If" + Optional("Not") + block_cond_name + "}"
        block_cond = block_cond_start + SkipTo(block_cond_end) + block_cond_end
        block_cond.setParseAction(self._replace_block_cond(options))

        block_iter_name = oneOf("Posts")
        block_iter_start = "{block:" + block_iter_name + "}"
        block_iter_end = "{/block:" + block_iter_name + "}"
        block_iter = block_iter_start + SkipTo(block_iter_end) + block_iter_end
        block_iter.setParseAction(self._replace_block_iter(options))

        parser = (block | block_type | block_cond | block_iter | variable)
        return parser.transformString(template)
开发者ID:pabluk,项目名称:tumblr-theme-parser,代码行数:33,代码来源:__init__.py

示例11: interpretLine

	def interpretLine(self,l):
		#Construct parsing rules
		natural=pyparsing.Word(pyparsing.nums)
		natural_n=pyparsing.Word(pyparsing.nums)
		natural_n.setParseAction(lambda t: int(t[0]))
		integer=pyparsing.Optional(pyparsing.oneOf(['-', '+']))+natural
		decimal=pyparsing.Word('.',pyparsing.nums)
		exponent=pyparsing.Literal('e')+integer
		number=pyparsing.Combine(integer+pyparsing.Optional(decimal)+pyparsing.Optional(exponent))
		number.setParseAction(lambda t: float(t[0]))
		cmd=pyparsing.Group(pyparsing.oneOf(CMDS)+natural_n)
		coord=pyparsing.Group(pyparsing.oneOf(AXES)+number)
		line=cmd+pyparsing.Group(pyparsing.ZeroOrMore(coord))

		if len(l.strip())==0: return False

		res=line.parseString(l).asList()
		letter=res[0][0]
		num=res[0][1]
		if letter=='G':
			return self.interpretG(num,res[1])
		if letter=='M':
			return self.interpretM(num)
		if letter=='T':
			return self.interpretT(num)
开发者ID:pathim,项目名称:xxl-cnc-heissdraht,代码行数:25,代码来源:gcode.py

示例12: parser

def parser():
    global _parser
    if _parser is None:
        ParserElement.setDefaultWhitespaceChars("")
        lbrack, rbrack, lbrace, rbrace, lparen, rparen = map(Literal, "[]{}()")

        reMacro = Combine("\\" + oneOf(list("dws")))
        escapedChar = ~ reMacro + Combine("\\" + oneOf(list(printables)))
        reLiteralChar = "".join(c for c in printables if c not in r"\[]{}().*?+|") + " \t"

        reRange = Combine(lbrack + SkipTo(rbrack, ignore=escapedChar) + rbrack)
        reLiteral = (escapedChar | oneOf(list(reLiteralChar)))
        reDot = Literal(".")
        repetition = (
                      (lbrace + Word(nums).setResultsName("count") + rbrace) |
                      (lbrace + Word(nums).setResultsName("minCount") + "," + Word(nums).setResultsName("maxCount") + rbrace) |
                      oneOf(list("*+?"))
                      )

        reRange.setParseAction(handle_range)
        reLiteral.setParseAction(handle_literal)
        reMacro.setParseAction(handle_macro)
        reDot.setParseAction(handle_dot)

        reTerm = (reLiteral | reRange | reMacro | reDot)
        reExpr = operatorPrecedence(reTerm, [
                (repetition, 1, opAssoc.LEFT, handle_repetition),
                (None, 2, opAssoc.LEFT, handle_sequence),
                (Suppress('|'), 2, opAssoc.LEFT, handle_alternative),
            ])

        _parser = reExpr

    return _parser
开发者ID:Bernie,项目名称:spyne,代码行数:34,代码来源:invregexp.py

示例13: defineConditionSyntax

	def defineConditionSyntax(self):
		num = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
               Optional(Word(nums)) +
               Optional(oneOf("e E")+Optional(oneOf("+ -")) +Word(nums)))
		op = oneOf("< == > >= <= !=")
		expr = Word(alphanums) + op + num

		return expr
开发者ID:icanos,项目名称:lightswitch,代码行数:8,代码来源:Schema.py

示例14: __init__

    def __init__(self):
        """
        Setup the Backus Normal Form (BNF) parser logic.
        """
        # Set an empty formula attribute
        self.formula = None

        # Instantiate blank parser for BNF construction
        self.bnf = Forward()

        # Expression for parenthesis, which are suppressed in the atoms
        # after matching.
        lpar = Literal(const.LPAR).suppress()
        rpar = Literal(const.RPAR).suppress()

        # Expression for mathematical constants: Euler number and Pi
        e = Keyword(const.EULER)
        pi = Keyword(const.PI)
        null = Keyword(const.NULL)
        _true = Keyword(const.TRUE)
        _false = Keyword(const.FALSE)

        # Prepare operator expressions
        addop = oneOf(const.ADDOP)
        multop = oneOf(const.MULTOP)
        powop = oneOf(const.POWOP)
        unary = reduce(operator.add, (Optional(x) for x in const.UNOP))

        # Expression for floating point numbers, allowing for scientific notation.
        number = Regex(const.NUMBER)

        # Variables are alphanumeric strings that represent keys in the input
        # data dictionary.
        variable = delimitedList(Word(alphanums), delim=const.VARIABLE_NAME_SEPARATOR, combine=True)

        # Functional calls
        function = Word(alphanums) + lpar + self.bnf + rpar

        # Atom core - a single element is either a math constant,
        # a function or a variable.
        atom_core = function | pi | e | null | _true | _false | number | variable

        # Atom subelement between parenthesis
        atom_subelement = lpar + self.bnf.suppress() + rpar

        # In atoms, pi and e need to be before the letters for it to be found
        atom = (
            unary + atom_core.setParseAction(self.push_first) | atom_subelement
        ).setParseAction(self.push_unary_operator)

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

        term = factor + ZeroOrMore((multop + factor).setParseAction(self.push_first))
        self.bnf << term + ZeroOrMore((addop + term).setParseAction(self.push_first))
开发者ID:geodesign,项目名称:django-raster,代码行数:58,代码来源:parser.py

示例15: _string_to_ast

    def _string_to_ast(self, input_string):
        """ Parse a smart search string and return it in an AST like form
        """

        # simple words
        # we need to use a regex to match on words because the regular
        # Word(alphanums) will only match on American ASCII alphanums and since
        # we try to be Unicode / internationally friendly we need to match much
        # much more. Trying to expand a word class to catch it all seems futile
        # so we match on everything *except* a few things, like our operators
        comp_word = Regex("[^*\s=><~!]+")
        word = Regex("[^*\s=><~!]+").setResultsName('word')
        # numbers
        comp_number = Word(nums)
        number = Word(nums).setResultsName('number')

        # IPv4 address
        ipv4_oct = Regex("((2(5[0-5]|[0-4][0-9])|[01]?[0-9][0-9]?))")
        comp_ipv4_address = Combine(ipv4_oct + ('.' + ipv4_oct*3))
        ipv4_address = Combine(ipv4_oct + ('.' + ipv4_oct*3)).setResultsName('ipv4_address')

        # IPv6 address
        ipv6_address = Regex("((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?").setResultsName('ipv6_address')
        ipv6_prefix = Combine(ipv6_address + Regex("/(12[0-8]|1[01][0-9]|[0-9][0-9]?)")).setResultsName('ipv6_prefix')

        # VRF RTs of the form number:number
        vrf_rt = Combine((comp_ipv4_address | comp_number) + Literal(':') + comp_number).setResultsName('vrf_rt')

        # tags
        tags = Combine( Literal('#') + comp_word).setResultsName('tag')

        # operators for matching
        match_op = oneOf(' '.join(self.match_operators)).setResultsName('operator')
        boolean_op = oneOf(' '.join(self.boolean_operators)).setResultsName('boolean')
        # quoted string
        d_quoted_string = QuotedString('"', unquoteResults=True, escChar='\\')
        s_quoted_string = QuotedString('\'', unquoteResults=True, escChar='\\')
        quoted_string = (s_quoted_string | d_quoted_string).setResultsName('quoted_string')
        # expression to match a certain value for an attribute
        expression = Group(word + match_op + (quoted_string | vrf_rt | word | number)).setResultsName('expression')
        # we work on atoms, which are single quoted strings, match expressions,
        # tags, VRF RT or simple words.
        # NOTE: Place them in order of most exact match first!
        atom = Group(ipv6_prefix | ipv6_address | quoted_string | expression | tags | vrf_rt | boolean_op | word)

        enclosed = Forward()
        parens = nestedExpr('(', ')', content=enclosed)
        enclosed << (
                parens | atom
                ).setResultsName('nested')

        content = Forward()
        content << (
                ZeroOrMore(enclosed)
                )

        res = content.parseString(input_string)
        return res
开发者ID:fredsod,项目名称:NIPAP,代码行数:58,代码来源:smart_parsing.py


注:本文中的pyparsing.oneOf函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。