sway_parse/
keywords.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use crate::{Parse, ParseResult, Parser, Peek, Peeker};

use sway_ast::keywords::*;
use sway_error::parser_error::ParseErrorKind;
use sway_types::Spanned;

fn peek_keyword<T: Keyword>(peeker: Peeker<'_>) -> Option<T> {
    let ident = peeker.peek_ident().ok()?;
    (ident.as_str() == T::AS_STR).then(|| T::new(ident.span()))
}

fn parse_keyword<T: Keyword + Peek>(parser: &mut Parser) -> ParseResult<T> {
    match parser.take() {
        Some(value) => Ok(value),
        None => Err(parser.emit_error(ParseErrorKind::ExpectedKeyword { word: T::AS_STR })),
    }
}

macro_rules! keyword_impls {
    ($($ty:ty),*) => {
        $(
            impl Peek for $ty {
                fn peek(peeker: Peeker<'_>) -> Option<Self> {
                    peek_keyword(peeker)
                }
            }

            impl Parse for $ty {
                fn parse(parser: &mut Parser) -> ParseResult<Self> {
                    parse_keyword(parser)
                }
            }
        )*
    };
}

keyword_impls! {
    ScriptToken,
    ContractToken,
    PredicateToken,
    LibraryToken,
    ModToken,
    PubToken,
    UseToken,
    AsToken,
    StructToken,
    ClassToken,
    EnumToken,
    SelfToken,
    FnToken,
    TraitToken,
    ImplToken,
    ForToken,
    InToken,
    AbiToken,
    ConstToken,
    StorageToken,
    StrToken,
    AsmToken,
    ReturnToken,
    IfToken,
    ElseToken,
    MatchToken,
    MutToken,
    LetToken,
    WhileToken,
    WhereToken,
    RefToken,
    TrueToken,
    FalseToken,
    BreakToken,
    ContinueToken,
    ConfigurableToken,
    TypeToken,
    PtrToken,
    SliceToken
}

fn peek_token<T: Token>(peeker: Peeker<'_>) -> Option<T> {
    let span = peeker
        .peek_punct_kinds(T::PUNCT_KINDS, T::NOT_FOLLOWED_BY)
        .ok()?;
    Some(T::new(span))
}

fn parse_token<T: Token + Peek>(parser: &mut Parser) -> ParseResult<T> {
    match parser.take() {
        Some(value) => Ok(value),
        None => {
            let kinds = T::PUNCT_KINDS.to_owned();
            Err(parser.emit_error(ParseErrorKind::ExpectedPunct { kinds }))
        }
    }
}

macro_rules! token_impls {
    ($($ty:ty),*) => {
        $(
            impl Peek for $ty {
                fn peek(peeker: Peeker<'_>) -> Option<Self> {
                    peek_token(peeker)
                }
            }

            impl Parse for $ty {
                fn parse(parser: &mut Parser) -> ParseResult<Self> {
                    parse_token(parser)
                }
            }
        )*
    };
}

token_impls! {
    SemicolonToken,
    ForwardSlashToken,
    DoubleColonToken,
    StarToken,
    DoubleStarToken,
    CommaToken,
    ColonToken,
    RightArrowToken,
    LessThanToken,
    GreaterThanToken,
    OpenAngleBracketToken,
    CloseAngleBracketToken,
    EqToken,
    AddEqToken,
    SubEqToken,
    StarEqToken,
    DivEqToken,
    ShlEqToken,
    ShrEqToken,
    FatRightArrowToken,
    DotToken,
    DoubleDotToken,
    BangToken,
    PercentToken,
    AddToken,
    SubToken,
    ShrToken,
    ShlToken,
    AmpersandToken,
    CaretToken,
    PipeToken,
    DoubleEqToken,
    BangEqToken,
    GreaterThanEqToken,
    LessThanEqToken,
    DoubleAmpersandToken,
    DoublePipeToken,
    UnderscoreToken,
    HashToken,
    HashBangToken
}

// Keep this in sync with the list in `sway-ast/keywords.rs` defined by define_keyword!
pub const RESERVED_KEYWORDS: phf::Set<&'static str> = phf::phf_set! {
    "script",
    "contract",
    "predicate",
    "library",
    "mod",
    "pub",
    "use",
    "as",
    "struct",
    "enum",
    "self",
    "fn",
    "trait",
    "impl",
    "for",
    "abi",
    "const",
    "storage",
    "str",
    "asm",
    "return",
    "if",
    "else",
    "match",
    "mut",
    "let",
    "while",
    "where",
    "ref",
    "true",
    "false",
    "break",
    "continue",
    "configurable",
    "type",
};