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


Python Group.addParseAction方法代码示例

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


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

示例1: SExpressionParser

# 需要导入模块: from pyparsing import Group [as 别名]
# 或者: from pyparsing.Group import addParseAction [as 别名]
class SExpressionParser(object):
    def __init__(self):
        self.lpar = Literal('(')
        self.rpar = Literal(')')

        self.word_chars = ''.join(c for c in printables if c not in ('()'))
        self.word = Word(self.word_chars) | quotedString
        self.atom = self.word

        self.expression = Forward()

        self.composite_expression = (
            Suppress(self.lpar) +
            ZeroOrMore(self.expression) +
            Suppress(self.rpar))('composite_expression')
        self.composite_expression.addParseAction(
            self._composite_expression_to_tuple)

        self.expression << (self.atom | self.composite_expression)

        self.expressions = Group(ZeroOrMore(self.expression))('expressions')
        self.expressions.addParseAction(self._expressions_to_tuple)

    def parse_expression(self, instring):
        return self.expression.parseString(instring, parseAll=True)[0]

    def parse_expressions(self, instring):
        return self.expressions.parseString(instring, parseAll=True)[0]

    @staticmethod
    def _composite_expression_to_tuple(toks):
        return SExpression(toks.composite_expression)

    @staticmethod
    def _expressions_to_tuple(toks):
        return SExpressionList(toks.expressions)
开发者ID:EdTsft,项目名称:ggp,代码行数:38,代码来源:sexpressions.py

示例2: __init__

# 需要导入模块: from pyparsing import Group [as 别名]
# 或者: from pyparsing.Group import addParseAction [as 别名]
    def __init__(self):
        # define SQL tokens
        selectStmt = Forward()
        selectToken = Keyword("select", caseless=True)
        fromToken   = Keyword("from", caseless=True)
        asToken   = Keyword("as", caseless=True)
        whereToken = Keyword("where", caseless=True)
        semicolon = Literal(";")

        ident = Word( alphas, alphanums + "_$" ).setName("identifier")

        columnName     = delimitedList( ident, ".", combine=True )
        #columnName.setParseAction(upcaseTokens)
        columnNameList = Group( columnName + ZeroOrMore("," + columnName))
#        selectableList = Forward()
        columnRvalList = Forward()
        functionExpr = ident + Optional("."+ident) + Literal('(') + columnRvalList + Literal(')')
        alias = Forward()
        identExpr  = functionExpr | ident
        self.identExpr = identExpr # Debug
        self.functionExpr = functionExpr # Debug
        alias = ident.copy()

        selectableName = identExpr | columnName

        selectableList = Group( selectableName + ZeroOrMore(","+selectableName))
        columnRef = columnName
        functionSpec = functionExpr
        valueExprPrimary = functionSpec | columnRef
        numPrimary = valueExprPrimary ## | numericValFunc
        factor = Optional(Literal("+") | Literal("-")) + numPrimary
        muldiv = oneOf("* /")
        term = Forward()
        term << factor + Optional(muldiv + factor)
        numericExpr = Forward()
        addsub = oneOf("+ -")
        numericExpr << term + Optional(addsub + numericExpr)

        arithop = oneOf("+ - * /")
        columnNumericExpr = Forward()
        cTerm = valueExprPrimary
        testme = valueExprPrimary + arithop + valueExprPrimary
        columnNumericExpr << cTerm + Optional(arithop + columnNumericExpr)
        colNumExpList = Group( columnNumericExpr + ZeroOrMore(","+columnNumericExpr)) 
        

        valueExpr = numericExpr ## | stringExpr | dateExpr | intervalExpr
        derivedColumn = valueExpr + Optional(asToken + alias)
        selectSubList = derivedColumn + ZeroOrMore("," + derivedColumn)


        tableName      = delimitedList( ident, ".", combine=True )
        # don't upcase table names anymore
        # tableName.setParseAction(upcaseTokens) 
        self.tableAction = []
        tableName.addParseAction(self.actionWrapper(self.tableAction))
        tableName.setResultsName("table")
        tableAlias = tableName + asToken + ident.setResultsName("aliasName")
        tableAlias.setResultsName("alias")
        genericTableName = tableAlias | tableName
        genericTableName = genericTableName.setResultsName("tablename")
        tableNameList  = Group( genericTableName 
                                + ZeroOrMore("," + genericTableName))

        whereExpression = Forward()
        and_ = Keyword("and", caseless=True)
        or_ = Keyword("or", caseless=True)
        in_ = Keyword("in", caseless=True)
        between_ = Keyword("between", caseless=True)
    
        E = CaselessLiteral("E")
        binop = oneOf("= != < > >= <= eq ne lt le gt ge", caseless=True)
        arithSign = Word("+-",exact=1)
        realNum = Combine( Optional(arithSign) + ( Word( nums ) + "." 
                                                   + Optional( Word(nums) )  |
                                                   ( "." + Word(nums) ) ) + 
                           Optional( E + Optional(arithSign) + Word(nums) ) )
        intNum = Combine( Optional(arithSign) + Word( nums ) + 
                          Optional( E + Optional("+") + Word(nums) ) )

        # need to add support for alg expressions
        columnRval = realNum | intNum | quotedString | columnNumericExpr# | numericExpr 
        columnRvalList << Group( columnRval + ZeroOrMore("," + columnRval))

        self.whereExpAction = []

        namedRv = columnRval.setResultsName("column")
        whereConditionFlat = Group(
            ( functionSpec + binop + columnRval) |
            ( namedRv + binop + columnRval ) |
            ( namedRv + in_ + "(" + columnRval + ZeroOrMore(","+namedRv) + ")" ) |
            ( namedRv + in_ + "(" + selectStmt + ")" ) |
            ( namedRv + between_ + namedRv + and_ + namedRv ) )

        whereConditionFlat.addParseAction(self.actionWrapper(self.whereExpAction))
        whereCondition = Group(whereConditionFlat 
                               | ( "(" + whereExpression + ")" ))

        # Test code to try to make an expression parse.
        # print whereConditionFlat.parseString("ABS(o1.ra - o2.ra) < 0.00083 / COS(RADIANS(o2.decl))")
#.........这里部分代码省略.........
开发者ID:lsst-dm,项目名称:legacy-qserv_master,代码行数:103,代码来源:sqlparser.py


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