示例#1
0
    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)
示例#2
0
def parseTypes(path):
  msgs = set()
  types = {}

  for line in lineGen(path):
    number = Word(nums)
    word = Word(alphanums + "-_")

    wordList = Forward()
    wordList = word + ZeroOrMore(',' + word)

    par = (Literal('NetworkPartition').setResultsName('type') +\
      '(' + Literal('Set') + '(' +\
      wordList.setResultsName('p1') + ')' + ',' +\
      Literal('Set') + '(' +\
      wordList.setResultsName('p2') + ')' + \
      ')')

    subType = (word + Optional(nestedExpr('(', ')'))).setResultsName('msg')
    msg = (Literal('MsgEvent').setResultsName('type') +\
       '(' + word.setResultsName('src') + ',' +\
       word.setResultsName('dst') + ',' +\
       subType  + ')')

    event = Word( nums ) +\
      Literal('Unique') + "(" + (msg | par) + ',' +\
      number.setResultsName('uid')  + ')'

    result = event.parseString(line)

    key = result.uid
    if result.type == 'MsgEvent':
      msg = list2tuple( result.msg.asList() )
      value = (result.type, result.src, result.dst, msg)
      msgs.add(msg)
    elif result.type == 'NetworkPartition':
      value = (result.type, result.p1, result.p2)

    types[key] = value

  return types
示例#3
0
def parseTypes(path):
    msgs = set()
    types = {}

    for line in lineGen(path):
        number = Word(nums)
        word = Word(alphanums + "-_")

        wordList = Forward()
        wordList = word + ZeroOrMore(',' + word)

        par = (Literal('NetworkPartition').setResultsName('type') +\
          '(' + Literal('Set') + '(' +\
          wordList.setResultsName('p1') + ')' + ',' +\
          Literal('Set') + '(' +\
          wordList.setResultsName('p2') + ')' + \
          ')')

        subType = (word + Optional(nestedExpr('(', ')'))).setResultsName('msg')
        msg = (Literal('MsgEvent').setResultsName('type') +\
           '(' + word.setResultsName('src') + ',' +\
           word.setResultsName('dst') + ',' +\
           subType  + ')')

        event = Word( nums ) +\
          Literal('Unique') + "(" + (msg | par) + ',' +\
          number.setResultsName('uid')  + ')'

        result = event.parseString(line)

        key = result.uid
        if result.type == 'MsgEvent':
            msg = list2tuple(result.msg.asList())
            value = (result.type, result.src, result.dst, msg)
            msgs.add(msg)
        elif result.type == 'NetworkPartition':
            value = (result.type, result.p1, result.p2)

        types[key] = value

    return types
示例#4
0
def parser(text):
    """
    str := \w+
    str := '\w+'
    exp := Var=str
    exp := exp & exp
    exp := exp ^ exp
    """
    
    # grammar
    #g_string = "'"+Word(alphas)+"'" | Word(alphas)
    g_quote = Literal("'").suppress()
    g_text = Regex("[\w\s\:\#\.]+").setResultsName("text")
    g_string = Optional(g_quote) + g_text + Optional(g_quote)
    g_equ =  Literal("!=").setResultsName("connector") | Literal("=").setResultsName("connector")
    g_amp = Literal("&").setResultsName("connector")
    g_hat = Literal("^").setResultsName("connector")
    g_or = Literal("|").suppress()
    g_seq = Literal("->").setResultsName("connector")
    g_hash = Literal("#").setResultsName("hash")

    g_left_brack = Literal("[").suppress()
    g_right_brack = Literal("]").suppress()

    
    g_vals = Forward()
    g_vals << g_string + ZeroOrMore(Group(g_or + g_vals).setResultsName("or_group"))

    # working
    """
    exp_basic = Group(Optional(g_hash) + g_string).setResultsName("left") + g_equ + Group(g_vals).setResultsName("right")
    exp = Group(exp_basic)
    exp = exp.setResultsName("left") + g_amp + exp.setResultsName("right") | \
            g_left_brack + exp.setResultsName("left") + g_hat + exp.setResultsName("right") + g_right_brack | \
            g_left_brack + exp.setResultsName("left") + g_seq + exp.setResultsName("right") + g_right_brack | \
            exp_basic
    """

    # recursion
    simpleq = Forward()
    complexq = Forward()

    exp = (simpleq | complexq).setResultsName("exp")
    exp_basic = Group(Group(Optional(g_hash) + g_string).setResultsName("left") + g_equ + Group(g_vals).setResultsName("right"))
    simpleq << (Group(exp_basic.setResultsName("left") + g_amp + simpleq.setResultsName("right")) | exp_basic)
    complexq << ( Group(g_left_brack + exp.setResultsName("left") + g_hat + exp.setResultsName("right") + g_right_brack) | \
                  Group(g_left_brack + exp.setResultsName("left") + g_seq + exp.setResultsName("right") + g_right_brack) )
    
    
    
    return exp.parseString(text)
示例#5
0
def parser():
    global _parser
    if _parser is None:
        ParserElement.setDefaultWhitespaceChars("")
        
        lbrack = Literal("[")
        rbrack = Literal("]")
        lbrace = Literal("{")
        rbrace = Literal("}")
        lparen = Literal("(")
        rparen = Literal(")")
        
        reMacro = Suppress("\\") + oneOf(list("dwsZ"))
        escapedChar = ~reMacro + Combine("\\" + oneOf(list(printables)))
        reLiteralChar = "".join(c for c in string.printable if c not in r"\[]{}().*?+|")

        reRange = Combine(lbrack.suppress() + SkipTo(rbrack,ignore=escapedChar) + rbrack.suppress())
        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("*+?"))
            )
        reExpr = Forward()
        reGroup = (lparen.suppress() +
                   Optional(Literal("?").suppress() + oneOf(list(":P"))).setResultsName("option") +
                   reExpr.setResultsName("expr") +
                   rparen.suppress())

        reTerm = ( reLiteral | reRange | reMacro | reDot | reGroup )
        reExpr << operatorPrecedence( reTerm,
            [
            (repetition, 1, opAssoc.LEFT, create(Repetition)),
            (None, 2, opAssoc.LEFT, create(Sequence)),
            (Suppress('|'), 2, opAssoc.LEFT, create(Alternation)),
            ]
            )

        reGroup.setParseAction(create(Group))
        reRange.setParseAction(create(Range))
        reLiteral.setParseAction(create(Character))
        reMacro.setParseAction(create(Macro))
        reDot.setParseAction(create(Dot))
        
        _parser = reExpr
        
    return _parser
示例#6
0
def parser():
    global _parser
    if _parser is None:
        ParserElement.setDefaultWhitespaceChars("")

        lbrack = Literal("[")
        rbrack = Literal("]")
        lbrace = Literal("{")
        rbrace = Literal("}")
        lparen = Literal("(")
        rparen = Literal(")")

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

        reRange = Combine(lbrack.suppress() +
                          SkipTo(rbrack, ignore=escapedChar) +
                          rbrack.suppress())
        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("*+?")))
        reExpr = Forward()
        reGroup = (lparen.suppress() +
                   Optional(Literal("?").suppress() +
                            oneOf(list(":P"))).setResultsName("option") +
                   reExpr.setResultsName("expr") + rparen.suppress())

        reTerm = (reLiteral | reRange | reMacro | reDot | reGroup)
        reExpr << operatorPrecedence(reTerm, [
            (repetition, 1, opAssoc.LEFT, create(Repetition)),
            (None, 2, opAssoc.LEFT, create(Sequence)),
            (Suppress('|'), 2, opAssoc.LEFT, create(Alternation)),
        ])

        reGroup.setParseAction(create(Group))
        reRange.setParseAction(create(Range))
        reLiteral.setParseAction(create(Character))
        reMacro.setParseAction(create(Macro))
        reDot.setParseAction(create(Dot))

        _parser = reExpr

    return _parser
示例#7
0
def build_grammer():

    selectStmt = Forward()
    compoundselectStmt = Forward()
    subgraphselectStmt = Forward()
    sampleselectStmt = Forward()
    setStmt = Forward()
    selectToken  = oneOf("select find get what search list", caseless=True)
    fromToken   = Keyword("from", caseless=True)
    whereToken   = Keyword("where", caseless=True)
    sampleToken =Keyword("sample",caseless=True)
    subgraphToken =Keyword("subgraph",caseless=True)
    neighborsToken =Keyword("neighbors",caseless=True)
    targetToken = oneOf("edges nodes  node edge",caseless=True)
    ident          = Word( alphas, alphanums + "_$").setName("identifier")

    columnName = Combine((oneOf("v u e")+"."+ ident)) | Combine(neighborsToken+"("+Word(nums).setResultsName("friends",listAllMatches=True)+")"+"."+ident) |ident


    whereExpression = Forward()
    runs_ = Keyword("number",caseless=True)
    and_ = Keyword("and", caseless=True)
    or_ = Keyword("or", caseless=True)
    in_ = Keyword("in", caseless=True)
    size_ = Keyword("size", caseless=True)
    identifier = Word(alphas+"_", alphanums+"_")


    E = CaselessLiteral("E")
    binop = oneOf("= != < > >= <= eq ne lt le gt ge", caseless=True)


    setop = oneOf("union intersect except", caseless=True)
    arithSign = Word("+-",exact=1)
    realNum = Combine( Optional(arithSign) + ( Word( nums ) + "." + Optional( Word(nums) )  |
                                           ( "." + Word(nums) ) ) +
                   Optional( E + Optional(arithSign) + Word(nums) ) )




    samplestmt = (sampleToken+"("+runs_+"="+Word( nums ).setResultsName("runs")+","+size_+"="+"["+Word( nums ).setResultsName("lb")+","+Word( nums ).setResultsName("sample")+"]"+")")

    subgraphstmt = (subgraphToken.setResultsName("type")+"("+Word(nums).setResultsName("startnode")+","+Word(nums).setResultsName("depth")+")")

    intNum = Combine( Optional(arithSign) + Word( nums ) +
                  Optional( E + Optional("+") + Word(nums) ) )

    columnRval = realNum | intNum | quotedString | columnName.setResultsName("column",listAllMatches=True)



    whereCondition = (columnName.setResultsName("column",listAllMatches=True)+ binop + columnRval )|  ( columnName.setResultsName("column",listAllMatches=True) + in_ + "(" + columnRval +ZeroOrMore("," + columnRval)  + ")" )

    whereCondition2 = Group((columnName.setResultsName("column",listAllMatches=True)+ binop + columnRval )|  ( columnName.setResultsName("column",listAllMatches=True) + in_ + "(" + columnRval +ZeroOrMore("," + columnRval)  + ")" ) | ( "(" + whereExpression + ")" ))

    whereExpression << whereCondition + ZeroOrMore( ( and_ | or_ ) + whereExpression )


    defstmt =ident.setResultsName( "graph")+"."+ targetToken.setResultsName( "type") + "="+  "{" + delimitedList( whereCondition2 ).setResultsName("compactwhere") + "}"

    function_call = identifier.setResultsName("func",listAllMatches=True) + "(" + ((delimitedList(identifier|Word(nums)).setResultsName("args",listAllMatches=True))) + ")" | identifier.setResultsName("func",listAllMatches=True)

    wfstmt = Optional(delimitedList(function_call))

    selectStmt      << ( selectToken +
                     targetToken.setResultsName( "type" ) +
                     fromToken +
                     (ident.setResultsName( "graph"))+
                     Optional(whereToken + (whereExpression).setResultsName("where", listAllMatches=True) ))



    sampleselectStmt  << ( selectToken +samplestmt+
                     targetToken.setResultsName( "type") +
                     fromToken +
                     (ident.setResultsName( "graph"))+
                     Optional(whereToken + (whereExpression).setResultsName("where", listAllMatches=True) ))



    subgraphselectStmt  << ( selectToken +subgraphstmt +
                     fromToken +
                     (ident.setResultsName( "graph")))



    compoundselectStmt << selectStmt.setResultsName("select",listAllMatches=True) + ZeroOrMore(setop.setResultsName("setop",listAllMatches=True)  + selectStmt )

    setStmt << ident.setResultsName("setname",listAllMatches=True) + ZeroOrMore (setop.setResultsName("setp",listAllMatches=True) + ident.setResultsName("setname"))


    SQL = sampleselectStmt|compoundselectStmt|subgraphselectStmt|setStmt


    bSQL = SQL
    SqlComment = "--" + restOfLine
    bSQL.ignore( SqlComment )

    return bSQL
示例#8
0
文件: transform.py 项目: bytekid/ctrl
tMod = OneOrMore(Or(mkLit(s) for s in mods) ^ sizeMod).setParseAction(mkMod)
tExpr = (lBvExpr | \
         (xFun  + tBase + to + sizeMod).setParseAction(pushXFun) | \
         (funName + Optional(tMod) + Optional(tArgs))\
           .setParseAction(pushTermFun))
var = Combine(percent + Word(alphanums)).setParseAction(pushExpr0( \
        lambda i: Ident(i)))

comment = Combine(Literal(";") + CharsNotIn("\n") + LineEnd()).suppress()
comments = ZeroOrMore(comment).suppress()
lineend = (LineEnd() + comments).suppress()
tVarDef = (var + assign + tExpr + Optional(comment)).setParseAction(assignVar)
side = OneOrMore(tVarDef).setParseAction(mkSide)
nameLine = name + CharsNotIn("\n").setResultsName("name") + \
           lineend.setParseAction(clearVars)
preLine = (pre + lBoolExpr.setResultsName("pre") + (lineend | comment))
rule = (nameLine + \
        Optional(preLine) + \
        side.setResultsName("lhs") + \
        Literal("=>") + \
        side.setResultsName("rhs")).setParseAction(addRule)
rules = OneOrMore(rule + comments)

def testPre(s):
  print("testPre: " + s)
  try:
    r = preLine.parseString(s)
    print("  " + r['pre'][0].toString())
  except Exception as exc:
    print("{}: {}".format(type(exc).__name__, exc))
insert_stmt = INSERT + INTO + table_name + VALUES +LPAR + insert_values + RPAR

# Delete Statement
# ================
delete_stmt = (DELETE + FROM +table_name + Optional(where_clause))


# Update Statement
# ================
#      <update>          ::= UPDATE <table>
#                            SET <list-set-clause>
#                            [<where-clause>]
#      <list-set-clause> ::= <set-clause> [, <set-clause>]*
#      <set-clause>      ::= <colname> = <update-source>
#      <update-source>   ::= <arith-expr>
update_source = arith_expr.setResultsName('update_source')
set_clause = Group(column_name + equal_op + update_source)
list_set_clauses = delimitedList(set_clause).setResultsName('list_set_clauses')
update_stmt = (UPDATE + table_name +
               SET + list_set_clauses +
               Optional(where_clause))

# Select Statement
# ================
#      <select>         ::= SELECT [DISTINCT] <select list>
#                           <from-clause>
#                           [<where-clause>]
#                           [<order-by-clause>]
#
#      <select list>    ::= <start> | <derived-col> [<comma> <derived-col>]*
#      <derived-col>    ::= <arith-expr> [AS <alias>]
示例#10
0
def _setup_QASMParser():
    """
    Routine to initialise and return parsing blocks
    """
    class _Op:
        """ Class to set up quantum operations """
        def __init__(self,
                     name,
                     argParser,
                     version="OPENQASM 2.0",
                     qop=False,
                     keyOverride=None):
            global cops
            global qops
            global _reservedKeys
            if name in qops or name in cops:
                raise IOError(dupTokenWarning.format("Operation", name))
            self.operation = name
            if keyOverride is not None:
                self.parser = (keyOverride + argParser).addParseAction(
                    lambda s, l, t: _override_keyword(t, name))
            else:
                self.parser = CaselessKeyword(name)("keyword") + argParser

            self.version = parse_version(version)
            self.parser.addParseAction(
                lambda s, l, t: _set_version(t, self.version))

            _reservedKeys.append(name)
            if qop:
                qops[name] = self
            else:
                cops[name] = self

    class _Routine():
        """ Class to set up quantum gates, circuits, etc. """
        def __init__(self,
                     name,
                     pargs=False,
                     spargs=False,
                     gargs=False,
                     qargs=False,
                     returnables=False,
                     prefixes=None,
                     version="OPENQASM 2.0"):
            global blocks
            global _reservedKeys
            if name in qops or name in cops:
                raise IOError(dupTokenWarning.format("Routine", name))
            self.operation = name

            self.parser = Keyword(name)("keyword") + validName("gateName")

            if prefixes:
                localPrefixParser = Each(map(Optional, map(
                    Keyword, prefixes))).addParseAction(prefix_setter)
            else:
                localPrefixParser = prefixParser
            self.parser = localPrefixParser + self.parser

            # Handle different args
            req = []
            if pargs:
                req.append(Optional(pargParser)("pargs"))
            if spargs:
                req.append(Optional(spargParser)("spargs"))
            if gargs:
                req.append(Optional(gargParser)("gargs"))
            self.parser = self.parser + Each(req)
            if qargs:
                self.parser = self.parser + qargParser("qargs")
            if returnables:
                self.parser = self.parser + Optional(returnParser)

            self.version = parse_version(version)
            self.parser.addParseAction(
                lambda s, l, t: _set_version(t, self.version))

            _reservedKeys.append(name)
            blocks[name] = self

    class _Block():
        """ Class to set up blocks such as if, for, etc. """
        def __init__(self, name, detParser, version="OPENQASM 2.0"):
            global blocks
            global _reservedKeys
            self.operation = name
            self.parser = Keyword(name)("keyword") + detParser

            self.version = parse_version(version)
            self.parser.addParseAction(
                lambda s, l, t: _set_version(t, self.version))

            _reservedKeys.append(name)
            blocks[name] = self

    sign = Word("+-", exact=1)
    number = Word(nums)
    expo = Combine(CaselessLiteral("e") + Optional(sign) +
                   number).setResultsName("exponent")

    pi = CaselessKeyword("pi")

    bitstring = Combine(OneOrMore(oneOf("0 1")) + Literal("b"))

    integer = Combine(number + Optional(expo))
    real = Combine(
        Optional(sign) + (("." + number) ^ (number + "." + Optional(number))) +
        Optional(expo))
    validName = Forward()
    lineEnd = Literal(";")

    _is_ = Keyword("to").suppress()
    _in_ = Keyword("in")
    _to_ = Literal("->").suppress()

    commentSyntax = "//"
    commentOpenStr = "/*"
    commentCloseStr = "*/"
    commentOpenSyntax = Literal(commentOpenStr)
    commentCloseSyntax = Literal(commentCloseStr)

    dirSyntax = "***"
    dirOpenStr = f"{dirSyntax} begin"
    dirCloseStr = f"{dirSyntax} end"

    dirSyntax = Keyword(dirSyntax)
    dirOpenSyntax = CaselessLiteral(dirOpenStr)
    dirCloseSyntax = CaselessLiteral(dirCloseStr)

    intFunc = oneOf("abs powrem countof fllog")
    realFunc = oneOf("abs powrem arcsin arccos arctan sin cos tan exp ln sqrt")
    boolFunc = oneOf("andof orof xorof")

    inL, inS, inR = map(Suppress, "[:]")
    vBar = Suppress("|")
    bSlash = Suppress("\\")
    brL, brR = map(Suppress, "()")

    intExp = Forward()
    realExp = Forward()
    boolExp = Forward()

    index = intExp.setResultsName("index")
    interval = Optional(intExp.setResultsName("start"), default=None) + inS \
        + Optional(intExp.setResultsName("end"), default=None) \
        + Optional(inS + Optional(intExp.setResultsName("step"), default=1))
    interRef = Group(inL + interval + inR)
    loopRef = Group(
        inL + intExp.setResultsName("start") + inS +
        intExp.setResultsName("end") +
        Optional(inS + Optional(intExp.setResultsName("step"), default=1)) +
        inR)
    ref = inL + Group(delimitedList(index ^ interval))("ref") + inR
    regNoRef = validName("var")
    regRef = Group(validName("var") + Optional(ref))
    regMustRef = Group(validName("var") + ref)
    regListNoRef = Group(delimitedList(regNoRef))
    regListRef = Group(delimitedList(regRef))

    inPlaceAlias = vBar + regListRef + vBar
    validQarg = regRef | inPlaceAlias
    aliasQarg = Group(regRef) | inPlaceAlias

    inPlaceCreg = bSlash + delimitedList(regRef | bitstring) + bSlash
    validCreg = (regRef | inPlaceCreg)

    def set_maths_type(toks, mathsType):
        """ Set logical or integer or floating point """
        toks["type"] = mathsType

    intVar = integer | regRef
    realVar = real | integer | pi | regRef
    boolVar = interRef | regRef | realExp | intExp | validCreg | bitstring
    intFuncVar = (intFunc + brL +
                  Group(Optional(delimitedList(intVar)))("args") +
                  brR).setParseAction(Function)
    realFuncVar = ((realFunc ^ intFunc) + brL +
                   Group(Optional(delimitedList(realVar)))("args") +
                   brR).setParseAction(Function)
    boolFuncVar = (boolFunc + brL +
                   Group(Optional(delimitedList(boolVar)))("args") +
                   brR).setParseAction(Function)

    mathOp = [(oneOf("- +"), 1, opAssoc.RIGHT, Binary),
              (oneOf("^"), 2, opAssoc.LEFT, Binary),
              (oneOf("* / div"), 2, opAssoc.LEFT, Binary),
              (oneOf("+ -"), 2, opAssoc.LEFT, Binary)]
    logOp = [(oneOf("! not"), 1, opAssoc.RIGHT, Binary),
             (oneOf("and or xor"), 2, opAssoc.LEFT, Binary),
             (oneOf("< <= == != >= >"), 2, opAssoc.LEFT, Binary),
             (oneOf("in"), 2, opAssoc.LEFT, Binary)]

    intExp <<= infixNotation(
        intFuncVar | intVar,
        mathOp).setParseAction(lambda s, l, t: set_maths_type(t, "int"))

    realExp <<= infixNotation(
        realFuncVar | realVar,
        mathOp).setParseAction(lambda s, l, t: set_maths_type(t, "float"))

    boolExp <<= infixNotation(
        boolFuncVar | boolVar,
        logOp).setParseAction(lambda s, l, t: set_maths_type(t, "bool"))

    mathExp = intExp ^ realExp ^ boolExp
    cregExp = bitstring("bit") ^ validCreg("reg")

    prefixes = ["unitary"]
    callMods = ["CTRL", "INV"]

    def prefix_setter(toks):
        """ Pull out prefixes of gate calls and add them into list """
        for prefix in prefixes:
            toks[prefix] = prefix in toks.asList()

    prefixParser = Each(map(Optional,
                            map(Keyword,
                                prefixes))).addParseAction(prefix_setter)

    pargParser = brL + delimitedList(validName)("pargs") + brR
    spargParser = inL + delimitedList(validName)("spargs") + inR
    gargParser = ungroup(
        nestedExpr("<", ">", delimitedList(ungroup(validName)), None))
    qargParser = delimitedList(regRef)

    callQargParser = delimitedList(validQarg)
    callPargParser = brL + delimitedList(realExp) + brR
    callSpargParser = inL + delimitedList(intExp) + inR

    fullArgParser = Each(
        (Optional(pargParser("pargs")), Optional(spargParser("spargs")),
         Optional(gargParser("gargs"))))

    callArgParser = Each(
        (Optional(callPargParser("pargs")),
         Optional(callSpargParser("spargs")), Optional(gargParser("gargs"))))

    returnParser = Optional(_to_ + validCreg("byprod"))

    modifiers = ZeroOrMore(Combine(oneOf(callMods) + Suppress("-")))

    commentLine = Literal(commentSyntax).suppress() + restOfLine("comment")
    commentBlock = cStyleComment("comment").addParseAction(
        removeQuotes).addParseAction(removeQuotes)
    comment = commentLine | commentBlock
    comment.addParseAction(lambda s, l, t: _set_version(t, (0, 0, 0)))

    directiveName = Word(alphas).setParseAction(downcaseTokens)
    directiveArgs = CharsNotIn(";")

    _Op("directive",
        directiveName("directive") + Suppress(White() * (1, )) +
        directiveArgs("args"),
        version="REQASM 1.0",
        keyOverride=(~dirOpenSyntax + ~dirCloseSyntax + dirSyntax))

    def split_args(toks):
        """ Split directive arguments out """
        toks[0]["keyword"] = "directive"
        toks[0]["args"] = toks[0]["args"].strip().split(" ")

    directiveStatement = directiveName("directive") + restOfLine("args") + \
        Group(ZeroOrMore(Combine(Optional(White(" ")) + ~dirCloseSyntax + Word(printables+" "))))("block")

    directiveBlock = ungroup(
        nestedExpr(
            dirOpenSyntax,
            dirCloseSyntax,
            content=directiveStatement,
            ignoreExpr=(comment | quotedString
                        )).setWhitespaceChars("\n").setParseAction(split_args))
    directiveBlock.addParseAction(lambda s, l, t: _set_version(t, (2, 1, 0)))

    # Programming lines
    _Op("version",
        Empty(),
        version=(0, 0, 0),
        keyOverride=Combine(
            oneOf(versions)("type") + White() +
            real("versionNumber"))("version"))
    _Op("include", quotedString("file").addParseAction(removeQuotes))

    # Gate-like structures
    _Op("opaque",
        validName("name") + fullArgParser + Optional(qargParser("qargs")) +
        returnParser,
        keyOverride=prefixParser + "opaque")
    _Routine("gate", pargs=True, qargs=True)
    _Routine("circuit",
             pargs=True,
             qargs=True,
             spargs=True,
             returnables=True,
             version="REQASM 1.0")

    # Variable-like structures
    _Op("creg", regRef("arg"))
    _Op("qreg", regRef("arg"))
    _Op("cbit", Group(regNoRef)("arg"), version="REQASM 1.0")
    _Op("qbit", Group(regNoRef)("arg"), version="REQASM 1.0")
    _Op("defAlias",
        regMustRef("alias"),
        keyOverride="alias",
        version="REQASM 1.0")

    # No more on-definition aliases
    _Op("alias",
        regRef("alias") + _is_ + aliasQarg("target"),
        keyOverride="set",
        version="REQASM 1.0")
    _Op("val",
        validName("var") + Literal("=").suppress() + mathExp("val"),
        version="REQASM 1.0")

    _Op("set", (Group(regRef)("var") ^ inPlaceCreg("var")) +
        Literal("=").suppress() + cregExp("val"),
        version="REQASM 1.0")

    # Operations-like structures
    _Op("measure", regRef("qreg") + _to_ + regRef("creg"), qop=True)
    _Op("barrier", regListNoRef("args"))
    _Op("output", regRef("value"), qop=True, version="REQASM 1.0")
    _Op("reset", regRef("qreg"))
    _Op("exit", Empty(), version="REQASM 1.0")

    _Op("free", validName("target"), version="REQASM 1.0")
    _Op("next", validName("loopVar"), qop=True, version="REQASM 1.0")
    _Op("finish", (Literal("quantum process") | validName)("loopVar"),
        qop=True,
        version="REQASM 1.0")
    _Op("end", validName("process"), qop=True, version="REQASM 1.0")

    # Special gate call handler
    callGate = Combine(Group(modifiers)("mods") + \
                       validName("gate")) + \
                       callArgParser + \
                       callQargParser("qargs").addParseAction(lambda s, l, t: _override_keyword(t, "call")) + \
                       returnParser
    callGate.addParseAction(lambda s, l, t: _set_version(t, (1, 2, 0)))

    # Block structures
    _Block("for",
           validName("var") + _in_ + loopRef("range"),
           version="REQASM 1.0")
    _Block("if", "(" + boolExp("cond") + ")", version="REQASM 1.0")
    _Block("while", "(" + boolExp("cond") + ")", version="OMEQASM 1.0")

    qopsParsers = list(map(lambda qop: qop.parser,
                           qops.values())) + [callGate, directiveBlock]
    blocksParsers = list(map(lambda block: block.parser, blocks.values()))

    _Op("if",
        blocks["if"].parser + Group(Group(Group(Or(qopsParsers))))("block"),
        version="OPENQASM 2.0",
        keyOverride=Empty())
    _Op("for",
        blocks["for"].parser + Group(Group(Group(Or(qopsParsers))))("block"),
        version="REQASM 1.0",
        keyOverride=Empty())
    _Op("while",
        blocks["while"].parser + Group(Group(Group(Or(qopsParsers))))("block"),
        version="OMEQASM 1.0",
        keyOverride=Empty())

    # Set-up line parsers
    reservedNames = Or(map(Keyword, _reservedKeys))
    validName <<= (~reservedNames) + Word(alphas, alphanums + "_")

    copsParsers = list(map(lambda cop: cop.parser, cops.values()))

    operations = ((
        (Or(copsParsers) ^ Or(qopsParsers)) |  # Classical/Quantum Operations
        callGate |  # Gate parsers
        White()  # Blank Line
    ) + lineEnd.suppress()) ^ directiveBlock  # ; or Directives

    validLine = Forward()
    codeBlock = nestedExpr("{", "}",
                           Suppress(White()) ^ Group(validLine),
                           (quotedString))

    validLine <<= (
        ((operations + Optional(comment)) ^
         (Or(blocksParsers) + codeBlock("block") + Optional(lineEnd))
         ^ comment))  # Whole line comment

    testLine = Forward()
    dummyCodeBlock = nestedExpr(
        "{", "}", testLine,
        (directiveBlock | quotedString | comment)) + Optional(lineEnd)

    ignoreSpecialBlocks = (~commentOpenSyntax + ~commentCloseSyntax +
                           ~dirOpenSyntax + ~dirCloseSyntax)

    testLine <<= (
        comment |  # Comments
        directiveBlock |  # Directives
        (ignoreSpecialBlocks + ZeroOrMore(CharsNotIn("{}")) + dummyCodeBlock)
        |  # Block operations
        (ignoreSpecialBlocks + ZeroOrMore(CharsNotIn("{};")) + lineEnd)
    )  # QASM Instructions

    testKeyword = (dirSyntax.setParseAction(
        lambda s, l, t: _override_keyword(t, "directive"))
                   | Word(alphas)("keyword"))

    code = (Group(directiveBlock)) | Group(validLine)

    return code, testLine, testKeyword, reservedNames, mathExp
示例#11
0
        (oneOf("* /"), 2, opAssoc.LEFT, FOLBinOp),
        (oneOf("+ -"), 2, opAssoc.LEFT, FOLBinOp),
        (oneOf("< <= > >= "), 2, opAssoc.LEFT, FOLBinOp),
    ],
)


# main parser for FOL formula
formula = Forward()
formula.ignore(comment)

forall_expression = Group(
    forall.setResultsName("quantifier")
    + delimitedList(variable).setResultsName("vars")
    + colon
    + formula.setResultsName("args")
).setParseAction(FOLQuant)
exists_expression = Group(
    exists.setResultsName("quantifier")
    + delimitedList(variable).setResultsName("vars")
    + colon
    + formula.setResultsName("args")
).setParseAction(FOLQuant)

operand = forall_expression | exists_expression | boolean | term

# specify the precedence -- highest precedence first, lowest last
operator_list = [(not_, 1, opAssoc.RIGHT, FOLUnOp)]
#
operator_list += [
    (equals, 2, opAssoc.RIGHT, FOLBinOp),
示例#12
0
column_val = real_num | int_num | quotedString | column_idr

select_stmt = Forward()
where_expr = Forward()
where_cond = Group(
  ( column_idr.setResultsName('left_operand') + 
    binary_op.setResultsName('operator') + 
    column_val.setResultsName('right_operand') 
    ) |
  ( column_idr.setResultsName('left_operand') + 
    in_ + 
    Suppress("(") + delimitedList( column_val ).setResultsName('right_operand') + Suppress(")") 
    ).setResultsName('in_list_condition') |
  ( column_idr.setResultsName('left_operand') + 
    in_ + 
    Suppress("(") + select_stmt.setResultsName('right_operand') + Suppress(")")
    ).setResultsName('in_query_condition') |
  ( Suppress("(") + where_expr + Suppress(")") )
  )

group_by_expr = Group(column_idr + ZeroOrMore( "," + column_idr ))

where_expr << where_cond + ZeroOrMore( (and_ | or_) + where_expr )

on_ = Keyword('on', caseless=True)
join = ((oneOf('left right') + 'join' ) | (Optional('inner') + 'join')
        ).setResultsName('join_type')

from_clause = table_idr.setResultsName('relation') + ZeroOrMore(Group(
    join + table_idr.setResultsName('relation') + on_ + where_cond.setResultsName('join_conditions') 
  )).setResultsName('joins', listAllMatches=True)
示例#13
0
# Individual parts of the signature
SIGNATURE_POSITIONAL = (
    (delimitedList(PARAMETER) + Suppress(",") +
     Suppress("/")).setName("[NAME ':'] TYPE {',' [NAME ':'] TYPE} ',' '/'"
                            ).setResultsName("positional"))
SIGNATURE_MIXED = (delimitedList(PARAMETER).setName(
    "[NAME ':'] TYPE {',' [NAME ':'] TYPE}").setResultsName("mixed"))
SIGNATURE_ARGS = (Suppress("*") + (Optional(
    PARAMETER, default=None).setResultsName("args"))).setName("'*' [TYPE]")
SIGNATURE_KEYWORDS = (delimitedList(NAMED_PARAMETER).setName(
    "NAME ':' TYPE {',' NAME ':' TYPE}").setResultsName("keywords"))
SIGNATURE_KWARGS = (
    Suppress("**") +
    Group(PARAMETER).setResultsName("kwargs")).setName("'**' TYPE")
SIGNATURE_RETURN = Suppress("->") + TYPE.setResultsName("returns").setName(
    "-> TYPE")

# Match for the entire parameter list
# This does a recursive ``head + Optional(delimiter + tail) or tail``. This
# creates any combination of optional tails starting from any head.
# Based on the Python 3.8 Function Definitions grammar
PARAMETER_LIST_STARARGS = MatchFirst((
    SIGNATURE_ARGS + Optional(Suppress(",") + SIGNATURE_KEYWORDS) +
    Optional(Suppress(",") + SIGNATURE_KWARGS),
    SIGNATURE_KWARGS,
))
PARAMETER_LIST_NO_POSONLY = MatchFirst((
    SIGNATURE_MIXED + Optional(Suppress(",") + PARAMETER_LIST_STARARGS),
    PARAMETER_LIST_STARARGS,
))
PARAMETER_LIST = MatchFirst((
示例#14
0
trueOrFalse = trueToken | falseToken
facetOrderBy = hitsToken | valueToken
facetSpec = Group(columnName + ":" + "(" + trueOrFalse + "," + intNum  + "," + intNum + "," + facetOrderBy + ")")
browseByClause = (browseByToken +
                  "(" + delimitedList(facetSpec).setResultsName("facet_specs") + ")")

groupByClause = (groupByToken +
                 columnName.setResultsName("groupby") +
                 Optional(topToken + intNum.setResultsName("max_per_group")))

selectStmt << (selectToken + 
               ('*' | columnNameList).setResultsName("columns") + 
               fromToken + 
               ident.setResultsName("index") + 
               Optional((whereToken + whereExpression.setResultsName("where"))) +
               ZeroOrMore(orderByClause |
                          limitClause |
                          browseByClause |
                          groupByClause
                          ) +
               Optional(";")
               )

simpleSQL = selectStmt

# Define comment format, and ignore them
sqlComment = "--" + restOfLine
simpleSQL.ignore(sqlComment)

logger = logging.getLogger("sensei_client")
示例#15
0
    Optional(arith_sign) + Word(nums) +
    Optional(E + Optional('+') + Word(nums)))

column_val = real_num | int_num | quotedString | column_idr

select_stmt = Forward()
where_expr = Forward()
where_cond = Group(
    (column_idr.setResultsName('left_operand') +
     binary_op.setResultsName('operator') +
     column_val.setResultsName('right_operand'))
    | (column_idr.setResultsName('left_operand') + in_ + Suppress("(") +
       delimitedList(column_val).setResultsName('right_operand') +
       Suppress(")")).setResultsName('in_list_condition')
    | (column_idr.setResultsName('left_operand') + in_ + Suppress("(") +
       select_stmt.setResultsName('right_operand') +
       Suppress(")")).setResultsName('in_query_condition')
    | (Suppress("(") + where_expr + Suppress(")")))

group_by_expr = Group(column_idr + ZeroOrMore("," + column_idr))

where_expr << where_cond + ZeroOrMore((and_ | or_) + where_expr)

on_ = Keyword('on', caseless=True)
join = ((oneOf('left right') + 'join') |
        (Optional('inner') + 'join')).setResultsName('join_type')

from_clause = table_idr.setResultsName('relation') + ZeroOrMore(
    Group(join + table_idr.setResultsName('relation') + on_ +
          where_cond.setResultsName('join_conditions'))).setResultsName(
              'joins', listAllMatches=True)
示例#16
0
def parser(text):
    var_any = Literal("_")
    p = Regex("[\w:]+").setResultsName("text")
    var_any = Regex("_") #handled by p anyway
    attribute = Literal("@").suppress()
    eq = Literal("=").suppress()
    closure = (Literal("?") | Literal("*") | Literal("+")).setResultsName("closure")

    test = Literal("^").setResultsName("modifier") + p | p + Literal("$").setResultsName("modifier") | p #| var_any
    axis = (Literal("\\\\*") | \
            Literal("\\\\") | \
            Literal("\\") | \
            Literal(".") | \
            Literal("//*") | \
            Literal("//") | \
            Literal("/") | \
            Literal("-->") | \
            Literal("<--") | \
            Literal("->") | \
            Literal("<-") | \
            Literal("==>") | \
            Literal("<==") | \
            Literal("=>") | \
            Literal("<=")).setResultsName("connector")

    g_left_brack = Literal("[").suppress()
    g_right_brack = Literal("]").suppress()

    # working
    """
    abspath = Forward()
    locstep = Forward()
    
    node = test.setResultsName("node")
    attr_test = Group(attribute.suppress() + node.setResultsName("attr") + eq.suppress() + node.setResultsName("attr_val")).setResultsName("attr_test")
    predicate = (Group(Literal("[").suppress() + attr_test + Literal("]").suppress()).setResultsName("predicate") |\
                 Group(Literal("[").suppress() + abspath + Literal("]").suppress()).setResultsName("predicate"))
    locstep << Group(axis.setResultsName("axis") + node + \
              Optional(predicate + Optional(closure).setResultsName("closure"))).setResultsName("locstep")

    abs2 = abspath
    abspath << ( Group(locstep.setResultsName("left_step") + abs2).setResultsName("abspath") | \
                 locstep.setResultsName("right_step") )

    # TODO
    locpath = abspath
    fexpr = locpath.setResultsName("exp")
    """

    # clean
    locpath = Forward()
    steps = Forward()

    fexpr = locpath.setResultsName("exp")

    attr_test = Group(attribute + p.setResultsName("attr") + eq + p.setResultsName("attr_val"))
    pred_opt = (fexpr.setResultsName("predicate") | attr_test.setResultsName("attr_test"))

    # connector order handling is the same as EmuQL, but the root lacks a left, as it refers to context node
    nodetest = Group(test + Optional(g_left_brack + pred_opt + g_right_brack + Optional(closure)))
    steps << ( Group(nodetest("left") + axis + steps("right")) | \
               Group(test + Optional(g_left_brack + pred_opt + g_right_brack + Optional(closure))))

    locpath << Group(axis + steps.setResultsName("right"))
    
    return fexpr.parseString(text)
示例#17
0
)

where_clause = CKeyword('WHERE') + W + expression

group_by_clause = CKeyword('GROUP BY') + W + comma_list(expression) + \
	Optional(W + CKeyword('HAVING') + W + expression)
order_by_clause = CKeyword('ORDER BY') + W + comma_list(ordering_term)
limit_clause = CKeyword('LIMIT') + W + expression + \
	Optional(( (W + CKeyword('OFFSET') + W) | (OW + ',' + OW)) + expression)


# N.B. this doesn't account for compound operators (union, intersect...)
select << (\
	CKeyword('SELECT') + W + Optional((CKeyword('DISTINCT') | CKeyword('ALL')) + W) + \
	result_column_list.setResultsName("result_columns") + \
	Optional(W + CKeyword('FROM') + W + join_source.setResultsName("join_source")) + \
	Optional(W + where_clause).setResultsName("where") + \
	Optional(W + group_by_clause).setResultsName("group_by") + \
	Optional(W + order_by_clause).setResultsName("order_by") + \
	Optional(W + limit_clause).setResultsName("limit")
)
	

SQL_select = pyparsing.StringStart() + OW + select + Optional(';') + pyparsing.StringEnd()

if __name__ == '__main__':
	'''
	def dbgStart(s, loc, grammar):
		print 'Starting', s
	def dbgSuccess(s, start, end, grammar, match): #
		print 'Success', s
示例#18
0
orderByExpression = Group(
    delimitedList(columnDef +
                  Optional(CaselessLiteral("DESC") | CaselessLiteral("ASC"))))

# LIMIT
limitExpression = intNum

# OFFSET
offsetExpression = intNum

# define the grammar
selectColumnList = Group(delimitedList(Group(columnDef + aliasDef)))
selectStmt << (
    selectToken + ('*' | selectColumnList).setResultsName("columns") +
    fromToken + tableNameList.setResultsName("tables") +
    Optional(whereToken + whereExpression.setResultsName("where"), "") +
    Optional(groupByToken + groupByExpression.setResultsName("groupby"), "") +
    Optional(orderByToken + orderByExpression.setResultsName("orderby"), "") +
    Optional(limitToken + limitExpression.setResultsName("limit"), "") +
    Optional(offsetToken + offsetExpression.setResultsName("offset"), ""))

sql_parser = selectStmt  # + stringEnd

sqlComment = "--" + restOfLine  # ignore comments
sql_parser.ignore(sqlComment)

########NEW FILE########
__FILENAME__ = version

VERSION = "0.3"
示例#19
0
def parse(input_string):
    def flatten_binary_operators(position, source, flattened_tokens):
        while len(flattened_tokens) >= 3:
            lhs, type_call, rhs = flattened_tokens[:3]
            flattened_tokens = [type_call(position, source, lhs, rhs)] + flattened_tokens[3:]
        return flattened_tokens[0]

    def flatten_unary_operators(position, source, flattened_tokens):
        type_call = flattened_tokens[0]
        return type_call(position, source, flattened_tokens[1])

    # Packrat
    ParserElement.enablePackrat()

    lit_form = Suppress("form")
    lit_if = Suppress("if")
    lit_else = Suppress("else")

    lit_l_curly = Suppress("{")
    lit_r_curly = Suppress("}")
    lit_l_paren = Suppress("(")
    lit_r_paren = Suppress(")")

    lit_colon = Suppress(":")
    lit_assign_op = Suppress("=")

    lit_op_multiplication = Literal("*").setParseAction(lambda _: ast.Multiplication)
    lit_op_division = Literal("/").setParseAction(lambda _: ast.Division)
    lit_op_subtract = Literal("-").setParseAction(lambda _: ast.Subtraction)
    lit_op_addition = Literal("+").setParseAction(lambda _: ast.Addition)
    lit_op_positive = Literal("+").setParseAction(lambda _: ast.Positive)
    lit_op_negative = Literal("-").setParseAction(lambda _: ast.Negative)

    lit_op_not = Literal("!").setParseAction(lambda _: ast.Negation)
    lit_op_lower_exclusive = Literal("<").setParseAction(lambda _: ast.LowerExclusive)
    lit_op_lower_inclusive = Literal("<=").setParseAction(lambda _: ast.LowerInclusive)
    lit_op_greater_inclusive = Literal(">=").setParseAction(lambda _: ast.GreaterInclusive)
    lit_op_greater_exclusive = Literal(">").setParseAction(lambda _: ast.GreaterExclusive)
    lit_op_equality = Literal("==").setParseAction(lambda _: ast.Equality)
    lit_op_inequality = Literal("!=").setParseAction(lambda _: ast.Inequality)
    lit_op_and = Literal("&&").setParseAction(lambda _: ast.And)
    lit_op_or = Literal("||").setParseAction(lambda _: ast.Or)

    type_money = Literal("money").setParseAction(
        lambda source, position, _: ast.Money(position, source))
    type_integer = Literal("integer").setParseAction(
        lambda source, position, _: ast.Integer(position, source))
    type_boolean = Literal("boolean").setParseAction(
        lambda source, position, _: ast.Boolean(position, source))

    type_string = Literal("string").setParseAction(
        lambda source, position, _: ast.String(position, source))

    data_types = (type_money | type_integer | type_boolean | type_string)

    true = Literal("true").setParseAction(
        lambda source, position, _: ast.Boolean(position, source, True))
    false = Literal("false").setParseAction(
        lambda source, position, _: ast.Boolean(position, source, False))
    boolean = (true | false)

    integer = Word(nums).setParseAction(
        lambda source, position, parsed_tokens: ast.Integer(position, source, int(parsed_tokens[0])))
    money = Combine(Word(nums) + Literal(".") + Word(nums)).setParseAction(
        lambda source, position, parsed_tokens: ast.Money(position, source, float(parsed_tokens[0])))
    number = (money | integer)

    string = QuotedString("'", unquoteResults=True)\
        .setParseAction(
            lambda source, position, parsed_tokens:
                ast.String(position, source, str(parsed_tokens[0])))

    reserved_words = (lit_form | lit_if | lit_else | boolean | number | data_types)

    name = ~reserved_words + Word(alphas, alphanums + '_').setResultsName(
        'identifier').setParseAction(
        lambda source, position, parsed_tokens: ast.Identifier(position, source, parsed_tokens[0]))

    operand_arith = (number | boolean | name | string)

    operand_list_arith = [
        (lit_op_positive | lit_op_negative | lit_op_not,
         1, opAssoc.RIGHT,
         lambda source, position, flattened_tokens: flatten_unary_operators(position, source, *flattened_tokens)),
        (lit_op_multiplication | lit_op_division,
         2, opAssoc.LEFT,
         lambda source, position, flattened_tokens: flatten_binary_operators(position, source, *flattened_tokens)),
        (lit_op_addition | lit_op_subtract,
         2, opAssoc.LEFT,
         lambda source, position, flattened_tokens: flatten_binary_operators(position, source, *flattened_tokens)),
    ]

    operand_list_bool = [
        (lit_op_lower_inclusive | lit_op_greater_inclusive | lit_op_greater_exclusive | lit_op_lower_exclusive,
         2, opAssoc.LEFT,
         lambda source, position, flattened_tokens: flatten_binary_operators(position, source, *flattened_tokens)),
        (lit_op_equality | lit_op_inequality,
         2, opAssoc.LEFT,
         lambda source, position, flattened_tokens: flatten_binary_operators(position, source, *flattened_tokens)),
        (lit_op_and,
         2, opAssoc.LEFT,
         lambda source, position, flattened_tokens: flatten_binary_operators(position, source, *flattened_tokens)),
        (lit_op_or,
         2, opAssoc.LEFT,
         lambda source, position, flattened_tokens: flatten_binary_operators(position, source, *flattened_tokens)),
    ]

    literal_precedence = infixNotation(
        operand_arith,
        (operand_list_arith + operand_list_bool)
    )

    expression = \
        OneOrMore(
            literal_precedence |
            (lit_l_paren + literal_precedence + lit_r_paren)
        )

    field = Forward()
    field_assignment = Forward()

    field_statement = (
        QuotedString('"', unquoteResults=True).setResultsName("title") +
        name.setResultsName("identifier") + lit_colon + data_types.setResultsName("data_type")
    )

    field <<= field_statement
    field.setParseAction(lambda source, position, parsed_tokens: ast.Field(position, source, *parsed_tokens))

    field_assignment <<= field_statement + lit_assign_op + expression
    field_assignment.setParseAction(
        lambda source, position, parsed_tokens: ast.Assignment(position, source, *parsed_tokens))

    field_order = field_assignment | field

    conditional_if = Forward()
    conditional_if_else = Forward()
    statement = Forward()
    body = Forward()

    if_statement = lit_if + lit_l_paren + expression + lit_r_paren + body
    conditional_if <<= if_statement
    conditional_if.setParseAction(ast.If)

    conditional_if_else <<= (
        if_statement +
        Optional(lit_else + body).setResultsName('else_statement')
    )
    conditional_if_else.setParseAction(ast.IfElse)

    conditional = conditional_if_else | conditional_if

    statement <<= (field_order | conditional)

    body <<= lit_l_curly + OneOrMore(statement) + lit_r_curly
    body.addParseAction(lambda parsed_tokens: [parsed_tokens.asList()])
    body.setResultsName('statement_list')

    form = (lit_form + name + body)\
        .addParseAction(lambda parsed_tokens: ast.Form(*parsed_tokens))\
        .setResultsName('form')\
        .parseWithTabs()
    return form.parseString(input_string).form
示例#20
0
    def parse(self, argument):
        """Procedure to parse constructor information.
        
        Variables:
            argument: Data to be parsed.
        """
        # pyparsing syntax parser definition
        classQualifier = Forward()
        classQualifierRegex = Regex(r'[a-zA-Z_0-9]+::')
        classQualifier << ZeroOrMore(' ') + classQualifierRegex + \
                            ZeroOrMore(classQualifier)
        className = Regex(r'[a-zA-Z_0-9]+')
        classAll = (ZeroOrMore(classQualifier) + className)

        vectorWord = Forward()
        vectorWord << ((ZeroOrMore(' ') + 'std::vector<' + ZeroOrMore(' ') + \
                       vectorWord + ZeroOrMore(' ') + '>') |  classAll  )

        anyString =  ( Regex(r'(.*)') ) .\
                        setResultsName('parameters_text')

        function = vectorWord.setResultsName('return_type') + \
                        ZeroOrMore(' ') + Combine(('operator()' | classAll)).\
                        setResultsName('function_name') + anyString

        #parse argument
        parse = None
        try:
            parse = function.parseString(argument, True)
        except ParseException as err:
            Logger().debug(err.line)
            Logger().debug(str(err))
            Logger().error('memberfunction parsing error "%s"' % argument)
            sys.exit(5)

        try:
            Logger().debug('entering conversion ...')
            print(parse)

            #set return_type and function_name
            self.return_type = ''
            for item in parse:
                if item != parse['function_name']:
                    self.return_type = self.return_type + item
                else:
                    break
            Logger().debug('memberfunction return_type %s' % self.return_type)

            text = self.return_type + ' ReturnType'
            self.return_type_parameter = Parameter(text)

            self.function_name = parse['function_name']
            Logger().debug('memberfunction function_name %s' %
                           self.function_name)

            Logger().debug('memberfunction parameter_list:')
            parameters_text = parse['parameters_text'][1:-1]

            #analyse parameter list - first step: include object parameter
            first_parameter = 'std::string ObjectId '
            if parameters_text:
                parameters_text = first_parameter + ', ' + parameters_text
            else:
                parameters_text = first_parameter

            # parse argument list
            if parameters_text:
                liste = parameters_text.split(',')
                Logger().debug(liste)
                pos = 0
                while pos < len(liste):
                    text = liste[pos].strip()
                    count = self.get_brackets(text)
                    while count != 0 and pos < len(liste):
                        pos += 1
                        if pos < len(liste):
                            text = text + ',' + liste[pos]
                            count = self.get_brackets(text)
                    Logger().debug('processing ' + text + ' ' + str(count))
                    if text and count == 0:
                        parameter = Parameter(text)
                        self.parameter_list.append(parameter)
                    else:
                        Logger().error(
                            'memberfunction error parameter "%s" in \
                            "%s" at position %d.' % (text, liste, pos))
                        sys.exit(5)
                    pos += 1

            Logger().debug('leaving conversion ...')
        except:
            Logger().error('memberfunction conversion error "%s"' % argument)
            sys.exit(5)