示例#1
0
def nonlocalDimensionAccessForComponents(components, codeBlock):
    """
    Find all places in the `codeBlock` where any of `components` are accessed with
    non-locally (usually integer-valued dimensions) and return a ``(componentName, resultDict, codeSlice)``
    tuple for each such occurrence. The companion of `nonlocalDimensionAccessForVectors` and
    to be used when `components` are components of vectors.
    """
    
    # Optimise for the common case: if the code doesn't contain the string "=>", then we know it doesn't have any nonlocal access
    if "=>" not in codeBlock.codeString:
        return []
    
    dictionaryElement = identifier + Suppress('=>') + sliceFor(Group(baseExpr))
    nonlocalAccessDictParser = Dict(
        ZeroOrMore(Group(dictionaryElement + Suppress(','))) + Group(dictionaryElement)
    )
    parser = identifier.setResultsName('name') \
                + nestedExpr('(', ')', nonlocalAccessDictParser, ignoreExpr).setResultsName('access')
    parser.ignore(cppStyleComment.copy())
    parser.ignore(quotedString.copy())
    results = []
    
    for tokens, start, end in parser.scanString(codeBlock.codeString):
        if tokens.name not in components: continue
        accessDict = {}
        tokenDict = tokens.access[0].asDict()
        for key, value in tokenDict.items():
            accessDict[key] = (' '.join(value[0].asList()), value.slice.start)
        results.append((tokens.name, accessDict, slice(start, end)))
    return results
示例#2
0
def targetComponentsForOperatorsInString(operatorNames, codeBlock):
    """
    Return a list of pairs of operator names and their targets that are in `codeString`.
    The valid operator names searched for are `operatorNames`. For example, if 'L' is in `operatorNames`,
    then in the code ``L[phi]`` the return value would be ``('L', 'phi', slice(firstCharacterIndex, lastCharacterIndex))``.
    """
    parser = MatchFirst(Keyword(operatorName) for operatorName in operatorNames).setResultsName('name') \
                + Optional(nestedExpr('[', ']', baseExpr, ignoreExpr).setResultsName('target'))
    parser.ignore(cppStyleComment.copy())
    parser.ignore(quotedString.copy())
    results = []
    for tokens, start, end in parser.scanString(codeBlock.codeString):
        if 'target' in tokens:
            results.append((tokens.name, ''.join(tokens.target.asList()[0]), slice(start, end)))
        else:
            raise CodeParserException(codeBlock, start, "Invalid use of '%s' operator in code block." % tokens.name)
    return results
示例#3
0
def checkForIntegerDivision(codeBlock):
    """
    Raise a CodeParserException if the code contains what looks like an integer division.
    i.e. ``9/2`` or the like. This is because the user is likely to get unexpected results.
    The most notorious example of this is ``1/2`` which evaluates to zero.
    """
    parser = numericConstant.setResultsName('numerator') + '/' + numericConstant.setResultsName('denominator')
    parser.ignore(cppStyleComment.copy())
    parser.ignore(quotedString.copy())
    for tokens, start, end in parser.scanString(codeBlock.codeString):
        if tokens.numerator.isdigit() and tokens.denominator.isdigit():
            raise CodeParserException(
                codeBlock, start,
                "It looks like you are trying to divide two integers.\n"
                "One of the oddities of the C language is that the result of such an expression\n"
                "is the floor of that division instead of the real value.\n"
                "For example '1/2' would give '0' instead of '0.5'.\n"
                "The way to fix this is to turn one or both of the integers into real numbers\n"
                "by adding a decimal point. For example, '1/2' should be written as '1.0/2.0'.\n\n"
                "If you feel this warning is given in error, send an email to [email protected]"
            )
示例#4
0
def performIPOperatorSanityCheck(componentName, propagationDimension, operatorCodeSlice, codeBlock):
    """
    Check that the user hasn't tried to use an IP operator where an IP operator cannot be used.
    
    IP operators must be diagonal, so one cannot have expressions of the form ``dy_dt = L[x];`` for IP operators.
    This is valid for EX operators, but not for IP. This is a common mistake for users to make, and so we should
    do our best to spot it and report the error. Another mistake users make is trying to multiply the operator,
    for example ``dy_dt = i*L[y];``. This code does a sophisticated validation by constructing a parse tree for
    each statement in the code taking into account operator precedence. This sanity checking is even able to pick
    up problems such as ``dphi_dt = i*(V*phi + U*mod2(phi)*phi + T[phi]);``.
    If the user's code passes this test, then it is a reasonable assumption that they are using IP operators safely.
    """
    
    operatorString = codeBlock.codeString[operatorCodeSlice]
    
    expr = Forward()
    
    operatorKeyword = Keyword(operatorString).setResultsName('targetOperator')
    
    operand = operatorKeyword \
                | (identifier + Group('(' + delimitedList(expr) + ')')) \
                | (identifier + Group(OneOrMore('[' + expr + ']'))) \
                | quotedString.copy() \
                | identifier \
                | numericConstant
    operand.ignore(cppStyleComment.copy())
    
    expr << operatorPrecedence(
        operand,
        [
            (oneOf('++ --'), 1, opAssoc.LEFT),
            (oneOf('. ->'), 2, opAssoc.LEFT),
            (~oneOf('-> -= += *= &= |=') + oneOf('+ - ! ~ * & ++ --'), 1, opAssoc.RIGHT),
            (~oneOf('*= /= %=') + oneOf('* / %'), 2, opAssoc.LEFT),
            (~oneOf('++ -- -> -= +=') + oneOf('+ -'), 2, opAssoc.LEFT),
# Although the operators below don't all have the same precedence, as we don't actually
# care about them as they are all invalid uses of the IP operator, we can cheat and lump
# them together
            (~oneOf('<<= >>= &= |=') + oneOf('<< >> < <= > >= == != & ^ | && ||'), 2, opAssoc.LEFT),
# Correct ordering
            # (~oneOf('<<= >>=') + oneOf('<< >>'), 2, opAssoc.LEFT),
            # (~oneOf('<< >> <<= >>=') + oneOf('< <= > >='), 2, opAssoc.LEFT),
            # (oneOf('== !='), 2, opAssoc.LEFT),
            # (~oneOf('&& &=') + '&', 2, opAssoc.LEFT),
            # ('^', 2, opAssoc.LEFT),
            # (~oneOf('|| |=') + '|', 2, opAssoc.LEFT),
            # ('&&', 2, opAssoc.LEFT),
            # ('||', 2, opAssoc.LEFT),
            (('?',':'), 3, opAssoc.RIGHT),
            (~Literal('==') + oneOf('= += -= *= /= %= <<= >>= &= ^= |= =>'), 2, opAssoc.RIGHT),
            (',', 2, opAssoc.LEFT),
        ]
    )
    expr.ignore(cppStyleComment.copy())
    
    statement = expr + Suppress(';')
    
    stack = []
    expectedAssignmentVariable = 'd%(componentName)s_d%(propagationDimension)s' % locals()
    
    def validateStack():
        """
        It is the job of this function to validate the operations that the located operator is involved in.
        The stack describes the part of the parse tree in which the operator was found. The first element in the stack
        is the outermost operation, and the last the innermost. The last element is guaranteed to be the operator itself.
        """
        # Reverse the stack as we want to search the parse tree from inner-most expression to outer-most.
        stack.reverse()
        assignmentHit = False
        errorMessageCommon = "Due to the way IP operators work, they can only contribute to the derivative of the variable " \
            "they act on, i.e. dx_dt = L[x]; not dy_dt = L[x];\n\n"
        
        # We don't need to check the first element of the stack
        # as we are guaranteed that it is the operator itself. This will be useful for determining
        # which part of the parse tree we should be looking at.
        for idx, node in enumerate(stack[1:]):
            if len(node) == 1: continue
            # idx is the index in the stack of the next element *deeper* in the parse tree.
            previousStackEntry = stack[idx]
            if not isinstance(stack[idx], basestring):
                previousStackEntry = previousStackEntry.asList()
            binaryOpIdx = node.asList().index(previousStackEntry) - 1
            if binaryOpIdx < 0: binaryOpIdx = 1
            # Unary '+' is safe.
            if node[0] == '+': continue
            # Binary '+' is safe.
            if node[binaryOpIdx] == '+': continue
            # Binary '-' is safe if the operator is the first argument.
            if node[binaryOpIdx] == '-' and node.asList().index(previousStackEntry) == 0: continue
            # Assignment is safe if it there is only one, and if it's to the right variable
            if node[binaryOpIdx] in ['=', '+=']:
                if node[0] == expectedAssignmentVariable:
                    assignmentHit = True
                    continue
                else:
                    return errorMessageCommon + "In this case, you should probably use an EX operator instead of an "\
                            "IP operator."
            else:
                return errorMessageCommon + "You appear to be using the IP operator in an unsafe operation. " \
                        "The most likely cause is trying to multiply it by something, e.g. dphi_dt = 0.5*L[phi]; "\
                        "If this is the cause and you are multiplying by a constant, just move the constant into the "\
                        "definition of the operator itself. i.e. L = -0.5*kx*kx; If you are multiplying by something "\
                        "that isn't constant e.g. dphi_dt = x*L[phi]; where x is a dimension, you must use an EX operator "\
                        "instead."
        if not assignmentHit:
            return errorMessageCommon + "You appear to be missing the assignment for this particular operator."
        return True
    
    class FoundTargetException(Exception): pass
    
    def findOperatorInParseTree(results):
        stack.append(results)
        if 'targetOperator' in results:
            stack.append(results.targetOperator)
            raise FoundTargetException()
        for item in results:
            if isinstance(item, basestring): continue
            findOperatorInParseTree(item)
        del stack[-1]
    
    try:
        foundOperator = False
        for tokens, start, end in statement.scanString(codeBlock.codeString):
            if start > operatorCodeSlice.stop or end < operatorCodeSlice.start: continue
            try:
                findOperatorInParseTree(tokens)
            except FoundTargetException:
                foundOperator = True
                result = validateStack()
                if result is not True:
                    raise CodeParserException(
                        codeBlock,
                        operatorCodeSlice.start,
                        result + ("\n\nThe conflict was caused by the operator '%s'." \
                        % operatorString)
                    )
        if not foundOperator:
            parserWarning(
                codeBlock.xmlElement,
                "Unable to check the safety of your IP operator '%s' because the containing expression could not be found. "
                "Please send a copy of your script to [email protected] so this problem can be investigated." \
                % operatorString
            )
    except RuntimeError:
        parserWarning(
            codeBlock.xmlElement,
            "Unable to check the safety of your IP operator because your code is too deeply nested."
        )
示例#5
0
    This class determines the line in the original script that
    corresponds to the part of the code block that triggered the
    exception.
    """
    def __init__(self, codeBlock, codeIndex, msg):
        ParserException.__init__(self, codeBlock.xmlElement, msg)
        
        self.columnNumber = col(codeIndex, codeBlock.codeString)
        self.lineNumber = codeBlock.scriptLineNumber + lineno(codeIndex, codeBlock.codeString)-1
    


identifier = Word(alphas + '_', alphanums + '_')
numericConstant = Regex(r'\b((0(x|X)[0-9a-fA-F]*)|(([0-9]+\.?[0-9]*)|(\.[0-9]+))((e|E)(\+|-)?[0-9]+)?)(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\b')

ignoreExpr = cppStyleComment.copy() | quotedString.copy()

baseExpr = Forward()

arrayAccess = originalTextFor(nestedExpr('[', ']', baseExpr, ignoreExpr))
parenthisedExpression = originalTextFor(nestedExpr('(', ')', baseExpr, ignoreExpr))
functionCall = nestedExpr('(', ')', delimitedList(baseExpr), ignoreExpr)
alphaNumPlusSafePunctuation = alphanums + '!#$%&\\*+-./:;<=>@^_`{|}~'

baseExpr << OneOrMore(originalTextFor(identifier + functionCall) | quotedString.copy() \
                | identifier | numericConstant | arrayAccess | parenthisedExpression \
                | Word(alphaNumPlusSafePunctuation))
baseExpr.ignore(cppStyleComment.copy())


def targetComponentsForOperatorsInString(operatorNames, codeBlock):