summaryrefslogtreecommitdiff
path: root/lib/python/qmk/c_parse.py
diff options
context:
space:
mode:
authorZach White <skullydazed@gmail.com>2020-12-01 16:04:22 -0800
committerZach White <skullydazed@drpepper.org>2021-01-07 21:21:12 -0800
commitb2c26f7cdd4b268e80f98cae7f444956559436ec (patch)
treea621c5ac6baee49c128ada7f9af940d50440eb3e /lib/python/qmk/c_parse.py
parent266a85eda0cbb63852565d0c1d7ddb24700b8c1f (diff)
get qmk generate-api into a good state
Diffstat (limited to 'lib/python/qmk/c_parse.py')
-rw-r--r--lib/python/qmk/c_parse.py26
1 files changed, 20 insertions, 6 deletions
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