Skip to content

Commit 811c0b5

Browse files
fix: bug with possible unicode bound fail
1 parent a3dd26b commit 811c0b5

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

cdracula/tests/test_capi.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ mod python {
3737
let src = CString::from_vec_unchecked(
3838
(String::from(
3939
r#"
40+
# entp için anayzer
41+
if index == 10:
42+
pass
4043
# skip this
4144
def python():
4245
"""
@@ -47,7 +50,7 @@ def python():
4750
) + "\0")
4851
.into(),
4952
);
50-
assert_eq!(get_meaningful_line_count(src.as_ptr(), PYTHON_LANG, 0), 2);
53+
assert_eq!(get_meaningful_line_count(src.as_ptr(), PYTHON_LANG, 0), 4);
5154
}
5255
}
5356

src/parse.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub struct Parser<'a, L: Language> {
279279
src: &'a str,
280280
index: usize,
281281
language_items: &'static [ParseItem],
282-
_marker: PhantomData<L>
282+
_marker: PhantomData<L>,
283283
}
284284

285285
// most this is only used in tests atm!
@@ -290,7 +290,7 @@ impl<L: Language> Parser<'_, L> {
290290
src,
291291
language_items: L::PARSE_ITEMS,
292292
index: 0,
293-
_marker: PhantomData::default()
293+
_marker: PhantomData::default(),
294294
}
295295
}
296296

@@ -304,18 +304,22 @@ impl<L: Language> Parser<'_, L> {
304304
.find_map(|i| Some((i, items[i].begin().matches(src)?)))
305305
.and_then(|(i, matches)| {
306306
(matches[2].end..src.len()).find_map(|b| {
307-
Some((
308-
i,
309-
b,
310-
if items[i].is_key_matched() {
311-
items[i].end().matches_with_key(
312-
&src[b..],
313-
&src[matches[1].start..matches[1].end],
314-
)?
315-
} else {
316-
items[i].end().matches(&src[b..])?
317-
},
318-
))
307+
if src.is_char_boundary(b) {
308+
Some((
309+
i,
310+
b,
311+
if items[i].is_key_matched() {
312+
items[i].end().matches_with_key(
313+
&src[b..],
314+
&src[matches[1].start..matches[1].end],
315+
)?
316+
} else {
317+
items[i].end().matches(&src[b..])?
318+
},
319+
))
320+
} else {
321+
None
322+
}
319323
})
320324
})
321325
{

src/tests.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ mod simple_python {
3939
#[test]
4040
fn try_parse() {
4141
let parsed = Parser::<Python>::new(
42-
r#"# some top level comments
42+
r#"# entp için anayzer
43+
if index == 10:
44+
pass
45+
# some top level comments
4346
def main():
4447
print("s");"""
4548
Multi-line Comments
@@ -53,6 +56,7 @@ mod simple_python {
5356
let mut line_count: usize = 0;
5457
let mut stack = vec![];
5558
for p in parsed {
59+
eprintln!("{:?}", p);
5660
if matches!(p, ParseOutput::EOL(_) | ParseOutput::EOF) {
5761
if stack.iter().any(|i| match i {
5862
ParseOutput::Source(s) => Python::is_meaningful_src(s),
@@ -65,7 +69,7 @@ mod simple_python {
6569
stack.push(p);
6670
}
6771
}
68-
assert_eq!(line_count, 3)
72+
assert_eq!(line_count, 5)
6973
}
7074
}
7175

0 commit comments

Comments
 (0)