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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (C) 2019-2024 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use std::fmt;

use serde::{Deserialize, Serialize};

use leo_span::{Symbol, sym};

/// Represents all valid Leo syntax tokens.
///
/// The notion of 'token' here is a bit more general than in the ABNF grammar:
/// since it includes comments and whitespace,
/// it corresponds to the notion of 'lexeme' in the ABNF grammar.
/// There are also a few other differences, noted in comments below.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Token {
    // Comments
    CommentLine(String),  // the string includes the starting '//' and the ending line feed
    CommentBlock(String), // the string includes the starting '/*' and the ending '*/'

    // Whitespace (we do not distinguish among different kinds here)
    WhiteSpace,

    // Literals (= atomic literals and numerals in the ABNF grammar)
    // The string in Integer(String) consists of digits
    // The string in AddressLit(String) has the form `aleo1...`.
    True,
    False,
    Integer(String), // = numeral (including tuple index) in the ABNF grammar
    AddressLit(String),
    StaticString(String),
    // The numeric literals in the ABNF grammar, which consist of numerals followed by types,
    // are represented not as single tokens here,
    // but as two separate tokens (one for the numeral and one for the type),
    // enforcing, during parsing, the absence of whitespace or comments between those two tokens
    // (see the parse_primary_expression function).

    // Identifiers
    Identifier(Symbol),

    // Symbols
    Not,
    And,
    AndAssign,
    Or,
    OrAssign,
    BitAnd,
    BitAndAssign,
    BitOr,
    BitOrAssign,
    BitXor,
    BitXorAssign,
    Eq,
    NotEq,
    Lt,
    LtEq,
    Gt,
    GtEq,
    Add,
    AddAssign,
    Sub,
    SubAssign,
    Mul,
    MulAssign,
    Div,
    DivAssign,
    Pow,
    PowAssign,
    Rem,
    RemAssign,
    Shl,
    ShlAssign,
    Shr,
    ShrAssign,
    Assign,
    LeftParen,
    RightParen,
    LeftSquare,
    RightSquare,
    LeftCurly,
    RightCurly,
    Comma,
    Dot,
    DotDot,
    Semicolon,
    Colon,
    DoubleColon,
    Question,
    Arrow,
    BigArrow,
    Underscore,
    At, // @ is not a symbol token in the ABNF grammar (see explanation about annotations below)
    // There is no symbol for `)group` here (unlike the ABNF grammar),
    // because we handle that differently in the parser: see the eat_group_partial function.

    // The ABNF grammar has annotations as tokens,
    // defined as @ immediately followed by an identifier.
    // Here instead we regard the @ sign alone as a token (see `At` above),
    // and we lex it separately from the identifier that is supposed to follow it in an annotation.
    // When parsing annotations, we check that there is no whitespace or comments
    // between the @ and the identifier, thus eventually complying to the ABNF grammar.
    // See the parse_annotation function.

    // Type keywords
    Address,
    Bool,
    Field,
    Group,
    I8,
    I16,
    I32,
    I64,
    I128,
    Record,
    Scalar,
    Signature,
    String,
    Struct,
    U8,
    U16,
    U32,
    U64,
    U128,

    // Other keywords
    Aleo,
    As,
    Assert,
    AssertEq,
    AssertNeq,
    Async,
    Block,
    Console,
    Const,
    Constant,
    Else,
    Fn,
    For,
    Function,
    Future,
    If,
    Import,
    In,
    Inline,
    Let,
    Mapping,
    Network,
    Private,
    Program,
    Public,
    Return,
    SelfLower,
    Transition,

    // Meta tokens
    Eof, // used to signal end-of-file, not an actual token of the language
    Leo, // only used for error messages, not an actual keyword
}

/// Represents all valid Leo keyword tokens.
/// This also includes the boolean literals `true` and `false`,
/// unlike the ABNF grammar, which classifies them as literals and not keywords.
/// But for the purposes of our lexer implementation,
/// it is fine to include the boolean literals in this list.
pub const KEYWORD_TOKENS: &[Token] = &[
    Token::Address,
    Token::Aleo,
    Token::As,
    Token::Assert,
    Token::AssertEq,
    Token::AssertNeq,
    Token::Async,
    Token::Bool,
    Token::Console,
    Token::Const,
    Token::Constant,
    Token::Else,
    Token::False,
    Token::Field,
    Token::Fn,
    Token::For,
    Token::Function,
    Token::Future,
    Token::Group,
    Token::I8,
    Token::I16,
    Token::I32,
    Token::I64,
    Token::I128,
    Token::If,
    Token::Import,
    Token::In,
    Token::Inline,
    Token::Let,
    Token::Mapping,
    Token::Network,
    Token::Private,
    Token::Program,
    Token::Public,
    Token::Record,
    Token::Return,
    Token::Scalar,
    Token::SelfLower,
    Token::Signature,
    Token::String,
    Token::Struct,
    Token::Transition,
    Token::True,
    Token::U8,
    Token::U16,
    Token::U32,
    Token::U64,
    Token::U128,
];

impl Token {
    /// Returns `true` if the `self` token equals a Leo keyword.
    pub fn is_keyword(&self) -> bool {
        KEYWORD_TOKENS.contains(self)
    }

    /// Converts `self` to the corresponding `Symbol` if it `is_keyword`.
    pub fn keyword_to_symbol(&self) -> Option<Symbol> {
        Some(match self {
            Token::Address => sym::address,
            Token::Aleo => sym::aleo,
            Token::As => sym::As,
            Token::Assert => sym::assert,
            Token::AssertEq => sym::assert_eq,
            Token::AssertNeq => sym::assert_neq,
            Token::Block => sym::block,
            Token::Bool => sym::bool,
            Token::Console => sym::console,
            Token::Const => sym::Const,
            Token::Constant => sym::constant,
            Token::Else => sym::Else,
            Token::False => sym::False,
            Token::Field => sym::field,
            Token::For => sym::For,
            Token::Function => sym::function,
            Token::Group => sym::group,
            Token::I8 => sym::i8,
            Token::I16 => sym::i16,
            Token::I32 => sym::i32,
            Token::I64 => sym::i64,
            Token::I128 => sym::i128,
            Token::If => sym::If,
            Token::Import => sym::import,
            Token::In => sym::In,
            Token::Inline => sym::inline,
            Token::Let => sym::Let,
            Token::Leo => sym::leo,
            Token::Mapping => sym::mapping,
            Token::Network => sym::network,
            Token::Private => sym::private,
            Token::Program => sym::program,
            Token::Public => sym::public,
            Token::Record => sym::record,
            Token::Return => sym::Return,
            Token::Scalar => sym::scalar,
            Token::Signature => sym::signature,
            Token::SelfLower => sym::SelfLower,
            Token::String => sym::string,
            Token::Struct => sym::Struct,
            Token::Transition => sym::transition,
            Token::True => sym::True,
            Token::U8 => sym::u8,
            Token::U16 => sym::u16,
            Token::U32 => sym::u32,
            Token::U64 => sym::u64,
            Token::U128 => sym::u128,
            _ => return None,
        })
    }
}

impl fmt::Display for Token {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use Token::*;
        match self {
            CommentLine(s) => write!(f, "{s}"),
            CommentBlock(s) => write!(f, "{s}"),

            WhiteSpace => write!(f, "whitespace"),

            True => write!(f, "true"),
            False => write!(f, "false"),
            Integer(s) => write!(f, "{s}"),
            AddressLit(s) => write!(f, "{s}"),
            StaticString(s) => write!(f, "\"{s}\""),

            Identifier(s) => write!(f, "{s}"),

            Not => write!(f, "!"),
            And => write!(f, "&&"),
            AndAssign => write!(f, "&&="),
            Or => write!(f, "||"),
            OrAssign => write!(f, "||="),
            BitAnd => write!(f, "&"),
            BitAndAssign => write!(f, "&="),
            BitOr => write!(f, "|"),
            BitOrAssign => write!(f, "|="),
            BitXor => write!(f, "^"),
            BitXorAssign => write!(f, "^="),
            Eq => write!(f, "=="),
            NotEq => write!(f, "!="),
            Lt => write!(f, "<"),
            LtEq => write!(f, "<="),
            Gt => write!(f, ">"),
            GtEq => write!(f, ">="),
            Add => write!(f, "+"),
            AddAssign => write!(f, "+="),
            Sub => write!(f, "-"),
            SubAssign => write!(f, "-="),
            Mul => write!(f, "*"),
            MulAssign => write!(f, "*="),
            Div => write!(f, "/"),
            DivAssign => write!(f, "/="),
            Pow => write!(f, "**"),
            PowAssign => write!(f, "**="),
            Rem => write!(f, "%"),
            RemAssign => write!(f, "%="),
            Shl => write!(f, "<<"),
            ShlAssign => write!(f, "<<="),
            Shr => write!(f, ">>"),
            ShrAssign => write!(f, ">>="),
            Assign => write!(f, "="),
            LeftParen => write!(f, "("),
            RightParen => write!(f, ")"),
            LeftSquare => write!(f, "["),
            RightSquare => write!(f, "]"),
            LeftCurly => write!(f, "{{"),
            RightCurly => write!(f, "}}"),
            Comma => write!(f, ","),
            Dot => write!(f, "."),
            DotDot => write!(f, ".."),
            Semicolon => write!(f, ";"),
            Colon => write!(f, ":"),
            DoubleColon => write!(f, "::"),
            Question => write!(f, "?"),
            Arrow => write!(f, "->"),
            BigArrow => write!(f, "=>"),
            Underscore => write!(f, "_"),
            At => write!(f, "@"),

            Address => write!(f, "address"),
            Bool => write!(f, "bool"),
            Field => write!(f, "field"),
            Group => write!(f, "group"),
            I8 => write!(f, "i8"),
            I16 => write!(f, "i16"),
            I32 => write!(f, "i32"),
            I64 => write!(f, "i64"),
            I128 => write!(f, "i128"),
            Record => write!(f, "record"),
            Scalar => write!(f, "scalar"),
            Signature => write!(f, "signature"),
            String => write!(f, "string"),
            Struct => write!(f, "struct"),
            U8 => write!(f, "u8"),
            U16 => write!(f, "u16"),
            U32 => write!(f, "u32"),
            U64 => write!(f, "u64"),
            U128 => write!(f, "u128"),

            Aleo => write!(f, "aleo"),
            As => write!(f, "as"),
            Assert => write!(f, "assert"),
            AssertEq => write!(f, "assert_eq"),
            AssertNeq => write!(f, "assert_neq"),
            Async => write!(f, "async"),
            Block => write!(f, "block"),
            Console => write!(f, "console"),
            Const => write!(f, "const"),
            Constant => write!(f, "constant"),
            Else => write!(f, "else"),
            Fn => write!(f, "Fn"),
            For => write!(f, "for"),
            Function => write!(f, "function"),
            Future => write!(f, "Future"),
            If => write!(f, "if"),
            Import => write!(f, "import"),
            In => write!(f, "in"),
            Inline => write!(f, "inline"),
            Let => write!(f, "let"),
            Mapping => write!(f, "mapping"),
            Network => write!(f, "network"),
            Private => write!(f, "private"),
            Program => write!(f, "program"),
            Public => write!(f, "public"),
            Return => write!(f, "return"),
            SelfLower => write!(f, "self"),
            Transition => write!(f, "transition"),

            Eof => write!(f, "<eof>"),
            Leo => write!(f, "leo"),
        }
    }
}

/// Describes delimiters of a token sequence.
#[derive(Copy, Clone)]
pub enum Delimiter {
    /// `( ... )`
    Parenthesis,
    /// `{ ... }`
    Brace,
    /// `[ ... ]`
    Bracket,
}

impl Delimiter {
    /// Returns the open/close tokens that the delimiter corresponds to.
    pub fn open_close_pair(self) -> (Token, Token) {
        match self {
            Self::Parenthesis => (Token::LeftParen, Token::RightParen),
            Self::Brace => (Token::LeftCurly, Token::RightCurly),
            Self::Bracket => (Token::LeftSquare, Token::RightSquare),
        }
    }
}