Skip to content

Commit e3191a0

Browse files
authored
Splicing unquote (raviqqe#54)
- Parse splicing unquote - Fix
1 parent db2c908 commit e3191a0

File tree

1 file changed

+113
-72
lines changed

1 file changed

+113
-72
lines changed

src/parse/parser.rs

Lines changed: 113 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,14 @@ fn expression<A: Allocator + Clone>(input: Input<A>) -> IResult<Expression<A>, A
116116
}
117117

118118
fn quote<A: Allocator + Clone>(input: Input<A>) -> IResult<Input<A>, A> {
119-
alt((tag("'"), tag("`"), tag(","), hash_semicolon, tag("#")))(input)
119+
alt((
120+
tag("'"),
121+
tag("`"),
122+
tag(",@"),
123+
tag(","),
124+
hash_semicolon,
125+
tag("#"),
126+
))(input)
120127
}
121128

122129
fn hash_semicolon<A: Allocator + Clone>(input: Input<A>) -> IResult<Input<A>, A> {
@@ -425,84 +432,118 @@ mod tests {
425432
);
426433
}
427434

428-
#[test]
429-
fn parse_quote() {
430-
assert_eq!(
431-
expression(Input::new_extra("'foo", Global)).unwrap().1,
432-
Expression::Quote(
433-
"'",
434-
Expression::Symbol("foo", Position::new(1, 4)).into(),
435-
Position::new(0, 4)
436-
)
437-
);
438-
}
435+
mod quote {
436+
use super::*;
437+
use pretty_assertions::assert_eq;
439438

440-
#[test]
441-
fn parse_quote_with_correct_position() {
442-
assert_eq!(
443-
expression(Input::new_extra(" 'foo", Global)).unwrap().1,
444-
Expression::Quote(
445-
"'",
446-
Expression::Symbol("foo", Position::new(2, 5)).into(),
447-
Position::new(1, 5)
448-
)
449-
);
450-
}
439+
#[test]
440+
fn parse_quote() {
441+
assert_eq!(
442+
expression(Input::new_extra("'foo", Global)).unwrap().1,
443+
Expression::Quote(
444+
"'",
445+
Expression::Symbol("foo", Position::new(1, 4)).into(),
446+
Position::new(0, 4)
447+
)
448+
);
449+
}
451450

452-
#[test]
453-
fn parse_unquote() {
454-
assert_eq!(
455-
expression(Input::new_extra(",foo", Global)).unwrap().1,
456-
Expression::Quote(
457-
",",
458-
Expression::Symbol("foo", Position::new(1, 4)).into(),
459-
Position::new(0, 4)
460-
)
461-
);
462-
}
451+
#[test]
452+
fn parse_quote_with_correct_position() {
453+
assert_eq!(
454+
expression(Input::new_extra(" 'foo", Global)).unwrap().1,
455+
Expression::Quote(
456+
"'",
457+
Expression::Symbol("foo", Position::new(2, 5)).into(),
458+
Position::new(1, 5)
459+
)
460+
);
461+
}
463462

464-
#[test]
465-
fn parse_hash_quote() {
466-
assert_eq!(
467-
expression(Input::new_extra("#()", Global)).unwrap().1,
468-
Expression::Quote(
469-
"#",
470-
Expression::List("(", ")", vec![], Position::new(1, 3)).into(),
471-
Position::new(0, 3)
472-
)
473-
);
474-
}
463+
#[test]
464+
fn parse_unquote() {
465+
assert_eq!(
466+
expression(Input::new_extra(",foo", Global)).unwrap().1,
467+
Expression::Quote(
468+
",",
469+
Expression::Symbol("foo", Position::new(1, 4)).into(),
470+
Position::new(0, 4)
471+
)
472+
);
473+
}
475474

476-
#[test]
477-
fn parse_hash_semicolon_quote() {
478-
assert_eq!(
479-
expression(Input::new_extra("#;()", Global)).unwrap().1,
480-
Expression::Quote(
481-
"#;",
482-
Expression::List("(", ")", vec![], Position::new(2, 4)).into(),
483-
Position::new(0, 4)
484-
)
485-
);
486-
}
475+
#[test]
476+
fn parse_hash_quote() {
477+
assert_eq!(
478+
expression(Input::new_extra("#()", Global)).unwrap().1,
479+
Expression::Quote(
480+
"#",
481+
Expression::List("(", ")", vec![], Position::new(1, 3)).into(),
482+
Position::new(0, 3)
483+
)
484+
);
485+
}
487486

488-
#[test]
489-
fn parse_shebang() {
490-
assert_eq!(
491-
hash_directive(Input::new_extra("#!/bin/sh\n", Global))
492-
.unwrap()
493-
.1,
494-
HashDirective::new("!/bin/sh", Position::new(0, 9))
495-
);
487+
#[test]
488+
fn parse_hash_semicolon_quote() {
489+
assert_eq!(
490+
expression(Input::new_extra("#;()", Global)).unwrap().1,
491+
Expression::Quote(
492+
"#;",
493+
Expression::List("(", ")", vec![], Position::new(2, 4)).into(),
494+
Position::new(0, 4)
495+
)
496+
);
497+
}
498+
499+
#[test]
500+
fn parse_quasi_quote() {
501+
assert_eq!(
502+
expression(Input::new_extra("`foo", Global)).unwrap().1,
503+
Expression::Quote(
504+
"`",
505+
Expression::Symbol("foo", Position::new(1, 4)).into(),
506+
Position::new(0, 4)
507+
)
508+
);
509+
}
510+
511+
#[test]
512+
fn parse_splicing_unquote() {
513+
assert_eq!(
514+
expression(Input::new_extra(",@foo", Global)).unwrap().1,
515+
Expression::Quote(
516+
",@",
517+
Expression::Symbol("foo", Position::new(2, 5)).into(),
518+
Position::new(0, 5)
519+
)
520+
);
521+
}
496522
}
497523

498-
#[test]
499-
fn parse_lang_directive() {
500-
assert_eq!(
501-
hash_directive(Input::new_extra("#lang r7rs\n", Global))
502-
.unwrap()
503-
.1,
504-
HashDirective::new("lang r7rs", Position::new(0, 10))
505-
);
524+
mod hash_directive {
525+
use super::*;
526+
use pretty_assertions::assert_eq;
527+
528+
#[test]
529+
fn parse_shebang() {
530+
assert_eq!(
531+
hash_directive(Input::new_extra("#!/bin/sh\n", Global))
532+
.unwrap()
533+
.1,
534+
HashDirective::new("!/bin/sh", Position::new(0, 9))
535+
);
536+
}
537+
538+
#[test]
539+
fn parse_lang_directive() {
540+
assert_eq!(
541+
hash_directive(Input::new_extra("#lang r7rs\n", Global))
542+
.unwrap()
543+
.1,
544+
HashDirective::new("lang r7rs", Position::new(0, 10))
545+
);
546+
}
506547
}
507548

508549
mod string {

0 commit comments

Comments
 (0)