Skip to content

Commit 34aa6e4

Browse files
fixed syntax highlighting bug
1 parent 0177c46 commit 34aa6e4

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Changes that are planned but not implemented yet:
2323
* Upgrade python version requirements to 3.11
2424
* Fixed a bug where embedded stringsd weren't properly parsed if they contained a newline character or there were multiple embedded strings per line
2525
* Fixed a bug where parsing properly discriminate between labels starting with `BYTE` string and the `BYTEx()` operator.
26+
* Fixed a bug in generating the syntax highlighting configuration that caused mnemonics with special characters to not be highlighted properly.
2627

2728
## [0.4.2]
2829
* Added support for The Minimal 64x4 Home Computer with an example and updated assembler functionality to support it.

src/bespokeasm/configgen/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from bespokeasm.assembler.model import AssemblerModel
23

34

@@ -50,7 +51,10 @@ def _replace_token_with_regex_list(
5051
self,
5152
template_str: str,
5253
token: str,
53-
regex_list: list[str]
54+
item_list: list[str]
5455
) -> str:
56+
# first, convert the item list into individual regex strings, which is
57+
# mostly escaping any periods or other special characters
58+
regex_list = [re.escape(item) for item in item_list]
5559
regex_str = '\\b' + '\\b|\\b'.join(regex_list) + '\\b'
5660
return template_str.replace(token, regex_str)

test/test_configgen.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,44 @@ def test_sublime_configgen_with_registers(self):
348348
self.assertIsFile(package_fp)
349349

350350
shutil.rmtree(test_destination_dir)
351+
352+
def test_sublime_escaping_of_names(self):
353+
# goal of this test is to ensure that entity names sourced from the ISA config are properly escaped
354+
# in the generated language configuration files, notablye the syntax highlighting configuration file.
355+
# for example, if an instruction mnemonic is 'ad.r', the generated regex should be '\\bad\\.r\\b'
356+
# where the '.' is escaped to '\.' and the whole string is wrapped in '\\b' to ensure it is a word boundary.
357+
test_destination_dir = tempfile.mkdtemp()
358+
test_tmp_dir = tempfile.mkdtemp()
359+
config_file = pkg_resources.files(config_files).joinpath('test_instructions_with_periods.yaml')
360+
configgen = SublimeConfigGenerator(
361+
str(config_file),
362+
0,
363+
str(test_destination_dir),
364+
None,
365+
None,
366+
'asmtest',
367+
)
368+
self.assertEqual(configgen.model.isa_name, 'test_instructions_with_periods', 'name should be in ISA config')
369+
# generate the files to inspect their content
370+
configgen._generate_files_in_dir(test_tmp_dir)
371+
372+
syntax_fp = os.path.join(test_tmp_dir, 'test_instructions_with_periods.sublime-syntax')
373+
self.assertIsFile(syntax_fp)
374+
with open(syntax_fp) as yaml_file:
375+
syntax_dict = yaml.safe_load(yaml_file)
376+
377+
for instr_dict in syntax_dict['contexts']['instructions']:
378+
if instr_dict['scope'] == 'variable.function.instruction':
379+
self._assert_grouped_item_list(
380+
instr_dict['match'],
381+
[
382+
'\\bnop\\b',
383+
'\\bma\\.hl\\b', # note the escaped '.'
384+
],
385+
'instructions'
386+
)
387+
elif instr_dict['scope'] == 'variable.function.macro':
388+
self.fail('There should be no macros defined in this list')
389+
390+
# clean up
391+
shutil.rmtree(test_tmp_dir)

0 commit comments

Comments
 (0)