• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::error::{Error, Result};
2 use crate::iter::Iter;
3 use proc_macro::{Delimiter, Group, Ident, Literal, Span, TokenTree};
4 
parse_punct(iter: Iter, ch: char) -> Result<()>5 pub fn parse_punct(iter: Iter, ch: char) -> Result<()> {
6     match iter.next() {
7         Some(TokenTree::Punct(ref punct)) if punct.as_char() == ch => Ok(()),
8         unexpected => {
9             let span = unexpected
10                 .as_ref()
11                 .map_or_else(Span::call_site, TokenTree::span);
12             Err(Error::new(span, format!("expected `{}`", ch)))
13         }
14     }
15 }
16 
parse_optional_punct(iter: Iter, ch: char) -> Option<()>17 pub fn parse_optional_punct(iter: Iter, ch: char) -> Option<()> {
18     match iter.peek() {
19         Some(TokenTree::Punct(punct)) if punct.as_char() == ch => iter.next().map(drop),
20         _ => None,
21     }
22 }
23 
parse_optional_keyword(iter: Iter, keyword: &str) -> Option<Span>24 pub fn parse_optional_keyword(iter: Iter, keyword: &str) -> Option<Span> {
25     match iter.peek() {
26         Some(TokenTree::Ident(ident)) if ident.to_string() == keyword => {
27             Some(iter.next().unwrap().span())
28         }
29         _ => None,
30     }
31 }
32 
parse_literal(iter: Iter) -> Result<Literal>33 pub fn parse_literal(iter: Iter) -> Result<Literal> {
34     match iter.next() {
35         Some(TokenTree::Literal(literal)) => Ok(literal),
36         unexpected => {
37             let span = unexpected
38                 .as_ref()
39                 .map_or_else(Span::call_site, TokenTree::span);
40             Err(Error::new(span, "expected literal"))
41         }
42     }
43 }
44 
parse_paren(introducer: &Ident, iter: Iter) -> Result<Group>45 pub fn parse_paren(introducer: &Ident, iter: Iter) -> Result<Group> {
46     match iter.peek() {
47         Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Parenthesis => {
48             match iter.next() {
49                 Some(TokenTree::Group(group)) => Ok(group),
50                 _ => unreachable!(),
51             }
52         }
53         Some(unexpected) => Err(Error::new(unexpected.span(), "expected `(`")),
54         None => Err(Error::new(
55             introducer.span(),
56             format!("expected `(` after `{}`", introducer),
57         )),
58     }
59 }
60 
parse_optional_paren(iter: Iter) -> Option<Group>61 pub fn parse_optional_paren(iter: Iter) -> Option<Group> {
62     match iter.peek() {
63         Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Parenthesis => {
64             match iter.next() {
65                 Some(TokenTree::Group(group)) => Some(group),
66                 _ => unreachable!(),
67             }
68         }
69         _ => None,
70     }
71 }
72 
parse_end(iter: Iter) -> Result<()>73 pub fn parse_end(iter: Iter) -> Result<()> {
74     match iter.next() {
75         None => Ok(()),
76         Some(unexpected) => Err(Error::new(unexpected.span(), "unexpected token")),
77     }
78 }
79