This reduces the complexity of the code that searches for config section and writes it into a StringIO. The new code just looks for a comment followed by [config] to open the section and a comment followed by [end config] to close it. It naively slurps up everything in between.
Signed-off-by: Dylan Baker <[email protected]> --- framework/glsl_parser_test.py | 85 ++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 53 deletions(-) diff --git a/framework/glsl_parser_test.py b/framework/glsl_parser_test.py index 0c3c900..40271b0 100644 --- a/framework/glsl_parser_test.py +++ b/framework/glsl_parser_test.py @@ -84,64 +84,43 @@ def glsl_parser_test(filepath): # Text of config section. text_io = StringIO() - # Parsing state. - PARSE_FIND_START = 0 - PARSE_IN_CONFIG = 1 - PARSE_DONE = 2 - PARSE_ERROR = 3 - parse_state = PARSE_FIND_START - - # Regexen that change parser state. - start = re.compile(r'\A(?P<indent>\s*(|//|/\*|\*)\s*)' - '(?P<content>\[config\]\s*\n)\Z') - empty = None # Empty line in config body. - internal = None # Non-empty line in config body. - end = None # Marks end of config body. + # Create a regex that can find all commented lines that contain content + is_header = re.compile(r'\s*(//|/\*|\*)\s*\[(config|end\sconfig)\]') + is_config = False # Parse the config file and get the config section, then write this section # to a StringIO and pass that to ConfigParser - with open(filepath, 'r') as f: - for line in f: - if parse_state == PARSE_FIND_START: - m = start.match(line) - if m: - parse_state = PARSE_IN_CONFIG - text_io.write(m.group('content')) - indent = '.' * len(m.group('indent')) - empty = re.compile(r'\A\s*(|//|/\*|\*)\s*\n\Z') - internal = re.compile(r'\A{indent}(?P<content>' - '.*\n)\Z'.format(indent=indent)) - end = re.compile(r'\A{indent}\[end( |_)' - 'config\]\s*\n\Z'.format(indent=indent)) - elif parse_state == PARSE_IN_CONFIG: - if start.match(line): - parse_state = PARSE_ERROR + with open(filepath, 'r') as testfile: + for line in testfile: + if is_header.match(line): + # If we hit this when is_config is true that makes it our the + # end of the config, and we can stop + if is_config: break - if end.match(line): - parse_state = PARSE_DONE - break - m = internal.match(line) - if m: - text_io.write(m.group('content')) - continue - m = empty.match(line) - if m: - text_io.write('\n') + is_config = True + if is_config: + # Remove all leading whitespace + temp = line.strip() + + # If the starting character is a two character comment remove + # that and any newly revealed whitespace, then write it into + # the StringIO + if temp[:2] in ['//', '/*', '*/']: + text_io.write(temp[2:].lstrip() + '\n') + # If we have just * then we're in the middle of a C style + # comment, do like above + elif temp[:1] == '*': + text_io.write(temp[1:].lstrip() + '\n') + # If strip renendered '' that means we had a blank newline, + # just go on + elif temp == '': continue - parse_state = PARSE_ERROR - break - - if parse_state == PARSE_DONE: - pass - elif parse_state == PARSE_FIND_START: - raise GLSLParserException("Failed to find start of config section") - elif parse_state == PARSE_IN_CONFIG: - raise GLSLParserException("Failed to find end of config section") - elif parse_state == PARSE_ERROR: - raise GLSLParserException("Failed to parse config section. This is " - "most likely due to whitespace") - else: - raise GLSLParserException("How did you hit this? This shouldnt happen") + else: + raise GLSLParserException( + "The config section is malformed." + "Check file {0}".format(filepath)) + else: + raise GLSLParserException("No [end config] section found!") config = ConfigParser.SafeConfigParser(defaults={'require_extensions': '', 'check_link': 'false'}) -- 1.8.5.4 _______________________________________________ Piglit mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/piglit
