]> Witch of Git - ess/blob - src/parser.rs
Add unquote on , and unquote-splicing on ,@
[ess] / src / parser.rs
1 //! Functions to parse s-expressions and expression atoms.
2 //!
3 //! This module contains the core parsing machinery.
4 //!
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.
9 //!
10 //! [`parse`]: fn.parse.html
11 //! [`parse_one`]: fn.parse_one.html
12 //! [`ParseResult`]: enum.ParseResult.html
13 //! [`parse_expression`]: fn.parse_expression.html
14
15 use sexp::Sexp;
16 use span::{Span, ByteSpan};
17
18 \f
19 // Parsing Types ///////////////////////////////////////////////////////////////
20
21 /// Represents what to do next in partially completed parsing.
22 ///
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`.
26 ///
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.
34 Done(&'a str, T),
35 /// The parser failed, the `E` represents the reason for the failure.
36 Error(E),
37 }
38
39 /// Indicates how parsing failed.
40 ///
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
49 /// found.
50 UnexpectedEof,
51 /// Some problem occurred while parsing a list, along with the cause of that
52 /// error.
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
65 /// the error.
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),
73 }
74 use self::ParseResult::*;
75
76 \f
77 // Parsing Utilities ///////////////////////////////////////////////////////////
78
79 trait IsDelimeter {
80 fn is_delimiter(&self) -> bool;
81 }
82
83 impl IsDelimeter for char {
84 fn is_delimiter(&self) -> bool {
85 self.is_whitespace() || *self == ';'
86 || *self == '(' || *self == ')'
87 || *self == '[' || *self == ']'
88 || *self == '{' || *self == '}'
89 || *self == '"' || *self == '\''
90 || *self == '`' || *self == ','
91 }
92 }
93
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)
98 } else {
99 return Error($ErrorFn(
100 Box::new(ParseError::UnexpectedEof),
101 ($input.len(), $input.len()).offset($start_loc)));
102 }
103 }
104 }
105
106 \f
107 // Top Level Parsers ///////////////////////////////////////////////////////////
108
109 /// Parse a sequence of s-expressions.
110 ///
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.
114 ///
115 /// # Errors
116 ///
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.
121 ///
122 /// # Examples
123 ///
124 /// We can get useful partial results
125 ///
126 /// ```rust
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());
131 /// ```
132 pub fn parse(mut input: &str) -> (Vec<Sexp>, Option<ParseError>) {
133 let mut start_loc = 0;
134 let mut results = Vec::new();
135 loop {
136 match parse_expression(input, start_loc) {
137 Done(rest, result) => {
138 input = rest;
139 start_loc = result.get_loc().1;
140 results.push(result);
141 if rest.trim() == "" {
142 return (results, None);
143 }
144 }
145 Error(err) => {
146 return (results, Some(err));
147 }
148 }
149 }
150 }
151
152 /// Parses a single s-expression, ignoring any trailing text.
153 ///
154 /// This function returns a pair of the parsed s-expression and the tail of the text.
155 ///
156 /// # Errors
157 ///
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.
161 ///
162 /// # Examples
163 ///
164 /// ```rust
165 /// # use ess::parser::parse_one;
166 /// let (expr, rest) = parse_one("1 (").unwrap();
167 /// assert_eq!(rest, " (");
168 /// ```
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),
173 }
174 }
175
176 \f
177 // Core Parsers ////////////////////////////////////////////////////////////////
178
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.
181
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);
185
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),
191 Some('\'') => {
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);
196 Done(rest,
197 Sexp::List(vec![Sexp::Sym("quote".into(), quote_span), result],
198 quote_span.union(&span)))
199 }
200 err => err,
201 }
202 }
203 Some('`') => {
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);
208 Done(rest,
209 Sexp::List(vec![Sexp::Sym("quasiquote".into(), quote_span),
210 result],
211 quote_span.union(&span)))
212 }
213 err => err,
214 }
215 }
216 Some(',') => {
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);
222 Done(rest,
223 Sexp::List(vec![
224 Sexp::Sym("unquote-splicing".into(), quote_span),
225 result
226 ], quote_span.union(&span)))
227 }
228 err => err,
229 }
230 } else {
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);
235 Done(rest,
236 Sexp::List(vec![
237 Sexp::Sym("unquote".into(), quote_span),
238 result
239 ], quote_span.union(&span)))
240 }
241 err => err,
242 }
243 }
244 }
245 Some(_) => parse_symbol(input, start_loc),
246 None => unreachable!(),
247 }
248 }
249
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);
253
254 match input.chars().nth(0) {
255 Some('(') => (),
256 Some(c) =>
257 return Error(ParseError::List(
258 Box::new(ParseError::Unexpected(c, 0)),
259 (0, 0).offset(start_loc))),
260 None => unreachable!(),
261 }
262
263 let mut input = &input[1..];
264 let mut loc = start_loc + 1;
265 let mut members = Vec::new();
266 loop {
267 {
268 let (new_input, new_loc) = consume_whitespace!(input, loc, ParseError::List);
269 input = new_input;
270 loc = new_loc;
271 }
272
273 match input.chars().nth(0) {
274 Some(')') =>
275 return Done(&input[1..],
276 Sexp::List(members, (start_loc, loc+1))),
277 Some(_) => (),
278 None => unreachable!(),
279 }
280
281 match parse_expression(input, loc) {
282 Done(new_input, member) => {
283 loc = member.get_loc().1;
284 members.push(member);
285 input = new_input;
286 }
287 Error(err) =>
288 return Error(ParseError::List(
289 Box::new(err),
290 (0, 0).offset(loc)))
291 }
292 }
293 }
294
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);
298
299 match input.chars().next() {
300 Some(c) if !c.is_digit(10) => {
301 return Error(ParseError::Number(
302 Box::new(ParseError::Unexpected(c, start_loc)),
303 (0, c.len_utf8()).offset(start_loc)));
304 }
305 None => return Error(ParseError::Number(
306 Box::new(ParseError::UnexpectedEof),
307 (0, 0).offset(start_loc))),
308 _ => (),
309 }
310
311 let base = 10;
312
313 let mut end = 0;
314 // Before the decimal point
315 for (i, c) in input.char_indices() {
316 if c == '.' {
317 end = i + 1;
318 break;
319 }
320
321 if c.is_delimiter() {
322 return Done(&input[i..],
323 Sexp::Int(input[..i].parse().expect("Already matched digits"),
324 (0, i).offset(start_loc)));
325 }
326
327 if !c.is_digit(base) {
328 return Error(ParseError::Number(
329 Box::new(ParseError::Unexpected(c, start_loc + i)),
330 (i, i).offset(start_loc)));
331 }
332
333 end = i + c.len_utf8();
334 }
335
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)));
340 }
341
342 // After the decimal point
343 for (i, c) in input[end..].char_indices() {
344 if c.is_delimiter() {
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)));
348 }
349
350 if !c.is_digit(base) {
351 return Error(ParseError::Number(
352 Box::new(ParseError::Unexpected(c, start_loc + i + end)),
353 (i+end, i+end).offset(start_loc)));
354 }
355 }
356
357 Done(&input[input.len()..],
358 Sexp::Float(input.parse().expect("Already matched digits.digits"),
359 (0, input.len()).offset(start_loc)))
360 }
361
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);
365
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_delimiter() =>
372 return Error(ParseError::Symbol(
373 Box::new(ParseError::Unexpected(c, start_loc)),
374 (0, 0).offset(start_loc))),
375 Some(_) => (),
376 None => unreachable!(),
377 }
378
379 for (i, c) in input.char_indices() {
380 if c.is_delimiter() {
381 return Done(&input[i..],
382 Sexp::Sym(input[..i].into(), (0, i).offset(start_loc)));
383 }
384 }
385
386 Done(&input[input.len()..],
387 Sexp::Sym(input.into(), (0, input.len()).offset(start_loc)))
388 }
389
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);
393
394 match input.chars().next() {
395 Some('"') => (),
396 Some(c) =>
397 return Error(ParseError::String(
398 Box::new(ParseError::Unexpected(c, start_loc)),
399 (0, 0).offset(start_loc))),
400 None => unreachable!(),
401 }
402
403 for (i, c) in input[1..].char_indices() {
404 if c == '"' {
405 return Done(&input[2+i..],
406 Sexp::Str(input[1..i+1].into(), (0, i+2).offset(start_loc)));
407 }
408 }
409
410 Error(ParseError::String(
411 Box::new(ParseError::UnexpectedEof),
412 (0, input.len()).offset(start_loc)))
413 }
414
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);
418
419 match input.chars().nth(0) {
420 Some('#') => (),
421 Some(c) =>
422 return Error(ParseError::Char(
423 Box::new(ParseError::Unexpected(c, start_loc)),
424 (0, 0).offset(start_loc))),
425 None =>
426 return Error(ParseError::Char(
427 Box::new(ParseError::UnexpectedEof),
428 (0, 0).offset(start_loc))),
429 }
430
431 match input.chars().nth(1) {
432 Some('\\') => (),
433 Some(c) =>
434 return Error(ParseError::Char(
435 Box::new(ParseError::Unexpected(c, start_loc + 1)),
436 (1, 1).offset(start_loc))),
437 None =>
438 return Error(ParseError::Char(
439 Box::new(ParseError::UnexpectedEof),
440 (1, 1).offset(start_loc)))
441 }
442
443 match input.chars().nth(2) {
444 Some(c) =>
445 Done(&input[3..], Sexp::Char(c, (0, 3).offset(start_loc))),
446 None =>
447 Error(ParseError::Char(
448 Box::new(ParseError::UnexpectedEof),
449 (2, 2).offset(start_loc)))
450 }
451 }
452
453 \f
454 // Tests ///////////////////////////////////////////////////////////////////////
455
456 #[cfg(test)]
457 mod test {
458 use sexp::Sexp;
459 use span::Span;
460 use parser::*;
461 use parser::ParseResult::*;
462
463 #[test]
464 fn test_parse() {
465 assert_eq!(parse("1 2 3"), (vec![
466 Sexp::Int(1, (0, 1)), Sexp::Int(2, (2, 3)), Sexp::Int(3, (4, 5))
467 ], None));
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)))));
471 }
472
473 #[test]
474 fn test_parse_one() {
475 assert_eq!(parse_one("1 2"),
476 Ok((Sexp::Int(1, (0, 1)), " 2")));
477 }
478
479 #[test]
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)),
498 ], (0, 9))));
499
500 assert_eq!(parse_expression("", 0),
501 Error(ParseError::Sexp(Box::new(ParseError::UnexpectedEof), (0, 0))));
502 }
503
504 #[test]
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)),
510 ], (0, 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)),
515 ], (0, 2))));
516 assert_eq!(parse_expression("' (1 2 3)", 0),
517 Done("", Sexp::List(vec![
518 Sexp::Sym("quote".into(), (0, 1)),
519 Sexp::List(vec![
520 Sexp::Int(1, (3, 4)),
521 Sexp::Int(2, (5, 6)),
522 Sexp::Int(3, (7, 8)),
523 ], (2, 9)),
524 ], (0, 9))));
525
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))));
530 }
531
532 #[test]
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)),
538 ], (0, 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)),
543 ], (0, 2))));
544 assert_eq!(parse_expression("` (1 2 3)", 0),
545 Done("", Sexp::List(vec![
546 Sexp::Sym("quasiquote".into(), (0, 1)),
547 Sexp::List(vec![
548 Sexp::Int(1, (3, 4)),
549 Sexp::Int(2, (5, 6)),
550 Sexp::Int(3, (7, 8)),
551 ], (2, 9)),
552 ], (0, 9))));
553 assert_eq!(parse_expression("`'a", 0),
554 Done("", Sexp::List(vec![
555 Sexp::Sym("quasiquote".into(), (0, 1)),
556 Sexp::List(vec![
557 Sexp::Sym("quote".into(), (1, 2)),
558 Sexp::Sym("a".into(), (2, 3)),
559 ], (1, 3)),
560 ], (0, 3))));
561
562 assert_eq!(parse_expression("`", 0),
563 Error(ParseError::Sexp(Box::new(ParseError::UnexpectedEof), (1, 1))));
564 }
565
566 #[test]
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)),
572 ], (0, 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)),
577 ], (0, 2))));
578 assert_eq!(parse_expression(", (1 2 3)", 0),
579 Done("", Sexp::List(vec![
580 Sexp::Sym("unquote".into(), (0, 1)),
581 Sexp::List(vec![
582 Sexp::Int(1, (3, 4)),
583 Sexp::Int(2, (5, 6)),
584 Sexp::Int(3, (7, 8)),
585 ], (2, 9)),
586 ], (0, 9))));
587 assert_eq!(parse_expression("`,a", 0),
588 Done("", Sexp::List(vec![
589 Sexp::Sym("quasiquote".into(), (0, 1)),
590 Sexp::List(vec![
591 Sexp::Sym("unquote".into(), (1, 2)),
592 Sexp::Sym("a".into(), (2, 3)),
593 ], (1, 3)),
594 ], (0, 3))));
595 assert_eq!(parse_expression("`(,@a)", 0),
596 Done("", Sexp::List(vec![
597 Sexp::Sym("quasiquote".into(), (0, 1)),
598 Sexp::List(vec![
599 Sexp::List(vec![
600 Sexp::Sym("unquote-splicing".into(), (2, 4)),
601 Sexp::Sym("a".into(), (4, 5)),
602 ], (2, 5)),
603 ], (1, 6)),
604 ], (0, 6))));
605
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))));
610 }
611
612 #[test]
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)),
623 ], (2, 17))));
624 }
625
626 #[test]
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))));
644
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))));
651 }
652
653 #[test]
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))));
665
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))));
672
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))));
679 }
680
681 #[test]
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))));
690
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))));
695 }
696
697 #[test]
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))));
702
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))));
711 }
712 }