forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_.rs
More file actions
141 lines (123 loc) · 4.47 KB
/
Copy pathtype_.rs
File metadata and controls
141 lines (123 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::convert::Infallible;
use super::{
Checkpoint, ErrProof, Parser, Recovery, define_scope,
expr::parse_expr,
param::GenericArgListScope,
parse_list,
path::{PathScope, is_path_segment},
token_stream::TokenStream,
};
use crate::{ExpectedKind, ParseError, SyntaxKind};
pub fn parse_type<S: TokenStream>(
parser: &mut Parser<S>,
checkpoint: Option<Checkpoint>,
) -> Result<Checkpoint, Recovery<ErrProof>> {
match parser.current_kind() {
Some(SyntaxKind::Star) => parser.parse_cp(PtrTypeScope::default(), checkpoint),
Some(SyntaxKind::MutKw | SyntaxKind::RefKw | SyntaxKind::OwnKw) => {
parser.parse_cp(ModeTypeScope::default(), checkpoint)
}
Some(SyntaxKind::LParen) => parser.parse_cp(TupleTypeScope::default(), checkpoint),
Some(SyntaxKind::LBracket) => parser.parse_cp(ArrayTypeScope::default(), checkpoint),
Some(SyntaxKind::Not) => parser
.parse_cp(NeverTypeScope::default(), checkpoint)
.map_err(|e| e.into()),
_ => parser.parse_cp(PathTypeScope::default(), checkpoint),
}
}
pub(crate) fn is_type_start(kind: SyntaxKind) -> bool {
match kind {
SyntaxKind::Star | SyntaxKind::SelfTypeKw | SyntaxKind::LParen | SyntaxKind::LBracket => {
true
}
SyntaxKind::MutKw | SyntaxKind::RefKw | SyntaxKind::OwnKw => true,
kind if is_path_segment(kind) => true,
_ => false,
}
}
define_scope!(PtrTypeScope, PtrType);
impl super::Parse for PtrTypeScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.set_newline_as_trivia(false);
parser.bump_expected(SyntaxKind::Star);
parse_type(parser, None).map(|_| ())
}
}
define_scope!(ModeTypeScope, ModeType);
impl super::Parse for ModeTypeScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.set_newline_as_trivia(false);
parser.expect(
&[SyntaxKind::MutKw, SyntaxKind::RefKw, SyntaxKind::OwnKw],
None,
)?;
parser.bump();
parse_type(parser, None).map(|_| ())
}
}
define_scope!(pub(crate) PathTypeScope , PathType);
impl super::Parse for PathTypeScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.set_newline_as_trivia(false);
parser.or_recover(|p| {
p.parse(PathScope::default()).map_err(|_| {
ParseError::expected(&[SyntaxKind::PathType], None, p.end_of_prev_token)
})
})?;
if parser.current_kind() == Some(SyntaxKind::Lt) {
parser.parse(GenericArgListScope::default())?;
}
Ok(())
}
}
define_scope! { pub(crate) TupleTypeScope, TupleType, (RParen, Comma) }
impl super::Parse for TupleTypeScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parse_list(
parser,
false,
SyntaxKind::TupleType,
(SyntaxKind::LParen, SyntaxKind::RParen),
|parser| {
parse_type(parser, None)?;
Ok(())
},
)
}
}
define_scope! { ArrayTypeScope, ArrayType }
impl super::Parse for ArrayTypeScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.set_newline_as_trivia(false);
parser.bump_expected(SyntaxKind::LBracket);
parser.set_scope_recovery_stack(&[SyntaxKind::SemiColon, SyntaxKind::RBracket]);
parse_type(parser, None)?;
if parser.find_and_pop(SyntaxKind::SemiColon, ExpectedKind::Unspecified)? {
parser.bump();
}
parse_expr(parser)?;
if parser.find_and_pop(
SyntaxKind::RBracket,
ExpectedKind::ClosingBracket {
bracket: SyntaxKind::RBracket,
parent: SyntaxKind::ArrayType,
},
)? {
parser.bump();
}
Ok(())
}
}
define_scope! {NeverTypeScope, NeverType}
impl super::Parse for NeverTypeScope {
type Error = Recovery<Infallible>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.bump_expected(SyntaxKind::Not);
Ok(())
}
}