Skip to content

Commit 8863e56

Browse files
committed
Ignore most of unwanted comments.
Signed-off-by: Cervenka Dusan <[email protected]>
1 parent a07e479 commit 8863e56

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

dissect/cstruct/cstruct.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ def __repr__(self):
8888
return '<Structure {name} +compiled>'
8989
"""
9090

91-
COMMENT_REGEX_START =r'(\n?[ ]*\/\/[^\n]*\n)*[ ]*(?P<commentBlock>/\*[^*]*\*+(?:[^/*][^*]*\*+)*/)?[ \t\r\n]*'
92-
COMMENT_REGEX_END =r'[ ]*(?P<commentBlockAfter>\/\*(\*(?!\/)|[^*])*\*\/)?(\n?[ ]*\/\/[^\n]*\n*)*'
91+
COMMENT_MULTILINE = r'/\*[^*]*\*+(?:[^/*][^*]*\*+)*/'
92+
COMMENT_INLINE = r'//[^\n]*'
93+
COMMENT_MULTILINE_REPEATED = r'(^[ ]*('+COMMENT_INLINE+r'|'+COMMENT_MULTILINE+r'([ ]*('+COMMENT_INLINE+r'|'+COMMENT_MULTILINE+r'))*)[ \t\r]*\n?)*^[ ]*('+COMMENT_INLINE+r'|(?P<commentBlock>'+COMMENT_MULTILINE+r'))+'
94+
COMMENT_REGEX_START=r'('+COMMENT_MULTILINE_REPEATED+r')?[ \t\r\n]*'
95+
COMMENT_REGEX_END =r'(?P<commentBlockAfter>(([ ]*'+COMMENT_MULTILINE+r')+)?[ ]*('+COMMENT_INLINE+r')?)?[ \t\r]*'
9396

97+
#print(f"COMMENT_REGEX_START:{COMMENT_REGEX_START}")
98+
#print(f"COMMENT_REGEX_END:{COMMENT_REGEX_END}")
9499

95100
class Error(Exception):
96101
pass
@@ -337,7 +342,7 @@ def _constants(self, data):
337342
def _enums(self, data):
338343
r = re.finditer(
339344
COMMENT_REGEX_START+r'enum\s+(?P<name>[^\s:{]+)\s*(:\s*(?P<type>[^\s]+)\s*)?\{(?P<values>[^}]+)\}\s*;'+COMMENT_REGEX_END,
340-
data,
345+
data, re.MULTILINE
341346
)
342347
for t in r:
343348
d = t.groupdict()
@@ -360,7 +365,7 @@ def _parse_fields_enums(self, s):
360365
valuesDetails = {}
361366
fields = re.finditer(
362367
COMMENT_REGEX_START+r'(?P<value>[a-zA-z][^\n,/]*),?'+COMMENT_REGEX_END,
363-
s,
368+
s, re.MULTILINE
364369
)
365370

366371
for f in fields:
@@ -387,7 +392,7 @@ def _structs(self, data):
387392
compiler = Compiler(self.cstruct)
388393
r = re.finditer(
389394
COMMENT_REGEX_START+r'(#(?P<flags>(?:compile))\s+)?((?P<typedef>typedef)\s+)?(?P<type>[^\s]+)\s+(__attribute__\(\([^)]+\)\)\s*)?(?P<name>[^\s]+)?(?P<fields>\s*\{(\s*//[^\n]*|/\*[^*]*\*/|[^}])+\}(?P<defs>\s+[^;\n]+)?)?\s*;'+COMMENT_REGEX_END,
390-
data,
395+
data, re.MULTILINE
391396
)
392397
for t in r:
393398
d = t.groupdict()
@@ -421,7 +426,7 @@ def _structs(self, data):
421426
def _parse_fields_struct(self, s):
422427
fields = re.finditer(
423428
COMMENT_REGEX_START+r'(?P<type>[^\s]+)\s+(?P<name>[^\s\[:]+)(\s*:\s*(?P<bits>\d+))?(\[(?P<count>[^;\n]*)\])?;'+COMMENT_REGEX_END,
424-
s,
429+
s, re.MULTILINE
425430
)
426431
r = []
427432
for f in fields:

tests/test_basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,13 +798,13 @@ def test_comment_field_parse_struct(capsys):
798798
*/
799799
int testVar1;
800800
// int testVar1;
801+
// int notAnStruct;
801802
int testVar2;
802803
/* dicardedCom2
803804
* @scale: 5
804805
* @unit: %testUnit2
805806
*/
806807
int testVar3;
807-
// int notAnStruct;
808808
};
809809
""", compiled=False)
810810

@@ -838,11 +838,11 @@ def test_comment_field_parse_enum(capsys):
838838
testEnumVar1=5,
839839
// testEnumVar1,
840840
testEnumVar2,
841+
// notAnEnum=14,
841842
/*
842843
* @comment: Comments are working 2
843844
*/
844845
testEnumVar3,
845-
// notAnEnum=14,
846846
};
847847
""", compiled=False)
848848

0 commit comments

Comments
 (0)