Skip to content

Commit bf3a976

Browse files
parser: fix comment parsing
for comments that will transition right into a different kind of block, like functions or python blocks. This will also merge consecutive comments into single comment blocks Closes #317 Signed-off-by: Konrad Weihmann <kweihmann@outlook.com>
1 parent b9edec1 commit bf3a976

5 files changed

Lines changed: 83 additions & 2 deletions

File tree

oelint_parser/parser.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
__regex_export_wval = regex.compile(r"^\s*?export(\s+|\t+)(?P<name>.+)\s*=\s*\"(?P<value>.*)\"")
3737
__regex_export_woval = regex.compile(r"^\s*?export(\s+|\t+)(?P<name>.+)\s*$")
3838
__regex_comments = regex.compile(r"^(\s|\t)*#+\s*(?P<body>.*)")
39+
__regex_comments_pure = regex.compile(r"^#+\s*(?P<body>.*)")
3940
__regex_python = regex.compile(r"^(\s*|\t*)def(\s+|\t+)(?P<funcname>[a-z0-9_\-]+)(\s*|\t*)\(.*\)\:")
4041
__regex_include = regex.compile(r"^(\s*|\t*)(?P<statement>include|require)(\s+|\t+)(?P<incname>[A-za-z0-9\-\./\$\{\}]+)")
4142
__regex_addtask = regex.compile(
@@ -120,13 +121,28 @@ def iterate(_iter: Iterable, buffer: str) -> tuple[str, str]:
120121
_, line = _iter.__next__()
121122
except StopIteration:
122123
stopiter = True
124+
elif RegexRpl.search(__regex_comments_pure, res):
125+
stopiter = False
126+
while not stopiter:
127+
try:
128+
_, line = _iter.__next__()
129+
if not RegexRpl.search(__regex_comments_pure, line):
130+
stopiter = True
131+
next_ = line.strip()
132+
else:
133+
res += line
134+
next_ = ''
135+
except StopIteration:
136+
stopiter = True
137+
next_ = ''
123138
elif res.strip().startswith("def "):
124139
stopiter = False
125140
while not stopiter:
126141
try:
127142
_, line = _iter.__next__()
128143
except StopIteration:
129144
stopiter = True
145+
next_ = ''
130146
if stopiter:
131147
break
132148
elif RegexRpl.match(__valid_func_name_regex__, line):
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Some comment
2+
# with more than a single line
3+
def some_python_code():
4+
result = ''
5+
return result
6+
7+
# Just a comment
8+
# across multiple lines
9+
10+
do_example() {
11+
bbwarn "This is an example warning"
12+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[
2+
{
3+
"Line": 1,
4+
"Raw": "# Some comment\n# with more than a single line\n",
5+
"InFileLine": 1,
6+
"RealRaw": "# Some comment\n# with more than a single line\n",
7+
"IsFromClass": false,
8+
"OverrideDelimiter": "_",
9+
"IsNewStyleOverrideSyntax": false,
10+
"InlineBlocks": []
11+
},
12+
{
13+
"FuncName": "some_python_code",
14+
"Line": 4,
15+
"Raw": "def some_python_code(): result = ''\n return result\n\n",
16+
"InFileLine": 4,
17+
"RealRaw": "def some_python_code(): result = ''\n return result\n\n",
18+
"IsFromClass": false,
19+
"OverrideDelimiter": "_",
20+
"IsNewStyleOverrideSyntax": false,
21+
"InlineBlocks": []
22+
},
23+
{
24+
"Line": 8,
25+
"Raw": "# Just a comment\n# across multiple lines\n",
26+
"InFileLine": 8,
27+
"RealRaw": "# Just a comment\n# across multiple lines\n",
28+
"IsFromClass": false,
29+
"OverrideDelimiter": "_",
30+
"IsNewStyleOverrideSyntax": false,
31+
"InlineBlocks": []
32+
},
33+
{
34+
"IsPython": false,
35+
"IsFakeroot": false,
36+
"FuncName": "do_example",
37+
"FuncNameComplete": "do_example",
38+
"SubItem": "",
39+
"SubItems": [],
40+
"FuncBody": " bbwarn \"This is an example warning\"",
41+
"FuncBodyStripped": "bbwarn \"This is an example warning\"",
42+
"FuncBodyRaw": "\nbbwarn \"This is an example warning\"\n",
43+
"Line": 10,
44+
"Raw": "do_example() {\n bbwarn \"This is an example warning\"\n}\n",
45+
"InFileLine": 10,
46+
"RealRaw": "do_example() {\n bbwarn \"This is an example warning\"\n}\n",
47+
"IsFromClass": false,
48+
"OverrideDelimiter": "_",
49+
"IsNewStyleOverrideSyntax": false,
50+
"InlineBlocks": []
51+
}
52+
]

tests/test_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,14 @@ def test_append_to_stash(self):
491491

492492
_stash = self.__stash.GetItemsFor(classifier=Comment.CLASSIFIER)
493493

494-
assert len(_stash) == 2
494+
assert len(_stash) == 1
495495

496496
new_comment = Comment('foo', 1, 1, '# abc', '# def', [])
497497
self.__stash.Append(new_comment)
498498

499499
_stash = self.__stash.GetItemsFor(classifier=Comment.CLASSIFIER)
500500

501-
assert len(_stash) == 3
501+
assert len(_stash) == 2
502502

503503
def test_inherit(self):
504504
from oelint_parser.cls_item import Inherit

tests/test_pattern.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'pattern_var.bb',
1010
'pattern_python.bb',
1111
'pattern_function.bb',
12+
'pattern_comment_py.bb',
1213
],
1314
)
1415
def test_pattern(file):

0 commit comments

Comments
 (0)