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
use crate::{ErrorEmitted, Parse, ParseErrorKind, ParseResult, ParseToEnd, Parser};
use sway_ast::brackets::{Braces, Parens, SquareBrackets};
use sway_ast::keywords::{CloseAngleBracketToken, OpenAngleBracketToken};
use sway_ast::token::Delimiter;
use sway_types::{Span, Spanned};
pub trait ParseBracket<T>: Sized {
fn try_parse(parser: &mut Parser) -> ParseResult<Option<Self>>
where
T: ParseToEnd;
fn parse_all_inner(
parser: &mut Parser,
on_error: impl FnOnce(Parser) -> ErrorEmitted,
) -> ParseResult<Self>
where
T: Parse;
fn try_parse_all_inner(
parser: &mut Parser,
on_error: impl FnOnce(Parser) -> ErrorEmitted,
) -> ParseResult<Option<Self>>
where
T: Parse;
}
macro_rules! impl_brackets (
($ty_name:ident, $delimiter:ident, $error:ident) => {
impl<T> ParseBracket<T> for $ty_name<T> {
fn try_parse(parser: &mut Parser) -> ParseResult<Option<$ty_name<T>>>
where
T: ParseToEnd
{
match parser.enter_delimited(Delimiter::$delimiter) {
Some((parser, span)) => {
let (inner, _consumed) = parser.parse_to_end()?;
Ok(Some($ty_name { inner, span }))
},
None => Ok(None),
}
}
fn parse_all_inner(
parser: &mut Parser,
on_error: impl FnOnce(Parser) -> ErrorEmitted,
) -> ParseResult<$ty_name<T>>
where
T: Parse
{
match parser.enter_delimited(Delimiter::$delimiter) {
Some((mut parser, span)) => {
let inner = parser.parse()?;
if !parser.is_empty() {
return Err(on_error(parser))
}
Ok($ty_name { inner, span })
},
None => Err(parser.emit_error(ParseErrorKind::$error)),
}
}
fn try_parse_all_inner(
parser: &mut Parser,
on_error: impl FnOnce(Parser) -> ErrorEmitted,
) -> ParseResult<Option<$ty_name<T>>>
where
T: Parse
{
match parser.enter_delimited(Delimiter::$delimiter) {
Some((mut parser, span)) => {
let inner = parser.parse()?;
if !parser.is_empty() {
return Err(on_error(parser))
}
Ok(Some($ty_name { inner, span }))
},
None => Ok(None),
}
}
}
impl<T> Parse for $ty_name<T>
where
T: ParseToEnd,
{
fn parse(parser: &mut Parser) -> ParseResult<$ty_name<T>> {
match parser.enter_delimited(Delimiter::$delimiter) {
Some((parser, span)) => {
let (inner, _consumed) = parser.parse_to_end()?;
Ok($ty_name { inner, span })
},
None => Err(parser.emit_error(ParseErrorKind::$error)),
}
}
}
};
);
impl_brackets!(Braces, Brace, ExpectedOpenBrace);
impl_brackets!(Parens, Parenthesis, ExpectedOpenParen);
impl_brackets!(SquareBrackets, Bracket, ExpectedOpenBracket);
#[derive(Clone, Debug)]
pub struct AngleBrackets<T> {
pub open_angle_bracket_token: OpenAngleBracketToken,
pub inner: T,
pub close_angle_bracket_token: CloseAngleBracketToken,
}
impl<T> Spanned for AngleBrackets<T> {
fn span(&self) -> Span {
Span::join(
self.open_angle_bracket_token.span(),
self.close_angle_bracket_token.span(),
)
}
}