-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathtoken.rs
More file actions
787 lines (720 loc) · 25.2 KB
/
Copy pathtoken.rs
File metadata and controls
787 lines (720 loc) · 25.2 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
use acvm::FieldElement;
use noirc_errors::{Position, Span, Spanned};
use std::{fmt, iter::Map, vec::IntoIter};
use crate::lexer::errors::LexerErrorKind;
/// Represents a token in noir's grammar - a word, number,
/// or symbol that can be used in noir's syntax. This is the
/// smallest unit of grammar. A parser may (will) decide to parse
/// items differently depending on the Tokens present but will
/// never parse the same ordering of identical tokens differently.
#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)]
pub enum Token {
Ident(String),
Int(FieldElement),
Bool(bool),
Str(String),
FmtStr(String),
Keyword(Keyword),
IntType(IntType),
Attribute(Attribute),
LineComment(String),
BlockComment(String),
/// <
Less,
/// <=
LessEqual,
/// >
Greater,
/// >=
GreaterEqual,
/// ==
Equal,
/// !=
NotEqual,
/// +
Plus,
/// -
Minus,
/// *
Star,
/// /
Slash,
/// %
Percent,
/// &
Ampersand,
/// ^
Caret,
/// <<
ShiftLeft,
/// >>
ShiftRight,
/// .
Dot,
/// ..
DoubleDot,
/// (
LeftParen,
/// )
RightParen,
/// {
LeftBrace,
/// }
RightBrace,
/// [
LeftBracket,
/// ]
RightBracket,
/// ->
Arrow,
/// |
Pipe,
/// #
Pound,
/// ,
Comma,
/// :
Colon,
/// ::
DoubleColon,
/// ;
Semicolon,
/// !
Bang,
/// =
Assign,
#[allow(clippy::upper_case_acronyms)]
EOF,
/// An invalid character is one that is not in noir's language or grammar.
///
/// We don't report invalid tokens in the source as errors until parsing to
/// avoid reporting the error twice (once while lexing, again when it is encountered
/// during parsing). Reporting during lexing then removing these from the token stream
/// would not be equivalent as it would change the resulting parse.
Invalid(char),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SpannedToken(Spanned<Token>);
impl PartialEq<SpannedToken> for Token {
fn eq(&self, other: &SpannedToken) -> bool {
self == &other.0.contents
}
}
impl PartialEq<Token> for SpannedToken {
fn eq(&self, other: &Token) -> bool {
&self.0.contents == other
}
}
impl From<SpannedToken> for Token {
fn from(spt: SpannedToken) -> Self {
spt.0.contents
}
}
impl SpannedToken {
pub fn new(token: Token, span: Span) -> SpannedToken {
SpannedToken(Spanned::from(span, token))
}
pub fn to_span(&self) -> Span {
self.0.span()
}
pub fn token(&self) -> &Token {
&self.0.contents
}
pub fn into_token(self) -> Token {
self.0.contents
}
pub fn kind(&self) -> TokenKind {
self.token().kind()
}
}
impl std::fmt::Display for SpannedToken {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.token().fmt(f)
}
}
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Token::Ident(ref s) => write!(f, "{s}"),
Token::Int(n) => write!(f, "{}", n.to_u128()),
Token::Bool(b) => write!(f, "{b}"),
Token::Str(ref b) => write!(f, "{b}"),
Token::FmtStr(ref b) => write!(f, "f{b}"),
Token::Keyword(k) => write!(f, "{k}"),
Token::Attribute(ref a) => write!(f, "{a}"),
Token::LineComment(ref s) => write!(f, "//{s}"),
Token::BlockComment(ref s) => write!(f, "/*{s}*/"),
Token::IntType(ref i) => write!(f, "{i}"),
Token::Less => write!(f, "<"),
Token::LessEqual => write!(f, "<="),
Token::Greater => write!(f, ">"),
Token::GreaterEqual => write!(f, ">="),
Token::Equal => write!(f, "=="),
Token::NotEqual => write!(f, "!="),
Token::Plus => write!(f, "+"),
Token::Minus => write!(f, "-"),
Token::Star => write!(f, "*"),
Token::Slash => write!(f, "/"),
Token::Percent => write!(f, "%"),
Token::Ampersand => write!(f, "&"),
Token::Caret => write!(f, "^"),
Token::ShiftLeft => write!(f, "<<"),
Token::ShiftRight => write!(f, ">>"),
Token::Dot => write!(f, "."),
Token::DoubleDot => write!(f, ".."),
Token::LeftParen => write!(f, "("),
Token::RightParen => write!(f, ")"),
Token::LeftBrace => write!(f, "{{"),
Token::RightBrace => write!(f, "}}"),
Token::LeftBracket => write!(f, "["),
Token::RightBracket => write!(f, "]"),
Token::Arrow => write!(f, "->"),
Token::Pipe => write!(f, "|"),
Token::Pound => write!(f, "#"),
Token::Comma => write!(f, ","),
Token::Colon => write!(f, ":"),
Token::DoubleColon => write!(f, "::"),
Token::Semicolon => write!(f, ";"),
Token::Assign => write!(f, "="),
Token::Bang => write!(f, "!"),
Token::EOF => write!(f, "end of input"),
Token::Invalid(c) => write!(f, "{c}"),
}
}
}
#[derive(PartialEq, Eq, Hash, Debug, Clone, Ord, PartialOrd)]
/// The different kinds of tokens that are possible in the target language
pub enum TokenKind {
Token(Token),
Ident,
Literal,
Keyword,
Attribute,
}
impl fmt::Display for TokenKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TokenKind::Token(ref tok) => write!(f, "{tok}"),
TokenKind::Ident => write!(f, "identifier"),
TokenKind::Literal => write!(f, "literal"),
TokenKind::Keyword => write!(f, "keyword"),
TokenKind::Attribute => write!(f, "attribute"),
}
}
}
impl Token {
pub fn kind(&self) -> TokenKind {
match *self {
Token::Ident(_) => TokenKind::Ident,
Token::Int(_) | Token::Bool(_) | Token::Str(_) | Token::FmtStr(_) => TokenKind::Literal,
Token::Keyword(_) => TokenKind::Keyword,
Token::Attribute(_) => TokenKind::Attribute,
ref tok => TokenKind::Token(tok.clone()),
}
}
pub fn is_ident(&self) -> bool {
matches!(self, Token::Ident(_))
}
pub(super) fn into_single_span(self, position: Position) -> SpannedToken {
self.into_span(position, position)
}
pub(super) fn into_span(self, start: Position, end: Position) -> SpannedToken {
SpannedToken(Spanned::from_position(start, end, self))
}
/// These are all the operators allowed as part of
/// a short-hand assignment: a <op>= b
pub fn assign_shorthand_operators() -> [Token; 10] {
use Token::*;
[Plus, Minus, Star, Slash, Percent, Ampersand, Caret, ShiftLeft, ShiftRight, Pipe]
}
pub fn try_into_binary_op(self, span: Span) -> Option<Spanned<crate::BinaryOpKind>> {
use crate::BinaryOpKind::*;
let binary_op = match self {
Token::Plus => Add,
Token::Ampersand => And,
Token::Caret => Xor,
Token::ShiftLeft => ShiftLeft,
Token::ShiftRight => ShiftRight,
Token::Pipe => Or,
Token::Minus => Subtract,
Token::Star => Multiply,
Token::Slash => Divide,
Token::Equal => Equal,
Token::NotEqual => NotEqual,
Token::Less => Less,
Token::LessEqual => LessEqual,
Token::Greater => Greater,
Token::GreaterEqual => GreaterEqual,
Token::Percent => Modulo,
_ => return None,
};
Some(Spanned::from(span, binary_op))
}
}
#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)]
pub enum IntType {
Unsigned(u32), // u32 = Unsigned(32)
Signed(u32), // i64 = Signed(64)
}
impl fmt::Display for IntType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
IntType::Unsigned(num) => write!(f, "u{num}"),
IntType::Signed(num) => write!(f, "i{num}"),
}
}
}
impl IntType {
// XXX: Result<Option<Token, LexerErrorKind>
// Is not the best API. We could split this into two functions. One that checks if the the
// word is a integer, which only returns an Option
pub(crate) fn lookup_int_type(word: &str, span: Span) -> Result<Option<Token>, LexerErrorKind> {
// Check if the first string is a 'u' or 'i'
let is_signed = if word.starts_with('i') {
true
} else if word.starts_with('u') {
false
} else {
return Ok(None);
};
// Word start with 'u' or 'i'. Check if the latter is an integer
let str_as_u32 = match word[1..].parse::<u32>() {
Ok(str_as_u32) => str_as_u32,
Err(_) => return Ok(None),
};
let max_bits = FieldElement::max_num_bits() / 2;
if str_as_u32 > max_bits {
return Err(LexerErrorKind::TooManyBits { span, max: max_bits, got: str_as_u32 });
}
if is_signed {
Ok(Some(Token::IntType(IntType::Signed(str_as_u32))))
} else {
Ok(Some(Token::IntType(IntType::Unsigned(str_as_u32))))
}
}
}
/// TestScope is used to specify additional annotations for test functions
#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)]
pub enum TestScope {
/// If a test has a scope of ShouldFailWith, then it can only pass
/// if it fails with the specified reason. If the reason is None, then
/// the test must unconditionally fail
ShouldFailWith { reason: Option<String> },
/// No scope is applied and so the test must pass
None,
}
impl TestScope {
fn lookup_str(string: &str) -> Option<TestScope> {
match string.trim() {
"should_fail" => Some(TestScope::ShouldFailWith { reason: None }),
s if s.starts_with("should_fail_with") => {
let parts: Vec<&str> = s.splitn(2, '=').collect();
if parts.len() == 2 {
let reason = parts[1].trim();
let reason = reason.trim_matches('"');
Some(TestScope::ShouldFailWith { reason: Some(reason.to_string()) })
} else {
None
}
}
_ => None,
}
}
}
impl fmt::Display for TestScope {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TestScope::None => write!(f, ""),
TestScope::ShouldFailWith { reason } => match reason {
Some(failure_reason) => write!(f, "(should_fail_with = ({failure_reason}))"),
None => write!(f, "should_fail"),
},
}
}
}
#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)]
// Attributes are special language markers in the target language
// An example of one is `#[SHA256]` . Currently only Foreign attributes are supported
// Calls to functions which have the foreign attribute are executed in the host language
pub struct Attributes {
// Each function can have a single Primary Attribute
pub function: Option<FunctionAttribute>,
// Each function can have many Secondary Attributes
pub secondary: Vec<SecondaryAttribute>,
}
impl Attributes {
pub fn empty() -> Self {
Self { function: None, secondary: Vec::new() }
}
/// Returns true if one of the secondary attributes is `contract_library_method`
///
/// This is useful for finding out if we should compile a contract method
/// as an entry point or not.
pub fn has_contract_library_method(&self) -> bool {
self.secondary
.iter()
.any(|attribute| attribute == &SecondaryAttribute::ContractLibraryMethod)
}
pub fn is_test_function(&self) -> bool {
matches!(self.function, Some(FunctionAttribute::Test(_)))
}
/// Returns note if a deprecated secondary attribute is found
pub fn get_deprecated_note(&self) -> Option<Option<String>> {
self.secondary.iter().find_map(|attr| match attr {
SecondaryAttribute::Deprecated(note) => Some(note.clone()),
_ => None,
})
}
pub fn get_field_attribute(&self) -> Option<String> {
for secondary in &self.secondary {
if let SecondaryAttribute::Field(field) = secondary {
return Some(field.to_lowercase());
}
}
None
}
}
/// An Attribute can be either a Primary Attribute or a Secondary Attribute
/// A Primary Attribute can alter the function type, thus there can only be one
/// A secondary attribute has no effect and is either consumed by a library or used as a notice for the developer
#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)]
pub enum Attribute {
Function(FunctionAttribute),
Secondary(SecondaryAttribute),
}
impl fmt::Display for Attribute {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Attribute::Function(attribute) => write!(f, "{attribute}"),
Attribute::Secondary(attribute) => write!(f, "{attribute}"),
}
}
}
impl Attribute {
/// If the string is a fixed attribute return that, else
/// return the custom attribute
pub(crate) fn lookup_attribute(word: &str, span: Span) -> Result<Token, LexerErrorKind> {
let word_segments: Vec<&str> = word
.split(|c| c == '(' || c == ')')
.filter(|string_segment| !string_segment.is_empty())
.collect();
let validate = |slice: &str| {
let is_valid = slice
.chars()
.all(|ch| {
ch.is_ascii_alphabetic()
|| ch.is_numeric()
|| ch == '_'
|| ch == '('
|| ch == ')'
|| ch == '='
|| ch == '"'
|| ch == ' '
})
.then_some(());
is_valid.ok_or(LexerErrorKind::MalformedFuncAttribute { span, found: word.to_owned() })
};
let attribute = match &word_segments[..] {
// Primary Attributes
["foreign", name] => {
validate(name)?;
Attribute::Function(FunctionAttribute::Foreign(name.to_string()))
}
["builtin", name] => {
validate(name)?;
Attribute::Function(FunctionAttribute::Builtin(name.to_string()))
}
["oracle", name] => {
validate(name)?;
Attribute::Function(FunctionAttribute::Oracle(name.to_string()))
}
["test"] => Attribute::Function(FunctionAttribute::Test(TestScope::None)),
["test", name] => {
validate(name)?;
let malformed_scope =
LexerErrorKind::MalformedFuncAttribute { span, found: word.to_owned() };
match TestScope::lookup_str(name) {
Some(scope) => Attribute::Function(FunctionAttribute::Test(scope)),
None => return Err(malformed_scope),
}
}
["field", name] => {
validate(name)?;
Attribute::Secondary(SecondaryAttribute::Field(name.to_string()))
}
// Secondary attributes
["deprecated"] => Attribute::Secondary(SecondaryAttribute::Deprecated(None)),
["contract_library_method"] => {
Attribute::Secondary(SecondaryAttribute::ContractLibraryMethod)
}
["event"] => Attribute::Secondary(SecondaryAttribute::Event),
["deprecated", name] => {
if !name.starts_with('"') && !name.ends_with('"') {
return Err(LexerErrorKind::MalformedFuncAttribute {
span,
found: word.to_owned(),
});
}
Attribute::Secondary(SecondaryAttribute::Deprecated(
name.trim_matches('"').to_string().into(),
))
}
tokens => {
tokens.iter().try_for_each(|token| validate(token))?;
Attribute::Secondary(SecondaryAttribute::Custom(word.to_owned()))
}
};
Ok(Token::Attribute(attribute))
}
}
/// Primary Attributes are those which a function can only have one of.
/// They change the FunctionKind and thus have direct impact on the IR output
#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)]
pub enum FunctionAttribute {
Foreign(String),
Builtin(String),
Oracle(String),
Test(TestScope),
}
impl FunctionAttribute {
pub fn builtin(self) -> Option<String> {
match self {
FunctionAttribute::Builtin(name) => Some(name),
_ => None,
}
}
pub fn foreign(self) -> Option<String> {
match self {
FunctionAttribute::Foreign(name) => Some(name),
_ => None,
}
}
pub fn is_foreign(&self) -> bool {
matches!(self, FunctionAttribute::Foreign(_))
}
pub fn is_low_level(&self) -> bool {
matches!(self, FunctionAttribute::Foreign(_) | FunctionAttribute::Builtin(_))
}
}
impl fmt::Display for FunctionAttribute {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FunctionAttribute::Test(scope) => write!(f, "#[test{scope}]"),
FunctionAttribute::Foreign(ref k) => write!(f, "#[foreign({k})]"),
FunctionAttribute::Builtin(ref k) => write!(f, "#[builtin({k})]"),
FunctionAttribute::Oracle(ref k) => write!(f, "#[oracle({k})]"),
}
}
}
/// Secondary attributes are those which a function can have many of.
/// They are not able to change the `FunctionKind` and thus do not have direct impact on the IR output
/// They are often consumed by libraries or used as notices for the developer
#[derive(PartialEq, Eq, Hash, Debug, Clone, PartialOrd, Ord)]
pub enum SecondaryAttribute {
Deprecated(Option<String>),
// This is an attribute to specify that a function
// is a helper method for a contract and should not be seen as
// the entry point.
ContractLibraryMethod,
Event,
Field(String),
Custom(String),
}
impl fmt::Display for SecondaryAttribute {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SecondaryAttribute::Deprecated(None) => write!(f, "#[deprecated]"),
SecondaryAttribute::Deprecated(Some(ref note)) => {
write!(f, r#"#[deprecated("{note}")]"#)
}
SecondaryAttribute::Custom(ref k) => write!(f, "#[{k}]"),
SecondaryAttribute::ContractLibraryMethod => write!(f, "#[contract_library_method]"),
SecondaryAttribute::Event => write!(f, "#[event]"),
SecondaryAttribute::Field(ref k) => write!(f, "#[field({k})]"),
}
}
}
impl AsRef<str> for FunctionAttribute {
fn as_ref(&self) -> &str {
match self {
FunctionAttribute::Foreign(string) => string,
FunctionAttribute::Builtin(string) => string,
FunctionAttribute::Oracle(string) => string,
FunctionAttribute::Test { .. } => "",
}
}
}
impl AsRef<str> for SecondaryAttribute {
fn as_ref(&self) -> &str {
match self {
SecondaryAttribute::Deprecated(Some(string)) => string,
SecondaryAttribute::Deprecated(None) => "",
SecondaryAttribute::Custom(string) | SecondaryAttribute::Field(string) => string,
SecondaryAttribute::ContractLibraryMethod => "",
SecondaryAttribute::Event => "",
}
}
}
/// Note that `self` is not present - it is a contextual keyword rather than a true one as it is
/// only special within `impl`s. Otherwise `self` functions as a normal identifier.
#[derive(PartialEq, Eq, Hash, Debug, Copy, Clone, PartialOrd, Ord)]
#[cfg_attr(test, derive(strum_macros::EnumIter))]
pub enum Keyword {
As,
Assert,
AssertEq,
Bool,
Char,
CompTime,
Constrain,
Contract,
Crate,
Dep,
Distinct,
Else,
Field,
Fn,
For,
FormatString,
Global,
If,
Impl,
In,
Internal,
Let,
Mod,
Mut,
Open,
Pub,
Return,
String,
Struct,
Trait,
Type,
Unconstrained,
Use,
Where,
While,
}
impl fmt::Display for Keyword {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Keyword::As => write!(f, "as"),
Keyword::Assert => write!(f, "assert"),
Keyword::AssertEq => write!(f, "assert_eq"),
Keyword::Bool => write!(f, "bool"),
Keyword::Char => write!(f, "char"),
Keyword::CompTime => write!(f, "comptime"),
Keyword::Constrain => write!(f, "constrain"),
Keyword::Contract => write!(f, "contract"),
Keyword::Crate => write!(f, "crate"),
Keyword::Dep => write!(f, "dep"),
Keyword::Distinct => write!(f, "distinct"),
Keyword::Else => write!(f, "else"),
Keyword::Field => write!(f, "Field"),
Keyword::Fn => write!(f, "fn"),
Keyword::For => write!(f, "for"),
Keyword::FormatString => write!(f, "fmtstr"),
Keyword::Global => write!(f, "global"),
Keyword::If => write!(f, "if"),
Keyword::Impl => write!(f, "impl"),
Keyword::In => write!(f, "in"),
Keyword::Internal => write!(f, "internal"),
Keyword::Let => write!(f, "let"),
Keyword::Mod => write!(f, "mod"),
Keyword::Mut => write!(f, "mut"),
Keyword::Open => write!(f, "open"),
Keyword::Pub => write!(f, "pub"),
Keyword::Return => write!(f, "return"),
Keyword::String => write!(f, "str"),
Keyword::Struct => write!(f, "struct"),
Keyword::Trait => write!(f, "trait"),
Keyword::Type => write!(f, "type"),
Keyword::Unconstrained => write!(f, "unconstrained"),
Keyword::Use => write!(f, "use"),
Keyword::Where => write!(f, "where"),
Keyword::While => write!(f, "while"),
}
}
}
impl Keyword {
/// Looks up a word in the source program and returns the associated keyword, if found.
pub(crate) fn lookup_keyword(word: &str) -> Option<Token> {
let keyword = match word {
"as" => Keyword::As,
"assert" => Keyword::Assert,
"assert_eq" => Keyword::AssertEq,
"bool" => Keyword::Bool,
"char" => Keyword::Char,
"comptime" => Keyword::CompTime,
"constrain" => Keyword::Constrain,
"contract" => Keyword::Contract,
"crate" => Keyword::Crate,
"dep" => Keyword::Dep,
"distinct" => Keyword::Distinct,
"else" => Keyword::Else,
"Field" => Keyword::Field,
"fn" => Keyword::Fn,
"for" => Keyword::For,
"fmtstr" => Keyword::FormatString,
"global" => Keyword::Global,
"if" => Keyword::If,
"impl" => Keyword::Impl,
"in" => Keyword::In,
"internal" => Keyword::Internal,
"let" => Keyword::Let,
"mod" => Keyword::Mod,
"mut" => Keyword::Mut,
"open" => Keyword::Open,
"pub" => Keyword::Pub,
"return" => Keyword::Return,
"str" => Keyword::String,
"struct" => Keyword::Struct,
"trait" => Keyword::Trait,
"type" => Keyword::Type,
"unconstrained" => Keyword::Unconstrained,
"use" => Keyword::Use,
"where" => Keyword::Where,
"while" => Keyword::While,
"true" => return Some(Token::Bool(true)),
"false" => return Some(Token::Bool(false)),
_ => return None,
};
Some(Token::Keyword(keyword))
}
}
#[cfg(test)]
mod keywords {
use strum::IntoEnumIterator;
use super::{Keyword, Token};
#[test]
fn lookup_consistency() {
for keyword in Keyword::iter() {
let resolved_token =
Keyword::lookup_keyword(&format!("{keyword}")).unwrap_or_else(|| {
panic!("Keyword::lookup_keyword couldn't find Keyword {keyword}")
});
assert_eq!(
resolved_token,
Token::Keyword(keyword),
"Keyword::lookup_keyword returns unexpected Keyword"
);
}
}
}
pub struct Tokens(pub Vec<SpannedToken>);
type TokenMapIter = Map<IntoIter<SpannedToken>, fn(SpannedToken) -> (Token, Span)>;
impl<'a> From<Tokens> for chumsky::Stream<'a, Token, Span, TokenMapIter> {
fn from(tokens: Tokens) -> Self {
let end_of_input = match tokens.0.last() {
Some(spanned_token) => spanned_token.to_span(),
None => Span::single_char(0),
};
fn get_span(token: SpannedToken) -> (Token, Span) {
let span = token.to_span();
(token.into_token(), span)
}
let iter = tokens.0.into_iter().map(get_span as fn(_) -> _);
chumsky::Stream::from_iter(end_of_input, iter)
}
}