Use .span() for positions, and separate line_no argument in Match

This reverts a previous change where line_no was removed and put into
a triple tuple. It was discovered that re.Match.span() conveniently
returns (start, end), so separating line_no again makes the code cleaner.
The legibility of the code heavily outweighs the issues pointed out by
Pylint (hence disabled).

Signed-off-by: Yuto Takano <yuto.takano@arm.com>
This commit is contained in:
Yuto Takano 2021-08-17 10:41:23 +01:00
parent b1417b4554
commit 704b0f77e1

View file

@ -67,13 +67,15 @@ class Match(): # pylint: disable=too-few-public-methods
Fields: Fields:
* filename: the file that the match was in. * filename: the file that the match was in.
* line: the full line containing the match. * line: the full line containing the match.
* pos: a tuple of (line_no, start, end) positions on the file line where the * line_no: the line number.
match is. * pos: a tuple of (start, end) positions on the line where the match is.
* name: the match itself. * name: the match itself.
""" """
def __init__(self, filename, line, pos, name): def __init__(self, filename, line, line_no, pos, name):
# pylint: disable=too-many-arguments
self.filename = filename self.filename = filename
self.line = line self.line = line
self.line_no = line_no
self.pos = pos self.pos = pos
self.name = name self.name = name
@ -81,8 +83,8 @@ class Match(): # pylint: disable=too-few-public-methods
""" """
Return a formatted code listing representation of the erroneous line. Return a formatted code listing representation of the erroneous line.
""" """
gutter = format(self.pos[0], "4d") gutter = format(self.line_no, "4d")
underline = self.pos[1] * " " + (self.pos[2] - self.pos[1]) * "^" underline = self.pos[0] * " " + (self.pos[1] - self.pos[0]) * "^"
return ( return (
" {0} |\n".format(" " * len(gutter)) + " {0} |\n".format(" " * len(gutter)) +
@ -338,7 +340,8 @@ class CodeParser():
macros.append(Match( macros.append(Match(
header_file, header_file,
line, line,
(line_no, macro.start(), macro.end()), line_no,
macro.span("macro"),
macro.group("macro"))) macro.group("macro")))
return macros return macros
@ -372,9 +375,9 @@ class CodeParser():
mbed_words.append(Match( mbed_words.append(Match(
filename, filename,
line, line,
(line_no, name.start(), name.end()), line_no,
name.group(0) name.span(0),
)) name.group(0)))
return mbed_words return mbed_words
@ -425,9 +428,8 @@ class CodeParser():
enum_consts.append(Match( enum_consts.append(Match(
header_file, header_file,
line, line,
(line_no, line_no,
enum_const.start("enum_const"), enum_const.span("enum_const"),
enum_const.end("enum_const")),
enum_const.group("enum_const"))) enum_const.group("enum_const")))
return enum_consts return enum_consts
@ -533,7 +535,8 @@ class CodeParser():
identifiers.append(Match( identifiers.append(Match(
header_file, header_file,
line, line,
(line_no, identifier.start(), identifier.end()), line_no,
identifier.span(),
group)) group))
return identifiers return identifiers
@ -722,7 +725,8 @@ class NameChecker():
problems.append(PatternMismatch(check_pattern, item_match)) problems.append(PatternMismatch(check_pattern, item_match))
# Double underscore should not be used for names # Double underscore should not be used for names
if re.search(r".*__.*", item_match.name): if re.search(r".*__.*", item_match.name):
problems.append(PatternMismatch("double underscore", item_match)) problems.append(
PatternMismatch("no double underscore allowed", item_match))
self.output_check_result( self.output_check_result(
"Naming patterns of {}".format(group_to_check), "Naming patterns of {}".format(group_to_check),