Skip to content

Commit c8adbe8

Browse files
authored
Merge pull request #113 from klauer/fix-end-position-of-comments-and-pragmas
Fix end position of comments and pragmas
2 parents 8afd73b + d7e3d6a commit c8adbe8

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ jobs:
137137
pip install docs-versions-menu
138138
139139
- name: Download documentation artifact
140-
uses: actions/download-artifact@v3
140+
uses: actions/download-artifact@v4
141141
with:
142142
name: Documentation
143143

blark/tests/test_parsing.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import pytest
44

5-
from blark.util import SourceType
6-
75
from ..parse import parse, parse_source_code, summarize
6+
from ..util import SourceType
87
from . import conftest
98

109
TEST_PATH = pathlib.Path(__file__).parent
@@ -97,3 +96,37 @@ def test_parsing_source(source_filename: str):
9796
def test_rule_smoke(grammar, name, value):
9897
result = conftest.get_grammar(start=name).parse(value)
9998
print(f"rule {name} value {value!r} into {result}")
99+
100+
101+
@pytest.mark.parametrize(
102+
("code", "expected_snippets"),
103+
[
104+
pytest.param(
105+
"""\
106+
(*
107+
123456789012345678
108+
*)
109+
// comment
110+
{pragma}
111+
VAR_GLOBAL
112+
dummy : BOOL;
113+
END_VAR
114+
""",
115+
[
116+
"(*\n123456789012345678\n*)",
117+
"// comment",
118+
"{pragma}",
119+
],
120+
id="issue_109",
121+
),
122+
],
123+
)
124+
def test_comment_parsing(code: str, expected_snippets: list[str]):
125+
result = parse_source_code(code)
126+
127+
print(result.comments)
128+
129+
snippets = [
130+
result.source_code[token.start_pos: token.end_pos] for token in result.comments
131+
]
132+
assert snippets == expected_snippets

blark/util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def get_token(
363363
end_line: int,
364364
end_col: int,
365365
) -> lark.Token:
366-
block = text[start_pos:end_pos + 1]
366+
block = text[start_pos:end_pos]
367367

368368
if block.startswith("//"):
369369
type_ = "SINGLE_LINE_COMMENT"
@@ -424,7 +424,7 @@ def get_token(
424424
start_pos,
425425
start_line,
426426
start_col,
427-
pos,
427+
pos + 1,
428428
lineno,
429429
colno + 1,
430430
)
@@ -452,9 +452,9 @@ def get_token(
452452
start_pos,
453453
start_line,
454454
start_col,
455-
pos + 1, # two character ending
455+
pos + 2, # two character ending
456456
lineno,
457-
colno + 1, # two character ending
457+
colno + 2, # two character ending
458458
)
459459
)
460460
skip = 1
@@ -466,7 +466,7 @@ def get_token(
466466
pos,
467467
lineno,
468468
colno,
469-
pos + (len(lines[lineno]) - colno - 1),
469+
pos + (len(lines[lineno]) - colno),
470470
lineno,
471471
len(lines[lineno]),
472472
)

0 commit comments

Comments
 (0)