def get_end_position(end_marker, text, position): try: end_match = next(unescaped_search_for(end_marker, text[position + 1:])) end_position = position + end_match.span()[1] except StopIteration: end_position = -1 return end_position
def get_valid_sequences(file, sequence, annotation_dict): """ A vaild sequence is a sequence that is outside of comments or strings. :param file: File that needs to be checked in the form of a list of strings. :param sequence: Sequence whose validity is to be checked. :param annotation_dict: A dictionary containing sourceranges of all the strings and comments within a file. :return: A tuple of AbsolutePosition's of all occurances of sequence outside of string's and comments. """ file_string = ''.join(file) # tuple since order is important sequence_positions = tuple() for sequence_match in unescaped_search_for(sequence, file_string): valid = True sequence_position = AbsolutePosition( file, sequence_match.start()) # ignore if within string for string in annotation_dict['strings']: if(gt_eq(sequence_position, string.start) and lt_eq(sequence_position, string.end)): valid = False # ignore if within comments for comment in annotation_dict['comments']: if(gt_eq(sequence_position, comment.start) and lt_eq(sequence_position, comment.end)): valid = False if valid: sequence_positions += (sequence_position,) return sequence_positions
def find_singleline_comments(filename, file, comments): """ Finds all single-line comments. :param filename: Name of the file on which the bear is running. :param file: Contents of the file in the form of tuple of lines. :param comments: A list containing different types of single-line comments. :return: A set of SourceRange objects with start as the beginning of the comment and end as the termination of line. """ text = ''.join(file) single_comments = set() for comment_type in comments: for found in unescaped_search_for(comment_type, text): start = found.start() end = text.find('\n', start) end = len(text) - 1 if end == -1 else end single_comments.add(SourceRange.from_absolute_position( filename, AbsolutePosition(file, start), AbsolutePosition(file, end))) return single_comments
def get_valid_sequences(file, sequence, annotation_dict, encapsulators=None, check_ending=False): """ A vaild sequence is a sequence that is outside of comments or strings. :param file: File that needs to be checked in the form of a list of strings. :param sequence: Sequence whose validity is to be checked. :param annotation_dict: A dictionary containing sourceranges of all the strings and comments within a file. :param encapsulators: A tuple of SourceRanges of code regions trapped in between a matching pair of encapsulators. :param check_ending: Check whether sequence falls at the end of the line. :return: A tuple of AbsolutePosition's of all occurances of sequence outside of string's and comments. """ file_string = ''.join(file) # tuple since order is important sequence_positions = tuple() for sequence_match in unescaped_search_for(sequence, file_string): valid = True sequence_position = AbsolutePosition(file, sequence_match.start()) sequence_line_text = file[sequence_position.line - 1] # ignore if within string for string in annotation_dict['strings']: if (gt_eq(sequence_position, string.start) and lt_eq(sequence_position, string.end)): valid = False # ignore if within comments for comment in annotation_dict['comments']: if (gt_eq(sequence_position, comment.start) and lt_eq(sequence_position, comment.end)): valid = False if (comment.start.line == sequence_position.line and comment.end.line == sequence_position.line and check_ending): sequence_line_text = sequence_line_text[:comment.start. column - 1] + sequence_line_text[ comment.end. column - 1:] if encapsulators: for encapsulator in encapsulators: if (gt_eq(sequence_position, encapsulator.start) and lt_eq(sequence_position, encapsulator.end)): valid = False if not sequence_line_text.rstrip().endswith(':') and check_ending: valid = False if valid: sequence_positions += (sequence_position, ) return sequence_positions
def get_valid_sequences(file, sequence, annotation_dict, encapsulators=None, check_ending=False): """ A vaild sequence is a sequence that is outside of comments or strings. :param file: File that needs to be checked in the form of a list of strings. :param sequence: Sequence whose validity is to be checked. :param annotation_dict: A dictionary containing sourceranges of all the strings and comments within a file. :param encapsulators: A tuple of SourceRanges of code regions trapped in between a matching pair of encapsulators. :param check_ending: Check whether sequence falls at the end of the line. :return: A tuple of AbsolutePosition's of all occurances of sequence outside of string's and comments. """ file_string = ''.join(file) # tuple since order is important sequence_positions = tuple() for sequence_match in unescaped_search_for(sequence, file_string): valid = True sequence_position = AbsolutePosition( file, sequence_match.start()) sequence_line_text = file[sequence_position.line - 1] # ignore if within string for string in annotation_dict['strings']: if(gt_eq(sequence_position, string.start) and lt_eq(sequence_position, string.end)): valid = False # ignore if within comments for comment in annotation_dict['comments']: if(gt_eq(sequence_position, comment.start) and lt_eq(sequence_position, comment.end)): valid = False if(comment.start.line == sequence_position.line and comment.end.line == sequence_position.line and check_ending): sequence_line_text = sequence_line_text[ :comment.start.column - 1] + sequence_line_text[ comment.end.column-1:] if encapsulators: for encapsulator in encapsulators: if(gt_eq(sequence_position, encapsulator.start) and lt_eq(sequence_position, encapsulator.end)): valid = False if not sequence_line_text.rstrip().endswith(':') and check_ending: valid = False if valid: sequence_positions += (sequence_position,) return sequence_positions