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


Python ParserElement.enablePackrat方法代码示例

本文整理汇总了Python中pyparsing.ParserElement.enablePackrat方法的典型用法代码示例。如果您正苦于以下问题:Python ParserElement.enablePackrat方法的具体用法?Python ParserElement.enablePackrat怎么用?Python ParserElement.enablePackrat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyparsing.ParserElement的用法示例。


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

示例1: defineParsers

# 需要导入模块: from pyparsing import ParserElement [as 别名]
# 或者: from pyparsing.ParserElement import enablePackrat [as 别名]
def defineParsers():
    #Enable a fast parsing mode with caching.
    ParserElement.enablePackrat()
    #end of line terminates statements, so it is not regular whitespace
    ParserElement.setDefaultWhitespaceChars('\t ')

    func_call = Forward() #forward declaration because this is a recursive rule   

    #The "terminal" rules
    symbol = Word(alphas+'_-', alphanums+'_-') .setParseAction(action_symbol)
    q_symbol = quotedString                    .setParseAction(action_q_symbol)
    bracket_term = Literal("(").suppress() - func_call \
                   + Literal(")").suppress()
    word = symbol | q_symbol | bracket_term
    
    #The function call
    #Parse: "foo | bar | baz" or "foo"
    pipeline = (word + ZeroOrMore("|" - word)) .setParseAction(action_pipeline) 
    #Parse "foo|bar op1 op2 op3"
    func_call << (pipeline - ZeroOrMore(word)) .setParseAction(action_func_call)
    
    #High level structure of program
    line = LineEnd() | func_call - LineEnd() #empty line or function call
    program = ZeroOrMore(line) + StringEnd() #multiple lines are a program
    
    #define the comments
    program.ignore('%' + restOfLine)
    #no tab expansion
    program.parseWithTabs()   
    #return additional func_call parser to make testing more easy
    return program, func_call
开发者ID:eike-welk,项目名称:freeode,代码行数:33,代码来源:simple_config_lang.py

示例2: __init__

# 需要导入模块: from pyparsing import ParserElement [as 别名]
# 或者: from pyparsing.ParserElement import enablePackrat [as 别名]
 def __init__(self, alphabet):
     self.operators = alphabet.getOperators()
     self.constants = alphabet.getConstants()
     self.notNeedSpace = alphabet.notNeedSpace()
     
     self.ffactory = FormulaFactory()
     self.__createGram()
     ParserElement.enablePackrat()
开发者ID:pszynk,项目名称:LIProjekt,代码行数:10,代码来源:parsers.py

示例3: _int_expression

# 需要导入模块: from pyparsing import ParserElement [as 别名]
# 或者: from pyparsing.ParserElement import enablePackrat [as 别名]
def _int_expression():
    from pyparsing import Word, alphanums, nums, Forward, ZeroOrMore, Combine, CaselessLiteral, srange, ParserElement, Optional
    ParserElement.enablePackrat()

    entry_name = Word(alphanums + ' _+:.-/')
    integer = Combine(Optional('-') + Word(nums)).addParseAction(lambda s,l,t: [Constant(int(t[0]))])
    hex = Combine(CaselessLiteral("0x") + Word(srange("[0-9a-fA-F]"))).addParseAction(lambda s,l,t:[Constant(int(t[0][2:], 16))])
    named_reference = ('${' + entry_name + '}').addParseAction(lambda s,l,t:ValueResult(t[1]))
    length_reference = ('len{' + entry_name + '}').addParseAction(lambda s,l,t:LengthResult(t[1]))

    expression = Forward()
    factor = hex | integer | named_reference | length_reference | ('(' + expression + ')').addParseAction(lambda s,l,t:t[1])

    entry = factor
    for ops in _operators:
        op_parse = reduce(operator.or_,
                [(character + entry).addParseAction(_half(op)) for character, op in ops])
        entry = (entry + ZeroOrMore(op_parse)).addParseAction(_collapse)
    expression << entry
    return expression
开发者ID:birdming22,项目名称:bdec,代码行数:22,代码来源:expression.py

示例4: dbobject

# 需要导入模块: from pyparsing import ParserElement [as 别名]
# 或者: from pyparsing.ParserElement import enablePackrat [as 别名]
                       nums,
                       alphas,
                       Combine,
                       oneOf,
                       opAssoc,
                       operatorPrecedence,
                       QuotedString,
                       Literal,
                       ParserElement,
                       ParseException,
                       Forward,
                       Group,
                       Suppress,
                       Optional,
                       Regex)
ParserElement.enablePackrat()

from sqlalchemy import and_, or_, func
#from sqlalchemy.orm import aliased

import operator
import re


def dbobject(obj):
    return getattr(obj, '__moyadbobject__', lambda: obj)()


@implements_to_string
class DBExpressionError(Exception):
    hide_py_traceback = True
开发者ID:esaye,项目名称:moya,代码行数:33,代码来源:dbexpression.py

示例5: make_amr_parser

# 需要导入模块: from pyparsing import ParserElement [as 别名]
# 或者: from pyparsing.ParserElement import enablePackrat [as 别名]
def make_amr_parser():
    """
    Pyparsing parser for AMRs. This will return an abstract syntax tree that
    needs to be converted into an AMR using ast_to_amr.
    """
    def debug(s, loc, tok):
        if len(tok) > 1:
            flat = [tok[0]] + tok[1:]
        else: 
            flat =  tok
        return flat

    def parse_concept_expr(s, loc, tok):       
        node_name = tok[0]
        concept_name = None

        roles = []
        if len(tok) > 1:
           if type(tok[1]) is tuple:
                roles = tok[1:]
           else: 
              concept_name = tok[1]
              if len(tok) > 2:
                roles = tok[2:]
        return (node_name, concept_name, roles)
   
    ParserElement.enablePackrat() # Hopefully no bug in here...


    def parse_role(s,loc,tok):
        if len(tok) >= 2:
            r, ch = tok[0], []
            for v in tok[1:]:
                if isinstance(v, StrLiteral):
                # Parse the node alignment and move it to the edge
                    parts = v.replace(" ","").rsplit("~",1)   
                    if len(parts) >= 2:
                        v, align = parts
                        v = StrLiteral(v)
                        r = "%s~%s" % (r.strip(), align.strip())
                elif isinstance(v, SpecialValue):
                    parts = v.replace(" ","").rsplit("~",1)
                    if len(parts) >= 2:                            
                        v, align = parts
                        v = StrLiteral(v)
                        r = "%s~%s" % (r.strip(), align.strip())
                ch.append(v)              
            return  r, ch       
        else:
            return tok[0] 

    # Number are all mapped to the same node in the graph because of interning
    parse_quantity = lambda s, loc, tok: StrLiteral(" ".join(tok)) #float(tok[0]) if "." in tok[0] else int(tok[0]) 

    parse_string_literal = lambda s, loc, tok: StrLiteral(" ".join(tok)) 

    parse_special_value = lambda s, loc, tok: SpecialValue(" ".join(tok))

    lpar  = Literal( "(" ).suppress()
    rpar  = Literal( ")" ).suppress()

    quantity = Word(nums+".,").setParseAction(parse_quantity)

    node_name =  Word(alphas+nums+"""@-_.~$/<>%&!+\*?^`"'""") #Word(alphas+nums+"[email protected]")  

    lit_string = Literal('"').suppress() + CharsNotIn('"') + Literal('"').suppress() 
    concept_name = lit_string | Word(alphas+nums+"""-_.,`~$/<>%&!+\*?^"'""")     
    role_name = Word(alphas+nums+"""-_.,~$/<>%&!+\*:?^`"'""") | Literal("#").suppress()+Word(alphas+nums+"[]-$_").setParseAction(lambda s, loc, tok: NonterminalLabel(tok[0]))     


    special_attr = (Literal("-") | Literal("interrogative") | Literal("SHOULD") | Literal("MUST") | Literal("HAVE-TO")| Literal("WOULD") | Literal("CAN") |  Literal("DARE-TO")| Literal("BE-TO") | Literal("MAY") | Literal("GOING-TO") | Literal("MIGHT") | Literal("USED-TO")) + Optional(Literal("~")+Word(alphas+nums+"."))
    

    expr = Forward()
    value =  expr |\
             quantity.setParseAction(parse_quantity) |\
             special_attr.setParseAction(parse_special_value) | \
             node_name |\
             (lit_string + Optional(Literal("~")+Word(alphas+nums+"."))).setParseAction(parse_string_literal) 
             
  

    valuelist = Forward()
    valuelist << (value + Literal(",").suppress() + valuelist | value).setParseAction(debug)
    role = (Literal(":").suppress() + role_name + valuelist).setParseAction(parse_role)
 
    expr.setParseAction(parse_concept_expr) 
    expr << (lpar +  node_name + Optional(Literal("/").suppress() + concept_name) + ZeroOrMore(role) + rpar)
    
    return expr 
开发者ID:christianbuck,项目名称:nlu,代码行数:92,代码来源:amr_parser.py

示例6: import

# 需要导入模块: from pyparsing import ParserElement [as 别名]
# 或者: from pyparsing.ParserElement import enablePackrat [as 别名]
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import math
import re
import pyparsing
from pyparsing import (ParserElement, Word, oneOf, Literal, CaselessLiteral,
                       Regex, Optional, Suppress, Forward, FollowedBy, NotAny,
                       Group, OneOrMore, ZeroOrMore,
                       nums, alphas, ParseResults)
ParserElement.enablePackrat() # Significant speedup.

from . import operators
from .units import Q_

# Definitions. #########################################################

# These are reserved words that cannot be variables, constants, or units.
keyword = oneOf("to")

# Special symbols for currencies.
currency_symbols = oneOf("€ £ $ ₪ ¥ ¥ ₩ ₩ ฿ ₹")

# Identifier, must start with unicode letter, can then contain unicode
# letter, unicode number, or underscore.
identifier = currency_symbols | ( NotAny( keyword ) + Regex(r'[^\W\d_]\w*') )
开发者ID:t-brink,项目名称:pscic,代码行数:33,代码来源:parseexpr.py

示例7: make_grammar

# 需要导入模块: from pyparsing import ParserElement [as 别名]
# 或者: from pyparsing.ParserElement import enablePackrat [as 别名]
def make_grammar():
    from pyparsing import (ParserElement, Literal, Word, Forward,
                           Optional, QuotedString, Combine,
                           ZeroOrMore, Keyword, alphas, alphanums,
                           nums)
    ParserElement.enablePackrat()

    plus = Literal("+")
    minus = Literal("-")

    mul = Literal("*")
    div = Literal("/")
    floordiv = Literal("//")
    mod = Literal("%")

    lt = Literal("<")
    le = Literal("<=")

    gt = Literal(">")
    ge = Literal(">=")

    lshift = Literal("<<")
    rshift = Literal(">>")

    equal = Literal("==") | Literal("=") | Literal("!=")

    bitwise_not = Literal("~")
    bitwise_and = Literal("&")
    bitwise_or = Literal("|")
    bitwise_xor = Literal("^")

    logical_not = Literal("!") | Keyword("not")
    logical_and = Literal("&&") | Literal("and") | Keyword("AND")
    logical_or = Literal("||") | Keyword("or") | Keyword("OR")

    ident = Word(alphas + "_", alphanums + "_")
    functionname = Word(alphas + "_", alphanums + "_")
    unit = Word(alphas)
    int_number = Word(nums)
    float_number = Combine(Word(nums) + Optional(Literal(".") + Word(nums)))
    number = (float_number | int_number) + Optional(unit)

    lparent = Literal("(").suppress()
    rparent = Literal(")").suppress()

    relational_op = (lt | le | gt | ge)
    shift = (lshift | rshift)
    add_op = (plus | minus)
    mul_op = (mul | floordiv | div | mod)

    expr = Forward()
    string = (QuotedString('"') | QuotedString("'"))
    primary_expr = ident | number | string | (lparent + expr + rparent)

    def make_op(s, loc, toks):
        if len(toks) == 1:
            return toks[0]
        else:
            def loop(lhs, rest):
                if len(rest) == 0:
                    return lhs
                else:
                    return loop(Operator(rest[0], lhs, rest[1]), rest[2:])

            return loop(Operator(toks[1], toks[0], toks[2]), toks[3:])

    def make_unary(s, loc, toks):
        if len(toks) == 1:
            return toks[0]
        else:
            return UnaryOperator(toks[0], make_unary(s, loc, toks[1:]))

    argument_expression_list = expr + ZeroOrMore(Literal(",").suppress() + expr)
    function_expression = (functionname + lparent + argument_expression_list + rparent)
    postfix_expression = function_expression | primary_expr
    unary_expr = ZeroOrMore(bitwise_not | logical_not | minus | plus) + postfix_expression
    cast_expresion = unary_expr | postfix_expression

    mult_expr        = cast_expresion   + ZeroOrMore(mul_op        + cast_expresion)  # noqa: E221
    add_expr         = mult_expr        + ZeroOrMore(add_op        + mult_expr)  # noqa: E221
    shift_expr       = add_expr         + ZeroOrMore(shift         + add_expr)  # noqa: E221
    relational_expr  = shift_expr       + ZeroOrMore(relational_op + shift_expr)  # noqa: E221
    equality_expr    = relational_expr  + ZeroOrMore(equal         + relational_expr)  # noqa: E221
    bitwise_and_expr = equality_expr    + ZeroOrMore(bitwise_and   + equality_expr)  # noqa: E221
    bitwise_xor_expr = bitwise_and_expr + ZeroOrMore(bitwise_xor   + bitwise_and_expr)  # noqa: E221
    bitwise_or_expr  = bitwise_xor_expr + ZeroOrMore(bitwise_or    + bitwise_xor_expr)  # noqa: E221
    logical_and_expr = bitwise_or_expr  + ZeroOrMore(logical_and   + bitwise_or_expr)  # noqa: E221
    logical_or_expr  = logical_and_expr + ZeroOrMore(logical_or    + logical_and_expr)  # noqa: E221
    expr <<= logical_or_expr

    function_expression.setParseAction(Function)

    int_number.setParseAction(lambda s, loc, toks: int(toks[0]))
    float_number.setParseAction(lambda s, loc, toks: float(toks[0]))
    number.setParseAction(Number)
    string.setParseAction(String)
    ident.setParseAction(Variable)

    unary_expr.setParseAction(make_unary)
    mult_expr.setParseAction(make_op)
#.........这里部分代码省略.........
开发者ID:Grumbel,项目名称:dirtool,代码行数:103,代码来源:expr.py

示例8: EvalConstant

# 需要导入模块: from pyparsing import ParserElement [as 别名]
# 或者: from pyparsing.ParserElement import enablePackrat [as 别名]
# Add // and % to multOp & EvalMultOp
# Keep integer values as integers until something converts them
# Allow longer var names

# Based on:
#
# eval_arith.py
#
# Copyright 2009, Paul McGuire
#
# Expansion on the pyparsing example simpleArith.py, to include evaluation
# of the parsed tokens.

from pyparsing import Word, nums, alphas, Combine, oneOf, Optional, \
    opAssoc, operatorPrecedence, ParserElement
ParserElement.enablePackrat() # Add memoization to parsing logic to increase performance

class EvalConstant():
    "Class to evaluate a parsed constant or variable"
    def __init__(self, tokens):
        self.value = tokens[0]
    def eval(self, vars_):
        if self.value in vars_:
            return vars_[self.value]
        else:
            # Try to return an int, then a float, and finally a string
            try:
                return int( self.value )
            except:
                pass
            try:
开发者ID:jbutler,项目名称:lenderbot,代码行数:33,代码来源:FilterParser.py


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