Skip to content

Commit fb24d94

Browse files
committed
Rewritten HdlSanitize as HDLReader
1 parent 56b1b63 commit fb24d94

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

hdltools/hdl_reader.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# Copyright (C) 2025 HDLtools Project
3+
#
4+
# SPDX-License-Identifier: GPL-3.0-or-later
5+
#
6+
7+
"""
8+
Reads and sanitizes the input HDL code by removing comments, extra whitespaces
9+
and newlines.
10+
"""
11+
12+
import re
13+
14+
15+
class HDLReader:
16+
"""Reads and sanitizes the input HDL code."""
17+
18+
def __init__(self, code=''):
19+
self.code = code
20+
21+
def read_file(self, path):
22+
"""Reads the HDL code from file."""
23+
with open(path, 'r', encoding='utf-8') as fobj:
24+
self.code = fobj.read()
25+
26+
def set_code(self, code):
27+
"""Directly sets the HDL code."""
28+
self.code = code
29+
30+
def get_code(self, is_vhdl=False):
31+
"""Retrieves the sanitized HDL code."""
32+
if is_vhdl:
33+
text = re.sub(r'--[^\n]*', '', self.code)
34+
else:
35+
text = re.sub(r'//[^\n]*', '', self.code)
36+
text = re.sub(r'/\*.*?\*/', '', text, flags=re.DOTALL)
37+
return re.sub(r'\s+', ' ', text).strip()

hdltools/hdl_sanitize.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)