1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::error::{Error, Result};
use crate::iter::Iter;
use proc_macro::{Delimiter, Group, Ident, Literal, Span, TokenTree};

pub fn parse_punct(iter: Iter, ch: char) -> Result<()> {
    match iter.next() {
        Some(TokenTree::Punct(ref punct)) if punct.as_char() == ch => Ok(()),
        unexpected => {
            let span = unexpected
                .as_ref()
                .map_or_else(Span::call_site, TokenTree::span);
            Err(Error::new(span, format!("expected `{}`", ch)))
        }
    }
}

pub fn parse_optional_punct(iter: Iter, ch: char) -> Option<()> {
    match iter.peek() {
        Some(TokenTree::Punct(punct)) if punct.as_char() == ch => iter.next().map(drop),
        _ => None,
    }
}

pub fn parse_optional_keyword(iter: Iter, keyword: &str) -> Option<Span> {
    match iter.peek() {
        Some(TokenTree::Ident(ident)) if ident.to_string() == keyword => {
            Some(iter.next().unwrap().span())
        }
        _ => None,
    }
}

pub fn parse_literal(iter: Iter) -> Result<Literal> {
    match iter.next() {
        Some(TokenTree::Literal(literal)) => Ok(literal),
        unexpected => {
            let span = unexpected
                .as_ref()
                .map_or_else(Span::call_site, TokenTree::span);
            Err(Error::new(span, "expected literal"))
        }
    }
}

pub fn parse_paren(introducer: &Ident, iter: Iter) -> Result<Group> {
    match iter.peek() {
        Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Parenthesis => {
            match iter.next() {
                Some(TokenTree::Group(group)) => Ok(group),
                _ => unreachable!(),
            }
        }
        Some(unexpected) => Err(Error::new(unexpected.span(), "expected `(`")),
        None => Err(Error::new(
            introducer.span(),
            format!("expected `(` after `{}`", introducer),
        )),
    }
}

pub fn parse_optional_paren(iter: Iter) -> Option<Group> {
    match iter.peek() {
        Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Parenthesis => {
            match iter.next() {
                Some(TokenTree::Group(group)) => Some(group),
                _ => unreachable!(),
            }
        }
        _ => None,
    }
}

pub fn parse_end(iter: Iter) -> Result<()> {
    match iter.next() {
        None => Ok(()),
        Some(unexpected) => Err(Error::new(unexpected.span(), "unexpected token")),
    }
}