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


Python Forward.enablePackrat方法代码示例

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


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

示例1: Grammar

# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import enablePackrat [as 别名]
def Grammar(home):
    # Productions
    cfoperator  =   equalsOp ^ notequalsOp
  
    pathElement=    seperator + identifier
    RelSpec    =    OneOrMore(pathElement)
    AbsSpec    =    identifier + RelSpec

    ExprField  =    AbsSpec ^ RelSpec

    ExprText   =    Forward()   
    BoolExpr   =    ExprField + cfoperator + ExprText 
    QueryExpr  =    BoolExpr + queryOp + ExprText + "/" +ExprText

    ExprText   <<  (  ExprField ^ \
    		          QueryExpr ^ \
                      LiteralVal  )

    ## Functions for parsing.
    def doBool(s,loc,toks):
    	modlogger.debug( "getbol\n")
        sense=(toks[1]=="!=")
    	return sense ^ ( str(toks[0]) == toks[2])


    def doQuery(s,loc,toks):
    	modlogger.debug( "doing query\n")
        if toks[0]:
    		return toks[2]
    	else:
    		return toks[4]

    def ExprFieldAction(s,loc,toks):
        #In this case our walk will fail so
        # just return None as an invalid thang.
        if home is None: return None

        #Determine whether abs or rel and
        # find our origin.
        # We use the home  objct - or
        # derefernce the object if it is an absolute reference.
        # - we can't just get the category because there is (currently)
        #   no object which represents those , but we can get an object
        #   and the grammar is specified such that an object must be spcified
        #   not just a category`
        if toks[0] == ":":
            origin  = home
            path    = toks[1:]
        else:
            origin  = home.get_root().get_object(toks[0],toks[2])
            path    = toks[4:]
        
        #Walk along the attributes
        field = origin
        canonpath = origin.get_nodeid()
        for ele in path:
            if ele != ":": #Skip ':' as grammar noise.
                if field is None:
                    raise NullReference(canonpath)
                canonpath ="%s:%s"%(field.get_nodeid(),ele)
                try:
                    field = field[ele]
                except KeyError:
                    raise NoAttribute("%s has no attribute `%s'"%(field.get_nodeid(),ele))

                if hasattr(field,"getSelf"):
                    field = field.getSelf()
        return field
    
    ## Bind functions to parse actions
    ExprField.setParseAction(ExprFieldAction)
    BoolExpr.setParseAction(doBool)
    QueryExpr.setParseAction(doQuery)

    ExprText.enablePackrat()
    ExprText.validate()
    return ExprText + stringEnd
开发者ID:rgammans,项目名称:MysteryMachine,代码行数:79,代码来源:grammar.py

示例2: Regex

# 需要导入模块: from pyparsing import Forward [as 别名]
# 或者: from pyparsing.Forward import enablePackrat [as 别名]
ID = ~MatchFirst([Keyword(w) for w in _keywords]) + Regex(r"[a-zA-Z_][a-zA-Z0-9_$]*")("id")
LP, RP, LB, RB, LC, RC, COLON, SEMICOLON, CAMMA, PERIOD, SHARP, EQUAL, AT, ASTA, Q, PLUS, MINUS, USC, APS = map(
    Suppress, ("()[]{}:;,.#[email protected]*?+-_'")
)

DSLASH = Suppress(Literal("//"))

for k in _keywords:
    setattr(this_mod, k.swapcase(), Keyword(k)("keyword"))
    # setattr(sys.modules[__name__],k,Literal(k))

with open(_non_terminal_symbols_file, "r") as f:
    for name in (line.strip() for line in f):
        sym = Forward()(name)
        sym.enablePackrat()
        sym.ignore(cStyleComment)
        # print("sym={0}".format(name))
        setattr(this_mod, name, sym)


def alias(grammar, name):
    if name:
        return Group(grammar)(name)
    else:
        return Group(grammar)


class ErrorReportException(ParseException):
    pass
开发者ID:jtfrom9,项目名称:yarl,代码行数:31,代码来源:parser.py


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