diff --git a/elasticai/creator/vhdl/_ghdl_report_parsing.py b/elasticai/creator/vhdl/_ghdl_report_parsing.py new file mode 100644 index 00000000..cadf102a --- /dev/null +++ b/elasticai/creator/vhdl/_ghdl_report_parsing.py @@ -0,0 +1,20 @@ +def parse(text): + lines = text.split("\n")[:-1] + + def split_first_five_colons(line): + all_split = line.split(":") + reassembled = all_split[0:5] + [":".join(all_split[5:])] + return reassembled + + separated = [split_first_five_colons(line) for line in lines] + fields = ("source", "line", "column", "time", "type", "content") + + def parse_line(line): + parsed = dict(zip(fields, line)) + parsed["type"] = parsed["type"][1:-1] + parsed["time"] = parsed["time"][1:] + parsed["line"] = int(parsed["line"]) + parsed["column"] = int(parsed["column"]) + return parsed + + return [parse_line(line) for line in separated] diff --git a/elasticai/creator/vhdl/_ghdl_report_parsing_test.py b/elasticai/creator/vhdl/_ghdl_report_parsing_test.py new file mode 100644 index 00000000..db70a443 --- /dev/null +++ b/elasticai/creator/vhdl/_ghdl_report_parsing_test.py @@ -0,0 +1,54 @@ +from ._ghdl_report_parsing import parse + + +def test_parse_ghdl_simulation_results_one_liner(): + simulation_output = "my_test_bench.vhd:64:17:@4ps:(report note):my report message\n" + expected = [ + { + "source": "my_test_bench.vhd", + "line": 64, + "column": 17, + "time": "4ps", + "type": "report note", + "content": "my report message", + } + ] + assert expected == parse(simulation_output) + + +def test_parse_ghdl_another_line(): + simulation_output = "A:1:2:@B:(C):D\n" + expected = [ + { + "source": "A", + "line": 1, + "column": 2, + "time": "B", + "type": "C", + "content": "D", + } + ] + assert expected == parse(simulation_output) + + +def test_parse_two_lines(): + simulation_output = ( + "source:1:1:@time:(type):content\nsource:1:1:@time:(type):content\nignored last" + " line" + ) + expected = [ + { + "source": "source", + "line": 1, + "column": 1, + "time": "time", + "type": "type", + "content": "content", + } + ] * 2 + assert expected == parse(simulation_output) + + +def test_put_colon_content_in_content_field(): + simulation_output = "A:1:2:@B:(C):D:e:f\n" + assert "D:e:f" == parse(simulation_output)[0]["content"]