aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/c_parse.py
diff options
context:
space:
mode:
authorNick Brassel <nick@tzarc.org>2021-02-28 07:22:21 +1100
committerNick Brassel <nick@tzarc.org>2021-02-28 07:22:21 +1100
commit1a5f6b54aff179732e3f4f4eb79e47454f0a1eb5 (patch)
treeebf645f55cb0442899c894765b1af4344fb734db /lib/python/qmk/c_parse.py
parent804d5c1c5d59d9a12c1d793289ccbd59cb650ec2 (diff)
parent624359b725c9bfe8176cf72cdc2c8bbb7513949f (diff)
downloadqmk_firmware-1a5f6b54aff179732e3f4f4eb79e47454f0a1eb5.tar.gz
qmk_firmware-1a5f6b54aff179732e3f4f4eb79e47454f0a1eb5.zip
2021 February 27 Breaking Changes Update (#12040)
Diffstat (limited to 'lib/python/qmk/c_parse.py')
-rw-r--r--lib/python/qmk/c_parse.py41
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"""
3from pathlib import Path 3from pathlib import Path
4import re
4 5
5from milc import cli 6from milc import cli
6 7
7from qmk.comment_remover import comment_remover 8from qmk.comment_remover import comment_remover
8 9
9default_key_entry = {'x': -1, 'y': 0, 'w': 1} 10default_key_entry = {'x': -1, 'y': 0, 'w': 1}
11single_comment_regex = re.compile(r' */[/*].*$')
12multi_comment_regex = re.compile(r'/\*(.|\n)*?\*/', re.MULTILINE)
13
14
15def strip_line_comment(string):
16 """Removes comments from a single line string.
17 """
18 return single_comment_regex.sub('', string)
19
20
21def strip_multiline_comment(string):
22 """Removes comments from a single line string.
23 """
24 return multi_comment_regex.sub('', string)
10 25
11 26
12def c_source_files(dir_names): 27def 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
80def parse_config_h_file(config_h_file, config_h=None): 93def 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