From b2c26f7cdd4b268e80f98cae7f444956559436ec Mon Sep 17 00:00:00 2001 From: Zach White Date: Tue, 1 Dec 2020 16:04:22 -0800 Subject: get qmk generate-api into a good state --- lib/python/qmk/c_parse.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'lib/python/qmk/c_parse.py') diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index e41e271a43..67e196f0ea 100644 --- a/lib/python/qmk/c_parse.py +++ b/lib/python/qmk/c_parse.py @@ -1,12 +1,27 @@ """Functions for working with config.h files. """ from pathlib import Path +import re from milc import cli from qmk.comment_remover import comment_remover default_key_entry = {'x': -1, 'y': 0, 'w': 1} +single_comment_regex = re.compile(r' */[/*].*$') +multi_comment_regex = re.compile(r'/\*(.|\n)*\*/', re.MULTILINE) + + +def strip_line_comment(string): + """Removes comments from a single line string. + """ + return single_comment_regex.sub('', string) + + +def strip_multiline_comment(string): + """Removes comments from a single line string. + """ + return multi_comment_regex.sub('', string) def c_source_files(dir_names): @@ -53,7 +68,8 @@ def find_layouts(file): parsed_layout = [_default_key(key) for key in layout.split(',')] for key in parsed_layout: - key['matrix'] = matrix_locations.get(key['label']) + if key['label'] in matrix_locations: + key['matrix'] = matrix_locations[key['label']] parsed_layouts[macro_name] = { 'key_count': len(parsed_layout), @@ -88,12 +104,10 @@ def parse_config_h_file(config_h_file, config_h=None): if config_h_file.exists(): config_h_text = config_h_file.read_text() config_h_text = config_h_text.replace('\\\n', '') + config_h_text = strip_multiline_comment(config_h_text) for linenum, line in enumerate(config_h_text.split('\n')): - line = line.strip() - - if '//' in line: - line = line[:line.index('//')].strip() + line = strip_line_comment(line).strip() if not line: continue @@ -156,6 +170,6 @@ def _parse_matrix_locations(matrix, file, macro_name): row = row.replace('{', '').replace('}', '') for col_num, identifier in enumerate(row.split(',')): if identifier != 'KC_NO': - matrix_locations[identifier] = (row_num, col_num) + matrix_locations[identifier] = [row_num, col_num] return matrix_locations -- cgit v1.2.3 From 30331b383f9ef4620e47aa07e4f9af7fae9d30b3 Mon Sep 17 00:00:00 2001 From: Zach White Date: Fri, 8 Jan 2021 00:00:15 -0800 Subject: fix bugs triggered by certain boards --- lib/python/qmk/c_parse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/python/qmk/c_parse.py') diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index 67e196f0ea..0338484ec7 100644 --- a/lib/python/qmk/c_parse.py +++ b/lib/python/qmk/c_parse.py @@ -9,7 +9,7 @@ from qmk.comment_remover import comment_remover default_key_entry = {'x': -1, 'y': 0, 'w': 1} single_comment_regex = re.compile(r' */[/*].*$') -multi_comment_regex = re.compile(r'/\*(.|\n)*\*/', re.MULTILINE) +multi_comment_regex = re.compile(r'/\*(.|\n)*?\*/', re.MULTILINE) def strip_line_comment(string): @@ -103,7 +103,7 @@ def parse_config_h_file(config_h_file, config_h=None): if config_h_file.exists(): config_h_text = config_h_file.read_text() - config_h_text = config_h_text.replace('\\\n', '') + config_h_text = config_h_text.replace('\\\n', '') # Why are you here? config_h_text = strip_multiline_comment(config_h_text) for linenum, line in enumerate(config_h_text.split('\n')): -- cgit v1.2.3 From 58fcdf8c07e5c1363b6b3eaf23883853dfd12f53 Mon Sep 17 00:00:00 2001 From: Zach White Date: Fri, 8 Jan 2021 00:21:51 -0800 Subject: remove extraneous comment --- lib/python/qmk/c_parse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/python/qmk/c_parse.py') diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index 0338484ec7..ade3e38059 100644 --- a/lib/python/qmk/c_parse.py +++ b/lib/python/qmk/c_parse.py @@ -103,7 +103,7 @@ def parse_config_h_file(config_h_file, config_h=None): if config_h_file.exists(): config_h_text = config_h_file.read_text() - config_h_text = config_h_text.replace('\\\n', '') # Why are you here? + config_h_text = config_h_text.replace('\\\n', '') config_h_text = strip_multiline_comment(config_h_text) for linenum, line in enumerate(config_h_text.split('\n')): -- cgit v1.2.3 From 23ef327e118307d276677d30e3fda064ace6713b Mon Sep 17 00:00:00 2001 From: Zach White Date: Wed, 24 Feb 2021 10:35:08 -0800 Subject: make LAYOUT parsing more robust --- lib/python/qmk/c_parse.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/python/qmk/c_parse.py') diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index ade3e38059..89dd278b7e 100644 --- a/lib/python/qmk/c_parse.py +++ b/lib/python/qmk/c_parse.py @@ -67,8 +67,10 @@ def find_layouts(file): layout = layout.strip() parsed_layout = [_default_key(key) for key in layout.split(',')] - for key in parsed_layout: - if key['label'] in matrix_locations: + for i, key in enumerate(parsed_layout): + if 'label' not in key: + cli.log.error('Invalid LAYOUT macro in %s: Empty parameter name in macro %s at pos %s.', file, macro_name, i) + elif key['label'] in matrix_locations: key['matrix'] = matrix_locations[key['label']] parsed_layouts[macro_name] = { -- cgit v1.2.3 From 1581ea48dcd48d0d3f42cc09b388c468aedec45d Mon Sep 17 00:00:00 2001 From: Zach White Date: Sat, 27 Feb 2021 12:00:50 -0800 Subject: Fix develop (#12039) Fixes file encoding errors on Windows, and layouts not correctly merging into info.json. * force utf8 encoding * correctly merge layouts and layout aliases * show what aliases point to --- lib/python/qmk/c_parse.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'lib/python/qmk/c_parse.py') diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index 89dd278b7e..d4f39c8839 100644 --- a/lib/python/qmk/c_parse.py +++ b/lib/python/qmk/c_parse.py @@ -46,7 +46,7 @@ def find_layouts(file): parsed_layouts = {} # Search the file for LAYOUT macros and aliases - file_contents = file.read_text() + file_contents = file.read_text(encoding='utf-8') file_contents = comment_remover(file_contents) file_contents = file_contents.replace('\\\n', '') @@ -87,12 +87,7 @@ def find_layouts(file): except ValueError: continue - # Populate our aliases - for alias, text in aliases.items(): - if text in parsed_layouts and 'KEYMAP' not in alias: - parsed_layouts[alias] = parsed_layouts[text] - - return parsed_layouts + return parsed_layouts, aliases def parse_config_h_file(config_h_file, config_h=None): @@ -104,7 +99,7 @@ def parse_config_h_file(config_h_file, config_h=None): config_h_file = Path(config_h_file) if config_h_file.exists(): - config_h_text = config_h_file.read_text() + config_h_text = config_h_file.read_text(encoding='utf-8') config_h_text = config_h_text.replace('\\\n', '') config_h_text = strip_multiline_comment(config_h_text) -- cgit v1.2.3