diff options
Diffstat (limited to 'lib/python/qmk/c_parse.py')
| -rw-r--r-- | lib/python/qmk/c_parse.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/lib/python/qmk/c_parse.py b/lib/python/qmk/c_parse.py index e41e271a4..89dd278b7 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): |
| @@ -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), |
| @@ -88,12 +106,10 @@ def parse_config_h_file(config_h_file, config_h=None): | |||
| 88 | if config_h_file.exists(): | 106 | if config_h_file.exists(): |
| 89 | config_h_text = config_h_file.read_text() | 107 | config_h_text = config_h_file.read_text() |
| 90 | config_h_text = config_h_text.replace('\\\n', '') | 108 | config_h_text = config_h_text.replace('\\\n', '') |
| 109 | config_h_text = strip_multiline_comment(config_h_text) | ||
| 91 | 110 | ||
| 92 | for linenum, line in enumerate(config_h_text.split('\n')): | 111 | for linenum, line in enumerate(config_h_text.split('\n')): |
| 93 | line = line.strip() | 112 | line = strip_line_comment(line).strip() |
| 94 | |||
| 95 | if '//' in line: | ||
| 96 | line = line[:line.index('//')].strip() | ||
| 97 | 113 | ||
| 98 | if not line: | 114 | if not line: |
| 99 | continue | 115 | continue |
| @@ -156,6 +172,6 @@ def _parse_matrix_locations(matrix, file, macro_name): | |||
| 156 | row = row.replace('{', '').replace('}', '') | 172 | row = row.replace('{', '').replace('}', '') |
| 157 | for col_num, identifier in enumerate(row.split(',')): | 173 | for col_num, identifier in enumerate(row.split(',')): |
| 158 | if identifier != 'KC_NO': | 174 | if identifier != 'KC_NO': |
| 159 | matrix_locations[identifier] = (row_num, col_num) | 175 | matrix_locations[identifier] = [row_num, col_num] |
| 160 | 176 | ||
| 161 | return matrix_locations | 177 | return matrix_locations |
