forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstmt.rs
More file actions
177 lines (147 loc) · 5.37 KB
/
Copy pathstmt.rs
File metadata and controls
177 lines (147 loc) · 5.37 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::convert::Infallible;
use unwrap_infallible::UnwrapInfallible;
use crate::{ExpectedKind, SyntaxKind};
use super::{
ErrProof, Parser, Recovery,
attr::parse_attr_list,
define_scope,
expr::{parse_condition_expr, parse_continuation_expr, parse_expr, parse_expr_no_struct},
expr_atom::BlockExprScope,
pat::parse_pat,
token_stream::TokenStream,
type_::parse_type,
};
pub fn parse_stmt<S: TokenStream>(parser: &mut Parser<S>) -> Result<(), Recovery<ErrProof>> {
use SyntaxKind::*;
// Check for attributes before for statements (for #[unroll])
let checkpoint = if parser.current_kind() == Some(Pound) {
parse_attr_list(parser)?
} else {
None
};
// Attributes parsed at statement position are currently only consumed by
// `for` statements (#[unroll]/#[unroll(never)]). Report other uses explicitly
// instead of silently dropping them.
if checkpoint.is_some() && parser.current_kind() != Some(ForKw) {
parser.error("statement attributes are only supported on `for` loops");
}
match parser.current_kind() {
Some(LetKw) => parser
.parse_cp(LetStmtScope::default(), checkpoint)
.map(|_| ()),
Some(ForKw) => parser
.parse_cp(ForStmtScope::default(), checkpoint)
.map(|_| ()),
Some(WhileKw) => parser
.parse_cp(WhileStmtScope::default(), checkpoint)
.map(|_| ()),
Some(ContinueKw) => {
parser
.parse_cp(ContinueStmtScope::default(), checkpoint)
.unwrap_infallible();
Ok(())
}
Some(BreakKw) => {
parser
.parse_cp(BreakStmtScope::default(), checkpoint)
.unwrap_infallible();
Ok(())
}
Some(ReturnKw) => parser
.parse_cp(ReturnStmtScope::default(), checkpoint)
.map(|_| ()),
_ => parser
.parse_cp(ExprStmtScope::default(), checkpoint)
.map(|_| ()),
}
}
define_scope! { LetStmtScope, LetStmt }
impl super::Parse for LetStmtScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.bump_expected(SyntaxKind::LetKw);
parser.set_newline_as_trivia(false);
parse_pat(parser)?;
if parser.current_kind() == Some(SyntaxKind::Colon) {
parser.bump_expected(SyntaxKind::Colon);
parse_type(parser, None)?;
}
if parser.bump_if(SyntaxKind::Eq) {
parse_continuation_expr(parser)?;
}
Ok(())
}
}
define_scope! { ForStmtScope, ForStmt }
impl super::Parse for ForStmtScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.bump_expected(SyntaxKind::ForKw);
parser.set_scope_recovery_stack(&[SyntaxKind::InKw, SyntaxKind::Ident, SyntaxKind::LBrace]);
parse_pat(parser)?;
if parser.find_and_pop(SyntaxKind::InKw, ExpectedKind::Unspecified)? {
parser.bump();
}
parse_expr_no_struct(parser)?;
// pop `Ident` recovery token, which is only included because it solves a contrived test case
parser.pop_recovery_stack();
if parser.find_and_pop(SyntaxKind::LBrace, ExpectedKind::Body(SyntaxKind::ForStmt))? {
parser.parse(BlockExprScope::default())?;
}
Ok(())
}
}
define_scope! { WhileStmtScope, WhileStmt }
impl super::Parse for WhileStmtScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.bump_expected(SyntaxKind::WhileKw);
parser.set_scope_recovery_stack(&[SyntaxKind::LBrace]);
parse_condition_expr(parser)?;
if parser.find_and_pop(
SyntaxKind::LBrace,
ExpectedKind::Body(SyntaxKind::WhileStmt),
)? {
parser.parse(BlockExprScope::default())?;
}
Ok(())
}
}
define_scope! { ContinueStmtScope, ContinueStmt }
impl super::Parse for ContinueStmtScope {
type Error = Infallible;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.bump_expected(SyntaxKind::ContinueKw);
Ok(())
}
}
define_scope! { BreakStmtScope, BreakStmt }
impl super::Parse for BreakStmtScope {
type Error = Infallible;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.bump_expected(SyntaxKind::BreakKw);
Ok(())
}
}
define_scope! { ReturnStmtScope, ReturnStmt }
impl super::Parse for ReturnStmtScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parser.bump_expected(SyntaxKind::ReturnKw);
parser.set_newline_as_trivia(false);
if !matches!(
parser.current_kind(),
None | Some(SyntaxKind::Newline | SyntaxKind::RBrace | SyntaxKind::Comma)
) {
parse_expr(parser)?;
}
Ok(())
}
}
define_scope! { ExprStmtScope, ExprStmt }
impl super::Parse for ExprStmtScope {
type Error = Recovery<ErrProof>;
fn parse<S: TokenStream>(&mut self, parser: &mut Parser<S>) -> Result<(), Self::Error> {
parse_expr(parser)
}
}