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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! A collection of parsers for `Token`s (similar to `combine::parser::{char, byte, item}`).

use crate::input::Token;
use combine::{ParseError, Parser, Stream, StreamOnce};
use combine::error::{ConsumedResult, FastResult::*, Info, Tracked};
use combine::stream::uncons;
use std::marker::PhantomData;

/// Parses an ident token and returns the inner `proc_macro::Ident`.
pub fn ident<I>() -> Ident<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    Ident(PhantomData)
}

#[derive(Copy, Clone)]
/// Represents the return type of `ident`.
pub struct Ident<I>(PhantomData<fn(I) -> I>)
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>;

impl<I> Parser for Ident<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    type Input = I;
    type Output = proc_macro2::Ident;
    type PartialState = ();

    fn parse_lazy(&mut self, input: &mut Self::Input) -> ConsumedResult<Self::Output, Self::Input> {
        let position = input.position();
        match uncons(input) {
            EmptyOk(tok) | ConsumedOk(tok) => match tok {
                Token::Ident(ident) => ConsumedOk(ident),
                _ => EmptyErr(I::Error::empty(position).into()),
            },
            EmptyErr(err) => EmptyErr(err),
            ConsumedErr(err) => ConsumedErr(err),
        }
    }

    fn add_error(&mut self, errors: &mut Tracked<<Self::Input as StreamOnce>::Error>) {
        errors.error.add_expected(Info::Borrowed("IDENT"));
    }
}

/// Parses an ident token and succeeds if the ident is equal to `word`.
pub fn keyword<I>(word: &'static str) -> Keyword<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    Keyword(word, PhantomData)
}

#[derive(Copy, Clone)]
/// Represents the return type of `keyword`.
pub struct Keyword<I>(&'static str, PhantomData<fn(I) -> I>)
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>;

impl<I> Parser for Keyword<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    type Input = I;
    type Output = Token;
    type PartialState = ();

    fn parse_lazy(&mut self, input: &mut Self::Input) -> ConsumedResult<Self::Output, Self::Input> {
        let position = input.position();
        match uncons(input) {
            EmptyOk(tok) | ConsumedOk(tok) => match tok {
                Token::Ident(ref ident) if ident.to_string() == self.0 => ConsumedOk(tok),
                _ => EmptyErr(I::Error::empty(position).into()),
            },
            EmptyErr(err) => EmptyErr(err),
            ConsumedErr(err) => ConsumedErr(err),
        }
    }

    fn add_error(&mut self, errors: &mut Tracked<<Self::Input as StreamOnce>::Error>) {
        errors.error.add_expected(Info::Borrowed(self.0));
    }
}

/// Parses a literal token (e.g. string, number, etc) and returns the inner `proc_macro::Literal`.
pub fn literal<I>() -> Literal<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    Literal(PhantomData)
}

#[derive(Copy, Clone)]
/// Represents the return type of `literal`.
pub struct Literal<I>(PhantomData<fn(I) -> I>)
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>;

impl<I> Parser for Literal<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    type Input = I;
    type Output = proc_macro2::Literal;
    type PartialState = ();

    fn parse_lazy(&mut self, input: &mut Self::Input) -> ConsumedResult<Self::Output, Self::Input> {
        let position = input.position();
        match uncons(input) {
            EmptyOk(tok) | ConsumedOk(tok) => match tok {
                Token::Literal(lit) => ConsumedOk(lit),
                _ => EmptyErr(I::Error::empty(position).into()),
            },
            EmptyErr(err) => EmptyErr(err),
            ConsumedErr(err) => ConsumedErr(err),
        }
    }

    fn add_error(&mut self, errors: &mut Tracked<<Self::Input as StreamOnce>::Error>) {
        errors.error.add_expected(Info::Borrowed("LITERAL"));
    }
}

/// Parses a punctuation token and succeeds if it's char representation is equal to `c`.
///
/// Cannot match delimiter characters (i.e. `(`, `)`, `{`, `}`, `[, `]`).
/// To match a delimiter use `delim` instead.
pub fn punct<I>(c: char) -> Punct<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    Punct(c, PhantomData)
}

#[derive(Copy, Clone)]
/// Represents the return type of `punct`.
pub struct Punct<I>(char, PhantomData<fn(I) -> I>)
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>;

impl<I> Parser for Punct<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    type Input = I;
    type Output = Token;
    type PartialState = ();

    fn parse_lazy(&mut self, input: &mut Self::Input) -> ConsumedResult<Self::Output, Self::Input> {
        let position = input.position();
        match uncons(input) {
            EmptyOk(tok) | ConsumedOk(tok) => match tok {
                Token::Punct(ref punct) if punct.as_char() == self.0 => ConsumedOk(tok),
                _ => EmptyErr(I::Error::empty(position).into()),
            },
            EmptyErr(err) => EmptyErr(err),
            ConsumedErr(err) => ConsumedErr(err),
        }
    }

    fn add_error(&mut self, errors: &mut Tracked<<Self::Input as StreamOnce>::Error>) {
        errors.error.add_expected(Info::Token(Token::Punct(proc_macro2::Punct::new(self.0, proc_macro2::Spacing::Alone))));
    }
}


/// Parses a delimiter if it's char representation is equal to `c`.
pub fn delim<I>(c: char) -> Delim<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    Delim(c, PhantomData)
}

#[derive(Copy, Clone)]
/// Represents the return type of `delim`.
pub struct Delim<I>(char, PhantomData<fn(I) -> I>)
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>;

impl<I> Parser for Delim<I>
where
    I: Stream<Item = Token>,
    I::Error: ParseError<I::Item, I::Range, I::Position>,
{
    type Input = I;
    type Output = Token;
    type PartialState = ();

    fn parse_lazy(&mut self, input: &mut Self::Input) -> ConsumedResult<Self::Output, Self::Input> {
        let position = input.position();
        match uncons(input) {
            EmptyOk(tok) | ConsumedOk(tok) => match tok {
                Token::Delim(ch, _) if ch == self.0 => ConsumedOk(tok),
                _ => EmptyErr(I::Error::empty(position).into()),
            },
            EmptyErr(err) => EmptyErr(err),
            ConsumedErr(err) => ConsumedErr(err),
        }
    }

    fn add_error(&mut self, errors: &mut Tracked<<Self::Input as StreamOnce>::Error>) {
        errors.error.add_expected(Info::Token(Token::Delim(self.0, proc_macro2::Span::call_site())));
    }
}