Skip to main content

fuzzy_pickles/parser/
mod.rs

1mod combinators;
2mod expression;
3
4#[cfg(test)]
5#[macro_use]
6mod test_utils;
7
8use crate::{Extent, HumanTextError};
9use crate::ast::*;
10use crate::combinators::*;
11use self::{
12    combinators::*,
13    expression::{
14        expr_byte,
15        expr_byte_string,
16        expr_macro_call,
17        expression,
18        statement_expression,
19    },
20};
21use crate::tokenizer::{self, Token};
22use peresil::combinators::*;
23use std::{self, fmt};
24use std::collections::BTreeSet;
25
26pub(crate) type Point<'s> = TokenPoint<'s, Token>;
27pub(crate) type Master<'s> = peresil::ParseMaster<Point<'s>, Error, State>;
28pub(crate) type Progress<'s, T> = peresil::Progress<Point<'s>, T, Error>;
29
30// ------
31
32/// A Point that allows splitting the tokens based on parser whims.
33///
34/// The tokenizer greedily constructs tokens such that `>>=` will be
35/// one token. Unfortunately, this can occur in a context where we
36/// want separate tokens:
37///
38/// ```rust,ignore
39/// let foo: Vec<Vec<u8>>= vec![];
40/// ```
41///
42/// To handle this, if the requested token fails, we attempt to split
43/// the current token. If the head of the split matches, we accept it
44/// and track that we are in the middle of a split through
45/// `sub_offset`.
46///
47/// This has the nice benefit of getting our automatic rewind
48/// capability from the point and the grammar logic can stay clean.
49pub(crate) struct TokenPoint<'s, T: 's> {
50    pub offset: usize,
51    pub sub_offset: Option<u8>,
52    pub s: &'s [T],
53}
54
55impl<'s, T: 's> fmt::Debug for TokenPoint<'s, T> {
56    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57        match self.sub_offset {
58            Some(s) => write!(f, "TokenPoint {{ {}.{} }}", self.offset, s),
59            None => write!(f, "TokenPoint {{ {} }}", self.offset),
60        }
61    }
62}
63
64impl<'s, T: 's> TokenPoint<'s, T> {
65    pub(crate) fn new(slice: &'s [T]) -> Self {
66        TokenPoint {
67            offset: 0,
68            sub_offset: None,
69            s: slice,
70        }
71    }
72
73    // You'd better know what you are doing, as this doesn't care about split tokens!
74    fn advance_by(&self, offset: usize) -> Self {
75        TokenPoint {
76            offset: self.offset + offset,
77            sub_offset: None,
78            s: &self.s[offset..],
79        }
80    }
81
82    fn location(&self) -> (usize, Option<u8>) {
83        (self.offset, self.sub_offset)
84    }
85}
86
87impl<'s, T> peresil::Point for TokenPoint<'s, T> {
88    fn zero() -> Self {
89        Self::new(&[])
90    }
91}
92
93impl<'s, T> Copy for TokenPoint<'s, T> {}
94impl<'s, T> Clone for TokenPoint<'s, T> {
95    fn clone(&self) -> Self { *self }
96}
97
98impl<'s, T> PartialOrd for TokenPoint<'s, T> {
99    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
100        Some(self.cmp(other))
101    }
102}
103
104impl<'s, T> Ord for TokenPoint<'s, T> {
105    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
106        self.location().cmp(&other.location())
107    }
108}
109
110impl<'s, T> PartialEq for TokenPoint<'s, T> {
111    fn eq(&self, other: &Self) -> bool {
112        self.location().eq(&other.location())
113    }
114}
115
116impl<'s, T> Eq for TokenPoint<'s, T> {}
117
118// -----
119
120#[derive(Debug, Default)]
121pub(crate) struct State {
122    expression_ambiguity: expression::ExpressionAmbiguity,
123}
124
125impl State {
126    pub(crate) fn new() -> Self {
127        State::default()
128    }
129
130    fn ex(&self, start: Point, end: Point) -> Extent {
131        use std::cmp::Ordering;
132
133        // When calculating the extent of an item, we need to look
134        // back one token from the end. Since that's already gone, we
135        // use the initial point.
136        let relative_tokens = start.s;
137
138        let start_offset = |pt: Point| -> usize {
139            let Extent(a, _) = relative_tokens[0].extent();
140            let a_x = pt.sub_offset.map_or(0, |x| x + 1) as usize;
141            a + a_x
142        };
143
144        let end_offset = |pt: Point| -> usize {
145            let offset = pt.offset - start.offset - 1;
146            let Extent(_, b) = relative_tokens[offset].extent();
147            let b_x = pt.sub_offset.map_or(0, |x| x + 1) as usize;
148            b + b_x
149        };
150
151        match start.offset.cmp(&end.offset) {
152            Ordering::Less => {
153                let a = start_offset(start);
154                let b = end_offset(end);
155                Extent(a, b)
156            }
157            Ordering::Equal => {
158                match start.sub_offset.cmp(&end.sub_offset) {
159                    Ordering::Less => {
160                        let a = start_offset(start);
161                        let b = start_offset(end);
162                        Extent(a, b)
163                    }
164                    Ordering::Equal => {
165                        let a = start_offset(start);
166                        Extent(a, a)
167                    }
168                    Ordering::Greater => panic!("points are backwards ({:?}, {:?})", start, end),
169                }
170            }
171            Ordering::Greater => panic!("points are backwards ({:?}, {:?})", start, end),
172        }
173    }
174}
175
176// define an error type - emphasis on errors. Need to implement Recoverable (more to discuss.
177#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
178pub(crate) enum Error {
179    ExpectedAmpersand,
180    ExpectedAmpersandEquals,
181    ExpectedAs,
182    ExpectedAsterisk,
183    ExpectedAsync,
184    ExpectedAt,
185    ExpectedAuto,
186    #[allow(unused)]
187    ExpectedBackslash,
188    ExpectedBang,
189    ExpectedBox,
190    ExpectedBreak,
191    ExpectedByte,
192    ExpectedByteString,
193    ExpectedByteStringRaw,
194    ExpectedCaret,
195    ExpectedCaretEquals,
196    ExpectedCharacter,
197    ExpectedColon,
198    ExpectedComma,
199    ExpectedConst,
200    ExpectedContinue,
201    ExpectedCrate,
202    ExpectedDefault,
203    ExpectedDyn,
204    ExpectedDivideEquals,
205    ExpectedDocCommentInnerBlock,
206    ExpectedDocCommentInnerLine,
207    ExpectedDocCommentOuterBlock,
208    ExpectedDocCommentOuterLine,
209    #[allow(unused)]
210    ExpectedDollar,
211    ExpectedDoubleAmpersand,
212    ExpectedDoubleColon,
213    ExpectedDoubleEquals,
214    ExpectedDoubleLeftAngle,
215    ExpectedDoublePeriod,
216    ExpectedDoublePeriodEquals,
217    ExpectedDoublePipe,
218    ExpectedDoubleRightAngle,
219    ExpectedElse,
220    ExpectedEnum,
221    ExpectedEquals,
222    ExpectedExtern,
223    ExpectedFn,
224    ExpectedFor,
225    ExpectedGreaterThanOrEquals,
226    ExpectedHash,
227    ExpectedIdent,
228    ExpectedIf,
229    ExpectedImpl,
230    ExpectedIn,
231    ExpectedLeftAngle,
232    ExpectedLeftCurly,
233    ExpectedLeftParen,
234    ExpectedLeftSquare,
235    ExpectedLessThanOrEquals,
236    ExpectedLet,
237    ExpectedLifetime,
238    ExpectedLoop,
239    ExpectedMatch,
240    ExpectedMinus,
241    ExpectedMinusEquals,
242    ExpectedMod,
243    ExpectedMove,
244    ExpectedMut,
245    ExpectedNotEqual,
246    ExpectedNumber,
247    ExpectedPercent,
248    ExpectedPercentEquals,
249    ExpectedPeriod,
250    ExpectedPipe,
251    ExpectedPipeEquals,
252    ExpectedPlus,
253    ExpectedPlusEquals,
254    ExpectedPub,
255    ExpectedQuestionMark,
256    ExpectedRef,
257    ExpectedReturn,
258    ExpectedRightAngle,
259    ExpectedRightCurly,
260    ExpectedRightParen,
261    ExpectedRightSquare,
262    ExpectedSelfIdent,
263    ExpectedSemicolon,
264    ExpectedShiftLeftEquals,
265    ExpectedShiftRightEquals,
266    ExpectedSlash,
267    ExpectedStatic,
268    ExpectedString,
269    ExpectedStringRaw,
270    ExpectedStruct,
271    ExpectedThickArrow,
272    ExpectedThinArrow,
273    #[allow(unused)]
274    ExpectedTilde,
275    ExpectedTimesEquals,
276    ExpectedTrait,
277    ExpectedTriplePeriod,
278    ExpectedType,
279    ExpectedUnion,
280    ExpectedUnsafe,
281    ExpectedUse,
282    ExpectedWhere,
283    ExpectedWhile,
284
285    ExpectedExpression,
286
287    BlockNotAllowedHere,
288}
289
290impl peresil::Recoverable for Error {
291    fn recoverable(&self) -> bool { true }
292}
293
294/// Information about a parsing error
295#[derive(Debug, PartialEq)]
296pub struct ErrorDetail {
297    pub(crate) location: usize,
298    pub(crate) errors: BTreeSet<Error>,
299}
300
301impl ErrorDetail {
302    /// Enhance the error with the source code
303    pub fn with_text<'a>(&'a self, text: &'a str) -> ErrorDetailText<'a> {
304        ErrorDetailText { detail: self, text }
305    }
306}
307
308/// Information about a parsing error including original source code
309#[derive(Debug)]
310pub struct ErrorDetailText<'a> {
311    detail: &'a ErrorDetail,
312    text: &'a str,
313}
314
315impl<'a> fmt::Display for ErrorDetailText<'a> {
316    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
317        let human = HumanTextError::new(self.text, self.detail.location);
318
319        writeln!(f, "Unable to parse text (line {}, column {})", human.line, human.column)?;
320        writeln!(f, "{}{}", human.head_of_line, human.tail_of_line)?;
321        writeln!(f, "{:>width$}", "^", width = human.column)?;
322        writeln!(f, "Expected:")?;
323        for e in &self.detail.errors {
324            writeln!(f, "  {:?}", e)?; // TODO: should be Display
325        }
326        Ok(())
327    }
328}
329
330pub(crate) fn item<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Item> {
331    pm.alternate(pt)
332        .one(map(attribute_containing, Item::AttributeContaining))
333        .one(map(p_const, Item::Const))
334        .one(map(extern_crate, Item::ExternCrate))
335        .one(map(extern_block, Item::ExternBlock))
336        .one(map(function, Item::Function))
337        .one(map(item_macro_call, Item::MacroCall))
338        .one(map(module, Item::Module))
339        .one(map(p_enum, Item::Enum))
340        .one(map(p_impl, Item::Impl))
341        .one(map(p_static, Item::Static))
342        .one(map(p_struct, Item::Struct))
343        .one(map(p_trait, Item::Trait))
344        .one(map(p_union, Item::Union))
345        .one(map(p_use, Item::Use))
346        .one(map(type_alias, Item::TypeAlias))
347        .finish()
348}
349
350macro_rules! shim {
351    ($name:ident, $matcher:expr, $error:expr) => {
352        shim!($name, $matcher, $error, Extent);
353    };
354    ($name:ident, $matcher:expr, $error:expr, $t:ty) => {
355        fn $name<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, $t> {
356            token($matcher, $error)(pm, pt)
357        }
358    };
359}
360
361macro_rules! shims {
362    [$( ($( $arg:tt )*), )*] => {
363        $( shim!($( $arg )*); )*
364    };
365}
366
367shims! [
368    // Match up these names better
369    (ident_normal, Token::into_ident, Error::ExpectedIdent),
370    (lifetime_normal, Token::into_lifetime, Error::ExpectedLifetime),
371    (number_normal, Token::into_number, Error::ExpectedNumber, tokenizer::Number),
372
373    (character, Token::into_character, Error::ExpectedCharacter),
374    (string, Token::into_string, Error::ExpectedString),
375    (string_raw, Token::into_string_raw, Error::ExpectedStringRaw),
376    (byte, Token::into_byte, Error::ExpectedByte),
377    (byte_string, Token::into_byte_string, Error::ExpectedByteString),
378    (byte_string_raw, Token::into_byte_string_raw, Error::ExpectedByteStringRaw),
379
380    // Keywords
381    (kw_as, Token::into_as, Error::ExpectedAs),
382    (kw_async, Token::into_async, Error::ExpectedAsync),
383    (kw_auto, Token::into_auto, Error::ExpectedAuto),
384    (kw_box, Token::into_box, Error::ExpectedBox),
385    (kw_break, Token::into_break, Error::ExpectedBreak),
386    (kw_const, Token::into_const, Error::ExpectedConst),
387    (kw_continue, Token::into_continue, Error::ExpectedContinue),
388    (kw_crate, Token::into_crate, Error::ExpectedCrate),
389    (kw_default, Token::into_default, Error::ExpectedDefault),
390    (kw_dyn, Token::into_dyn, Error::ExpectedDyn),
391    (kw_else, Token::into_else, Error::ExpectedElse),
392    (kw_enum, Token::into_enum, Error::ExpectedEnum),
393    (kw_extern, Token::into_extern, Error::ExpectedExtern),
394    (kw_fn, Token::into_fn, Error::ExpectedFn),
395    (kw_for, Token::into_for, Error::ExpectedFor),
396    (kw_if, Token::into_if, Error::ExpectedIf),
397    (kw_impl, Token::into_impl, Error::ExpectedImpl),
398    (kw_in, Token::into_in, Error::ExpectedIn),
399    (kw_let, Token::into_let, Error::ExpectedLet),
400    (kw_loop, Token::into_loop, Error::ExpectedLoop),
401    (kw_match, Token::into_match, Error::ExpectedMatch),
402    (kw_mod, Token::into_mod, Error::ExpectedMod),
403    (kw_move, Token::into_move, Error::ExpectedMove),
404    (kw_mut, Token::into_mut, Error::ExpectedMut),
405    (kw_pub, Token::into_pub, Error::ExpectedPub),
406    (kw_ref, Token::into_ref, Error::ExpectedRef),
407    (kw_return, Token::into_return, Error::ExpectedReturn),
408    (kw_self_ident, Token::into_self_ident, Error::ExpectedSelfIdent),
409    (kw_static, Token::into_static, Error::ExpectedStatic),
410    (kw_struct, Token::into_struct, Error::ExpectedStruct),
411    (kw_trait, Token::into_trait, Error::ExpectedTrait),
412    (kw_type, Token::into_type, Error::ExpectedType),
413    (kw_union, Token::into_union, Error::ExpectedUnion),
414    (kw_unsafe, Token::into_unsafe, Error::ExpectedUnsafe),
415    (kw_use, Token::into_use, Error::ExpectedUse),
416    (kw_where, Token::into_where, Error::ExpectedWhere),
417    (kw_while, Token::into_while, Error::ExpectedWhile),
418
419    // Paired delimiters
420    (left_angle, Token::into_left_angle, Error::ExpectedLeftAngle),
421    (left_curly, Token::into_left_curly, Error::ExpectedLeftCurly),
422    (left_paren, Token::into_left_paren, Error::ExpectedLeftParen),
423    (left_square, Token::into_left_square, Error::ExpectedLeftSquare),
424    (right_angle, Token::into_right_angle, Error::ExpectedRightAngle),
425    (right_curly, Token::into_right_curly, Error::ExpectedRightCurly),
426    (right_paren, Token::into_right_paren, Error::ExpectedRightParen),
427    (right_square, Token::into_right_square, Error::ExpectedRightSquare),
428
429    // Symbols
430    (ampersand, Token::into_ampersand, Error::ExpectedAmpersand),
431    (ampersand_equals, Token::into_ampersand_equals, Error::ExpectedAmpersandEquals),
432    (asterisk, Token::into_asterisk, Error::ExpectedAsterisk),
433    (at, Token::into_at, Error::ExpectedAt),
434    (bang, Token::into_bang, Error::ExpectedBang),
435    (caret, Token::into_caret, Error::ExpectedCaret),
436    (caret_equals, Token::into_caret_equals, Error::ExpectedCaretEquals),
437    (colon, Token::into_colon, Error::ExpectedColon),
438    (comma, Token::into_comma, Error::ExpectedComma),
439    (divide_equals, Token::into_divide_equals, Error::ExpectedDivideEquals),
440    (double_ampersand, Token::into_double_ampersand, Error::ExpectedDoubleAmpersand),
441    (double_colon, Token::into_double_colon, Error::ExpectedDoubleColon),
442    (double_equals, Token::into_double_equals, Error::ExpectedDoubleEquals),
443    (double_left_angle, Token::into_double_left_angle, Error::ExpectedDoubleLeftAngle),
444    (double_period, Token::into_double_period, Error::ExpectedDoublePeriod),
445    (double_period_equals, Token::into_double_period_equals, Error::ExpectedDoublePeriodEquals),
446    (double_pipe, Token::into_double_pipe, Error::ExpectedDoublePipe),
447    (double_right_angle, Token::into_double_right_angle, Error::ExpectedDoubleRightAngle),
448    (equals, Token::into_equals, Error::ExpectedEquals),
449    (greater_than_or_equals, Token::into_greater_than_or_equals, Error::ExpectedGreaterThanOrEquals),
450    (hash, Token::into_hash, Error::ExpectedHash),
451    (less_than_or_equals, Token::into_less_than_or_equals, Error::ExpectedLessThanOrEquals),
452    (minus, Token::into_minus, Error::ExpectedMinus),
453    (minus_equals, Token::into_minus_equals, Error::ExpectedMinusEquals),
454    (not_equal, Token::into_not_equal, Error::ExpectedNotEqual),
455    (percent, Token::into_percent, Error::ExpectedPercent),
456    (percent_equals, Token::into_percent_equals, Error::ExpectedPercentEquals),
457    (period, Token::into_period, Error::ExpectedPeriod),
458    (pipe, Token::into_pipe, Error::ExpectedPipe),
459    (pipe_equals, Token::into_pipe_equals, Error::ExpectedPipeEquals),
460    (plus, Token::into_plus, Error::ExpectedPlus),
461    (plus_equals, Token::into_plus_equals, Error::ExpectedPlusEquals),
462    (question_mark, Token::into_question_mark, Error::ExpectedQuestionMark),
463    (semicolon, Token::into_semicolon, Error::ExpectedSemicolon),
464    (shift_left_equals, Token::into_shift_left_equals, Error::ExpectedShiftLeftEquals),
465    (shift_right_equals, Token::into_shift_right_equals, Error::ExpectedShiftRightEquals),
466    (slash, Token::into_slash, Error::ExpectedSlash),
467    (thick_arrow, Token::into_thick_arrow, Error::ExpectedThickArrow),
468    (thin_arrow, Token::into_thin_arrow, Error::ExpectedThinArrow),
469    (times_equals, Token::into_times_equals, Error::ExpectedTimesEquals),
470    (triple_period, Token::into_triple_period, Error::ExpectedTriplePeriod),
471
472    (doc_comment_inner_block, Token::into_doc_comment_inner_block, Error::ExpectedDocCommentInnerBlock),
473    (doc_comment_inner_line, Token::into_doc_comment_inner_line, Error::ExpectedDocCommentInnerLine),
474    (doc_comment_outer_block, Token::into_doc_comment_outer_block, Error::ExpectedDocCommentOuterBlock),
475    (doc_comment_outer_line, Token::into_doc_comment_outer_line, Error::ExpectedDocCommentOuterLine),
476];
477
478fn token<'s, F, T>(token_convert: F, error: Error) ->
479    impl FnOnce(&mut Master<'s>, Point<'s>) -> Progress<'s, T>
480    where F: Fn(Token) -> Option<T>
481{
482    move |_, pt| {
483        let original_token = match pt.s.first() {
484            Some(&token) => token,
485            None => return Progress::failure(pt, error),
486        };
487
488        let token = match pt.sub_offset {
489            Some(sub_offset) => {
490                split(original_token, sub_offset).expect("Cannot resume a split token").1
491            },
492            None => original_token,
493        };
494
495        match token_convert(token) {
496            Some(v) => {
497                // We exactly matched the requested token
498                Progress::success(pt.advance_by(1), v)
499            }
500            None => {
501                // Maybe we can split the token
502                let sub_offset = pt.sub_offset.map(|x| x + 1).unwrap_or(0);
503                match split(original_token, sub_offset) {
504                    Some((token, _)) => {
505                        match token_convert(token) {
506                            // The split did match
507                            Some(v) => {
508                                let pt = Point {
509                                    sub_offset: Some(sub_offset),
510                                    ..pt
511                                };
512                                Progress::success(pt, v)
513                            }
514                            None => {
515                                // The split did not match
516                                Progress::failure(pt, error)
517                            }
518                        }
519                    }
520                    None => {
521                        // Cannot split
522                        Progress::failure(pt, error)
523                    }
524                }
525            }
526        }
527    }
528}
529
530fn split(token: Token, n: u8) -> Option<(Token, Token)> {
531    match (token, n) {
532        (Token::DoubleLeftAngle(extent), 0) => {
533            let Extent(s, e) = extent;
534            let a = Token::LeftAngle(Extent(s, s+1));
535            let b = Token::LeftAngle(Extent(s+1, e));
536            Some((a, b))
537        }
538        (Token::DoubleRightAngle(extent), 0) => {
539            let Extent(s, e) = extent;
540            let a = Token::RightAngle(Extent(s, s+1));
541            let b = Token::RightAngle(Extent(s+1, e));
542            Some((a, b))
543        }
544        (Token::ShiftRightEquals(extent), 0) => {
545            let Extent(s, e) = extent;
546            let a = Token::RightAngle(Extent(s, s+1));
547            let b = Token::GreaterThanOrEquals(Extent(s+1, e));
548            Some((a, b))
549        }
550        (Token::ShiftRightEquals(extent), 1) => {
551            let Extent(s, e) = extent;
552            let a = Token::RightAngle(Extent(s+1, s+2));
553            let b = Token::Equals(Extent(s+2, e));
554            Some((a, b))
555        }
556        (Token::GreaterThanOrEquals(extent), 0) => {
557            let Extent(s, e) = extent;
558            let a = Token::RightAngle(Extent(s, s+1));
559            let b = Token::Equals(Extent(s+1, e));
560            Some((a, b))
561        }
562        (Token::DoublePipe(extent), 0) => {
563            let Extent(s, e) = extent;
564            let a = Token::Pipe(Extent(s, s+1));
565            let b = Token::Pipe(Extent(s+1, e));
566            Some((a, b))
567        }
568        (Token::DoubleAmpersand(extent), 0) => {
569            let Extent(s, e) = extent;
570            let a = Token::Ampersand(Extent(s, s+1));
571            let b = Token::Ampersand(Extent(s+1, e));
572            Some((a, b))
573        }
574        _ => None
575    }
576}
577
578fn function<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Function> {
579    sequence!(pm, pt, {
580        spt    = point;
581        header = function_header;
582        body   = block;
583    }, |pm: &mut Master, pt| Function {
584        extent: pm.state.ex(spt, pt),
585        header,
586        body,
587        whitespace: Vec::new()
588    })
589}
590
591fn function_header<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, FunctionHeader> {
592    sequence!(pm, pt, {
593        spt         = point;
594        visibility  = optional(visibility);
595        qualifiers  = function_qualifiers;
596        _           = kw_fn;
597        name        = ident;
598        generics    = optional(generic_declarations);
599        arguments   = function_arglist;
600        return_type = optional(function_return_type);
601        wheres      = optional(where_clause);
602    }, |pm: &mut Master, pt| {
603        FunctionHeader {
604            extent: pm.state.ex(spt, pt),
605            visibility,
606            qualifiers,
607            name,
608            generics,
609            arguments,
610            return_type,
611            wheres: wheres.unwrap_or_else(Vec::new),
612            whitespace: Vec::new(),
613        }})
614}
615
616// TODO: This is overly loose; we can't really have a `default extern` function.
617// TODO: Not all places that call this really can allow all of these qualifiers
618fn function_qualifiers<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
619    Progress<'s, FunctionQualifiers>
620{
621    sequence!(pm, pt, {
622        spt        = point;
623        is_default = optional(ext(kw_default));
624        is_const   = optional(ext(kw_const));
625        is_unsafe  = optional(ext(kw_unsafe));
626        is_async   = optional(ext(kw_async));
627        is_extern  = optional(function_qualifier_extern);
628    }, |pm: &mut Master, pt| {
629        let is_extern = is_extern;
630        let (is_extern, abi) = match is_extern {
631            Some((ex, abi)) => (Some(ex), abi),
632            None => (None, None),
633        };
634        FunctionQualifiers {
635            extent: pm.state.ex(spt, pt),
636            is_default,
637            is_const,
638            is_unsafe,
639            is_async,
640            is_extern,
641            abi,
642            whitespace: Vec::new(),
643        }
644    })
645}
646
647fn function_qualifier_extern<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
648    Progress<'s, (Extent, Option<String>)>
649{
650    sequence!(pm, pt, {
651        is_extern = ext(kw_extern);
652        abi       = optional(string_literal);
653    }, |_, _| (is_extern, abi))
654}
655
656fn ident<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Ident> {
657    pm.alternate(pt)
658        // Contextual or edition keywords
659        .one(kw_default)
660        .one(kw_self_ident)
661        .one(kw_dyn)
662        .one(kw_union)
663        .one(ident_normal)
664        .finish()
665        .map(|extent| Ident { extent })
666        .map_err(|_| Error::ExpectedIdent)
667}
668
669fn generic_declarations<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, GenericDeclarations> {
670    sequence!(pm, pt, {
671        spt       = point;
672        _         = left_angle;
673        lifetimes = zero_or_more_tailed_values(comma, attributed(generic_declaration_lifetime));
674        types     = zero_or_more_tailed_values(comma, attributed(generic_declaration_type));
675        consts    = zero_or_more_tailed_values(comma, attributed(generic_declaration_const));
676        _         = right_angle;
677    }, |pm: &mut Master, pt| GenericDeclarations {
678        extent: pm.state.ex(spt, pt),
679        lifetimes,
680        types,
681        consts,
682        whitespace: Vec::new(),
683    })
684}
685
686fn generic_declaration_lifetime<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, GenericDeclarationLifetime> {
687    sequence!(pm, pt, {
688        spt        = point;
689        name       = lifetime;
690        bounds     = optional(generic_declaration_lifetime_bounds);
691    }, |pm: &mut Master, pt| GenericDeclarationLifetime {
692        extent: pm.state.ex(spt, pt),
693        name,
694        bounds: bounds.unwrap_or_else(Vec::new),
695        whitespace: Vec::new(),
696    })
697}
698
699fn generic_declaration_lifetime_bounds<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Vec<Lifetime>> {
700    sequence!(pm, pt, {
701        _      = colon;
702        bounds = zero_or_more_tailed_values(plus, lifetime);
703    }, |_, _| bounds)
704}
705
706fn generic_declaration_type<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, GenericDeclarationType> {
707    sequence!(pm, pt, {
708        spt        = point;
709        name       = ident;
710        // Over-permissive; allows interleaving trait bounds and default types
711        bounds     = optional(generic_declaration_type_bounds);
712        default    = optional(generic_declaration_type_default);
713    }, |pm: &mut Master, pt| GenericDeclarationType {
714        extent: pm.state.ex(spt, pt),
715        name,
716        bounds,
717        default,
718        whitespace: Vec::new(),
719    })
720}
721
722fn generic_declaration_type_bounds<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitBounds> {
723    sequence!(pm, pt, {
724        _      = colon;
725        bounds = trait_bounds;
726    }, |_, _| bounds)
727}
728
729fn generic_declaration_type_default<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Type> {
730    sequence!(pm, pt, {
731        _   = equals;
732        typ = typ;
733    }, |_, _| typ)
734}
735
736fn generic_declaration_const<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, GenericDeclarationConst> {
737    sequence!(pm, pt, {
738        spt  = point;
739        _    = kw_const;
740        name = ident;
741        _    = colon;
742        typ  = typ;
743    }, |pm: &mut Master, pt| GenericDeclarationConst {
744        extent: pm.state.ex(spt, pt),
745        name,
746        typ,
747        whitespace: Vec::new(),
748    })
749}
750
751fn function_arglist<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Vec<Argument>> {
752    sequence!(pm, pt, {
753        _        = left_paren;
754        self_arg = optional(map(self_argument, Argument::SelfArgument));
755        args     = zero_or_more_tailed_values_append(self_arg, comma, function_argument);
756        _        = right_paren;
757    }, move |_, _| args)
758}
759
760fn self_argument<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, SelfArgument> {
761    pm.alternate(pt)
762        .one(map(self_argument_longhand, SelfArgument::Longhand))
763        .one(map(self_argument_shorthand, SelfArgument::Shorthand))
764        .finish()
765}
766
767fn self_argument_longhand<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, SelfArgumentLonghand> {
768    sequence!(pm, pt, {
769        spt    = point;
770        is_mut = optional(kw_mut);
771        name   = kw_self_ident;
772        _      = colon;
773        typ    = typ;
774        _      = optional(comma);
775    }, |pm: &mut Master, pt| SelfArgumentLonghand {
776        extent: pm.state.ex(spt, pt),
777        is_mut,
778        name: Ident { extent: name },
779        typ,
780        whitespace: Vec::new(),
781    })
782}
783
784fn self_argument_shorthand<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, SelfArgumentShorthand> {
785    sequence!(pm, pt, {
786        spt       = point;
787        qualifier = optional(self_argument_qualifier);
788        name      = kw_self_ident;
789        _         = optional(comma);
790    }, |pm: &mut Master, pt| SelfArgumentShorthand {
791        extent: pm.state.ex(spt, pt),
792        qualifier,
793        name: Ident { extent: name },
794        whitespace: Vec::new(),
795    })
796}
797
798fn self_argument_qualifier<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
799    Progress<'s, SelfArgumentShorthandQualifier>
800{
801    pm.alternate(pt)
802        .one(map(typ_reference_kind, SelfArgumentShorthandQualifier::Reference))
803        .one(map(ext(kw_mut), SelfArgumentShorthandQualifier::Mut))
804        .finish()
805}
806
807fn function_argument<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Argument> {
808    sequence!(pm, pt, {
809        spt  = point;
810        name = pattern;
811        _    = colon;
812        typ  = typ;
813    }, |pm: &mut Master, pt| Argument::Named(NamedArgument {
814        extent: pm.state.ex(spt, pt),
815        name,
816        typ,
817        whitespace: Vec::new(),
818    }))
819}
820
821fn function_return_type<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Type> {
822    sequence!(pm, pt, {
823        _   = thin_arrow;
824        typ = typ;
825    }, |_, _| typ)
826}
827
828fn where_clause<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Vec<Where>> {
829    sequence!(pm, pt, {
830        _ = kw_where;
831        w = one_or_more_tailed_values(comma, where_clause_item);
832    }, |_, _| w)
833}
834
835fn where_clause_item<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Where> {
836    sequence!(pm, pt, {
837        spt   = point;
838        hrtbs = optional(higher_ranked_trait_bounds);
839        kind  = where_clause_kind;
840    }, |pm: &mut Master, pt|  Where {
841        extent: pm.state.ex(spt, pt),
842        higher_ranked_trait_bounds: hrtbs.unwrap_or_else(Vec::new),
843        kind,
844        whitespace: Vec::new(),
845    })
846}
847
848fn where_clause_kind<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, WhereKind> {
849    pm.alternate(pt)
850        .one(map(where_lifetime, WhereKind::Lifetime))
851        .one(map(where_type, WhereKind::Type))
852        .finish()
853}
854
855fn where_lifetime<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, WhereLifetime> {
856    sequence!(pm, pt, {
857        spt    = point;
858        name   = lifetime;
859        bounds = generic_declaration_lifetime_bounds;
860    }, |pm: &mut Master, pt| WhereLifetime {
861        extent: pm.state.ex(spt, pt),
862        name,
863        bounds,
864        whitespace: Vec::new(),
865    })
866}
867
868fn where_type<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, WhereType> {
869    sequence!(pm, pt, {
870        spt    = point;
871        name   = typ;
872        bounds = generic_declaration_type_bounds;
873    }, |pm: &mut Master, pt| WhereType {
874        extent: pm.state.ex(spt, pt),
875        name,
876        bounds,
877        whitespace: Vec::new(),
878    })
879}
880
881fn trait_bounds<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitBounds> {
882    sequence!(pm, pt, {
883        spt   = point;
884        types = zero_or_more_tailed_values(plus, trait_bound);
885    }, |pm: &mut Master, pt| TraitBounds {
886        extent: pm.state.ex(spt, pt),
887        types,
888        whitespace: Vec::new(),
889    })
890}
891
892fn trait_bound<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitBound> {
893    pm.alternate(pt)
894        .one(map(trait_bound_lifetime, TraitBound::Lifetime))
895        .one(map(trait_bound_normal, TraitBound::Normal))
896        .one(map(trait_bound_relaxed, TraitBound::Relaxed))
897        .finish()
898}
899
900fn trait_bound_lifetime<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitBoundLifetime> {
901    sequence!(pm, pt, {
902        spt      = point;
903        lifetime = lifetime;
904    }, |pm: &mut Master, pt| TraitBoundLifetime {
905        extent: pm.state.ex(spt, pt),
906        lifetime,
907        whitespace: Vec::new(),
908    })
909}
910
911fn trait_bound_normal<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitBoundNormal> {
912    sequence!(pm, pt, {
913        spt = point;
914        typ = trait_bound_normal_child;
915    }, |pm: &mut Master, pt| TraitBoundNormal {
916        extent: pm.state.ex(spt, pt),
917        typ,
918        whitespace: Vec::new(),
919    })
920}
921
922fn trait_bound_normal_child<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
923    Progress<'s, TraitBoundType>
924{
925    pm.alternate(pt)
926        .one(map(typ_named, TraitBoundType::Named))
927        .one(map(typ_higher_ranked_trait_bounds, TraitBoundType::HigherRankedTraitBounds))
928        .finish()
929}
930
931fn trait_bound_relaxed<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitBoundRelaxed> {
932    sequence!(pm, pt, {
933        spt = point;
934        _   = question_mark;
935        typ = trait_bound_normal_child;
936    }, |pm: &mut Master, pt| TraitBoundRelaxed { extent: pm.state.ex(spt, pt), typ, whitespace: Vec::new() })
937}
938
939fn block<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Block> {
940    sequence!(pm, pt, {
941        spt               = point;
942        _                 = left_curly;
943        (mut stmts, term) = zero_or_more_implicitly_tailed_values_terminated(semicolon, statement);
944        _                 = right_curly;
945    }, |pm: &mut Master, pt| {
946        let expr = if !term && stmts.last().map_or(false, Statement::is_expression) {
947            stmts.pop().and_then(Statement::into_expression)
948        } else {
949            None
950        };
951
952        Block {
953            extent: pm.state.ex(spt, pt),
954            statements: stmts,
955            expression: expr,
956            whitespace: Vec::new(),
957        }
958    })
959}
960
961fn statement<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Statement> {
962    pm.alternate(pt)
963        .one(map(attributed(item), Statement::Item))
964        .one(map(statement_expression, Statement::Expression))
965        .one(map(statement_empty, Statement::Empty))
966        .finish()
967}
968
969fn statement_empty<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Extent> {
970    sequence!(pm, pt, {
971        spt = point;
972        _   = peek(semicolon);
973    }, |pm: &mut Master, pt| pm.state.ex(spt, pt))
974}
975
976impl ImplicitSeparator for Statement {
977    fn is_implicit_separator(&self) -> bool {
978        match *self {
979            Statement::Expression(ref e) => e.may_terminate_statement(),
980            Statement::Item(_)           => true,
981            Statement::Empty(_)          => false,
982        }
983    }
984}
985
986// TODO: There's a good amount of duplication here; revisit and DRY up
987// Mostly in the required ; for paren and square...
988fn item_macro_call<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, MacroCall> {
989    sequence!(pm, pt, {
990        spt  = point;
991        name = pathed_ident;
992        _    = bang;
993        arg  = optional(ident);
994        args = item_macro_call_args;
995    }, |pm: &mut Master, pt| MacroCall {
996        extent: pm.state.ex(spt, pt),
997        name,
998        arg,
999        args,
1000        whitespace: Vec::new(),
1001    })
1002}
1003
1004fn item_macro_call_args<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, MacroCallArgs> {
1005    pm.alternate(pt)
1006        .one(map(item_macro_call_paren, MacroCallArgs::Paren))
1007        .one(map(item_macro_call_square, MacroCallArgs::Square))
1008        .one(map(item_macro_call_curly, MacroCallArgs::Curly))
1009        .finish()
1010}
1011
1012fn item_macro_call_paren<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Extent> {
1013    sequence!(pm, pt, {
1014        _    = left_paren;
1015        args = parse_nested_until(Token::is_left_paren, Token::is_right_paren);
1016        _    = right_paren;
1017        _    = semicolon;
1018    }, |_, _| args)
1019}
1020
1021fn item_macro_call_square<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Extent> {
1022    sequence!(pm, pt, {
1023        _    = left_square;
1024        args = parse_nested_until(Token::is_left_square, Token::is_right_square);
1025        _    = right_square;
1026        _    = semicolon;
1027    }, |_, _| args)
1028}
1029
1030fn item_macro_call_curly<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Extent> {
1031    sequence!(pm, pt, {
1032        _    = left_curly;
1033        args = parse_nested_until(Token::is_left_curly, Token::is_right_curly);
1034        _    = right_curly;
1035    }, |_, _| args)
1036}
1037
1038fn character_literal<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Character> {
1039    character(pm, pt)
1040        .map(|extent| Character { extent, value: extent }) // FIXME: value
1041}
1042
1043fn string_literal<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, String> {
1044    // FIXME: value
1045    pm.alternate(pt)
1046        .one(map(string, |extent| String { extent, value: extent }))
1047        .one(map(string_raw, |extent| String { extent, value: extent }))
1048        .finish()
1049}
1050
1051fn number_literal<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Number> {
1052    pm.alternate(pt)
1053        .one(map(number_normal, convert_number))
1054        .finish()
1055}
1056
1057fn convert_number(n: tokenizer::Number) -> Number {
1058    match n {
1059        tokenizer::Number::Binary(tokenizer::NumberBinary { extent, integral, fractional, exponent, type_suffix, .. }) => {
1060            let value = NumberValue::Binary(NumberBinary { extent, decimal: integral, fraction: fractional, exponent, suffix: type_suffix });
1061            Number { extent, is_negative: None, value, whitespace: Vec::new() }
1062        }
1063        tokenizer::Number::Octal(tokenizer::NumberOctal { extent, integral, fractional, exponent, type_suffix, .. }) => {
1064            let value = NumberValue::Octal(NumberOctal { extent, decimal: integral, fraction: fractional, exponent, suffix: type_suffix });
1065            Number { extent, is_negative: None, value, whitespace: Vec::new() }
1066        }
1067        tokenizer::Number::Hexadecimal(tokenizer::NumberHexadecimal { extent, integral, fractional, exponent, type_suffix, .. }) => {
1068            let value = NumberValue::Hexadecimal(NumberHexadecimal { extent, decimal: integral, fraction: fractional, exponent, suffix: type_suffix });
1069            Number { extent, is_negative: None, value, whitespace: Vec::new() }
1070        }
1071        tokenizer::Number::Decimal(tokenizer::NumberDecimal { extent, integral, fractional, exponent, type_suffix, .. }) => {
1072            let value = NumberValue::Decimal(NumberDecimal { extent, decimal: integral, fraction: fractional, exponent, suffix: type_suffix });
1073            Number { extent, is_negative: None, value, whitespace: Vec::new() }
1074        }
1075    }
1076}
1077
1078fn path<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Path> {
1079    sequence!(pm, pt, {
1080        spt        = point;
1081        _          = optional(double_colon);
1082        components = one_or_more_tailed_values(double_colon, ident);
1083    }, |pm: &mut Master, pt| Path {
1084        extent: pm.state.ex(spt, pt),
1085        components,
1086        whitespace: Vec::new(),
1087    })
1088}
1089
1090fn pathed_ident<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PathedIdent> {
1091    sequence!(pm, pt, {
1092        spt        = point;
1093        _          = optional(double_colon);
1094        components = one_or_more_tailed_values(double_colon, path_component);
1095    }, |pm: &mut Master, pt| PathedIdent {
1096        extent: pm.state.ex(spt, pt),
1097        components,
1098        whitespace: Vec::new(),
1099    })
1100}
1101
1102fn path_component<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PathComponent> {
1103    sequence!(pm, pt, {
1104        spt       = point;
1105        ident     = path_member;
1106        turbofish = optional(turbofish);
1107    }, |pm: &mut Master, pt| PathComponent {
1108        extent: pm.state.ex(spt, pt),
1109        ident,
1110        turbofish,
1111        whitespace: Vec::new(),
1112    })
1113}
1114
1115fn turbofish<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Turbofish> {
1116    sequence!(pm, pt, {
1117        spt       = point;
1118        _         = double_colon;
1119        _         = left_angle;
1120        lifetimes = zero_or_more_tailed_values(comma, lifetime);
1121        types     = zero_or_more_tailed_values(comma, typ);
1122        _     = right_angle;
1123    }, |pm: &mut Master, pt| Turbofish {
1124        extent: pm.state.ex(spt, pt),
1125        lifetimes,
1126        types,
1127        whitespace: Vec::new(),
1128    })
1129}
1130
1131fn pattern<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Pattern> {
1132    sequence!(pm, pt, {
1133        spt  = point;
1134        name = optional(pattern_name);
1135        kind = pattern_kind;
1136    }, |pm: &mut Master, pt| Pattern {
1137        extent: pm.state.ex(spt, pt),
1138        name,
1139        kind,
1140        whitespace: Vec::new(),
1141    })
1142}
1143
1144fn pattern_name<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternName> {
1145    sequence!(pm, pt, {
1146        spt    = point;
1147        is_ref = optional(ext(kw_ref));
1148        is_mut = optional(ext(kw_mut));
1149        name   = ident;
1150        _      = at;
1151    }, |pm: &mut Master, _| PatternName { extent: pm.state.ex(spt, pt), is_ref, is_mut, name, whitespace: Vec::new() })
1152}
1153
1154fn pattern_kind<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternKind> {
1155    pm.alternate(pt)
1156        // Must precede character and number as it contains them
1157        .one(map(pattern_range_exclusive, PatternKind::RangeExclusive))
1158        .one(map(pattern_range_inclusive, PatternKind::RangeInclusive))
1159        .one(map(pattern_char, PatternKind::Character))
1160        .one(map(pattern_byte, PatternKind::Byte))
1161        .one(map(pattern_number, PatternKind::Number))
1162        .one(map(pattern_reference, PatternKind::Reference))
1163        .one(map(pattern_byte_string, PatternKind::ByteString))
1164        .one(map(pattern_string, PatternKind::String))
1165        .one(map(pattern_struct, PatternKind::Struct))
1166        .one(map(pattern_tuple, PatternKind::Tuple))
1167        .one(map(pattern_slice, PatternKind::Slice))
1168        .one(map(pattern_macro_call, PatternKind::MacroCall))
1169        .one(map(pattern_box, PatternKind::Box))
1170        // Must be last, otherwise it collides with struct names
1171        .one(map(pattern_ident, PatternKind::Ident))
1172        .finish()
1173}
1174
1175fn pattern_ident<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternIdent> {
1176    sequence!(pm, pt, {
1177        spt    = point;
1178        is_ref = optional(ext(kw_ref));
1179        is_mut = optional(ext(kw_mut));
1180        ident  = pathed_ident;
1181        tuple  = optional(pattern_tuple);
1182    }, |pm: &mut Master, pt| PatternIdent {
1183        extent: pm.state.ex(spt, pt),
1184        is_ref,
1185        is_mut,
1186        ident,
1187        tuple,
1188        whitespace: Vec::new(),
1189    })
1190}
1191
1192fn pattern_tuple<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternTuple> {
1193    sequence!(pm, pt, {
1194        spt     = point;
1195        _       = left_paren;
1196        members = zero_or_more_tailed_values(comma, pattern_tuple_member);
1197        _       = right_paren;
1198    }, |pm: &mut Master, pt| PatternTuple {
1199        extent: pm.state.ex(spt, pt),
1200        members,
1201        whitespace: Vec::new(),
1202    })
1203}
1204
1205fn pattern_tuple_member<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1206    Progress<'s, PatternTupleMember>
1207{
1208    pm.alternate(pt)
1209        .one(map(pattern, PatternTupleMember::Pattern))
1210        .one(map(ext(double_period), PatternTupleMember::Wildcard))
1211        .finish()
1212}
1213
1214fn pattern_slice<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternSlice> {
1215    sequence!(pm, pt, {
1216        spt     = point;
1217        _       = left_square;
1218        members = zero_or_more_tailed_values(comma, pattern_slice_member);
1219        _       = right_square;
1220    }, |pm: &mut Master, pt| PatternSlice {
1221        extent: pm.state.ex(spt, pt),
1222        members,
1223        whitespace: Vec::new(),
1224    })
1225}
1226
1227fn pattern_slice_member<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1228    Progress<'s, PatternSliceMember>
1229{
1230    pm.alternate(pt)
1231        .one(map(pattern_slice_subslice, PatternSliceMember::Subslice))
1232        .one(map(pattern, PatternSliceMember::Pattern))
1233        .one(map(ext(double_period), PatternSliceMember::Wildcard))
1234        .finish()
1235}
1236
1237fn pattern_slice_subslice<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternSliceSubslice> {
1238    sequence!(pm, pt, {
1239        spt    = point;
1240        is_ref = optional(kw_ref);
1241        is_mut = optional(kw_mut);
1242        name   = ident;
1243        _      = at;
1244        _      = double_period;
1245    }, |pm: &mut Master, pt| PatternSliceSubslice {
1246        extent: pm.state.ex(spt, pt),
1247        is_ref,
1248        is_mut,
1249        name,
1250        whitespace: Vec::new(),
1251    })
1252}
1253
1254fn pattern_struct<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternStruct> {
1255    sequence!(pm, pt, {
1256        spt      = point;
1257        name     = pathed_ident;
1258        _        = left_curly;
1259        fields   = zero_or_more_tailed_values(comma, pattern_struct_field);
1260        wildcard = optional(double_period);
1261        _        = right_curly;
1262    }, |pm: &mut Master, pt| PatternStruct {
1263        extent: pm.state.ex(spt, pt),
1264        name,
1265        fields,
1266        wildcard: wildcard.is_some(),
1267        whitespace: Vec::new(),
1268    })
1269}
1270
1271fn pattern_struct_field<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternStructField> {
1272    pm.alternate(pt)
1273        .one(map(pattern_struct_field_long, PatternStructField::Long))
1274        .one(map(map(pattern_ident, |ident| {
1275            PatternStructFieldShort { extent: ident.extent, ident, whitespace: Vec::new() }
1276        }), PatternStructField::Short))
1277        .finish()
1278}
1279
1280fn pattern_struct_field_long<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1281    Progress<'s, PatternStructFieldLong>
1282{
1283    sequence!(pm, pt, {
1284        spt     = point;
1285        name    = ident;
1286        _       = colon;
1287        pattern = pattern;
1288    }, |pm: &mut Master, pt| PatternStructFieldLong { extent: pm.state.ex(spt, pt), name, pattern, whitespace: Vec::new() })
1289}
1290
1291fn pattern_byte<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternByte> {
1292    expr_byte(pm, pt).map(|value| PatternByte { extent: value.extent, value })
1293}
1294
1295fn pattern_char<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternCharacter> {
1296    character_literal(pm, pt).map(|value| PatternCharacter { extent: value.extent, value })
1297}
1298
1299fn pattern_byte_string<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternByteString> {
1300    expr_byte_string(pm, pt).map(|value| PatternByteString { extent: value.extent, value })
1301}
1302
1303fn pattern_string<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternString> {
1304    string_literal(pm, pt).map(|value| PatternString { extent: value.extent, value })
1305}
1306
1307fn pattern_number<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternNumber> {
1308    sequence!(pm, pt, {
1309        spt         = point;
1310        is_negative = optional(minus);
1311        value       = number_literal;
1312    }, |pm: &mut Master, pt| PatternNumber {
1313        extent: pm.state.ex(spt, pt),
1314        is_negative,
1315        value,
1316        whitespace: Vec::new(),
1317    })
1318}
1319
1320fn pattern_reference<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternReference> {
1321    sequence!(pm, pt, {
1322        spt     = point;
1323        _       = ampersand;
1324        is_mut  = optional(ext(kw_mut));
1325        pattern = pattern;
1326    }, |pm: &mut Master, pt| PatternReference {
1327        extent: pm.state.ex(spt, pt),
1328        is_mut,
1329        pattern: Box::new(pattern),
1330        whitespace: Vec::new()
1331    })
1332}
1333
1334fn pattern_box<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternBox> {
1335    sequence!(pm, pt, {
1336        spt     = point;
1337        _       = kw_box;
1338        pattern = pattern;
1339    }, |pm: &mut Master, pt| PatternBox {
1340        extent: pm.state.ex(spt, pt),
1341        pattern: Box::new(pattern),
1342        whitespace: Vec::new(),
1343    })
1344}
1345
1346fn pattern_range_exclusive<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1347    Progress<'s, PatternRangeExclusive>
1348{
1349    sequence!(pm, pt, {
1350        spt   = point;
1351        start = pattern_range_component;
1352        _     = double_period;
1353        end   = pattern_range_component;
1354    }, |pm: &mut Master, pt| PatternRangeExclusive {
1355        extent: pm.state.ex(spt, pt),
1356        start,
1357        end,
1358        whitespace: Vec::new()
1359    })
1360}
1361
1362fn range_inclusive_operator<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1363    Progress<'s, RangeInclusiveOperator>
1364{
1365    pm.alternate(pt)
1366        .one(map(triple_period, RangeInclusiveOperator::Legacy))
1367        .one(map(double_period_equals, RangeInclusiveOperator::Recommended))
1368        .finish()
1369}
1370
1371fn pattern_range_inclusive<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1372    Progress<'s, PatternRangeInclusive>
1373{
1374    sequence!(pm, pt, {
1375        spt      = point;
1376        start    = pattern_range_component;
1377        operator = range_inclusive_operator;
1378        end      = pattern_range_component;
1379    }, |pm: &mut Master, pt| PatternRangeInclusive {
1380        extent: pm.state.ex(spt, pt),
1381        start,
1382        operator,
1383        end,
1384        whitespace: Vec::new()
1385    })
1386}
1387
1388fn pattern_range_component<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternRangeComponent> {
1389    pm.alternate(pt)
1390        .one(map(pathed_ident, PatternRangeComponent::Ident))
1391        .one(map(character_literal, PatternRangeComponent::Character))
1392        .one(map(expr_byte, PatternRangeComponent::Byte))
1393        .one(map(pattern_number, PatternRangeComponent::Number))
1394        .finish()
1395}
1396
1397fn pattern_macro_call<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, PatternMacroCall> {
1398    expr_macro_call(pm, pt).map(|value| PatternMacroCall {
1399        extent: value.extent,
1400        value,
1401        whitespace: Vec::new(),
1402    })
1403}
1404
1405fn p_struct<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Struct> {
1406    sequence!(pm, pt, {
1407        spt            = point;
1408        visibility     = optional(visibility);
1409        _              = kw_struct;
1410        name           = ident;
1411        generics       = optional(generic_declarations);
1412        (body, wheres) = struct_defn_body;
1413    }, |pm: &mut Master, pt| Struct {
1414        extent: pm.state.ex(spt, pt),
1415        visibility,
1416        name,
1417        generics,
1418        wheres: wheres.unwrap_or_else(Vec::new),
1419        body,
1420        whitespace: Vec::new(),
1421    })
1422}
1423
1424fn struct_defn_body<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1425    Progress<'s, (StructDefinitionBody, Option<Vec<Where>>)>
1426{
1427    pm.alternate(pt)
1428        .one(map(struct_defn_body_brace, |(b, w)| (StructDefinitionBody::Brace(b), w)))
1429        .one(map(struct_defn_body_tuple, |(b, w)| (StructDefinitionBody::Tuple(b), w)))
1430        .one(map(ext(semicolon), |b| (StructDefinitionBody::Empty(b), None)))
1431        .finish()
1432}
1433
1434fn struct_defn_body_brace<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1435    Progress<'s, (StructDefinitionBodyBrace, Option<Vec<Where>>)>
1436{
1437    sequence!(pm, pt, {
1438        wheres = optional(where_clause);
1439        body   = struct_defn_body_brace_only;
1440    }, |_, _| (body, wheres))
1441}
1442
1443fn struct_defn_body_brace_only<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1444    Progress<'s, StructDefinitionBodyBrace>
1445{
1446    sequence!(pm, pt, {
1447        spt    = point;
1448        _      = left_curly;
1449        fields = zero_or_more_tailed_values(comma, attributed(struct_defn_field));
1450        _      = right_curly;
1451    }, |pm: &mut Master, pt| StructDefinitionBodyBrace { extent: pm.state.ex(spt, pt), fields, whitespace: Vec::new() })
1452}
1453
1454fn struct_defn_body_tuple<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1455    Progress<'s, (StructDefinitionBodyTuple, Option<Vec<Where>>)>
1456{
1457    sequence!(pm, pt, {
1458        spt    = point;
1459        fields = struct_defn_body_tuple_only;
1460        wheres = optional(where_clause);
1461        _      = semicolon;
1462    }, |pm: &mut Master, pt| (StructDefinitionBodyTuple { extent: pm.state.ex(spt, pt), fields, whitespace: Vec::new() }, wheres))
1463}
1464
1465fn struct_defn_body_tuple_only<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Vec<Attributed<StructDefinitionFieldUnnamed>>> {
1466    sequence!(pm, pt, {
1467        _     = left_paren;
1468        types = zero_or_more_tailed_values(comma, attributed(tuple_defn_field));
1469        _     = right_paren;
1470    }, |_, _| types)
1471}
1472
1473fn tuple_defn_field<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, StructDefinitionFieldUnnamed> {
1474    sequence!(pm, pt, {
1475        spt        = point;
1476        visibility = optional(visibility);
1477        typ        = typ;
1478    }, |pm: &mut Master, pt| StructDefinitionFieldUnnamed {
1479        extent: pm.state.ex(spt, pt),
1480        visibility,
1481        typ,
1482        whitespace: Vec::new(),
1483    })
1484}
1485
1486fn struct_defn_field<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, StructDefinitionFieldNamed> {
1487    sequence!(pm, pt, {
1488        spt        = point;
1489        visibility = optional(visibility);
1490        name       = ident;
1491        _          = colon;
1492        typ        = typ;
1493    }, |pm: &mut Master, pt| StructDefinitionFieldNamed {
1494        extent: pm.state.ex(spt, pt),
1495        visibility,
1496        name,
1497        typ,
1498        whitespace: Vec::new(),
1499    })
1500}
1501
1502fn p_union<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Union> {
1503    sequence!(pm, pt, {
1504        spt        = point;
1505        visibility = optional(visibility);
1506        _          = kw_union;
1507        name       = ident;
1508        generics   = optional(generic_declarations);
1509        wheres     = optional(where_clause);
1510        _          = left_curly;
1511        fields     = zero_or_more_tailed_values(comma, attributed(struct_defn_field));
1512        _          = right_curly;
1513    }, |pm: &mut Master, pt| Union {
1514        extent: pm.state.ex(spt, pt),
1515        visibility,
1516        name,
1517        generics,
1518        wheres: wheres.unwrap_or_else(Vec::new),
1519        fields,
1520        whitespace: Vec::new(),
1521    })
1522}
1523
1524fn p_enum<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Enum> {
1525    sequence!(pm, pt, {
1526        spt        = point;
1527        visibility = optional(visibility);
1528        _          = kw_enum;
1529        name       = ident;
1530        generics   = optional(generic_declarations);
1531        wheres     = optional(where_clause);
1532        _          = left_curly;
1533        variants   = zero_or_more_tailed_values(comma, attributed(enum_variant));
1534        _          = right_curly;
1535    }, |pm: &mut Master, pt| Enum {
1536        extent: pm.state.ex(spt, pt),
1537        visibility,
1538        name,
1539        generics,
1540        wheres: wheres.unwrap_or_else(Vec::new),
1541        variants,
1542        whitespace: Vec::new(),
1543    })
1544}
1545
1546fn enum_variant<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, EnumVariant> {
1547    sequence!(pm, pt, {
1548        spt  = point;
1549        name = ident;
1550        body = enum_variant_body;
1551    }, |pm: &mut Master, pt| EnumVariant {
1552        extent: pm.state.ex(spt, pt),
1553        name,
1554        body,
1555        whitespace: Vec::new(),
1556    })
1557}
1558
1559fn enum_variant_body<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, EnumVariantBody> {
1560    pm.alternate(pt)
1561        .one(map(struct_defn_body_tuple_only, EnumVariantBody::Tuple))
1562        .one(map(struct_defn_body_brace_only, EnumVariantBody::Struct))
1563        .one(map(optional(enum_discriminant), EnumVariantBody::Unit))
1564        .finish()
1565}
1566
1567fn enum_discriminant<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Attributed<Expression>> {
1568    sequence!(pm, pt, {
1569        _     = equals;
1570        value = expression;
1571    }, |_, _| value)
1572}
1573
1574fn p_trait<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Trait> {
1575    sequence!(pm, pt, {
1576        spt        = point;
1577        visibility = optional(visibility);
1578        is_unsafe  = optional(kw_unsafe);
1579        is_auto    = optional(kw_auto);
1580        _          = kw_trait;
1581        name       = ident;
1582        generics   = optional(generic_declarations);
1583        bounds     = optional(generic_declaration_type_bounds);
1584        wheres     = optional(where_clause);
1585        _          = left_curly;
1586        members    = zero_or_more(attributed(trait_impl_member));
1587        _          = right_curly;
1588    }, |pm: &mut Master, pt| Trait {
1589        extent: pm.state.ex(spt, pt),
1590        visibility,
1591        is_unsafe,
1592        is_auto,
1593        name,
1594        generics,
1595        bounds,
1596        wheres: wheres.unwrap_or_else(Vec::new),
1597        members,
1598        whitespace: Vec::new(),
1599    })
1600}
1601
1602// TOOD: this is a terrrrrrible name. It is not an impl!
1603fn trait_impl_member<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitMember> {
1604    pm.alternate(pt)
1605        .one(map(trait_member_function, TraitMember::Function))
1606        .one(map(trait_member_type, TraitMember::Type))
1607        .one(map(trait_member_const, TraitMember::Const))
1608        .one(map(item_macro_call, TraitMember::MacroCall))
1609        .finish()
1610}
1611
1612fn trait_member_function<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitMemberFunction> {
1613    sequence!(pm, pt, {
1614        spt    = point;
1615        header = trait_impl_function_header;
1616        body   = trait_impl_function_body;
1617    }, |pm: &mut Master, pt| TraitMemberFunction {
1618        extent: pm.state.ex(spt, pt),
1619        header,
1620        body,
1621        whitespace: Vec::new(),
1622    })
1623}
1624
1625fn trait_member_type<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitMemberType> {
1626    sequence!(pm, pt, {
1627        spt     = point;
1628        _       = kw_type;
1629        name    = ident;
1630        bounds  = optional(generic_declaration_type_bounds);
1631        default = optional(generic_declaration_type_default);
1632        _       = semicolon;
1633    }, |pm: &mut Master, pt| TraitMemberType {
1634        extent: pm.state.ex(spt, pt),
1635        name,
1636        bounds,
1637        default,
1638        whitespace: Vec::new(),
1639    })
1640}
1641
1642fn trait_member_const<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitMemberConst> {
1643    sequence!(pm, pt, {
1644        spt   = point;
1645        _     = kw_const;
1646        name  = ident;
1647        _     = colon;
1648        typ   = typ;
1649        value = optional(trait_member_const_value);
1650        _     = semicolon;
1651    }, |pm: &mut Master, pt| TraitMemberConst { extent: pm.state.ex(spt, pt), name, typ, value, whitespace: Vec::new() })
1652}
1653
1654fn trait_member_const_value<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Attributed<Expression>> {
1655    sequence!(pm, pt, {
1656        _     = equals;
1657        value = expression;
1658    }, |_, _| value)
1659}
1660
1661fn visibility<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Visibility> {
1662    pm.alternate(pt)
1663        .one(map(visibility_public, Visibility::Public))
1664        .one(map(kw_crate, Visibility::Crate))
1665        .finish()
1666}
1667
1668fn visibility_public<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, VisibilityPublic> {
1669    sequence!(pm, pt, {
1670        spt       = point;
1671        _         = kw_pub;
1672        qualifier = optional(visibility_public_qualifier);
1673    }, |pm: &mut Master, pt| VisibilityPublic { extent: pm.state.ex(spt, pt), qualifier, whitespace: Vec::new() })
1674}
1675
1676fn visibility_public_qualifier<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1677    Progress<'s, VisibilityPublicQualifier>
1678{
1679    sequence!(pm, pt, {
1680        _         = left_paren;
1681        qualifier = visibility_qualifier_kind;
1682        _         = right_paren;
1683    }, |_, _| qualifier)
1684}
1685
1686fn visibility_qualifier_kind<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1687    Progress<'s, VisibilityPublicQualifier>
1688{
1689    pm.alternate(pt)
1690        .one(map(kw_self_ident, |_| VisibilityPublicQualifier::SelfIdent))
1691        .one(map(kw_crate, |_| VisibilityPublicQualifier::Crate))
1692        .one(map(path, VisibilityPublicQualifier::Path))
1693        .finish()
1694}
1695
1696// TODO: Massively duplicated!!!
1697fn trait_impl_function_header<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitImplFunctionHeader> {
1698    sequence!(pm, pt, {
1699        spt         = point;
1700        visibility  = optional(visibility);
1701        qualifiers  = function_qualifiers; // TODO: shouldn't allow const / default
1702        _           = kw_fn;
1703        name        = ident;
1704        generics    = optional(generic_declarations);
1705        arguments   = trait_impl_function_arglist;
1706        return_type = optional(function_return_type);
1707        wheres      = optional(where_clause);
1708    }, |pm: &mut Master, pt| {
1709        TraitImplFunctionHeader {
1710            extent: pm.state.ex(spt, pt),
1711            visibility,
1712            qualifiers,
1713            name,
1714            generics,
1715            arguments,
1716            return_type,
1717            wheres: wheres.unwrap_or_else(Vec::new),
1718            whitespace: Vec::new(),
1719        }})
1720}
1721
1722fn trait_impl_function_arglist<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Vec<TraitImplArgument>> {
1723    sequence!(pm, pt, {
1724        _        = left_paren;
1725        self_arg = optional(map(self_argument, TraitImplArgument::SelfArgument));
1726        args     = zero_or_more_tailed_values_append(self_arg, comma, trait_impl_function_argument);
1727        _        = right_paren;
1728    }, move |_, _| args)
1729}
1730
1731fn trait_impl_function_argument<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TraitImplArgument> {
1732    sequence!(pm, pt, {
1733        spt  = point;
1734        name = optional(trait_impl_function_argument_name);
1735        typ  = typ;
1736    }, |pm: &mut Master, pt| TraitImplArgument::Named(TraitImplArgumentNamed {
1737        extent: pm.state.ex(spt, pt),
1738        name,
1739        typ,
1740        whitespace: Vec::new(),
1741    }))
1742}
1743
1744fn trait_impl_function_argument_name<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1745    Progress<'s, Pattern>
1746{
1747    sequence!(pm, pt, {
1748        name = pattern;
1749        _    = colon;
1750    }, |_, _| name)
1751}
1752
1753fn trait_impl_function_body<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Option<Block>> {
1754    pm.alternate(pt)
1755        .one(map(block, Some))
1756        .one(map(semicolon, |_| None))
1757        .finish()
1758}
1759
1760fn p_impl<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Impl> {
1761    sequence!(pm, pt, {
1762        spt       = point;
1763        is_unsafe = optional(ext(kw_unsafe));
1764        _         = kw_impl;
1765        generics  = optional(generic_declarations);
1766        kind      = p_impl_kind;
1767        wheres    = optional(where_clause);
1768        _         = left_curly;
1769        body      = zero_or_more(attributed(impl_member));
1770        _         = right_curly;
1771    }, |pm: &mut Master, pt| Impl {
1772        extent: pm.state.ex(spt, pt),
1773        is_unsafe,
1774        generics,
1775        kind,
1776        wheres: wheres.unwrap_or_else(Vec::new),
1777        body,
1778        whitespace: Vec::new(),
1779    })
1780}
1781
1782fn p_impl_kind<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ImplKind> {
1783    pm.alternate(pt)
1784        .one(map(p_impl_of_trait, ImplKind::Trait))
1785        .one(map(p_impl_of_inherent, ImplKind::Inherent))
1786        .finish()
1787}
1788
1789fn p_impl_of_inherent<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ImplOfInherent> {
1790    sequence!(pm, pt, {
1791        spt       = point;
1792        type_name = typ;
1793    }, |pm: &mut Master, pt| ImplOfInherent {
1794        extent: pm.state.ex(spt, pt),
1795        type_name,
1796        whitespace: Vec::new(),
1797    })
1798}
1799
1800fn p_impl_of_trait<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ImplOfTrait> {
1801    sequence!(pm, pt, {
1802        spt         = point;
1803        is_negative = optional(ext(bang));
1804        trait_name  = typ;
1805        _           = kw_for;
1806        type_name   = type_or_wildcard;
1807    }, |pm: &mut Master, pt| ImplOfTrait {
1808        extent: pm.state.ex(spt, pt),
1809        is_negative,
1810        trait_name,
1811        type_name,
1812        whitespace: Vec::new(),
1813    })
1814}
1815
1816fn type_or_wildcard<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ImplOfTraitType> {
1817    pm.alternate(pt)
1818        .one(map(typ, ImplOfTraitType::Type))
1819        .one(map(ext(double_period), ImplOfTraitType::Wildcard))
1820        .finish()
1821}
1822
1823fn impl_member<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ImplMember> {
1824    pm.alternate(pt)
1825        .one(map(impl_const, ImplMember::Const))
1826        .one(map(impl_function, ImplMember::Function))
1827        .one(map(impl_type, ImplMember::Type))
1828        .one(map(item_macro_call, ImplMember::MacroCall))
1829        .finish()
1830}
1831
1832fn impl_function<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ImplFunction> {
1833    sequence!(pm, pt, {
1834        spt    = point;
1835        header = function_header;
1836        body   = block;
1837    }, |pm: &mut Master, pt| ImplFunction {
1838        extent: pm.state.ex(spt, pt),
1839        header,
1840        body,
1841        whitespace: Vec::new(),
1842    })
1843}
1844
1845fn impl_type<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ImplType> {
1846    sequence!(pm, pt, {
1847        spt  = point;
1848        _    = kw_type;
1849        name = ident;
1850        _    = equals;
1851        typ  = typ;
1852        _    = semicolon;
1853    }, |pm: &mut Master, pt| ImplType { extent: pm.state.ex(spt, pt), name, typ, whitespace: Vec::new() })
1854}
1855
1856fn impl_const<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ImplConst> {
1857    sequence!(pm, pt, {
1858        spt        = point;
1859        visibility = optional(visibility);
1860        _          = kw_const;
1861        name       = ident;
1862        _          = colon;
1863        typ        = typ;
1864        _          = equals;
1865        value      = expression;
1866        _          = semicolon;
1867    }, |pm: &mut Master, pt| ImplConst {
1868        extent: pm.state.ex(spt, pt),
1869        visibility, name,
1870        typ,
1871        value,
1872        whitespace: Vec::new(),
1873    })
1874}
1875
1876// TODO: optional could take E that is `into`, or just a different one
1877
1878fn p_const<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Const> {
1879    sequence!(pm, pt, {
1880        spt        = point;
1881        visibility = optional(visibility);
1882        _          = kw_const;
1883        name       = ident;
1884        _          = colon;
1885        typ        = typ;
1886        _          = equals;
1887        value      = expression;
1888        _          = semicolon;
1889    }, |pm: &mut Master, pt| Const { extent: pm.state.ex(spt, pt), visibility, name, typ, value, whitespace: Vec::new() })
1890}
1891
1892fn p_static<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Static> {
1893    sequence!(pm, pt, {
1894        spt        = point;
1895        visibility = optional(visibility);
1896        _          = kw_static;
1897        is_mut     = optional(ext(kw_mut));
1898        name       = ident;
1899        _          = colon;
1900        typ        = typ;
1901        _          = equals;
1902        value      = expression;
1903        _          = semicolon;
1904    }, |pm: &mut Master, pt| Static { extent: pm.state.ex(spt, pt), visibility, is_mut, name, typ, value, whitespace: Vec::new() })
1905}
1906
1907fn extern_crate<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Crate> {
1908    sequence!(pm, pt, {
1909        spt        = point;
1910        visibility = optional(visibility);
1911        _          = kw_extern;
1912        _          = kw_crate;
1913        name       = ident;
1914        rename     = optional(extern_crate_rename);
1915        _          = semicolon;
1916    }, |pm: &mut Master, pt| Crate { extent: pm.state.ex(spt, pt), visibility, name, rename, whitespace: Vec::new() })
1917}
1918
1919fn extern_crate_rename<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Ident> {
1920    sequence!(pm, pt, {
1921        _    = kw_as;
1922        name = ident;
1923    }, |_, _| name)
1924}
1925
1926fn extern_block<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ExternBlock> {
1927    sequence!(pm, pt, {
1928        spt     = point;
1929        _       = kw_extern;
1930        abi     = optional(string_literal);
1931        _       = left_curly;
1932        members = zero_or_more(attributed(extern_block_member));
1933        _       = right_curly;
1934    }, |pm: &mut Master, pt| ExternBlock { extent: pm.state.ex(spt, pt), abi, members, whitespace: Vec::new() })
1935}
1936
1937fn extern_block_member<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ExternBlockMember> {
1938    pm.alternate(pt)
1939        .one(map(extern_block_static, ExternBlockMember::Static))
1940        .one(map(extern_block_type, ExternBlockMember::Type))
1941        .one(map(extern_block_member_function, ExternBlockMember::Function))
1942        .finish()
1943}
1944
1945// TODO: very similar to regular statics; DRY
1946fn extern_block_static<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ExternBlockMemberStatic> {
1947    sequence!(pm, pt, {
1948        spt        = point;
1949        visibility = optional(visibility);
1950        _          = kw_static;
1951        is_mut     = optional(ext(kw_mut));
1952        name       = ident;
1953        _          = colon;
1954        typ        = typ;
1955        _          = semicolon;
1956    }, |pm: &mut Master, pt| ExternBlockMemberStatic { extent: pm.state.ex(spt, pt), visibility, is_mut, name, typ, whitespace: Vec::new() })
1957}
1958
1959fn extern_block_type<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, ExternBlockMemberType> {
1960    sequence!(pm, pt, {
1961        spt        = point;
1962        visibility = optional(visibility);
1963        _          = kw_type;
1964        name       = ident;
1965        _          = semicolon;
1966    }, |pm: &mut Master, pt| ExternBlockMemberType {
1967        extent: pm.state.ex(spt, pt),
1968        visibility,
1969        name,
1970        whitespace: Vec::new(),
1971    })
1972}
1973
1974// TODO: Massively duplicated!!!
1975fn extern_block_member_function<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
1976    Progress<'s, ExternBlockMemberFunction>
1977{
1978    sequence!(pm, pt, {
1979        spt         = point;
1980        visibility  = optional(visibility);
1981        _           = kw_fn;
1982        name        = ident;
1983        generics    = optional(generic_declarations);
1984        arguments   = extern_block_function_arglist;
1985        return_type = optional(function_return_type);
1986        wheres      = optional(where_clause);
1987        _           = semicolon;
1988    }, |pm: &mut Master, pt| {
1989        ExternBlockMemberFunction {
1990            extent: pm.state.ex(spt, pt),
1991            visibility,
1992            name,
1993            generics,
1994            arguments,
1995            return_type,
1996            wheres: wheres.unwrap_or_else(Vec::new),
1997            whitespace: Vec::new(),
1998        }
1999    })
2000}
2001
2002fn extern_block_function_arglist<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2003    Progress<'s, Vec<ExternBlockMemberFunctionArgument>>
2004{
2005    sequence!(pm, pt, {
2006        _    = left_paren;
2007        args = zero_or_more_tailed_values(comma, extern_block_function_argument);
2008        _    = right_paren;
2009    }, move |_, _| args)
2010}
2011
2012fn extern_block_function_argument<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2013    Progress<'s, ExternBlockMemberFunctionArgument>
2014{
2015    pm.alternate(pt)
2016        .one(map(extern_block_function_argument_named, ExternBlockMemberFunctionArgument::Named))
2017        .one(map(extern_block_function_argument_variadic, ExternBlockMemberFunctionArgument::Variadic))
2018        .finish()
2019}
2020
2021fn extern_block_function_argument_named<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2022    Progress<'s, ExternBlockMemberFunctionArgumentNamed>
2023{
2024    sequence!(pm, pt, {
2025        spt  = point;
2026        name = pattern;
2027        _    = colon;
2028        typ  = typ;
2029    }, |pm: &mut Master, pt| ExternBlockMemberFunctionArgumentNamed {
2030        extent: pm.state.ex(spt, pt),
2031        name,
2032        typ,
2033        whitespace: Vec::new(),
2034    })
2035}
2036
2037fn extern_block_function_argument_variadic<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2038    Progress<'s, ExternBlockMemberFunctionArgumentVariadic>
2039{
2040    sequence!(pm, pt, {
2041        spt = point;
2042        _   = triple_period;
2043    }, |pm: &mut Master, pt| ExternBlockMemberFunctionArgumentVariadic { extent: pm.state.ex(spt, pt) })
2044}
2045
2046fn p_use<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Use> {
2047    sequence!(pm, pt, {
2048        spt        = point;
2049        visibility = optional(visibility);
2050        _          = kw_use;
2051        _          = optional(double_colon);
2052        path       = use_path;
2053        _          = semicolon;
2054    }, move |pm: &mut Master, pt| {
2055        Use { extent: pm.state.ex(spt, pt), visibility, path, whitespace: Vec::new() }
2056    })
2057}
2058
2059fn use_path<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, UsePath> {
2060    sequence!(pm, pt, {
2061        spt  = point;
2062        path = zero_or_more(use_path_component);
2063        tail = use_tail;
2064    }, move |pm: &mut Master, pt| UsePath {
2065        extent: pm.state.ex(spt, pt),
2066        path,
2067        tail,
2068        whitespace: Vec::new(),
2069    })
2070}
2071
2072fn use_path_component<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Ident> {
2073    sequence!(pm, pt, {
2074        name = path_member;
2075        _    = double_colon;
2076    }, |_, _| name)
2077}
2078
2079fn path_member<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Ident> {
2080    pm.alternate(pt)
2081        .one(map(kw_crate, |extent| Ident { extent }))
2082        .one(ident)
2083        .finish()
2084}
2085
2086fn use_tail<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, UseTail> {
2087    pm.alternate(pt)
2088        .one(map(use_tail_ident, UseTail::Ident))
2089        .one(map(use_tail_glob, UseTail::Glob))
2090        .one(map(use_tail_multi, UseTail::Multi))
2091        .finish()
2092}
2093
2094fn use_tail_ident<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, UseTailIdent> {
2095    sequence!(pm, pt, {
2096        spt    = point;
2097        name   = ident;
2098        rename = optional(use_tail_ident_rename);
2099    }, |pm: &mut Master, pt| UseTailIdent {
2100        extent: pm.state.ex(spt, pt),
2101        name,
2102        rename,
2103        whitespace: Vec::new(),
2104    })
2105}
2106
2107fn use_tail_ident_rename<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Ident> {
2108    sequence!(pm, pt, {
2109        _    = kw_as;
2110        name = ident;
2111    }, |_, _| name)
2112}
2113
2114fn use_tail_glob<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, UseTailGlob> {
2115    sequence!(pm, pt, {
2116        spt = point;
2117        _   = asterisk;
2118    }, |pm: &mut Master, pt| UseTailGlob { extent: pm.state.ex(spt, pt) })
2119}
2120
2121fn use_tail_multi<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, UseTailMulti> {
2122    sequence!(pm, pt, {
2123        spt   = point;
2124        _     = left_curly;
2125        paths = zero_or_more_tailed_values(comma, use_path);
2126        _     = right_curly;
2127    }, |pm: &mut Master, pt| UseTailMulti {
2128        extent: pm.state.ex(spt, pt),
2129        paths,
2130        whitespace: Vec::new(),
2131    })
2132}
2133
2134fn type_alias<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeAlias> {
2135    sequence!(pm, pt, {
2136        spt        = point;
2137        visibility = optional(visibility);
2138        _          = kw_type;
2139        name       = ident;
2140        generics   = optional(generic_declarations);
2141        wheres     = optional(where_clause);
2142        _          = equals;
2143        defn       = typ;
2144        _          = semicolon;
2145    }, |pm: &mut Master, pt| TypeAlias {
2146        extent: pm.state.ex(spt, pt),
2147        visibility,
2148        name,
2149        generics,
2150        wheres: wheres.unwrap_or_else(Vec::new),
2151        defn,
2152        whitespace: Vec::new(),
2153    })
2154}
2155
2156fn module<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Module> {
2157    sequence!(pm, pt, {
2158        spt        = point;
2159        visibility = optional(visibility);
2160        _          = kw_mod;
2161        name       = ident;
2162        body       = module_body_or_not;
2163    }, |pm: &mut Master, pt| Module { extent: pm.state.ex(spt, pt), visibility, name, body, whitespace: Vec::new() })
2164}
2165
2166fn module_body_or_not<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Option<Vec<Attributed<Item>>>> {
2167    pm.alternate(pt)
2168        .one(map(module_body, Some))
2169        .one(map(semicolon, |_| None))
2170        .finish()
2171}
2172
2173fn module_body<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Vec<Attributed<Item>>> {
2174    sequence!(pm, pt, {
2175        _    = left_curly;
2176        body = zero_or_more(attributed(item));
2177        _    = right_curly;
2178    }, |_, _| body)
2179}
2180
2181fn typ<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Type> {
2182    sequence!(pm, pt, {
2183        spt        = point;
2184        kind       = typ_kind;
2185        additional = zero_or_more_tailed_values_resume(plus, typ_additional);
2186    }, |pm: &mut Master, pt| Type {
2187        extent: pm.state.ex(spt, pt),
2188        kind,
2189        additional,
2190        whitespace: Vec::new(),
2191    })
2192}
2193
2194fn typ_single<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Type> {
2195    sequence!(pm, pt, {
2196        spt  = point;
2197        kind = typ_kind;
2198    }, |pm: &mut Master, pt| Type {
2199        extent: pm.state.ex(spt, pt),
2200        kind,
2201        additional: vec![],
2202        whitespace: Vec::new(),
2203    })
2204}
2205
2206fn typ_kind<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeKind> {
2207    pm.alternate(pt)
2208        .one(map(typ_array, TypeKind::Array))
2209        .one(map(typ_disambiguation, TypeKind::Disambiguation))
2210        .one(map(typ_function, TypeKind::Function))
2211        .one(map(typ_higher_ranked_trait_bounds, TypeKind::HigherRankedTraitBounds))
2212        .one(map(typ_dyn_trait, TypeKind::DynTrait))
2213        .one(map(typ_impl_trait, TypeKind::ImplTrait))
2214        .one(map(typ_named, TypeKind::Named))
2215        .one(map(typ_pointer, TypeKind::Pointer))
2216        .one(map(typ_reference, TypeKind::Reference))
2217        .one(map(typ_slice, TypeKind::Slice))
2218        .one(map(typ_tuple, TypeKind::Tuple))
2219        .one(map(ext(bang), TypeKind::Uninhabited))
2220        .finish()
2221}
2222
2223fn typ_reference<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeReference> {
2224    sequence!(pm, pt, {
2225        spt  = point;
2226        kind = typ_reference_kind;
2227        typ  = typ;
2228    }, |pm: &mut Master, pt| TypeReference {
2229        extent: pm.state.ex(spt, pt),
2230        kind,
2231        typ: Box::new(typ),
2232        whitespace: Vec::new(),
2233    })
2234}
2235
2236fn typ_reference_kind<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeReferenceKind> {
2237    sequence!(pm, pt, {
2238        spt      = point;
2239        _        = ampersand;
2240        lifetime = optional(lifetime);
2241        mutable  = optional(ext(kw_mut));
2242    }, |pm: &mut Master, pt| TypeReferenceKind { extent: pm.state.ex(spt, pt), lifetime, mutable, whitespace: Vec::new() })
2243}
2244
2245fn typ_pointer<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypePointer> {
2246    sequence!(pm, pt, {
2247        spt  = point;
2248        _    = asterisk;
2249        kind = typ_pointer_kind;
2250        typ  = typ;
2251    }, |pm: &mut Master, pt| TypePointer { extent: pm.state.ex(spt, pt), kind, typ: Box::new(typ), whitespace: Vec::new() })
2252}
2253
2254fn typ_pointer_kind<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypePointerKind> {
2255    pm.alternate(pt)
2256        .one(map(kw_const, |_| TypePointerKind::Const))
2257        .one(map(kw_mut, |_| TypePointerKind::Mutable))
2258        .finish()
2259}
2260
2261fn typ_tuple<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeTuple> {
2262    sequence!(pm, pt, {
2263        spt   = point;
2264        _     = left_paren;
2265        types = zero_or_more_tailed_values(comma, typ);
2266        _     = right_paren;
2267    }, |pm: &mut Master, pt| TypeTuple {
2268        extent: pm.state.ex(spt, pt),
2269        types,
2270        whitespace: Vec::new(),
2271    })
2272}
2273
2274fn typ_higher_ranked_trait_bounds<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2275    Progress<'s, TypeHigherRankedTraitBounds>
2276{
2277    sequence!(pm, pt, {
2278        spt       = point;
2279        lifetimes = higher_ranked_trait_bounds;
2280        child     = typ_higher_ranked_trait_bounds_child;
2281    }, |pm: &mut Master, pt| TypeHigherRankedTraitBounds { extent: pm.state.ex(spt, pt), lifetimes, child, whitespace: Vec::new() })
2282}
2283
2284fn higher_ranked_trait_bounds<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2285    Progress<'s, Vec<Lifetime>>
2286{
2287    sequence!(pm, pt, {
2288        _         = kw_for;
2289        _         = left_angle;
2290        lifetimes = zero_or_more_tailed_values(comma, lifetime);
2291        _         = right_angle;
2292    }, |_, _| lifetimes)
2293}
2294
2295fn typ_higher_ranked_trait_bounds_child<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2296    Progress<'s, TypeHigherRankedTraitBoundsChild>
2297{
2298    pm.alternate(pt)
2299        .one(map(typ_named, TypeHigherRankedTraitBoundsChild::Named))
2300        .one(map(typ_function, TypeHigherRankedTraitBoundsChild::Function))
2301        .one(map(typ_reference, TypeHigherRankedTraitBoundsChild::Reference))
2302        .finish()
2303}
2304
2305fn typ_dyn_trait<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeDynTrait> {
2306    sequence!(pm, pt, {
2307        spt  = point;
2308        _    = kw_dyn;
2309        name = typ_named;
2310    }, |pm: &mut Master, pt| TypeDynTrait { extent: pm.state.ex(spt, pt), name, whitespace: Vec::new() })
2311}
2312
2313fn typ_impl_trait<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeImplTrait> {
2314    sequence!(pm, pt, {
2315        spt  = point;
2316        _    = kw_impl;
2317        name = typ_named;
2318    }, |pm: &mut Master, pt| TypeImplTrait { extent: pm.state.ex(spt, pt), name, whitespace: Vec::new() })
2319}
2320
2321fn typ_additional<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2322    Progress<'s, TypeAdditional>
2323{
2324    pm.alternate(pt)
2325        .one(map(typ_named, TypeAdditional::Named))
2326        .one(map(lifetime, TypeAdditional::Lifetime))
2327        .finish()
2328}
2329
2330fn typ_named<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeNamed> {
2331    sequence!(pm, pt, {
2332        spt  = point;
2333        _    = optional(double_colon);
2334        path = one_or_more_tailed_values(double_colon, typ_named_component);
2335    }, |pm: &mut Master, pt| TypeNamed {
2336        extent: pm.state.ex(spt, pt),
2337        path,
2338        whitespace: Vec::new(),
2339    })
2340}
2341
2342fn typ_named_component<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeNamedComponent> {
2343    sequence!(pm, pt, {
2344        spt      = point;
2345        ident    = path_member;
2346        generics = optional(typ_generics);
2347    }, |pm: &mut Master, pt| TypeNamedComponent {
2348        extent: pm.state.ex(spt, pt),
2349        ident,
2350        generics,
2351        whitespace: Vec::new(),
2352    })
2353}
2354
2355fn typ_disambiguation<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeDisambiguation> {
2356    sequence!(pm, pt, {
2357        spt  = point;
2358        core = disambiguation_core;
2359        path = zero_or_more_tailed_values_resume(double_colon, typ_named_component);
2360    }, move |pm: &mut Master, pt| TypeDisambiguation {
2361        extent: pm.state.ex(spt, pt),
2362        from_type: Box::new(core.from_type),
2363        to_type: core.to_type.map(Box::new),
2364        path,
2365        whitespace: core.whitespace,
2366    })
2367}
2368
2369struct DisambiguationCore {
2370    from_type: Type,
2371    to_type: Option<TypeNamed>,
2372    whitespace: Vec<Whitespace>,
2373}
2374
2375fn disambiguation_core<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, DisambiguationCore> {
2376    sequence!(pm, pt, {
2377        _         = left_angle;
2378        from_type = typ;
2379        to_type   = optional(disambiguation_core_to_type);
2380        _         = right_angle;
2381    }, |_, _| DisambiguationCore { from_type, to_type, whitespace: Vec::new() })
2382}
2383
2384fn disambiguation_core_to_type<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2385    Progress<'s, TypeNamed>
2386{
2387    sequence!(pm, pt, {
2388        _       = kw_as;
2389        to_type = typ_named;
2390    }, |_, _| to_type)
2391}
2392
2393fn typ_array<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeArray> {
2394    sequence!(pm, pt, {
2395        spt   = point;
2396        _     = left_square;
2397        typ   = typ;
2398        _     = semicolon;
2399        count = expression;
2400        _     = right_square;
2401    }, |pm: &mut Master, pt| TypeArray {
2402        extent: pm.state.ex(spt, pt),
2403        typ: Box::new(typ),
2404        count: Box::new(count),
2405        whitespace: Vec::new(),
2406    })
2407}
2408
2409fn typ_slice<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeSlice> {
2410    sequence!(pm, pt, {
2411        spt = point;
2412        _   = left_square;
2413        typ = typ;
2414        _   = right_square;
2415    }, |pm: &mut Master, pt| TypeSlice { extent: pm.state.ex(spt, pt), typ: Box::new(typ), whitespace: Vec::new() })
2416}
2417
2418fn typ_generics<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeGenerics> {
2419    pm.alternate(pt)
2420        .one(map(typ_generics_fn, TypeGenerics::Function))
2421        .one(map(typ_generics_angle, TypeGenerics::Angle))
2422        .finish()
2423}
2424
2425fn typ_generics_fn<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeGenericsFunction> {
2426    sequence!(pm, pt, {
2427        spt         = point;
2428        _           = left_paren;
2429        types       = zero_or_more_tailed_values(comma, typ);
2430        _           = right_paren;
2431        return_type = optional(function_return_type);
2432    }, |pm: &mut Master, pt| TypeGenericsFunction {
2433        extent: pm.state.ex(spt, pt),
2434        types,
2435        return_type: return_type.map(Box::new),
2436        whitespace: Vec::new(),
2437    })
2438}
2439
2440fn typ_generics_angle<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeGenericsAngle> {
2441    sequence!(pm, pt, {
2442        spt     = point;
2443        _       = left_angle;
2444        members = zero_or_more_tailed_values(comma, typ_generics_angle_member);
2445        _       = right_angle;
2446    }, |pm: &mut Master, pt| TypeGenericsAngle { extent: pm.state.ex(spt, pt), members, whitespace: Vec::new() })
2447}
2448
2449// Parsing all of these equally is a bit inconsistent with the
2450// compler. The compiler *parses* lifetimes after types, but later
2451// errors about it. It does *not* parse associated types before types
2452// though.
2453fn typ_generics_angle_member<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2454    Progress<'s, TypeGenericsAngleMember>
2455{
2456    pm.alternate(pt)
2457        .one(map(associated_type, TypeGenericsAngleMember::AssociatedType))
2458        .one(map(lifetime, TypeGenericsAngleMember::Lifetime))
2459        .one(map(typ, TypeGenericsAngleMember::Type))
2460        .finish()
2461}
2462
2463fn associated_type<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, AssociatedType> {
2464    sequence!(pm, pt, {
2465        spt   = point;
2466        name  = ident;
2467        value = associated_type_value;
2468    }, |pm: &mut Master, pt| AssociatedType { extent: pm.state.ex(spt, pt), name, value, whitespace: Vec::new() })
2469}
2470
2471fn associated_type_value<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2472    Progress<'s, AssociatedTypeValue>
2473{
2474    pm.alternate(pt)
2475        .one(map(associated_type_value_equal, AssociatedTypeValue::Equal))
2476        .one(map(associated_type_value_bound, AssociatedTypeValue::Bound))
2477        .finish()
2478}
2479
2480fn associated_type_value_equal<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, AssociatedTypeValueEqual> {
2481    sequence!(pm, pt, {
2482        spt   = point;
2483        _     = equals;
2484        value = typ;
2485    }, |pm: &mut Master, pt| AssociatedTypeValueEqual { extent: pm.state.ex(spt, pt), value, whitespace: Vec::new() })
2486}
2487
2488fn associated_type_value_bound<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, AssociatedTypeValueBound> {
2489    sequence!(pm, pt, {
2490        spt    = point;
2491        _      = colon;
2492        bounds = trait_bounds;
2493    }, |pm: &mut Master, pt| AssociatedTypeValueBound { extent: pm.state.ex(spt, pt), bounds, whitespace: Vec::new() })
2494}
2495
2496fn typ_function<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, TypeFunction> {
2497    sequence!(pm, pt, {
2498        spt         = point;
2499        qualifiers  = function_qualifiers; // TODO: shouldn't allow const / default
2500        _           = kw_fn;
2501        _           = left_paren;
2502        arguments   = zero_or_more_tailed_values(comma, typ_function_argument);
2503        arguments   = zero_or_more_tailed_values_append(arguments, comma, typ_function_argument_variadic);
2504        _           = right_paren;
2505        return_type = optional(function_return_type);
2506    }, |pm: &mut Master, pt| TypeFunction {
2507        extent: pm.state.ex(spt, pt),
2508        qualifiers,
2509        arguments,
2510        return_type: return_type.map(Box::new),
2511        whitespace: Vec::new(),
2512    })
2513}
2514
2515fn typ_function_argument<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2516    Progress<'s, TypeFunctionArgument>
2517{
2518    sequence!(pm, pt, {
2519        spt  = point;
2520        name = optional(typ_function_argument_name);
2521        typ  = typ;
2522    }, |pm: &mut Master, pt| TypeFunctionArgument::Named(TypeFunctionArgumentNamed {
2523        extent: pm.state.ex(spt, pt),
2524        name,
2525        typ,
2526        whitespace: Vec::new(),
2527    }))
2528}
2529
2530fn typ_function_argument_name<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2531    Progress<'s, Ident>
2532{
2533    sequence!(pm, pt, {
2534        name = ident;
2535        _    = colon;
2536    }, |_, _| name)
2537}
2538
2539fn typ_function_argument_variadic<'s>(pm: &mut Master<'s>, pt: Point<'s>) ->
2540    Progress<'s, TypeFunctionArgument>
2541{
2542    map(triple_period, TypeFunctionArgument::Variadic)(pm, pt)
2543}
2544
2545fn lifetime<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Lifetime> {
2546    lifetime_normal(pm, pt)
2547        .map(|extent| Lifetime { extent, name: Ident { extent } })
2548    // FIXME: value; can we actually have whitespace here?
2549}
2550
2551pub(crate) fn attributed<'s, F, T>(f: F) ->
2552    impl Fn(&mut Master<'s>, Point<'s>) -> Progress<'s, Attributed<T>>
2553where
2554    F: Fn(&mut Master<'s>, Point<'s>) -> Progress<'s, T>
2555{
2556    move |pm, pt| {
2557        sequence!(pm, pt, {
2558            spt        = point;
2559            attributes = zero_or_more(attribute);
2560            value      = f;
2561        }, |pm: &mut Master<'s>, pt| Attributed {
2562            extent: pm.state.ex(spt, pt),
2563            attributes,
2564            value,
2565            whitespace: Vec::new(),
2566        })
2567    }
2568}
2569
2570fn attribute<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, Attribute> {
2571    pm.alternate(pt)
2572        .one(map(doc_comment_outer_line, Attribute::DocCommentLine))
2573        .one(map(doc_comment_outer_block, Attribute::DocCommentBlock))
2574        .one(map(attribute_literal, Attribute::Literal))
2575        .finish()
2576}
2577
2578fn attribute_literal<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, AttributeLiteral> {
2579    sequence!(pm, pt, {
2580        spt  = point;
2581        _    = hash;
2582        _    = left_square;
2583        text = parse_nested_until(Token::is_left_square, Token::is_right_square);
2584        _    = right_square;
2585    }, |pm: &mut Master, pt| AttributeLiteral {
2586        extent: pm.state.ex(spt, pt),
2587        text,
2588        whitespace: Vec::new(),
2589    })
2590}
2591
2592fn attribute_containing<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, AttributeContaining> {
2593    pm.alternate(pt)
2594        .one(map(doc_comment_inner_line, AttributeContaining::DocCommentLine))
2595        .one(map(doc_comment_inner_block, AttributeContaining::DocCommentBlock))
2596        .one(map(attribute_containing_literal, AttributeContaining::Literal))
2597        .finish()
2598}
2599
2600fn attribute_containing_literal<'s>(pm: &mut Master<'s>, pt: Point<'s>) -> Progress<'s, AttributeContainingLiteral> {
2601    sequence!(pm, pt, {
2602        spt  = point;
2603        _    = hash;
2604        _    = bang;
2605        _    = left_square;
2606        text = parse_nested_until(Token::is_left_square, Token::is_right_square);
2607        _    = right_square;
2608    }, |pm: &mut Master, pt| AttributeContainingLiteral {
2609        extent: pm.state.ex(spt, pt),
2610        text,
2611        whitespace: Vec::new(),
2612    })
2613}
2614
2615#[cfg(test)]
2616mod test {
2617    use super::*;
2618    use super::test_utils::*;
2619
2620    #[test]
2621    fn parse_use() {
2622        let p = qp(p_use, "use foo::Bar;");
2623        assert_extent!(p, (0, 13))
2624    }
2625
2626    #[test]
2627    fn parse_use_public() {
2628        let p = qp(p_use, "pub use foo::Bar;");
2629        assert_extent!(p, (0, 17))
2630    }
2631
2632    #[test]
2633    fn parse_use_glob() {
2634        let p = qp(p_use, "use foo::*;");
2635        assert_extent!(p, (0, 11))
2636    }
2637
2638    #[test]
2639    fn parse_use_with_multi() {
2640        let p = qp(p_use, "use foo::{Bar, Baz};");
2641        assert_extent!(p, (0, 20))
2642    }
2643
2644    #[test]
2645    fn parse_use_no_path() {
2646        let p = qp(p_use, "use {Bar, Baz};");
2647        assert_extent!(p, (0, 15))
2648    }
2649
2650    #[test]
2651    fn parse_use_absolute_path() {
2652        let p = qp(p_use, "use ::{Bar, Baz};");
2653        assert_extent!(p, (0, 17))
2654    }
2655
2656    #[test]
2657    fn parse_use_rename() {
2658        let p = qp(p_use, "use foo as bar;");
2659        assert_extent!(p, (0, 15))
2660    }
2661
2662    #[test]
2663    fn parse_use_with_multi_rename() {
2664        let p = qp(p_use, "use foo::{bar as a, baz as b};");
2665        assert_extent!(p, (0, 30))
2666    }
2667
2668    #[test]
2669    fn parse_use_with_nested() {
2670        let p = qp(p_use, "use foo::{self, inner::{self, Type}};");
2671        assert_extent!(p, (0, 37))
2672    }
2673
2674    #[test]
2675    fn parse_use_with_crate() {
2676        let p = qp(p_use, "use crate::foo;");
2677        assert_extent!(p, (0, 15))
2678    }
2679
2680    #[test]
2681    fn parse_use_all_space() {
2682        let p = qp(p_use, "use foo :: { bar as a , baz as b } ;");
2683        assert_extent!(p, (0, 36))
2684    }
2685
2686    #[test]
2687    fn item_mod_multiple() {
2688        let p = qp(item, "mod foo { use super::*; }");
2689        assert_extent!(p, (0, 25))
2690    }
2691
2692    #[test]
2693    fn item_macro_call_with_parens() {
2694        let p = qp(item, "foo!();");
2695        assert_extent!(p, (0, 7))
2696    }
2697
2698    #[test]
2699    fn item_macro_call_with_square_brackets() {
2700        let p = qp(item, "foo![];");
2701        assert_extent!(p, (0, 7))
2702    }
2703
2704    #[test]
2705    fn item_macro_call_with_curly_braces() {
2706        let p = qp(item, "foo! { }");
2707        assert_extent!(p, (0, 8))
2708    }
2709
2710    #[test]
2711    fn item_macro_call_with_ident() {
2712        let p = qp(item, "macro_rules! name { }");
2713        assert_extent!(p, (0, 21))
2714    }
2715
2716    #[test]
2717    fn item_macro_call_with_path() {
2718        let p = qp(item, "foo::bar!();");
2719        assert_extent!(p, (0, 12))
2720    }
2721
2722    #[test]
2723    fn item_macro_call_all_space() {
2724        let p = qp(item, "foo :: bar ! baz [ ] ;");
2725        assert_extent!(p, (0, 22))
2726    }
2727
2728    #[test]
2729    fn item_mod() {
2730        let p = qp(module, "mod foo { }");
2731        assert_extent!(p, (0, 11))
2732    }
2733
2734    #[test]
2735    fn item_mod_public() {
2736        let p = qp(module, "pub mod foo;");
2737        assert_extent!(p, (0, 12))
2738    }
2739
2740    #[test]
2741    fn item_mod_another_file() {
2742        let p = qp(module, "mod foo;");
2743        assert_extent!(p, (0, 8))
2744    }
2745
2746    #[test]
2747    fn item_trait() {
2748        let p = qp(item, "trait Foo {}");
2749        assert_extent!(p, (0, 12))
2750    }
2751
2752    #[test]
2753    fn item_auto_trait() {
2754        let p = qp(item, "auto trait Foo {}");
2755        assert_extent!(p, (0, 17))
2756    }
2757
2758    #[test]
2759    fn item_trait_public() {
2760        let p = qp(item, "pub trait Foo {}");
2761        assert_extent!(p, (0, 16))
2762    }
2763
2764    #[test]
2765    fn item_trait_unsafe() {
2766        let p = qp(item, "unsafe trait Foo {}");
2767        assert_extent!(p, (0, 19))
2768    }
2769
2770    #[test]
2771    fn item_trait_with_generics() {
2772        let p = qp(item, "trait Foo<T> {}");
2773        assert_extent!(p, (0, 15))
2774    }
2775
2776    #[test]
2777    fn item_trait_with_members() {
2778        let p = qp(item, "trait Foo { fn bar(&self) -> u8; }");
2779        assert_extent!(p, (0, 34))
2780    }
2781
2782    #[test]
2783    fn item_trait_with_members_with_patterns() {
2784        let p = qp(item, "trait Foo { fn bar(&self, &a: &u8) -> u8; }");
2785        assert_extent!(p, (0, 43))
2786    }
2787
2788    #[test]
2789    fn item_trait_with_members_with_body() {
2790        let p = qp(item, "trait Foo { fn bar(&self) -> u8 { 42 } }");
2791        assert_extent!(p, (0, 40))
2792    }
2793
2794    #[test]
2795    fn item_trait_with_unnamed_parameters() {
2796        let p = qp(item, "trait Foo { fn bar(&self, u8); }");
2797        assert_extent!(p, (0, 32))
2798    }
2799
2800    #[test]
2801    fn item_trait_with_qualified_function() {
2802        let p = qp(item, r#"trait Foo { const unsafe extern "C" fn bar(); }"#);
2803        assert_extent!(p, (0, 47))
2804    }
2805
2806    #[test]
2807    fn item_trait_with_associated_type() {
2808        let p = qp(item, "trait Foo { type Bar; }");
2809        assert_extent!(p, (0, 23))
2810    }
2811
2812    #[test]
2813    fn item_trait_with_associated_type_with_bounds() {
2814        let p = qp(item, "trait Foo { type Bar: Baz; }");
2815        assert_extent!(p, (0, 28))
2816    }
2817
2818    #[test]
2819    fn item_trait_with_associated_type_with_default() {
2820        let p = qp(item, "trait Foo { type Bar = (); }");
2821        assert_extent!(p, (0, 28))
2822    }
2823
2824    #[test]
2825    fn item_trait_with_associated_type_with_bounds_and_default() {
2826        let p = qp(item, "trait Foo { type Bar: Baz = (); }");
2827        assert_extent!(p, (0, 33))
2828    }
2829
2830    #[test]
2831    fn item_trait_with_associated_const() {
2832        let p = qp(item, "trait Foo { const Bar: u8; }");
2833        assert_extent!(p, (0, 28))
2834    }
2835
2836    #[test]
2837    fn item_trait_with_associated_const_with_default() {
2838        let p = qp(item, "trait Foo { const Bar: u8 = 42; }");
2839        assert_extent!(p, (0, 33))
2840    }
2841
2842    #[test]
2843    fn item_trait_with_supertraits() {
2844        let p = qp(item, "trait Foo: Bar + Baz {}");
2845        assert_extent!(p, (0, 23))
2846    }
2847
2848    #[test]
2849    fn item_trait_with_where_clause() {
2850        let p = qp(item, "trait Foo where A: B {}");
2851        assert_extent!(p, (0, 23))
2852    }
2853
2854    #[test]
2855    fn item_trait_with_macro() {
2856        let p = qp(item, "trait Foo { bar!{} }");
2857        assert_extent!(p, (0, 20))
2858    }
2859
2860    #[test]
2861    fn item_trait_all_space() {
2862        let p = qp(item, "trait Foo : Bar { type A : B ; fn a ( a : u8) -> u8 { a } }");
2863        assert_extent!(p, (0, 59))
2864    }
2865
2866    #[test]
2867    fn item_type_alias() {
2868        let p = qp(item, "type Foo<T> = Bar<T, u8>;");
2869        assert_extent!(p, (0, 25))
2870    }
2871
2872    #[test]
2873    fn item_type_alias_public() {
2874        let p = qp(item, "pub type Foo<T> = Bar<T, u8>;");
2875        assert_extent!(p, (0, 29))
2876    }
2877
2878    #[test]
2879    fn item_type_alias_with_trait_bounds() {
2880        let p = qp(item, "type X<T: Foo> where T: Bar = Option<T>;");
2881        assert_extent!(p, (0, 40))
2882    }
2883
2884    #[test]
2885    fn item_const() {
2886        let p = qp(item, r#"const FOO: &'static str = "hi";"#);
2887        assert_extent!(p, (0, 31))
2888    }
2889
2890    #[test]
2891    fn item_const_public() {
2892        let p = qp(item, "pub const FOO: u8 = 42;");
2893        assert_extent!(p, (0, 23))
2894    }
2895
2896    #[test]
2897    fn item_static() {
2898        let p = qp(item, r#"static FOO: &'static str = "hi";"#);
2899        assert_extent!(p, (0, 32))
2900    }
2901
2902    #[test]
2903    fn item_static_mut() {
2904        let p = qp(item, r#"static mut FOO: &'static str = "hi";"#);
2905        assert_extent!(p, (0, 36))
2906    }
2907
2908    #[test]
2909    fn item_static_public() {
2910        let p = qp(item, "pub static FOO: u8 = 42;");
2911        assert_extent!(p, (0, 24))
2912    }
2913
2914    #[test]
2915    fn item_extern_crate() {
2916        let p = qp(item, "extern crate foo;");
2917        assert_extent!(p, (0, 17))
2918    }
2919
2920    #[test]
2921    fn item_extern_crate_public() {
2922        let p = qp(item, "pub extern crate foo;");
2923        assert_extent!(p, (0, 21))
2924    }
2925
2926    #[test]
2927    fn item_extern_crate_rename() {
2928        let p = qp(item, "extern crate foo as bar;");
2929        assert_extent!(p, (0, 24))
2930    }
2931
2932    #[test]
2933    fn item_extern_block() {
2934        let p = qp(item, r#"extern {}"#);
2935        assert_extent!(p, (0, 9))
2936    }
2937
2938    #[test]
2939    fn item_extern_block_with_abi() {
2940        let p = qp(item, r#"extern "C" {}"#);
2941        assert_extent!(p, (0, 13))
2942    }
2943
2944    #[test]
2945    fn item_extern_block_with_fn() {
2946        let p = qp(item, r#"extern { fn foo(bar: u8) -> bool; }"#);
2947        assert_extent!(p, (0, 35))
2948    }
2949
2950    #[test]
2951    fn item_extern_block_with_variadic_fn() {
2952        let p = qp(item, r#"extern { fn foo(bar: u8, ...) -> bool; }"#);
2953        assert_extent!(p, (0, 40))
2954    }
2955
2956    #[test]
2957    fn item_extern_block_with_fn_and_generics() {
2958        let p = qp(item, r#"extern { fn foo<A, B>(bar: A) -> B; }"#);
2959        assert_extent!(p, (0, 37))
2960    }
2961
2962    #[test]
2963    fn item_extern_block_with_attribute() {
2964        let p = qp(item, r#"extern { #[wow] static A: u8; }"#);
2965        assert_extent!(p, (0, 31))
2966    }
2967
2968    #[test]
2969    fn item_extern_block_with_static() {
2970        let p = qp(item, r#"extern { static FOO: u32; }"#);
2971        assert_extent!(p, (0, 27))
2972    }
2973
2974    #[test]
2975    fn item_extern_block_with_static_and_qualifiers() {
2976        let p = qp(item, r#"extern { pub static mut FOO: u32; }"#);
2977        assert_extent!(p, (0, 35))
2978    }
2979
2980    #[test]
2981    fn item_extern_block_with_type() {
2982        let p = qp(item, r#"extern { type opaque; }"#);
2983        assert_extent!(p, (0, 23))
2984    }
2985
2986    #[test]
2987    fn item_extern_block_with_type_and_qualifiers() {
2988        let p = qp(item, r#"extern { pub type opaque; }"#);
2989        assert_extent!(p, (0, 27))
2990    }
2991
2992    #[test]
2993    fn item_attribute_containing() {
2994        let p = qp(item, r#"#![feature(sweet)]"#);
2995        assert_extent!(p, (0, 18))
2996    }
2997
2998    #[test]
2999    fn item_attribute_containing_doc_comment_line() {
3000        let p = qp(item, "//! Hello");
3001        assert_extent!(p, (0, 9))
3002    }
3003
3004    #[test]
3005    fn item_attribute_containing_doc_comment_block() {
3006        let p = qp(item, "/*! Hello */");
3007        assert_extent!(p, (0, 12))
3008    }
3009
3010    #[test]
3011    fn inherent_impl() {
3012        let p = qp(p_impl, "impl Bar {}");
3013        assert_extent!(p, (0, 11))
3014    }
3015
3016    #[test]
3017    fn inherent_impl_with_function() {
3018        let p = qp(p_impl, "impl Bar { fn foo() {} }");
3019        assert_extent!(p, (0, 24))
3020    }
3021
3022    #[test]
3023    fn inherent_impl_with_const_function() {
3024        let p = qp(p_impl, "impl Bar { const fn foo() {} }");
3025        assert_extent!(p, (0, 30))
3026    }
3027
3028    #[test]
3029    fn inherent_impl_with_default_function() {
3030        let p = qp(p_impl, "impl Bar { default fn foo() {} }");
3031        assert_extent!(p, (0, 32))
3032    }
3033
3034    #[test]
3035    fn inherent_impl_with_unsafe_function() {
3036        let p = qp(p_impl, "impl Bar { unsafe fn foo() {} }");
3037        assert_extent!(p, (0, 31))
3038    }
3039
3040    #[test]
3041    fn inherent_impl_with_extern_function() {
3042        let p = qp(p_impl, "impl Bar { extern fn foo() {} }");
3043        assert_extent!(p, (0, 31))
3044    }
3045
3046    #[test]
3047    fn inherent_impl_with_default_const_unsafe_function() {
3048        let p = qp(p_impl, "impl Bar { default const unsafe fn foo() {} }");
3049        assert_extent!(p, (0, 45))
3050    }
3051
3052    #[test]
3053    fn inherent_impl_with_default_unsafe_extern_function() {
3054        let p = qp(p_impl, "impl Bar { default unsafe extern fn foo() {} }");
3055        assert_extent!(p, (0, 46))
3056    }
3057
3058    #[test]
3059    fn impl_with_trait() {
3060        let p = qp(p_impl, "impl Foo for Bar {}");
3061        assert_extent!(p, (0, 19))
3062    }
3063
3064    #[test]
3065    fn impl_with_negative_trait() {
3066        let p = qp(p_impl, "impl !Foo for Bar {}");
3067        assert_extent!(p, (0, 20))
3068    }
3069
3070    #[test]
3071    fn impl_trait_with_wildcard_type() {
3072        let p = qp(p_impl, "impl Foo for .. {}");
3073        assert_extent!(p, (0, 18))
3074    }
3075
3076    #[test]
3077    fn impl_with_generics() {
3078        let p = qp(p_impl, "impl<'a, T> Foo<'a, T> for Bar<'a, T> {}");
3079        assert_extent!(p, (0, 40))
3080    }
3081
3082    #[test]
3083    fn impl_with_generics_no_space() {
3084        let p = qp(p_impl, "impl<'a,T>Foo<'a,T>for Bar<'a,T>{}");
3085        assert_extent!(p, (0, 34))
3086    }
3087
3088    #[test]
3089    fn impl_with_trait_bounds() {
3090        let p = qp(p_impl, "impl<T> Foo for Bar<T> where T: Quux {}");
3091        assert_extent!(p, (0, 39))
3092    }
3093
3094    #[test]
3095    fn impl_with_attribute() {
3096        let p = qp(p_impl, "impl Foo { #[attribute] fn bar() {} }");
3097        assert_extent!(p, (0, 37))
3098    }
3099
3100    #[test]
3101    fn impl_with_attributes() {
3102        let p = qp(p_impl, "impl Foo { #[a] #[b] fn bar() {} }");
3103        assert_extent!(p, (0, 34))
3104    }
3105
3106    #[test]
3107    fn impl_with_associated_type() {
3108        let p = qp(p_impl, "impl Foo { type A = B; }");
3109        assert_extent!(p, (0, 24))
3110    }
3111
3112    #[test]
3113    fn impl_with_associated_const() {
3114        let p = qp(p_impl, "impl Foo { const A: i32 = 42; }");
3115        assert_extent!(p, (0, 31))
3116    }
3117
3118    #[test]
3119    fn impl_with_public_associated_const() {
3120        let p = qp(p_impl, "impl Foo { pub(crate) const A: i32 = 42; }");
3121        assert_extent!(p, (0, 42))
3122    }
3123
3124    #[test]
3125    fn impl_with_unsafe() {
3126        let p = qp(p_impl, "unsafe impl Foo {}");
3127        assert_extent!(p, (0, 18))
3128    }
3129
3130    #[test]
3131    fn impl_with_macro_call() {
3132        let p = qp(p_impl, "impl Foo { bar!(); }");
3133        assert_extent!(p, (0, 20))
3134    }
3135
3136    #[test]
3137    fn enum_with_trailing_stuff() {
3138        let p = qp(p_enum, "enum A {} impl Foo for Bar {}");
3139        assert_extent!(p, (0, 9))
3140    }
3141
3142    #[test]
3143    fn enum_with_generic_types() {
3144        let p = qp(p_enum, "enum A { Foo(Vec<u8>) }");
3145        assert_extent!(p, (0, 23))
3146    }
3147
3148    #[test]
3149    fn enum_with_generic_declarations() {
3150        let p = qp(p_enum, "enum A<T> { Foo(Vec<T>) }");
3151        assert_extent!(p, (0, 25))
3152    }
3153
3154    #[test]
3155    fn enum_with_struct_variant() {
3156        let p = qp(p_enum, "enum A { Foo { a: u8 } }");
3157        assert_extent!(p, (0, 24))
3158    }
3159
3160    #[test]
3161    fn enum_with_attribute() {
3162        let p = qp(p_enum, "enum Foo { #[attr] A(u8)}");
3163        assert_extent!(p, (0, 25))
3164    }
3165
3166    #[test]
3167    fn enum_with_attribute_on_value() {
3168        let p = qp(p_enum, "enum Foo { A(#[attr] u8) }");
3169        assert_extent!(p, (0, 26))
3170    }
3171
3172    #[test]
3173    fn enum_with_discriminant() {
3174        let p = qp(p_enum, "enum Foo { A = 1, B = 2 }");
3175        assert_extent!(p, (0, 25))
3176    }
3177
3178    #[test]
3179    fn enum_with_where_clause() {
3180        let p = qp(p_enum, "enum Foo<A> where A: Bar { Z }");
3181        assert_extent!(p, (0, 30))
3182    }
3183
3184    #[test]
3185    fn enum_public() {
3186        let p = qp(p_enum, "pub enum A {}");
3187        assert_extent!(p, (0, 13))
3188    }
3189
3190    #[test]
3191    fn fn_with_public_modifier() {
3192        let p = qp(function_header, "pub fn foo()");
3193        assert_extent!(p, (0, 12))
3194    }
3195
3196    #[test]
3197    fn fn_with_const_modifier() {
3198        let p = qp(function_header, "const fn foo()");
3199        assert_extent!(p, (0, 14))
3200    }
3201
3202    #[test]
3203    fn fn_with_extern_modifier() {
3204        let p = qp(function_header, "extern fn foo()");
3205        assert_extent!(p, (0, 15))
3206    }
3207
3208    #[test]
3209    fn fn_with_extern_modifier_and_abi() {
3210        let p = qp(function_header, r#"extern "C" fn foo()"#);
3211        assert_extent!(p, (0, 19))
3212    }
3213
3214    #[test]
3215    fn fn_with_async_modifier() {
3216        let p = qp(function_header, "async fn foo()");
3217        assert_extent!(p, (0, 14))
3218    }
3219
3220    #[test]
3221    fn fn_with_self_type_reference() {
3222        let p = qp(function_header, "fn foo(&self)");
3223        assert_extent!(p, (0, 13))
3224    }
3225
3226    #[test]
3227    fn fn_with_self_type_value() {
3228        let p = qp(function_header, "fn foo(self)");
3229        assert_extent!(p, (0, 12))
3230    }
3231
3232    #[test]
3233    fn fn_with_self_type_value_mut() {
3234        let p = qp(function_header, "fn foo(mut self)");
3235        assert_extent!(p, (0, 16))
3236    }
3237
3238    #[test]
3239    fn fn_with_self_type_reference_mut() {
3240        let p = qp(function_header, "fn foo(&mut self)");
3241        assert_extent!(p, (0, 17))
3242    }
3243
3244    #[test]
3245    fn fn_with_self_type_with_lifetime() {
3246        let p = qp(function_header, "fn foo<'a>(&'a self)");
3247        assert_extent!(p, (0, 20))
3248    }
3249
3250    #[test]
3251    fn fn_with_self_type_and_regular() {
3252        let p = qp(function_header, "fn foo(&self, a: u8)");
3253        assert_extent!(p, (0, 20))
3254    }
3255
3256    #[test]
3257    fn fn_with_self_type_explicit_type() {
3258        let p = qp(function_header, "fn foo(self: &mut Self)");
3259        assert_extent!(p, (0, 23))
3260    }
3261
3262    #[test]
3263    fn fn_with_self_type_explicit_type_mutable() {
3264        let p = qp(function_header, "fn foo(mut self: &mut Self)");
3265        assert_extent!(p, (0, 27))
3266    }
3267
3268    #[test]
3269    fn fn_with_argument() {
3270        let p = qp(function_header, "fn foo(a: u8)");
3271        assert_extent!(p, (0, 13))
3272    }
3273
3274    #[test]
3275    fn fn_with_arguments_all_space() {
3276        let p = qp(function_header, "fn foo ( a : u8 )");
3277        assert_extent!(p, (0, 17))
3278    }
3279
3280    #[test]
3281    fn fn_with_argument_with_generic() {
3282        let p = qp(function_header, "fn foo(a: Vec<u8>)");
3283        assert_extent!(p, (0, 18))
3284    }
3285
3286    #[test]
3287    fn fn_with_arguments() {
3288        let p = qp(function_header, "fn foo(a: u8, b: u8)");
3289        assert_extent!(p, (0, 20))
3290    }
3291
3292    #[test]
3293    fn fn_with_arguments_with_patterns() {
3294        let p = qp(function_header, "fn foo(&a: &u8)");
3295        assert_extent!(p, (0, 15))
3296    }
3297
3298    #[test]
3299    fn fn_with_return_type() {
3300        let p = qp(function_header, "fn foo() -> bool");
3301        assert_extent!(p, (0, 16))
3302    }
3303
3304    #[test]
3305    fn fn_with_generics() {
3306        let p = qp(function_header, "fn foo<A, B>()");
3307        assert_extent!(p, (0, 14))
3308    }
3309
3310    #[test]
3311    fn fn_with_lifetimes() {
3312        let p = qp(function_header, "fn foo<'a, 'b>()");
3313        assert_extent!(p, (0, 16))
3314    }
3315
3316    #[test]
3317    fn fn_with_lifetimes_and_generics() {
3318        let p = qp(function_header, "fn foo<'a, T>()");
3319        assert_extent!(p, (0, 15))
3320    }
3321
3322    #[test]
3323    fn fn_with_whitespace_before_arguments() {
3324        let p = qp(function_header, "fn foo () -> ()");
3325        assert_extent!(p, (0, 15))
3326    }
3327
3328    #[test]
3329    fn fn_with_whitespace_before_generics() {
3330        let p = qp(function_header, "fn foo <'a, T>() -> ()");
3331        assert_extent!(p, (0, 22))
3332    }
3333
3334    #[test]
3335    fn fn_with_unsafe_qualifier() {
3336        let p = qp(function_header, "unsafe fn foo()");
3337        assert_extent!(p, (0, 15))
3338    }
3339
3340    #[test]
3341    fn block_with_multiple_implicit_statement_macro_calls() {
3342        let p = qp(block, "{ a! {} b! {} }");
3343        assert_extent!(p, (0, 15));
3344    }
3345
3346    #[test]
3347    fn block_promotes_implicit_statement_to_expression() {
3348        let p = qp(block, "{ if a {} }");
3349        assert!(p.statements.is_empty());
3350        assert_extent!(p.expression.unwrap(), (2, 9));
3351    }
3352
3353    #[test]
3354    fn block_with_multiple_empty_statements() {
3355        let p = qp(block, "{ ; ; ; }");
3356        assert_extent!(p, (0, 9));
3357    }
3358
3359    #[test]
3360    fn statement_match_no_semicolon() {
3361        let p = qp(statement, "match a { _ => () }");
3362        assert_extent!(p.into_expression().unwrap(), (0, 19))
3363    }
3364
3365    #[test]
3366    fn statement_use() {
3367        let p = qp(statement, "use foo::Bar;");
3368        assert_extent!(p, (0, 13))
3369    }
3370
3371    #[test]
3372    fn statement_any_item() {
3373        let p = qp(statement, "struct Foo {}");
3374        assert_extent!(p, (0, 13))
3375    }
3376
3377    #[test]
3378    fn statement_union() {
3379        // Since `union` is a weird contextual keyword, it's worth testing specially
3380        let p = qp(statement, "union union { union: union }");
3381        assert_extent!(p, (0, 28))
3382    }
3383
3384    #[test]
3385    fn statement_braced_expression_followed_by_method() {
3386        let p = qp(statement, "match 1 { _ => 1u8 }.count_ones()");
3387        assert_extent!(p, (0, 33))
3388    }
3389
3390    #[test]
3391    fn pathed_ident_with_leading_separator() {
3392        let p = qp(pathed_ident, "::foo");
3393        assert_extent!(p, (0, 5))
3394    }
3395
3396    #[test]
3397    fn pathed_ident_with_crate() {
3398        let p = qp(pathed_ident, "crate::foo");
3399        assert_extent!(p, (0, 10))
3400    }
3401
3402    #[test]
3403    fn pathed_ident_with_turbofish() {
3404        let p = qp(pathed_ident, "foo::<Vec<u8>>");
3405        assert_extent!(p, (0, 14))
3406    }
3407
3408    #[test]
3409    fn pathed_ident_with_turbofish_with_lifetime() {
3410        let p = qp(pathed_ident, "StructWithLifetime::<'a, u8>");
3411        assert_extent!(p, (0, 28))
3412    }
3413
3414    #[test]
3415    fn pathed_ident_all_space() {
3416        let p = qp(pathed_ident, "foo :: < Vec < u8 > , Option < bool > >");
3417        assert_extent!(p, (0, 39))
3418    }
3419
3420    #[test]
3421    fn number_decimal_cannot_start_with_underscore() {
3422        let p = parse_full(number_literal, "_123");
3423        let (err_loc, errs) = unwrap_progress_err(p);
3424        assert_eq!(err_loc, 0);
3425        assert!(errs.contains(&Error::ExpectedNumber));
3426    }
3427
3428    #[test]
3429    fn number_with_exponent() {
3430        let p = qp(number_literal, "1e2");
3431        assert_extent!(p, (0, 3))
3432    }
3433
3434    #[test]
3435    fn number_with_prefix_and_exponent() {
3436        let p = qp(number_literal, "0x1e2");
3437        assert_extent!(p, (0, 5))
3438    }
3439
3440    #[test]
3441    fn number_with_fractional() {
3442        let p = qp(number_literal, "1.2");
3443        assert_extent!(p, (0, 3))
3444    }
3445
3446    #[test]
3447    fn number_with_fractional_with_suffix() {
3448        let p = qp(number_literal, "1.2f32");
3449        assert_extent!(p, (0, 6))
3450    }
3451
3452    #[test]
3453    fn number_with_prefix_and_fractional() {
3454        let p = qp(number_literal, "0x1.2");
3455        assert_extent!(p, (0, 5))
3456    }
3457
3458    #[test]
3459    fn number_with_prefix_exponent_and_fractional() {
3460        let p = qp(number_literal, "0o7.3e9");
3461        assert_extent!(p, (0, 7))
3462    }
3463
3464    #[test]
3465    fn number_with_prefix_can_have_underscore_after_prefix() {
3466        let p = qp(number_literal, "0x_123");
3467        assert_extent!(p, (0, 6))
3468    }
3469
3470    #[test]
3471    fn number_binary_can_have_suffix() {
3472        let p = qp(number_literal, "0b111u8");
3473        assert_extent!(p, (0, 7))
3474    }
3475
3476    #[test]
3477    fn number_decimal_can_have_suffix() {
3478        let p = qp(number_literal, "123i16");
3479        assert_extent!(p, (0, 6))
3480    }
3481
3482    #[test]
3483    fn number_hexadecimal_can_have_suffix() {
3484        let p = qp(number_literal, "0xBEEF__u32");
3485        assert_extent!(p, (0, 11))
3486    }
3487
3488    #[test]
3489    fn number_octal_can_have_suffix() {
3490        let p = qp(number_literal, "0o777_isize");
3491        assert_extent!(p, (0, 11))
3492    }
3493
3494    #[test]
3495    fn pattern_with_path() {
3496        let p = qp(pattern, "foo::Bar::Baz");
3497        assert_extent!(p, (0, 13))
3498    }
3499
3500    #[test]
3501    fn pattern_with_ref() {
3502        let p = qp(pattern, "ref a");
3503        assert_extent!(p, (0, 5))
3504    }
3505
3506    #[test]
3507    fn pattern_with_tuple() {
3508        let p = qp(pattern, "(a, b)");
3509        assert_extent!(p, (0, 6))
3510    }
3511
3512    #[test]
3513    fn pattern_with_enum_tuple() {
3514        let p = qp(pattern, "Baz(a)");
3515        assert_extent!(p, (0, 6))
3516    }
3517
3518    #[test]
3519    fn pattern_with_tuple_wildcard() {
3520        let p = qp(pattern, "(..)");
3521        assert_extent!(p, (0, 4))
3522    }
3523
3524    #[test]
3525    fn pattern_with_tuple_wildcard_anywhere() {
3526        let p = qp(pattern, "(a, .., b)");
3527        assert_extent!(p, (0, 10))
3528    }
3529
3530    #[test]
3531    fn pattern_with_tuple_all_space() {
3532        let p = qp(pattern, "( a , .. , b )");
3533        assert_extent!(p, (0, 14))
3534    }
3535
3536    #[test]
3537    fn pattern_with_enum_struct() {
3538        let p = qp(pattern, "Baz { a: a }");
3539        assert_extent!(p, (0, 12))
3540    }
3541
3542    #[test]
3543    fn pattern_with_enum_struct_shorthand() {
3544        let p = qp(pattern, "Baz { a }");
3545        assert_extent!(p, (0, 9))
3546    }
3547
3548    #[test]
3549    fn pattern_with_enum_struct_shorthand_with_ref() {
3550        let p = qp(pattern, "Baz { ref a }");
3551        assert_extent!(p, (0, 13))
3552    }
3553
3554    #[test]
3555    fn pattern_with_enum_struct_wildcard() {
3556        let p = qp(pattern, "Baz { .. }");
3557        assert_extent!(p, (0, 10))
3558    }
3559
3560    #[test]
3561    fn pattern_with_byte_literal() {
3562        let p = qp(pattern, "b'a'");
3563        assert_extent!(p, (0, 4))
3564    }
3565
3566    #[test]
3567    fn pattern_with_char_literal() {
3568        let p = qp(pattern, "'a'");
3569        assert_extent!(p, (0, 3))
3570    }
3571
3572    #[test]
3573    fn pattern_with_byte_string_literal() {
3574        let p = qp(pattern, r#"b"hello""#);
3575        assert_extent!(p, (0, 8))
3576    }
3577
3578    #[test]
3579    fn pattern_with_string_literal() {
3580        let p = qp(pattern, r#""hello""#);
3581        assert_extent!(p, (0, 7))
3582    }
3583
3584    #[test]
3585    fn pattern_with_numeric_literal() {
3586        let p = qp(pattern, "42");
3587        assert_extent!(p, (0, 2))
3588    }
3589
3590    #[test]
3591    fn pattern_with_numeric_literal_negative() {
3592        let p = qp(pattern, "- 42");
3593        assert_extent!(p, (0, 4))
3594    }
3595
3596    #[test]
3597    fn pattern_with_slice() {
3598        let p = qp(pattern, "[a, b]");
3599        assert_extent!(p, (0, 6))
3600    }
3601
3602    #[test]
3603    fn pattern_with_slice_and_subslices() {
3604        let p = qp(pattern, "[a, b @ ..]");
3605        assert_extent!(p, (0, 11))
3606    }
3607
3608    #[test]
3609    fn pattern_with_slice_and_modified_subslices() {
3610        let p = qp(pattern, "[a, ref mut b @ ..]");
3611        assert_extent!(p, (0, 19))
3612    }
3613
3614    #[test]
3615    fn pattern_with_reference() {
3616        let p = qp(pattern, "&a");
3617        assert_extent!(p, (0, 2))
3618    }
3619
3620    #[test]
3621    fn pattern_with_reference_mutable() {
3622        let p = qp(pattern, "&mut ()");
3623        assert!(p.kind.is_reference());
3624        assert_extent!(p, (0, 7));
3625    }
3626
3627    #[test]
3628    fn pattern_with_named_subpattern() {
3629        let p = qp(pattern, "a @ 1");
3630        assert_extent!(p, (0, 5));
3631    }
3632
3633    #[test]
3634    fn pattern_with_named_subpattern_qualifiers() {
3635        let p = qp(pattern, "ref mut a @ 1");
3636        assert_extent!(p, (0, 13));
3637    }
3638
3639    #[test]
3640    fn pattern_with_numeric_inclusive_range() {
3641        let p = qp(pattern, "1 ..= 10");
3642        assert_extent!(p, (0, 8))
3643    }
3644
3645    #[test]
3646    fn pattern_with_numeric_inclusive_range_negative() {
3647        let p = qp(pattern, "-10 ..= -1");
3648        assert_extent!(p, (0, 10))
3649    }
3650
3651    #[test]
3652    fn pattern_with_character_inclusive_range() {
3653        let p = qp(pattern, "'a'..='z'");
3654        assert_extent!(p, (0, 9))
3655    }
3656
3657    #[test]
3658    fn pattern_with_byte_inclusive_range() {
3659        let p = qp(pattern, "b'a'..=b'z'");
3660        assert_extent!(p, (0, 11))
3661    }
3662
3663    #[test]
3664    fn pattern_with_pathed_ident_inclusive_range() {
3665        let p = qp(pattern, "foo::a..=z");
3666        assert_extent!(p, (0, 10))
3667    }
3668
3669    #[test]
3670    fn pattern_with_legacy_numeric_inclusive_range() {
3671        let p = qp(pattern, "1 ... 10");
3672        assert_extent!(p, (0, 8))
3673    }
3674
3675    #[test]
3676    fn pattern_with_legacy_numeric_inclusive_range_negative() {
3677        let p = qp(pattern, "-10 ... -1");
3678        assert_extent!(p, (0, 10))
3679    }
3680
3681    #[test]
3682    fn pattern_with_legacy_character_inclusive_range() {
3683        let p = qp(pattern, "'a'...'z'");
3684        assert_extent!(p, (0, 9))
3685    }
3686
3687    #[test]
3688    fn pattern_with_legacy_byte_inclusive_range() {
3689        let p = qp(pattern, "b'a'...b'z'");
3690        assert_extent!(p, (0, 11))
3691    }
3692
3693    #[test]
3694    fn pattern_with_legacy_pathed_ident_inclusive_range() {
3695        let p = qp(pattern, "foo::a...z");
3696        assert_extent!(p, (0, 10))
3697    }
3698
3699    #[test]
3700    fn pattern_with_numeric_exclusive_range() {
3701        let p = qp(pattern, "1 .. 10");
3702        assert_extent!(p, (0, 7))
3703    }
3704
3705    #[test]
3706    fn pattern_with_numeric_exclusive_range_negative() {
3707        let p = qp(pattern, "-10 .. -1");
3708        assert_extent!(p, (0, 9))
3709    }
3710
3711    #[test]
3712    fn pattern_with_character_exclusive_range() {
3713        let p = qp(pattern, "'a'..'z'");
3714        assert_extent!(p, (0, 8))
3715    }
3716
3717    #[test]
3718    fn pattern_with_byte_exclusive_range() {
3719        let p = qp(pattern, "b'a'..b'z'");
3720        assert_extent!(p, (0, 10))
3721    }
3722
3723    #[test]
3724    fn pattern_with_pathed_ident_exclusive_range() {
3725        let p = qp(pattern, "foo::a..z");
3726        assert_extent!(p, (0, 9))
3727    }
3728
3729    #[test]
3730    fn pattern_with_macro_call() {
3731        let p = qp(pattern, "foo![]");
3732        assert_extent!(p, (0, 6))
3733    }
3734
3735    #[test]
3736    fn pattern_with_box() {
3737        let p = qp(pattern, "box a");
3738        assert_extent!(p, (0, 5))
3739    }
3740
3741    #[test]
3742    fn type_tuple() {
3743        let p = qp(typ, "(u8, u8)");
3744        assert_extent!(p, (0, 8))
3745    }
3746
3747    #[test]
3748    fn type_tuple_all_space() {
3749        let p = qp(typ, "( u8 , u8 )");
3750        assert_extent!(p, (0, 11))
3751    }
3752
3753    #[test]
3754    fn type_with_generics() {
3755        let p = qp(typ, "A<T>");
3756        assert_extent!(p, (0, 4))
3757    }
3758
3759    #[test]
3760    fn type_with_generics_all_space() {
3761        let p = qp(typ, "A < T >");
3762        assert_extent!(p, (0, 7))
3763    }
3764
3765    #[test]
3766    fn type_dyn_trait() {
3767        let p = qp(typ, "dyn Foo");
3768        assert_extent!(p, (0, 7))
3769    }
3770
3771    #[test]
3772    fn type_impl_trait() {
3773        let p = qp(typ, "impl Foo");
3774        assert_extent!(p, (0, 8))
3775    }
3776
3777    #[test]
3778    fn type_fn_trait() {
3779        let p = qp(typ, "Fn(u8) -> u8");
3780        assert_extent!(p, (0, 12))
3781    }
3782
3783    #[test]
3784    fn type_ref() {
3785        let p = qp(typ, "&mut Foo");
3786        assert_extent!(p, (0, 8))
3787    }
3788
3789    #[test]
3790    fn type_mut_ref() {
3791        let p = qp(typ, "&mut Foo");
3792        assert_extent!(p, (0, 8))
3793    }
3794
3795    #[test]
3796    fn type_mut_ref_with_lifetime() {
3797        let p = qp(typ, "&'a mut Foo");
3798        assert_extent!(p, (0, 11))
3799    }
3800
3801    #[test]
3802    fn type_const_pointer() {
3803        let p = qp(typ, "*const Foo");
3804        assert_extent!(p, (0, 10))
3805    }
3806
3807    #[test]
3808    fn type_mut_pointer() {
3809        let p = qp(typ, "*mut Foo");
3810        assert_extent!(p, (0, 8))
3811    }
3812
3813    #[test]
3814    fn type_uninhabited() {
3815        let p = qp(typ, "!");
3816        assert_extent!(p, (0, 1))
3817    }
3818
3819    #[test]
3820    fn type_slice() {
3821        let p = qp(typ, "[u8]");
3822        assert_extent!(p, (0, 4))
3823    }
3824
3825    #[test]
3826    fn type_array() {
3827        let p = qp(typ, "[u8; 42]");
3828        assert_extent!(p, (0, 8))
3829    }
3830
3831    #[test]
3832    fn type_array_allows_expressions() {
3833        let p = qp(typ, "[u8; 1 + 1]");
3834        assert_extent!(p, (0, 11))
3835    }
3836
3837    #[test]
3838    fn type_fn() {
3839        let p = qp(typ, "fn(u8) -> u8");
3840        assert_extent!(p, (0, 12))
3841    }
3842
3843    #[test]
3844    fn type_fn_with_names() {
3845        let p = qp(typ, "fn(a: u8) -> u8");
3846        assert_extent!(p, (0, 15))
3847    }
3848
3849    #[test]
3850    fn type_fn_with_const() {
3851        let p = qp(typ, "const fn()");
3852        assert_extent!(p, (0, 10))
3853    }
3854
3855    #[test]
3856    fn type_fn_with_unsafe() {
3857        let p = qp(typ, "unsafe fn()");
3858        assert_extent!(p, (0, 11))
3859    }
3860
3861    #[test]
3862    fn type_fn_with_extern() {
3863        let p = qp(typ, "extern fn()");
3864        assert_extent!(p, (0, 11))
3865    }
3866
3867    #[test]
3868    fn type_fn_with_extern_and_abi() {
3869        let p = qp(typ, r#"extern "C" fn()"#);
3870        assert_extent!(p, (0, 15))
3871    }
3872
3873    #[test]
3874    fn type_fn_with_variadic() {
3875        let p = qp(typ, r#"fn(...)"#);
3876        assert_extent!(p, (0, 7))
3877    }
3878
3879    #[test]
3880    fn type_higher_ranked_trait_bounds() {
3881        let p = qp(typ, "for <'a> Foo<'a>");
3882        assert_extent!(p, (0, 16))
3883    }
3884
3885    #[test]
3886    fn type_higher_ranked_trait_bounds_on_functions() {
3887        let p = qp(typ, "for <'a> fn(&'a u8)");
3888        assert_extent!(p, (0, 19))
3889    }
3890
3891    #[test]
3892    fn type_higher_ranked_trait_bounds_on_references() {
3893        let p = qp(typ, "for <'a> &'a u8");
3894        assert_extent!(p, (0, 15))
3895    }
3896
3897    #[test]
3898    fn type_with_path() {
3899        let p = qp(typ, "crate::foo::Bar");
3900        assert_extent!(p, (0, 15))
3901    }
3902
3903    #[test]
3904    fn type_with_additional_named_type() {
3905        let p = qp(typ, "Foo + Bar");
3906        assert_extent!(p, (0, 9))
3907    }
3908
3909    #[test]
3910    fn type_with_additional_lifetimes() {
3911        let p = qp(typ, "Foo + 'a");
3912        assert_extent!(p, (0, 8))
3913    }
3914
3915    #[test]
3916    fn type_disambiguation() {
3917        let p = qp(typ, "<Foo as Bar>");
3918        assert_extent!(p, (0, 12))
3919    }
3920
3921    #[test]
3922    fn type_disambiguation_with_associated_type() {
3923        let p = qp(typ, "<Foo as Bar>::Quux");
3924        assert_extent!(p, (0, 18))
3925    }
3926
3927    #[test]
3928    fn type_disambiguation_without_disambiguation() {
3929        let p = qp(typ, "<Foo>");
3930        assert_extent!(p, (0, 5))
3931    }
3932
3933    #[test]
3934    fn type_disambiguation_with_double_angle_brackets() {
3935        let p = qp(typ, "<<A as B> as Option<T>>");
3936        assert_extent!(p, (0, 23))
3937    }
3938
3939    #[test]
3940    fn struct_basic() {
3941        let p = qp(p_struct, "struct S { field: TheType, other: OtherType }");
3942        assert_extent!(p, (0, 45))
3943    }
3944
3945    #[test]
3946    fn struct_with_generic_fields() {
3947        let p = qp(p_struct, "struct S { field: Option<u8> }");
3948        assert_extent!(p, (0, 30))
3949    }
3950
3951    #[test]
3952    fn struct_with_fields_with_no_space() {
3953        let p = qp(p_struct, "struct S{a:u8}");
3954        assert_extent!(p, (0, 14))
3955    }
3956
3957    #[test]
3958    fn struct_with_fields_with_all_space() {
3959        let p = qp(p_struct, "struct S { a : u8 }");
3960        assert_extent!(p, (0, 19))
3961    }
3962
3963    #[test]
3964    fn struct_with_generic_declarations() {
3965        let p = qp(p_struct, "struct S<T> { field: Option<T> }");
3966        assert_extent!(p, (0, 32))
3967    }
3968
3969    #[test]
3970    fn struct_public() {
3971        let p = qp(p_struct, "pub struct S {}");
3972        assert_extent!(p, (0, 15))
3973    }
3974
3975    #[test]
3976    fn struct_public_field() {
3977        let p = qp(p_struct, "struct S { pub age: u8 }");
3978        assert_extent!(p, (0, 24))
3979    }
3980
3981    #[test]
3982    fn struct_with_attributed_field() {
3983        let p = qp(p_struct, "struct S { #[foo(bar)] #[baz(quux)] field: u8 }");
3984        assert_extent!(p, (0, 47))
3985    }
3986
3987    #[test]
3988    fn struct_with_block_documented_field() {
3989        let p = qp(p_struct, "struct S { /** use me */ field: u8 }");
3990        assert_extent!(p, (0, 36))
3991    }
3992
3993    #[test]
3994    fn struct_with_line_documented_field() {
3995        let p = qp(p_struct, "struct S { /// use me\n field: u8 }");
3996        assert_extent!(p, (0, 34))
3997    }
3998
3999    #[test]
4000    fn struct_with_tuple() {
4001        let p = qp(p_struct, "struct S(u8);");
4002        assert_extent!(p, (0, 13))
4003    }
4004
4005    #[test]
4006    fn struct_with_tuple_and_annotation() {
4007        let p = qp(p_struct, "struct S(#[foo] u8);");
4008        assert_extent!(p, (0, 20))
4009    }
4010
4011    #[test]
4012    fn struct_with_tuple_and_visibility() {
4013        let p = qp(p_struct, "struct S(pub u8);");
4014        assert_extent!(p, (0, 17))
4015    }
4016
4017    #[test]
4018    fn struct_empty() {
4019        let p = qp(p_struct, "struct S;");
4020        assert_extent!(p, (0, 9))
4021    }
4022
4023    #[test]
4024    fn struct_with_where_clause() {
4025        let p = qp(p_struct, "struct S<A> where A: Foo { a: A }");
4026        assert_extent!(p, (0, 33))
4027    }
4028
4029    #[test]
4030    fn struct_with_tuple_and_where_clause() {
4031        let p = qp(p_struct, "struct S<A>(A) where A: Foo;");
4032        assert_extent!(p, (0, 28))
4033    }
4034
4035    #[test]
4036    fn union_basic() {
4037        let p = qp(p_union, "union U { field: TheType, other: OtherType }");
4038        assert_extent!(p, (0, 44))
4039    }
4040
4041    #[test]
4042    fn union_is_still_an_ident() {
4043        let p = qp(p_union, "union union { union: union }");
4044        assert_extent!(p, (0, 28))
4045    }
4046
4047    #[test]
4048    fn union_with_generic_declarations() {
4049        let p = qp(p_union, "union S<T> { field: Option<T> }");
4050        assert_extent!(p, (0, 31))
4051    }
4052
4053    #[test]
4054    fn union_with_where_clause() {
4055        let p = qp(p_union, "union S<A> where A: Foo { a: A }");
4056        assert_extent!(p, (0, 32))
4057    }
4058
4059    #[test]
4060    fn union_public() {
4061        let p = qp(p_union, "pub union S {}");
4062        assert_extent!(p, (0, 14))
4063    }
4064
4065    #[test]
4066    fn union_public_field() {
4067        let p = qp(p_union, "union S { pub age: u8 }");
4068        assert_extent!(p, (0, 23))
4069    }
4070
4071    #[test]
4072    fn union_with_attributed_field() {
4073        let p = qp(p_union, "union S { #[foo(bar)] #[baz(quux)] field: u8 }");
4074        assert_extent!(p, (0, 46))
4075    }
4076
4077    #[test]
4078    fn where_clause_with_path() {
4079        let p = qp(where_clause_item, "P: foo::bar::baz::Quux<'a>");
4080        assert_extent!(p, (0, 26))
4081    }
4082
4083    #[test]
4084    fn where_clause_with_multiple_bounds() {
4085        let p = qp(where_clause_item, "P: A + B");
4086        assert_extent!(p, (0, 8))
4087    }
4088
4089    #[test]
4090    fn where_clause_with_multiple_types() {
4091        let p = qp(where_clause, "where P: A, Q: B");
4092        assert_extent!(p[1], (12, 16))
4093    }
4094
4095    #[test]
4096    fn where_clause_with_lifetimes() {
4097        let p = qp(where_clause_item, "'a: 'b + 'c");
4098        assert_extent!(p, (0, 11))
4099    }
4100
4101    #[test]
4102    fn where_clause_with_higher_ranked_trait_bounds() {
4103        let p = qp(where_clause_item, "for<'a> [u8; 4]: Send");
4104        assert_extent!(p, (0, 21))
4105    }
4106
4107    #[test]
4108    fn ident_with_leading_underscore() {
4109        let p = qp(ident, "_foo");
4110        assert_extent!(p, (0, 4))
4111    }
4112
4113    #[test]
4114    fn ident_can_have_keyword_substring() {
4115        let p = qp(ident, "form");
4116        assert_extent!(p, (0, 4))
4117    }
4118
4119    #[test]
4120    fn lifetime_ident() {
4121        let p = qp(lifetime, "'a");
4122        assert_extent!(p, (0, 2))
4123    }
4124
4125    #[test]
4126    fn lifetime_static() {
4127        let p = qp(lifetime, "'static");
4128        assert_extent!(p, (0, 7))
4129    }
4130
4131    #[test]
4132    fn generic_declarations_() {
4133        let p = qp(generic_declarations, "<A>");
4134        assert_extent!(p, (0, 3))
4135    }
4136
4137    #[test]
4138    fn generic_declarations_allow_type_bounds() {
4139        let p = qp(generic_declarations, "<A: Foo>");
4140        assert_extent!(p, (0, 8))
4141    }
4142
4143    #[test]
4144    fn generic_declarations_with_default_types() {
4145        let p = qp(generic_declarations, "<A = Bar>");
4146        assert_extent!(p, (0, 9))
4147    }
4148
4149    #[test]
4150    fn generic_declarations_with_type_bounds_and_default_types() {
4151        let p = qp(generic_declarations, "<A: Foo = Bar>");
4152        assert_extent!(p, (0, 14))
4153    }
4154
4155    #[test]
4156    fn generic_declarations_allow_lifetime_bounds() {
4157        let p = qp(generic_declarations, "<'a: 'b>");
4158        assert_extent!(p, (0, 8))
4159    }
4160
4161    #[test]
4162    fn generic_declarations_allow_const_bounds() {
4163        let p = qp(generic_declarations, "<const N: usize>");
4164        assert_extent!(p, (0, 16))
4165    }
4166
4167    #[test]
4168    fn generic_declarations_with_attributes() {
4169        let p = qp(generic_declarations, "<#[foo] 'a, #[bar] B>");
4170        assert_extent!(p, (0, 21))
4171    }
4172
4173    #[test]
4174    fn generic_declarations_all_space() {
4175        let p = qp(generic_declarations, "< 'a : 'b , A : Foo >");
4176        assert_extent!(p, (0, 21))
4177    }
4178
4179    #[test]
4180    fn trait_bounds_with_lifetime() {
4181        let p = qp(trait_bounds, "'a + 'b");
4182        assert_extent!(p, (0, 7))
4183    }
4184
4185    #[test]
4186    fn trait_bounds_with_relaxed() {
4187        let p = qp(trait_bounds, "?A + ?B");
4188        assert_extent!(p, (0, 7))
4189    }
4190
4191    #[test]
4192    fn trait_bounds_with_associated_type_equality() {
4193        let p = qp(trait_bounds, "A<B, C = D>");
4194        assert_extent!(p, (0, 11))
4195    }
4196
4197    #[test]
4198    fn trait_bounds_with_associated_type_bounds() {
4199        let p = qp(trait_bounds, "A<B, C: D>");
4200        assert_extent!(p, (0, 10))
4201    }
4202
4203    #[test]
4204    fn visibility_self() {
4205        let p = qp(visibility, "pub(self)");
4206        assert_extent!(p, (0, 9))
4207    }
4208
4209    #[test]
4210    fn visibility_super() {
4211        let p = qp(visibility, "pub(super)");
4212        assert_extent!(p, (0, 10))
4213    }
4214
4215    #[test]
4216    fn visibility_crate() {
4217        let p = qp(visibility, "pub(crate)");
4218        assert_extent!(p, (0, 10))
4219    }
4220
4221    #[test]
4222    fn visibility_crate_shorthand() {
4223        let p = qp(visibility, "crate");
4224        assert_extent!(p, (0, 5))
4225    }
4226
4227    #[test]
4228    fn visibility_path() {
4229        let p = qp(visibility, "pub(::foo::bar)");
4230        assert_extent!(p, (0, 15))
4231    }
4232}