Skip to content

Commit 939bf9a

Browse files
authored
Fix comment parser with hash vector (raviqqe#48)
1 parent 7a0d6ce commit 939bf9a

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/parse.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,16 @@ mod tests {
116116
)])
117117
);
118118
}
119+
120+
#[test]
121+
fn parse_vector() {
122+
assert_eq!(
123+
parse("#()", Global),
124+
Ok(vec![Expression::Quote(
125+
"#",
126+
Expression::List("(", ")", vec![], Position::new(1, 3)).into(),
127+
Position::new(0, 3)
128+
)])
129+
);
130+
}
119131
}

src/parse/parser.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn comments<A: Allocator + Clone>(input: Input<A>) -> IResult<Vec<Comment, A
3636
map(none_of("\";#"), |_| None),
3737
map(raw_string, |_| None),
3838
map(raw_symbol, |_| None),
39-
map(hash_semicolon, |_| None),
39+
map(quote, |_| None),
4040
map(comment, Some),
4141
)),
4242
move || Vec::new_in(allocator.clone()),
@@ -104,16 +104,7 @@ fn expression<A: Allocator + Clone>(input: Input<A>) -> IResult<Expression<A>, A
104104
context(
105105
"quote",
106106
map(
107-
token(positioned(tuple((
108-
recognize(alt((
109-
tag("'"),
110-
tag("`"),
111-
tag(","),
112-
hash_semicolon,
113-
tag("#"),
114-
))),
115-
expression,
116-
)))),
107+
token(positioned(tuple((quote, expression)))),
117108
move |((sign, expression), position)| {
118109
Expression::Quote(&sign, Box::new_in(expression, allocator.clone()), position)
119110
},
@@ -124,6 +115,10 @@ fn expression<A: Allocator + Clone>(input: Input<A>) -> IResult<Expression<A>, A
124115
))(input)
125116
}
126117

118+
fn quote<A: Allocator + Clone>(input: Input<A>) -> IResult<Input<A>, A> {
119+
alt((tag("'"), tag("`"), tag(","), hash_semicolon, tag("#")))(input)
120+
}
121+
127122
fn hash_semicolon<A: Allocator + Clone>(input: Input<A>) -> IResult<Input<A>, A> {
128123
tag("#;")(input)
129124
}
@@ -371,6 +366,28 @@ mod tests {
371366

372367
#[test]
373368
fn parse_vector() {
369+
assert_eq!(
370+
expression(Input::new_extra("#(1 2 3)", Global)).unwrap().1,
371+
Expression::Quote(
372+
"#",
373+
Expression::List(
374+
"(",
375+
")",
376+
vec![
377+
Expression::Symbol("1", Position::new(2, 3)),
378+
Expression::Symbol("2", Position::new(4, 5)),
379+
Expression::Symbol("3", Position::new(6, 7))
380+
],
381+
Position::new(1, 8)
382+
)
383+
.into(),
384+
Position::new(0, 8)
385+
)
386+
);
387+
}
388+
389+
#[test]
390+
fn parse_bracket_vector() {
374391
assert_eq!(
375392
expression(Input::new_extra("[1 2 3]", Global)).unwrap().1,
376393
Expression::List(
@@ -602,5 +619,10 @@ mod tests {
602619
vec![Comment::new("foo", Position::new(3, 7))]
603620
);
604621
}
622+
623+
#[test]
624+
fn parse_comment_with_vector() {
625+
assert_eq!(comments(Input::new_extra("#()", Global)).unwrap().1, vec![]);
626+
}
605627
}
606628
}

0 commit comments

Comments
 (0)