diff options
Diffstat (limited to 'lib/python/qmk/c_parse.py')
| -rw-r--r-- | lib/python/qmk/c_parse.py | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index e41e271a4..d4f39c883 100644 --- a/lib/python/qmk/c_parse.py +++ b/lib/python/qmk/c_parse.py | |||
| @@ -1,12 +1,27 @@ | |||
| 1 | """Functions for working with config.h files. | 1 | """Functions for working with config.h files. |
| 2 | """ | 2 | """ |
| 3 | from pathlib import Path | 3 | from pathlib import Path |
| 4 | import re | ||
| 4 | 5 | ||
| 5 | from milc import cli | 6 | from milc import cli |
| 6 | 7 | ||
| 7 | from qmk.comment_remover import comment_remover | 8 | from qmk.comment_remover import comment_remover |
| 8 | 9 | ||
| 9 | default_key_entry = {'x': -1, 'y': 0, 'w': 1} | 10 | default_key_entry = {'x': -1, 'y': 0, 'w': 1} |
| 11 | single_comment_regex = re.compile(r' */[/*].*$') | ||
| 12 | multi_comment_regex = re.compile(r'/\*(.|\n)*?\*/', re.MULTILINE) | ||
| 13 | |||
| 14 | |||
| 15 | def strip_line_comment(string): | ||
| 16 | """Removes comments from a single line string. | ||
| 17 | """ | ||
| 18 | return single_comment_regex.sub('', string) | ||
| 19 | |||
| 20 | |||
| 21 | def strip_multiline_comment(string): | ||
| 22 | """Removes comments from a single line string. | ||
| 23 | """ | ||
| 24 | return multi_comment_regex.sub('', string) | ||
| 10 | 25 | ||
| 11 | 26 | ||
| 12 | def c_source_files(dir_names): | 27 | def c_source_files(dir_names): |
| @@ -31,7 +46,7 @@ def find_layouts(file): | |||
| 31 | parsed_layouts = {} | 46 | parsed_layouts = {} |
| 32 | 47 | ||
| 33 | # Search the file for LAYOUT macros and aliases | 48 | # Search the file for LAYOUT macros and aliases |
| 34 | file_contents = file.read_text() | 49 | file_contents = file.read_text(encoding='utf-8') |
| 35 | file_contents = comment_remover(file_contents) | 50 | file_contents = comment_remover(file_contents) |
| 36 | file_contents = file_contents.replace('\\\n', '') | 51 | file_contents = file_contents.replace('\\\n', '') |
| 37 | 52 | ||
| @@ -52,8 +67,11 @@ def find_layouts(file): | |||
| 52 | layout = layout.strip() | 67 | layout = layout.strip() |
| 53 | parsed_layout = [_default_key(key) for key in layout.split(',')] | 68 | parsed_layout = [_default_key(key) for key in layout.split(',')] |
| 54 | 69 | ||
| 55 | for key in parsed_layout: | 70 | for i, key in enumerate(parsed_layout): |
| 56 | key['matrix'] = matrix_locations.get(key['label']) | 71 | if 'label' not in key: |
| 72 | cli.log.error('Invalid LAYOUT macro in %s: Empty parameter name in macro %s at pos %s.', file, macro_name, i) | ||
| 73 | elif key['label'] in matrix_locations: | ||
| 74 | key['matrix'] = matrix_locations[key['label']] | ||
| 57 | 75 | ||
| 58 | parsed_layouts[macro_name] = { | 76 | parsed_layouts[macro_name] = { |
| 59 | 'key_count': len(parsed_layout), | 77 | 'key_count': len(parsed_layout), |
| @@ -69,12 +87,7 @@ def find_layouts(file): | |||
| 69 | except ValueError: | 87 | except ValueError: |
| 70 | continue | 88 | continue |
| 71 | 89 | ||
| 72 | # Populate our aliases | 90 | return parsed_layouts, aliases |
| 73 | for alias, text in aliases.items(): | ||
| 74 | if text in parsed_layouts and 'KEYMAP' not in alias: | ||
| 75 | parsed_layouts[alias] = parsed_layouts[text] | ||
| 76 | |||
| 77 | return parsed_layouts | ||
| 78 | 91 | ||
| 79 | 92 | ||
| 80 | def parse_config_h_file(config_h_file, config_h=None): | 93 | def parse_config_h_file(config_h_file, config_h=None): |
| @@ -86,14 +99,12 @@ def parse_config_h_file(config_h_file, config_h=None): | |||
| 86 | config_h_file = Path(config_h_file) | 99 | config_h_file = Path(config_h_file) |
| 87 | 100 | ||
| 88 | if config_h_file.exists(): | 101 | if config_h_file.exists(): |
| 89 | config_h_text = config_h_file.read_text() | 102 | config_h_text = config_h_file.read_text(encoding='utf-8') |
| 90 | config_h_text = config_h_text.replace('\\\n', '') | 103 | config_h_text = config_h_text.replace('\\\n', '') |
| 104 | config_h_text = strip_multiline_comment(config_h_text) | ||
| 91 | 105 | ||
| 92 | for linenum, line in enumerate(config_h_text.split('\n')): | 106 | for linenum, line in enumerate(config_h_text.split('\n')): |
| 93 | line = line.strip() | 107 | line = strip_line_comment(line).strip() |
| 94 | |||
| 95 | if '//' in line: | ||
| 96 | line = line[:line.index('//')].strip() | ||
| 97 | 108 | ||
| 98 | if not line: | 109 | if not line: |
| 99 | continue | 110 | continue |
| @@ -156,6 +167,6 @@ def _parse_matrix_locations(matrix, file, macro_name): | |||
| 156 | row = row.replace('{', '').replace('}', '') | 167 | row = row.replace('{', '').replace('}', '') |
| 157 | for col_num, identifier in enumerate(row.split(',')): | 168 | for col_num, identifier in enumerate(row.split(',')): |
| 158 | if identifier != 'KC_NO': | 169 | if identifier != 'KC_NO': |
| 159 | matrix_locations[identifier] = (row_num, col_num) | 170 | matrix_locations[identifier] = [row_num, col_num] |
| 160 | 171 | ||
| 161 | return matrix_locations | 172 | return matrix_locations |
