]>
Witch of Git - ess/blob - src/parser.rs
1 //! Functions to parse s-expressions and expression atoms.
3 //! This module contains the core parsing machinery.
5 //! * If you're interested in getting a parsed s-expression that you can use,
6 //! then looking at [`parse`] and [`parse_one`] are your best bet.
7 //! * If you want to write your own parsers that contain s-expressions,
8 //! [`ParseResult`] and [`parse_expression`] will be the most useful to you.
10 //! [`parse`]: fn.parse.html
11 //! [`parse_one`]: fn.parse_one.html
12 //! [`ParseResult`]: enum.ParseResult.html
13 //! [`parse_expression`]: fn.parse_expression.html
16 use span
::{Span
, ByteSpan
};
19 // Parsing Types ///////////////////////////////////////////////////////////////
21 /// Represents what to do next in partially completed parsing.
23 /// `ParseResult` is returned from all intermediate parsers. If you just want to
24 /// get back parsed s-expressions, you won't need to worry about this type since
25 /// the top level parsers just return a `Result`.
27 /// If the parser failed to produce a result, it will return `Error`, and if it
28 /// succeeded we'll get the `Done` variant containing the value produced and the
29 /// rest of the text to work on.
30 #[derive(Debug, PartialEq, Eq, Clone)]
31 pub enum ParseResult
<'a
, T
, E
> {
32 /// The parser succeeded, this contains first the un-consumed portion of the
33 /// input then the result produced by parsing.
35 /// The parser failed, the `E` represents the reason for the failure.
39 /// Indicates how parsing failed.
41 /// Most `ParseError` variants contain a `Box<ParseError>` that represents the
42 /// cause of that error. Using this, `ParseError` variants can be chained to
43 /// produce a more complete picture of what exactly went wrong during parsing.
44 #[derive(Debug, PartialEq, Eq, Clone)]
45 pub enum ParseError
<Loc
=ByteSpan
> where Loc
: Span
{
46 /// Parsing reached the end of input where not expecting to, usually this
47 /// will be contained inside another `ParseError` like `String(box
48 /// UnexpectedEof, ...)` which indicates that the closing quote was never
51 /// Some problem occurred while parsing a list, along with the cause of that
53 List(Box
<ParseError
>, Loc
),
54 /// Some problem occurred while parsing an s-expression. This will only be
55 /// generated if EOF is reached unexpectedly at the beginning of
56 /// `parse_expression`, so it should probably be removed.
57 Sexp(Box
<ParseError
>, Loc
),
58 /// Some problem occurred while parsing a character literal, along with the
59 /// cause of the error.
60 Char(Box
<ParseError
>, Loc
),
61 /// Some problem occurred while parsing a string literal, along with the
62 /// cause of the error.
63 String(Box
<ParseError
>, Loc
),
64 /// Some problem occurred while parsing a symbol, along with the cause of
66 Symbol(Box
<ParseError
>, Loc
),
67 /// Some problem occurred while parsing a number literal, along with the
68 /// cause of the error.
69 Number(Box
<ParseError
>, Loc
),
70 /// An unexpected character was found. This will usually be the root cause
71 /// in some chain of `ParseError`s.
72 Unexpected(char, Loc
::Begin
),
74 use self::ParseResult
::*;
77 // Parsing Utilities ///////////////////////////////////////////////////////////
80 fn is_delimiter(&self) -> bool
;
83 impl IsDelimeter
for char {
84 fn is_delimiter(&self) -> bool
{
85 self.is
_wh
itespace
() || *self == '
;'
86 || *self == '
('
|| *self == '
)'
87 || *self == '
['
|| *self == '
]'
88 || *self == '
{'
|| *self == '
}'
89 || *self == '
"' || *self == '\''
90 || *self == '`' || *self == ','
94 macro_rules! consume_whitespace {
95 ($input:expr, $start_loc:expr, $ErrorFn:expr) => {
96 if let Some(pos) = $input.find(|c: char| !c.is_whitespace()) {
97 (&$input[pos..], $start_loc + pos)
99 return Error($ErrorFn(
100 Box::new(ParseError::UnexpectedEof),
101 ($input.len(), $input.len()).offset($start_loc)));
107 // Top Level Parsers ///////////////////////////////////////////////////////////
109 /// Parse a sequence of s-expressions.
111 /// This function returns `(Vec<Sexp>, Option<ParseError>)` so that it can
112 /// return partial results, for when some component parses successfully and a
113 /// later part fails.
117 /// If the text contains an invalid s-expression (imbalanced parenthesis,
118 /// quotes, invalid numbers like 123q, etc.) then the parser will stop and
119 /// return an error. Every s-expression before that point that successfully
120 /// parsed will still be returned.
124 /// We can get useful partial results
127 /// # use ess::parser::parse;
128 /// let (exprs, err) = parse("1 2 3 ( 4");
129 /// assert_eq!(exprs.len(), 3);
130 /// assert!(err.is_some());
132 pub fn parse(mut input: &str) -> (Vec<Sexp>, Option<ParseError>) {
133 let mut start_loc = 0;
134 let mut results = Vec::new();
136 match parse_expression(input, start_loc) {
137 Done(rest, result) => {
139 start_loc = result.get_loc().1;
140 results.push(result);
141 if rest.trim() == "" {
142 return (results, None);
146 return (results, Some(err));
152 /// Parses a single s-expression, ignoring any trailing text.
154 /// This function returns a pair of the parsed s-expression and the tail of the text.
158 /// If the text begins with an invalid s-expression (imbalanced parenthesis,
159 /// quotes, invalid numbers like 123q, etc.) then the parser will return an
160 /// error. Any text after the first s-expression doesn't affect the parsing.
165 /// # use ess::parser::parse_one;
166 /// let (expr, rest) = parse_one("1 (").unwrap();
167 /// assert_eq!(rest, " (");
169 pub fn parse_one(input: &str) -> Result<(Sexp, &str), ParseError> {
170 match parse_expression(input, 0) {
171 Done(rest, result) => Ok((result, rest)),
172 Error(err) => Err(err),
177 // Core Parsers ////////////////////////////////////////////////////////////////
179 // TODO: All of these parsers deserve docs, but since they're somewhat internal
180 // parsers, it's less critical than the rest of the API.
182 #[allow(missing_docs)]
183 pub fn parse_expression(input: &str, start_loc: usize) -> ParseResult<Sexp, ParseError> {
184 let (input, start_loc) = consume_whitespace!(input, start_loc, ParseError::Sexp);
186 match input.chars().next() {
187 Some('0'...'9') => parse_number(input, start_loc),
188 Some('(') => parse_list(input, start_loc),
189 Some('#') => parse_character(input, start_loc),
190 Some('"'
) => parse_string(input
, start_loc
),
192 match parse_expression(&input
[1..], start_loc
+ 1) {
193 Done(rest
, result
) => {
194 let span
= *result
.get_loc();
195 let quote_span
= (0, 1).offset(start_loc
);
197 Sexp
::List(vec
![Sexp
::Sym("quote".into
(), quote_span
), result
],
198 quote_span
.un
ion
(&span
)))
204 match parse_expression(&input
[1..], start_loc
+ 1) {
205 Done(rest
, result
) => {
206 let span
= *result
.get_loc();
207 let quote_span
= (0, 1).offset(start_loc
);
209 Sexp
::List(vec
![Sexp
::Sym("quasiquote".into
(), quote_span
),
211 quote_span
.un
ion
(&span
)))
217 if input
[1..].chars().next() == Some('@'
) {
218 match parse_expression(&input
[2..], start_loc
+ 2) {
219 Done(rest
, result
) => {
220 let span
= *result
.get_loc();
221 let quote_span
= (0, 2).offset(start_loc
);
224 Sexp
::Sym("unquote-splicing".into
(), quote_span
),
226 ], quote_span
.un
ion
(&span
)))
231 match parse_expression(&input
[1..], start_loc
+ 1) {
232 Done(rest
, result
) => {
233 let span
= *result
.get_loc();
234 let quote_span
= (0, 1).offset(start_loc
);
237 Sexp
::Sym("unquote".into
(), quote_span
),
239 ], quote_span
.un
ion
(&span
)))
245 Some(_
) => parse_symbol(input
, start_loc
),
246 None
=> unreachable
!(),
250 #[allow(missing_docs)]
251 pub fn parse_list(input
: &str, start_loc
: usize) -> ParseResult
<Sexp
, ParseError
> {
252 let (input
, start_loc
) = consume_whitespace
!(input
, start_loc
, ParseError
::List
);
254 match input
.chars().nth(0) {
257 return Error(ParseError
::List(
258 Box
::new(ParseError
::Unexpected(c
, 0)),
259 (0, 0).offset(start_loc
))),
260 None
=> unreachable
!(),
263 let mut input
= &input
[1..];
264 let mut loc
= start_loc
+ 1;
265 let mut members
= Vec
::new();
268 let (new_input
, new_loc
) = consume_whitespace
!(input
, loc
, ParseError
::List
);
273 match input
.chars().nth(0) {
275 return Done(&input
[1..],
276 Sexp
::List(members
, (start_loc
, loc
+1))),
278 None
=> unreachable
!(),
281 match parse_expression(input
, loc
) {
282 Done(new_input
, member
) => {
283 loc
= member
.get_loc().1;
284 members
.push(member
);
288 return Error(ParseError
::List(
295 #[allow(missing_docs)]
296 pub fn parse_number(input
: &str, start_loc
: usize) -> ParseResult
<Sexp
, ParseError
> {
297 let (input
, start_loc
) = consume_whitespace
!(input
, start_loc
, ParseError
::Number
);
299 match input
.chars().next() {
300 Some(c
) if !c
.is
_d
ig
it
(10) => {
301 return Error(ParseError
::Number(
302 Box
::new(ParseError
::Unexpected(c
, start_loc
)),
303 (0, c
.len_utf8()).offset(start_loc
)));
305 None
=> return Error(ParseError
::Number(
306 Box
::new(ParseError
::UnexpectedEof
),
307 (0, 0).offset(start_loc
))),
314 // Before the decimal point
315 for (i
, c
) in input
.char_indices() {
321 if c
.is
_del
im
iter
() {
322 return Done(&input
[i
..],
323 Sexp
::Int(input
[..i].parse().expect("Already matched digits"),
324 (0, i
).offset(start_loc
)));
327 if !c
.is
_d
ig
it
(base
) {
328 return Error(ParseError
::Number(
329 Box
::new(ParseError
::Unexpected(c
, start_loc
+ i
)),
330 (i
, i
).offset(start_loc
)));
333 end
= i
+ c
.len_utf8();
336 if input
[end
..].is
_empty
() {
337 return Done(&input
[end
..],
338 Sexp
::Int(input
.parse().expect("Already matched digits"),
339 (0, end
).offset(start_loc
)));
342 // After the decimal point
343 for (i
, c
) in input
[end
..].char_indices() {
344 if c
.is
_del
im
iter
() {
345 return Done(&input
[i
+end
..],
346 Sexp
::Float(input
[..end
+i
].parse().expect("Already matched digits.digits"),
347 (0, end
+i
).offset(start_loc
)));
350 if !c
.is
_d
ig
it
(base
) {
351 return Error(ParseError
::Number(
352 Box
::new(ParseError
::Unexpected(c
, start_loc
+ i
+ end
)),
353 (i
+end
, i
+end
).offset(start_loc
)));
357 Done(&input
[input
.len()..],
358 Sexp
::Float(input
.parse().expect("Already matched digits.digits"),
359 (0, input
.len()).offset(start_loc
)))
362 #[allow(missing_docs)]
363 pub fn parse_symbol(input
: &str, start_loc
: usize) -> ParseResult
<Sexp
, ParseError
> {
364 let (input
, start_loc
) = consume_whitespace
!(input
, start_loc
, ParseError
::Symbol
);
366 match input
.chars().next() {
367 Some(c@'
#') | Some(c@':') | Some(c@'0'...'9') =>
368 return Error(ParseError
::Symbol(
369 Box
::new(ParseError
::Unexpected(c
, start_loc
)),
370 (0, 0).offset(start_loc
))),
371 Some(c
) if c
.is
_del
im
iter
() =>
372 return Error(ParseError
::Symbol(
373 Box
::new(ParseError
::Unexpected(c
, start_loc
)),
374 (0, 0).offset(start_loc
))),
376 None
=> unreachable
!(),
379 for (i
, c
) in input
.char_indices() {
380 if c
.is
_del
im
iter
() {
381 return Done(&input
[i
..],
382 Sexp
::Sym(input
[..i].into
(), (0, i
).offset(start_loc
)));
386 Done(&input
[input
.len()..],
387 Sexp
::Sym(input
.into
(), (0, input
.len()).offset(start_loc
)))
390 #[allow(missing_docs)]
391 pub fn parse_string(input
: &str, start_loc
: usize) -> ParseResult
<Sexp
, ParseError
> {
392 let (input
, start_loc
) = consume_whitespace
!(input
, start_loc
, ParseError
::String
);
394 match input
.chars().next() {
397 return Error(ParseError::String(
398 Box::new(ParseError::Unexpected(c, start_loc)),
399 (0, 0).offset(start_loc))),
400 None => unreachable!(),
403 for (i, c) in input[1..].char_indices() {
405 return Done(&input
[2+i
..],
406 Sexp
::Str(input
[1..i+1].into
(), (0, i
+2).offset(start_loc
)));
410 Error(ParseError
::String(
411 Box
::new(ParseError
::UnexpectedEof
),
412 (0, input
.len()).offset(start_loc
)))
415 #[allow(missing_docs)]
416 pub fn parse_character(input
: &str, start_loc
: usize) -> ParseResult
<Sexp
, ParseError
> {
417 let (input
, start_loc
) = consume_whitespace
!(input
, start_loc
, ParseError
::Char
);
419 match input
.chars().nth(0) {
422 return Error(ParseError
::Char(
423 Box
::new(ParseError
::Unexpected(c
, start_loc
)),
424 (0, 0).offset(start_loc
))),
426 return Error(ParseError
::Char(
427 Box
::new(ParseError
::UnexpectedEof
),
428 (0, 0).offset(start_loc
))),
431 match input
.chars().nth(1) {
434 return Error(ParseError
::Char(
435 Box
::new(ParseError
::Unexpected(c
, start_loc
+ 1)),
436 (1, 1).offset(start_loc
))),
438 return Error(ParseError
::Char(
439 Box
::new(ParseError
::UnexpectedEof
),
440 (1, 1).offset(start_loc
)))
443 match input
.chars().nth(2) {
445 Done(&input
[3..], Sexp
::Char(c
, (0, 3).offset(start_loc
))),
447 Error(ParseError
::Char(
448 Box
::new(ParseError
::UnexpectedEof
),
449 (2, 2).offset(start_loc
)))
454 // Tests ///////////////////////////////////////////////////////////////////////
461 use parser
::ParseResult
::*;
465 assert_eq
!(parse("1 2 3"), (vec
![
466 Sexp
::Int(1, (0, 1)), Sexp
::Int(2, (2, 3)), Sexp
::Int(3, (4, 5))
468 assert_eq
!(parse("1 2 )"), (vec
![
469 Sexp
::Int(1, (0, 1)), Sexp
::Int(2, (2, 3))
470 ], Some(ParseError
::Symbol(Box
::new(ParseError
::Unexpected('
)'
, 4)), (4, 4)))));
474 fn test_parse_one() {
475 assert_eq
!(parse_one("1 2"),
476 Ok((Sexp
::Int(1, (0, 1)), " 2")));
480 fn test_parse_expression() {
481 assert_eq
!(parse_expression(" 1", 0),
482 Done("", Sexp
::Int(1, (1, 2))));
483 assert_eq
!(parse_expression("2.2", 0),
484 Done("", Sexp
::Float(2.2, (0, 3))));
485 assert_eq
!(parse_expression(" a", 0),
486 Done("", Sexp
::Sym("a".into
(), (1, 2))));
487 assert_eq
!(parse_expression("#\\c", 0),
488 Done("", Sexp
::Char('c'
, (0, 3))));
489 assert_eq
!(parse_expression(r
#""hi""#, 0),
490 Done("", Sexp
::Str("hi".into
(), (0, 4))));
491 assert_eq
!(parse_expression("()", 0),
492 Done("", Sexp
::List(vec
![], (0, 2))));
493 assert_eq
!(parse_expression("( 1 2 3 )", 0),
494 Done("", Sexp
::List(vec
![
495 Sexp
::Int(1, (2, 3)),
496 Sexp
::Int(2, (4, 5)),
497 Sexp
::Int(3, (6, 7)),
500 assert_eq
!(parse_expression("", 0),
501 Error(ParseError
::Sexp(Box
::new(ParseError
::UnexpectedEof
), (0, 0))));
505 fn test_parse_expr_quote() {
506 assert_eq
!(parse_expression("'a", 0),
507 Done("", Sexp
::List(vec
![
508 Sexp
::Sym("quote".into
(), (0, 1)),
509 Sexp
::Sym("a".into
(), (1, 2)),
511 assert_eq
!(parse_expression("'1", 0),
512 Done("", Sexp
::List(vec
![
513 Sexp
::Sym("quote".into
(), (0, 1)),
514 Sexp
::Int(1, (1, 2)),
516 assert_eq
!(parse_expression("' (1 2 3)", 0),
517 Done("", Sexp
::List(vec
![
518 Sexp
::Sym("quote".into
(), (0, 1)),
520 Sexp
::Int(1, (3, 4)),
521 Sexp
::Int(2, (5, 6)),
522 Sexp
::Int(3, (7, 8)),
526 assert_eq
!(parse_expression("'", 0),
527 Error(ParseError
::Sexp(Box
::new(ParseError
::UnexpectedEof
), (1, 1))));
528 assert_eq
!(parse_expression("`'", 0),
529 Error(ParseError
::Sexp(Box
::new(ParseError
::UnexpectedEof
), (2, 2))));
533 fn test_parse_expr_quasiquote() {
534 assert_eq
!(parse_expression("`a", 0),
535 Done("", Sexp
::List(vec
![
536 Sexp
::Sym("quasiquote".into
(), (0, 1)),
537 Sexp
::Sym("a".into
(), (1, 2)),
539 assert_eq
!(parse_expression("`1", 0),
540 Done("", Sexp
::List(vec
![
541 Sexp
::Sym("quasiquote".into
(), (0, 1)),
542 Sexp
::Int(1, (1, 2)),
544 assert_eq
!(parse_expression("` (1 2 3)", 0),
545 Done("", Sexp
::List(vec
![
546 Sexp
::Sym("quasiquote".into
(), (0, 1)),
548 Sexp
::Int(1, (3, 4)),
549 Sexp
::Int(2, (5, 6)),
550 Sexp
::Int(3, (7, 8)),
553 assert_eq
!(parse_expression("`'a", 0),
554 Done("", Sexp
::List(vec
![
555 Sexp
::Sym("quasiquote".into
(), (0, 1)),
557 Sexp
::Sym("quote".into
(), (1, 2)),
558 Sexp
::Sym("a".into
(), (2, 3)),
562 assert_eq
!(parse_expression("`", 0),
563 Error(ParseError
::Sexp(Box
::new(ParseError
::UnexpectedEof
), (1, 1))));
567 fn test_parse_expr_unquote() {
568 assert_eq
!(parse_expression(",a", 0),
569 Done("", Sexp
::List(vec
![
570 Sexp
::Sym("unquote".into
(), (0, 1)),
571 Sexp
::Sym("a".into
(), (1, 2)),
573 assert_eq
!(parse_expression(",1", 0),
574 Done("", Sexp
::List(vec
![
575 Sexp
::Sym("unquote".into
(), (0, 1)),
576 Sexp
::Int(1, (1, 2)),
578 assert_eq
!(parse_expression(", (1 2 3)", 0),
579 Done("", Sexp
::List(vec
![
580 Sexp
::Sym("unquote".into
(), (0, 1)),
582 Sexp
::Int(1, (3, 4)),
583 Sexp
::Int(2, (5, 6)),
584 Sexp
::Int(3, (7, 8)),
587 assert_eq
!(parse_expression("`,a", 0),
588 Done("", Sexp
::List(vec
![
589 Sexp
::Sym("quasiquote".into
(), (0, 1)),
591 Sexp
::Sym("unquote".into
(), (1, 2)),
592 Sexp
::Sym("a".into
(), (2, 3)),
595 assert_eq
!(parse_expression("`(,@a)", 0),
596 Done("", Sexp
::List(vec
![
597 Sexp
::Sym("quasiquote".into
(), (0, 1)),
600 Sexp
::Sym("unquote-splicing".into
(), (2, 4)),
601 Sexp
::Sym("a".into
(), (4, 5)),
606 assert_eq
!(parse_expression(",", 0),
607 Error(ParseError
::Sexp(Box
::new(ParseError
::UnexpectedEof
), (1, 1))));
608 assert_eq
!(parse_expression(",@", 0),
609 Error(ParseError
::Sexp(Box
::new(ParseError
::UnexpectedEof
), (2, 2))));
613 fn test_parse_list() {
614 assert_eq
!(parse_list("()", 0),
615 Done("", Sexp
::List(vec
![], (0, 2))));
616 assert_eq
!(parse_list("(1)", 0),
617 Done("", Sexp
::List(vec
![Sexp
::Int(1, (1, 2))], (0, 3))));
618 assert_eq
!(parse_list(" ( 1 2 3 a )", 0), Done("", Sexp
::List(vec
![
619 Sexp
::Int(1, (4, 5)),
620 Sexp
::Int(2, (9, 10)),
621 Sexp
::Int(3, (12, 13)),
622 Sexp
::Sym("a".into
(), (14, 15)),
627 fn test_parse_number() {
628 assert_eq
!(parse_number("1", 0),
629 Done("", Sexp
::Int(1, (0, 1))));
630 assert_eq
!(parse_number(" 13", 0),
631 Done("", Sexp
::Int(13, (1, 3))));
632 assert_eq
!(parse_number("1.2", 0),
633 Done("", Sexp
::Float(1.2, (0, 3))));
634 assert_eq
!(parse_number("\u{3000}4.2", 0),
635 Done("", Sexp
::Float(4.2, (0, 3).offset('
\u{3000}'
.len_utf8()))));
636 assert_eq
!(parse_number(" 42 ", 0),
637 Done(" ", Sexp
::Int(42, (2, 4))));
638 assert_eq
!(parse_number(" 4.2 ", 0),
639 Done(" ", Sexp
::Float(4.2, (1, 4))));
640 assert_eq
!(parse_number("1()", 0),
641 Done("()", Sexp
::Int(1, (0, 1))));
642 assert_eq
!(parse_number("3.6()", 0),
643 Done("()", Sexp
::Float(3.6, (0, 3))));
645 assert_eq
!(parse_number("", 0),
646 Error(ParseError
::Number(Box
::new(ParseError
::UnexpectedEof
), (0, 0))));
647 assert_eq
!(parse_number("123a", 0),
648 Error(ParseError
::Number(Box
::new(ParseError
::Unexpected('a'
, 3)), (3, 3))));
649 assert_eq
!(parse_number("66.6+", 0),
650 Error(ParseError
::Number(Box
::new(ParseError
::Unexpected('
+'
, 4)), (4, 4))));
654 fn test_parse_ident() {
655 assert_eq
!(parse_symbol("+", 0),
656 Done("", Sexp
::Sym("+".into
(), (0, 1))));
657 assert_eq
!(parse_symbol(" nil?", 0),
658 Done("", Sexp
::Sym("nil?".into
(), (1, 5))));
659 assert_eq
!(parse_symbol(" ->socket", 0),
660 Done("", Sexp
::Sym("->socket".into
(), (1, 9))));
661 assert_eq
!(parse_symbol("fib(", 0),
662 Done("(", Sexp
::Sym("fib".into
(), (0, 3))));
663 assert_eq
!(parse_symbol("foo2", 0),
664 Done("", Sexp
::Sym("foo2".into
(), (0, 4))));
666 // We reserve #foo for the implementation to do as it wishes
667 assert_eq
!(parse_symbol("#hi", 0),
668 Error(ParseError
::Symbol(Box
::new(ParseError
::Unexpected('
#', 0)), (0, 0))));
669 // We reserve :foo for keywords
670 assert_eq
!(parse_symbol(":hi", 0),
671 Error(ParseError
::Symbol(Box
::new(ParseError
::Unexpected('
:'
, 0)), (0, 0))));
673 assert_eq
!(parse_symbol("", 0),
674 Error(ParseError
::Symbol(Box
::new(ParseError
::UnexpectedEof
), (0, 0))));
675 assert_eq
!(parse_symbol("0", 0),
676 Error(ParseError
::Symbol(Box
::new(ParseError
::Unexpected('
0'
, 0)), (0, 0))));
677 assert_eq
!(parse_symbol("()", 0),
678 Error(ParseError
::Symbol(Box
::new(ParseError
::Unexpected('
('
, 0)), (0, 0))));
682 fn test_parse_string() {
683 assert_eq
!(parse_string(r
#""""#, 0),
684 Done("", Sexp
::Str("".into
(), (0, 2))));
685 assert_eq
!(parse_string(r
#""hello""#, 0),
686 Done("", Sexp
::Str("hello".into
(), (0, 7))));
687 assert_eq
!(parse_string(r
#" "this is a nice string
688 with
0123 things
in it
""#, 0),
689 Done("", Sexp
::Str("this is a nice string\nwith 0123 things in it".into
(), (2, 48))));
691 assert_eq
!(parse_string("", 0),
692 Error(ParseError
::String(Box
::new(ParseError
::UnexpectedEof
), (0, 0))));
693 assert_eq
!(parse_string(r
#""hi"#, 0),
694 Error(ParseError::String(Box::new(ParseError::UnexpectedEof), (0, 3))));
698 fn test_parse_char() {
699 assert_eq!(parse_character(r#"#\""#, 0), Done("", Sexp::Char('"', (0, 3))));
700 assert_eq!(parse_character(r#"#\ "#, 0), Done("", Sexp::Char(' ', (0, 3))));
701 assert_eq!(parse_character(r#" #\\"#, 0), Done("", Sexp::Char('\\', (2, 5))));
703 assert_eq!(parse_character("", 0),
704 Error(ParseError::Char(Box::new(ParseError::UnexpectedEof), (0, 0))));
705 assert_eq!(parse_character("#", 0),
706 Error(ParseError::Char(Box::new(ParseError::UnexpectedEof), (1, 1))));
707 assert_eq!(parse_character("#\\", 0),
708 Error(ParseError::Char(Box::new(ParseError::UnexpectedEof), (2, 2))));
709 assert_eq!(parse_character("a", 0),
710 Error(ParseError::Char(Box::new(ParseError::Unexpected('a', 0)), (0, 0))));