Exemplo n.º 1
0
def handMakeScope():
    scope = ScopeLevelBean()
    
    scope.append(FunDefBean([VarBean('str'), VarBean("$rept")], VarBean('None'), "print", someKwargs={"end": VarBean("str"), "sep": VarBean("str"), "file": VarBean("file"), "flush" : VarBean("bool")}))
    rangeType = VarBean('range')
    rangeType.h**o = True
    rangeType.compType = [VarBean("int")]
    
    scope.append(FunDefBean([VarBean('int')], rangeType, "range"))
    scope.append(FunDefBean([VarBean('str'), VarBean('str')], VarBean("file"), "open"))
    scope.append(FunDefBean([VarBean('str')], VarBean('None'), "write"))
    
    scope.append(VarBean("Exception", "ValueError"))
    
    scope.append(FunDefBean([], VarBean("byte"), "byte"))
    scope.append(FunDefBean([VarBean('list')], VarBean('set'), 'set', minParams = 0))
        
    return scope
Exemplo n.º 2
0
def handMakeScope():
    scope = ScopeLevelBean()

    scope.append(
        FunDefBean(
            [VarBean('str'), VarBean("$rept")],
            VarBean('None'),
            "print",
            someKwargs={
                "end": VarBean("str"),
                "sep": VarBean("str"),
                "file": VarBean("file"),
                "flush": VarBean("bool")
            }))
    rangeType = VarBean('range')
    rangeType.h**o = True
    rangeType.compType = [VarBean("int")]

    scope.append(FunDefBean([VarBean('int')], rangeType, "range"))
    scope.append(
        FunDefBean([VarBean('str'), VarBean('str')], VarBean("file"), "open"))
    scope.append(FunDefBean([VarBean('str')], VarBean('None'), "write"))

    scope.append(VarBean("Exception", "ValueError"))

    scope.append(FunDefBean([], VarBean("byte"), "byte"))
    scope.append(
        FunDefBean([VarBean('list')], VarBean('set'), 'set', minParams=0))

    return scope
Exemplo n.º 3
0
def handMakeNameSpace():
    nameSpace = NameSpaceBean()
    intClass = ClassDefBean('int', None, ScopeLevelBean(), rent="num")
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__add__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__div__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__mod__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__or__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__and__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__ne__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__pow__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__floordiv__'))
    intClass.dataMembers.append(FunDefBean([], VarBean('int'), '__bool__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('bool'), '__eq__'))
    intClass.dataMembers.append(FunDefBean([], VarBean('float'), '__float__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('bool'), '__ge__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('bool'), '__gt__'))
    intClass.dataMembers.append(FunDefBean([], VarBean('int'), '__invert__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('bool'), '__le__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__lshift__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('bool'), '__lt__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__mul__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__rshift__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__xor__'))
    intClass.dataMembers.append(FunDefBean([], VarBean('str'), '__str__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__sub__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__pos__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__neg__'))
    intClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('int'), '__index__'))
    nameSpace.put(intClass.name, intClass)

    strClass = ClassDefBean('str', None, ScopeLevelBean())
    strClass.dataMembers.append(
        FunDefBean([VarBean('str')], VarBean('str'), '__add__', minParams=1))
    strClass.dataMembers.append(
        FunDefBean([VarBean('str')], VarBean('bool'), '__contains__'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('str')], VarBean('bool'), '__eq__'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('str')], VarBean('bool'), '__ge__'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('str')], VarBean('bool'), '__gt__'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('str')], VarBean('bool'), '__le__'))
    strClass.dataMembers.append(FunDefBean([], VarBean('int'), '__len__'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('str'), '__mul__'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('str')], VarBean('bool'), '__ne__'))
    strClass.dataMembers.append(FunDefBean([], VarBean('str'), 'casefold'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('int')], VarBean('str'), 'center'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('str'), VarBean('str')], VarBean('bool'),
                   'encode'))
    strClass.dataMembers.append(
        FunDefBean(
            [VarBean('str'), VarBean('int'),
             VarBean('int')], VarBean('int'), 'find'))
    strClass.dataMembers.append(
        FunDefBean(
            [VarBean('str'), VarBean('int'),
             VarBean('int')], VarBean('int'), 'index'))
    strClass.dataMembers.append(FunDefBean([], VarBean('bool'), 'isdecimal'))
    strClass.dataMembers.append(FunDefBean([], VarBean('bool'), 'idigit'))
    strClass.dataMembers.append(FunDefBean([], VarBean('bool'), 'islower'))
    strClass.dataMembers.append(FunDefBean([], VarBean('bool'), 'isnumeric'))
    strClass.dataMembers.append(FunDefBean([], VarBean('bool'), 'isprintable'))
    strClass.dataMembers.append(FunDefBean([], VarBean('bool'), 'isspace'))
    strClass.dataMembers.append(FunDefBean([], VarBean('bool'), 'isupper'))
    strClass.dataMembers.append(FunDefBean([], VarBean('str'), 'join'))
    strClass.dataMembers.append(FunDefBean([], VarBean('str'), 'lower'))
    strClass.dataMembers.append(
        FunDefBean(
            [VarBean('str'), VarBean('str'),
             VarBean('int')], VarBean('str'), 'replace'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('str'), VarBean('int')], VarBean('list'), 'split'))
    strClass.dataMembers.append(
        FunDefBean([VarBean('boolean')], VarBean('list'), 'splitlines'))
    strClass.dataMembers.append(FunDefBean([], VarBean('str'), 'upper'))
    nameSpace.put(strClass.name, strClass)

    numClass = ClassDefBean('num', None, ScopeLevelBean())
    nameSpace.put(numClass.name, numClass)

    boolClass = ClassDefBean('bool', None, ScopeLevelBean())
    boolClass.dataMembers.append(
        FunDefBean([VarBean('bool')], VarBean('bool'), '__eq__'))
    boolClass.dataMembers.append(
        FunDefBean([VarBean('bool')], VarBean('bool'), '__bool__'))
    nameSpace.put(boolClass.name, boolClass)

    listClass = ClassDefBean('list', None, ScopeLevelBean())
    listClass.dataMembers.append(
        FunDefBean([VarBean('object')], VarBean('bool'), '__contains__'))
    listClass.dataMembers.append(
        FunDefBean([VarBean('self')], VarBean('object'), '__iter__'))
    listClass.dataMembers.append(
        FunDefBean([VarBean('object')], VarBean("None"), 'append'))
    nameSpace.put(listClass.name, listClass)

    setClass = ClassDefBean('set', None, ScopeLevelBean())
    setClass.dataMembers.append(
        FunDefBean([VarBean('object')], VarBean('bool'), '__contains__'))
    setClass.dataMembers.append(
        FunDefBean([VarBean('self')], VarBean('object'), '__iter__'))
    setClass.dataMembers.append(
        FunDefBean([VarBean('range')], VarBean('None'), 'update'))
    nameSpace.put(setClass.name, setClass)

    tupleClass = ClassDefBean('tuple', None, ScopeLevelBean())
    tupleClass.dataMembers.append(
        FunDefBean([VarBean('object')], VarBean('bool'), '__contains__'))
    tupleClass.dataMembers.append(
        FunDefBean([VarBean('self')], VarBean('object'), '__iter__'))
    nameSpace.put(tupleClass.name, tupleClass)

    dictClass = ClassDefBean('dict', None, ScopeLevelBean())
    dictClass.dataMembers.append(
        FunDefBean([VarBean('object')], VarBean('bool'), '__contains__'))
    nameSpace.put(dictClass.name, dictClass)

    bytesClass = ClassDefBean('bytes', None, ScopeLevelBean())
    nameSpace.put(bytesClass.name, bytesClass)

    exceptionClass = ClassDefBean("Exception", None, ScopeLevelBean())
    nameSpace.put(exceptionClass.name, exceptionClass)

    floatClass = ClassDefBean('float', None, ScopeLevelBean(), rent="num")
    floatClass.dataMembers.append(
        FunDefBean([VarBean('float')], VarBean('float'), '__add__'))
    floatClass.dataMembers.append(
        FunDefBean([VarBean('float')], VarBean('float'), '__sub__'))
    nameSpace.put(floatClass.name, floatClass)

    generatorClass = ClassDefBean("generator", None, ScopeLevelBean())
    generatorClass.dataMembers.append(
        FunDefBean([], VarBean('object'), '__iter__'))
    nameSpace.put(generatorClass.name, generatorClass)

    fileClass = ClassDefBean("file", None, ScopeLevelBean())
    fileClass.dataMembers.append(FunDefBean([], VarBean("list"), "read"))
    fileClass.dataMembers.append(
        FunDefBean([VarBean('str')], VarBean('None'), 'write'))
    nameSpace.put(fileClass.name, fileClass)

    rangeClass = ClassDefBean("range", None, ScopeLevelBean())
    rangeClass.dataMembers.append(FunDefBean([], VarBean('int'), "__iter__"))
    nameSpace.put(rangeClass.name, rangeClass)

    return nameSpace
Exemplo n.º 4
0
def parseDocString(docString,returnList = True):
    """Takes in a doc string in the form of a string and returns a list of VarBeans
    or a ScopeLevelBean. Defaults to returning a list of VarBeans
    @docString:string
    @returnList:bool
    """
    lineList = docString.splitlines()
    for i in lineList:
        if not re.match('@.+', i, flags=0):
            lineList.remove(i)
    finalList = []
    matchPattern = '\*\*?.*|.*\*\*?|\^.*|.*\^'
    for i in lineList:
        current = i[1:] #Remove the @
        current = current.replace(" ","") #Remove all spaces for easier parsing
        current = current.split(':',maxsplit=1)
        variables = current[0].split(',')
        currentType = current[1]
        compType = None
        if re.search(matchPattern,currentType):
            if len(currentType) <= 2:
                isHomo = False
                if currentType == '*':
                    raise Exceptions.HeteroCollecionException(variables,-1)
                elif currentType == '^':
                    raise Exceptions.HeteroCollecionException(variables,-1)
                elif currentType == '**':
                    raise Exceptions.HeteroCollecionException(variables,-1)
            else:
                isHomo = True
                #We'll handle Tuples first.
                if currentType[0] == '^':
                    compType = currentType[1:]
                    mainVarType = 'tuple'
                elif currentType[-1] == '^':
                    compType = currentType[:-1]
                    mainVarType = 'tuple'
                #Tuple handled.
                elif currentType[-2] == '*':
                    try:
                        compType, valueType = currentType[:-2].split(':')
                    except:
                        compType = currentType[:-2]
                        valueType = compType
                    #List is [key,value]
                    mainVarType = 'dict'
                elif currentType[1] == '*':
                    try:
                        compType, valueType = currentType[2:].split(':')
                    except:
                        compType = currentType[2:]
                        valueType = compType
                    mainVarType = 'dict'
                #Dicts handled
                elif currentType[0] == '*':
                    compType = currentType[1:]
                    mainVarType = 'list'
                elif currentType[-1] == '*':
                    compType = currentType[:-1]
                    mainVarType = 'list'
                #lists handled
        else:
            isHomo = False
            mainVarType = currentType

        for key in variables:
            currentVar = VarBean(mainVarType, key)
            if isHomo == False:
                currentVar.h**o = False
            else:
                if len(compType) < 2:
                    currentVar.h**o = True
                else:
                    currentVar.h**o = True
                    #Handle dict here, not sure how to set keys or values
                    currentVar.compType = [VarBean(compType)]
                    if mainVarType == "dict":
                        currentVar.valType = [VarBean(valueType)]
            finalList.append(currentVar)
    
    if returnList == False:
        return ScopeLevelBean(finalList)
    else:
        return finalList
Exemplo n.º 5
0
def parseDocString(docString, returnList=True):
    """Takes in a doc string in the form of a string and returns a list of VarBeans
    or a ScopeLevelBean. Defaults to returning a list of VarBeans
    @docString:string
    @returnList:bool
    """
    lineList = docString.splitlines()
    for i in lineList:
        if not re.match("@.+", i, flags=0):
            lineList.remove(i)
    finalList = []
    matchPattern = "\*\*?.*|.*\*\*?|\^.*|.*\^"
    for i in lineList:
        current = i[1:]  # Remove the @
        current = current.replace(" ", "")  # Remove all spaces for easier parsing
        current = current.split(":", maxsplit=1)
        variables = current[0].split(",")
        currentType = current[1]
        compType = None
        if re.search(matchPattern, currentType):
            if len(currentType) <= 2:
                isHomo = False
                if currentType == "*":
                    raise Exceptions.HeteroCollecionException(variables, -1)
                elif currentType == "^":
                    raise Exceptions.HeteroCollecionException(variables, -1)
                elif currentType == "**":
                    raise Exceptions.HeteroCollecionException(variables, -1)
            else:
                isHomo = True
                # We'll handle Tuples first.
                if currentType[0] == "^":
                    compType = currentType[1:]
                    mainVarType = "tuple"
                elif currentType[-1] == "^":
                    compType = currentType[:-1]
                    mainVarType = "tuple"
                # Tuple handled.
                elif currentType[-2] == "*":
                    try:
                        compType, valueType = currentType[:-2].split(":")
                    except:
                        compType = currentType[:-2]
                        valueType = compType
                    # List is [key,value]
                    mainVarType = "dict"
                elif currentType[1] == "*":
                    try:
                        compType, valueType = currentType[2:].split(":")
                    except:
                        compType = currentType[2:]
                        valueType = compType
                    mainVarType = "dict"
                # Dicts handled
                elif currentType[0] == "*":
                    compType = currentType[1:]
                    mainVarType = "list"
                elif currentType[-1] == "*":
                    compType = currentType[:-1]
                    mainVarType = "list"
                # lists handled
        else:
            isHomo = False
            mainVarType = currentType

        for key in variables:
            currentVar = VarBean(mainVarType, key)
            if isHomo == False:
                currentVar.h**o = False
            else:
                if len(compType) < 2:
                    currentVar.h**o = True
                else:
                    currentVar.h**o = True
                    # Handle dict here, not sure how to set keys or values
                    currentVar.compType = [VarBean(compType)]
                    if mainVarType == "dict":
                        currentVar.valType = [VarBean(valueType)]
            finalList.append(currentVar)

    if returnList == False:
        return ScopeLevelBean(finalList)
    else:
        return finalList