def gen_assign_exp_token(assign_tree, call_from_main): """ generate correct and incorrect string tokens from the AssignmentExpression tree args: assign_tree : the test tree of AssignmentExpression type call_from_main : to check function is called my main or as a part of sub expression return: list of correct and incorrect string tokens """ # get assignment operator op = assign_tree.operator x_pos = [] x_neg = [] # get the left and right tree left, _ = TypeChecker.check_type_get_token(assign_tree.left) right, _ = TypeChecker.check_type_get_token(assign_tree.right) # form the correct example x_pos = x_pos + left x_pos = x_pos + [op] x_pos = x_pos + right return x_pos, x_neg
def gen_binary_exp_token(assign_tree, call_from_main): """ generate correct and incorrect string tokens from the binary expression tree args: assign_tree : the test tree of BinaryExpression type call_from_main : to check function is called my main or as a part of sub expression return: list of correct and incorrect string tokens """ # Keys : all binary operators, values : coressponding wrong operator wrong_op = { ">": "<", "<": "> ", ">=": "<=", "<=": ">=", "!=": "==", "==": "!=", "===": "!==", "!==": "===", "&": "&&", "|": "||", "+": "-", "-": "+", "/": "*", "*": "/", "%": "/", "<<": ">>", ">>": "<<", ">>>": "<<<" } # binary operator op = assign_tree.operator x_pos = [] x_neg = [] # get the tokens of the left and right tree of binary expression left, _ = TypeChecker.check_type_get_token(assign_tree.left) right, _ = TypeChecker.check_type_get_token(assign_tree.right) # form list of tokens x_pos = x_pos + left x_pos = x_pos + [op] x_pos = x_pos + right # Type 5: Wrong operator # replace binary operator with corresponding wrong operator to generate incorrect examples if op in wrong_op.keys(): x_neg = x_neg + left x_neg = x_neg + [wrong_op[op]] x_neg = x_neg + right return x_pos, x_neg
def gen_unary_exp_token(unary_tree, call_from_main): """ generate correct and incorrect string tokens from the unary expression tree args: unary_tree : the test tree of UnaryExpression type call_from_main : to check function is called my main or as a part of sub expression return: list of correct and incorrect string tokens """ # get the unary operator op = unary_tree.operator x_pos = [] x_neg = [] #unary_operator = ["!", "~", "-"] unary_operator = ["!"] # get the argument of the unary tree arg, _ = TypeChecker.check_type_get_token(unary_tree.argument) # form the correct token x_pos = x_pos + [op] x_pos = x_pos + arg # Type 4: negated condition # add only the argument, remove the ! unary operator if op in unary_operator: x_neg += arg return x_pos, x_neg
def gen_logical_exp_token(logical_tree, call_from_main): """ generate correct and incorrect string tokens from the logical expression tree args: logical_tree : the test tree of LogicalExpression type call_from_main : to check function is called my main or as a part of sub expression """ # fetch the logical operator from the AST tree op = logical_tree.operator x_pos = [] x_neg = [] x_neg_list = [] # obtain the left and right tree from the corresponding trees left, _ = TypeChecker.check_type_get_token(logical_tree.left) right, _ = TypeChecker.check_type_get_token(logical_tree.right) x_pos = x_pos + left x_pos = x_pos + [op] x_pos = x_pos + right cond1_type = ["Identifier", "Literal"] cond2_type = ["MemberExpression", "CallExpression"] # incorrect token formation if op == "&&": # Type 1 : Incomplete Conditional Expression # store only the right tree if (logical_tree.left.type == "Identifier") and (logical_tree.right.type not in cond1_type): x_neg = right # Type 2 : incorrectly ordered Boolean expression # swap the left and right tree on both sides of the operator else: if logical_tree.right.type in cond2_type: x_neg = x_neg + right x_neg = x_neg + [op] x_neg = x_neg + left return x_pos, x_neg
def gen_logical_exp_token(Call_tree, call_from_main=False): """ generate correct and incorrect string tokens from the callee expression tree args: Call_tree : the test tree of CalleeExpression type call_from_main : to check function is called my main or as a part of sub expression return: list of correct and incorrect string tokens """ x_pos = [] x_neg = [] argument_vector = [] wrong_arg_done = False # get the token for the callee callee, _ = TypeChecker.check_type_get_token(Call_tree.callee) # get the string token for each argument from the AST tree for arg in Call_tree.arguments: arg_token, _ = TypeChecker.check_type_get_token(arg) argument_vector.append(arg_token) # geenrate correct sample x_pos = x_pos + callee for arg in argument_vector: x_pos = x_pos + arg # Type 3: Wrong Identifier # generate incorrect sample by changing the argument to "incorrect" if call_from_main == True: x_neg = x_neg + callee for arg in argument_vector: if not wrong_arg_done and len(arg) == 1: arg = ["wrong_arg"] wrong_arg_done = True x_neg = x_neg + arg return x_pos, x_neg
def gen_member_exp_token(member_tree, call_from_main=False): """ generate correct and incorrect string tokens from the member expression tree args: member_tree : the test tree of MemberExpression type call_from_main : to check function is called my main or as a part of sub expression return: list of correct and incorrect string tokens """ x_pos = [] x_neg = [] # get the object and property sub tree tree obj, _ = TypeChecker.check_type_get_token(member_tree.object) prop, _ = TypeChecker.check_type_get_token(member_tree.property) # form the correct token x_pos = x_pos + obj x_pos = x_pos + prop return x_pos, x_neg
def gen_update_exp_token(update_tree, call_from_main): """ generate correct and incorrect string tokens from the update expression tree args: Call_tree : the test tree of UpdateExpression type call_from_main : to check function is called my main or as a part of sub expression return: list of correct and incorrect string tokens """ # get the operator of the update expression op = update_tree.operator # type of update expression, pre operation or post operation prefix = update_tree.prefix x_pos = [] x_neg = [] # string token of the argument arg, _ = TypeChecker.check_type_get_token(update_tree.argument) # form correct token # Type 3: wrong identifier # if the argument is of type identifier, change the identifier to "incorrect" if prefix == "True": x_pos = x_pos + [op] x_pos = x_pos + arg if update_tree.argument.type == "Identifier": x_neg = x_neg + [op] x_neg = ["incorrect"] else: x_pos = x_pos + arg x_pos = x_pos + [op] if update_tree.argument.type == "Identifier": x_neg = ["incorrect"] x_neg = x_neg + [op] return x_pos, x_neg