sway_error/
parser_error.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
use sway_types::ast::PunctKind;
use sway_types::{Ident, Span};
use thiserror::Error;

#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
pub enum ParseErrorKind {
    #[error("Expected an import name, group of imports, or `*`.")]
    ExpectedImportNameGroupOrGlob,
    #[error("Expected an item.")]
    ExpectedAnItem,
    #[error("Expected an item after doc comment.")]
    ExpectedAnItemAfterDocComment,
    #[error("Expected a comma or closing parenthesis in function arguments.")]
    ExpectedCommaOrCloseParenInFnArgs,
    #[error("Unknown assembly instruction.")]
    UnrecognizedOpCode {
        known_op_codes: &'static [&'static str],
    },
    #[error("Unexpected token in statement.")]
    UnexpectedTokenInStatement,
    #[error("This expression cannot be assigned to.")]
    UnassignableExpression {
        /// The friendly name of the kind of the expression
        /// that makes the overall expression unassignable.
        /// E.g., "function call", or "struct instantiation".
        erroneous_expression_kind: &'static str,
        /// [Span] that points to either the whole left-hand
        /// side of the reassignment, or to a [Span] of an
        /// erroneous nested expression, if only a part of
        /// the assignment target expression is erroneous.
        erroneous_expression_span: Span,
    },
    #[error("Unexpected token after array index.")]
    UnexpectedTokenAfterArrayIndex,
    #[error("Invalid literal to use as a field name.")]
    InvalidLiteralFieldName,
    #[error("Invalid statement.")]
    InvalidStatement,
    #[error("Invalid item.")]
    InvalidItem,
    #[error("Integer field names cannot have type suffixes.")]
    IntFieldWithTypeSuffix,
    #[error("Expected a field name.")]
    ExpectedFieldName,
    #[error("Expected a comma or closing parenthesis in this tuple or parenthesized expression.")]
    ExpectedCommaOrCloseParenInTupleOrParenExpression,
    #[error("Expected an expression.")]
    ExpectedExpression,
    #[error("Unexpected token after array length.")]
    UnexpectedTokenAfterArrayLength,
    #[error("Expected a comma, semicolon or closing bracket when parsing this array.")]
    ExpectedCommaSemicolonOrCloseBracketInArray,
    #[error("Unexpected token after asm return type.")]
    UnexpectedTokenAfterAsmReturnType,
    #[error("Malformed asm immediate value.")]
    MalformedAsmImmediate,
    #[error("Expected an identifier.")]
    ExpectedIdent,
    #[error("Expected an pattern.")]
    ExpectedPattern,
    #[error("Unexpected token after str length.")]
    UnexpectedTokenAfterStrLength,
    #[error("Expected a type.")]
    ExpectedType,
    #[error("Unexpected token after array type length.")]
    UnexpectedTokenAfterArrayTypeLength,
    #[error("Expected an opening brace.")]
    ExpectedOpenBrace,
    #[error("Expected an opening parenthesis.")]
    ExpectedOpenParen,
    #[error("Expected an opening square bracket.")]
    ExpectedOpenBracket,
    #[error("Expected a literal.")]
    ExpectedLiteral,
    #[error("Expected a module kind (script, contract, predicate or library).")]
    ExpectedModuleKind,
    #[error("Expected `{}`.", kinds.iter().map(PunctKind::as_char).collect::<String>())]
    ExpectedPunct { kinds: Vec<PunctKind> },
    #[error("Expected `{}`.", word)]
    ExpectedKeyword { word: &'static str },
    #[error("Unexpected token after abi address.")]
    UnexpectedTokenAfterAbiAddress,
    #[error("Expected an attribute.")]
    ExpectedAnAttribute,
    #[error("Unexpected token after an attribute.")]
    UnexpectedTokenAfterAttribute,
    #[error("Identifiers cannot begin with a double underscore, as that naming convention is reserved for compiler intrinsics.")]
    InvalidDoubleUnderscore,
    #[error("Unexpected rest token, must be at the end of pattern.")]
    UnexpectedRestPattern,
    #[error("Identifiers cannot be a reserved keyword.")]
    ReservedKeywordIdentifier,
    #[error("Unnecessary visibility qualifier, `{}` is implied here.", visibility)]
    UnnecessaryVisibilityQualifier { visibility: Ident },
    #[error("Expected a doc comment.")]
    ExpectedDocComment,
    #[error("Top of file doc comments are reserved for module level documentation.\nTry using the `//!` syntax.")]
    ExpectedModuleDocComment,
    #[error("Use the `struct` keyword to define records, instead of `class`.")]
    UnexpectedClass,
    #[error("Field projections, e.g., `foo.bar` cannot have type arguments.")]
    FieldProjectionWithGenericArgs,
    #[error("Unexpected token after __ptr type.")]
    UnexpectedTokenAfterPtrType,
    #[error("Unexpected token after __slice type.")]
    UnexpectedTokenAfterSliceType,
    #[error("Expected a path type.")]
    ExpectedPathType,
    #[error("Expected ':'. Enum variants must be in the form `Variant: ()`, `Variant: <type>`, or `Variant: (<type1>, ..., <typeN>)`. E.g., `Foo: (), or `Bar: (bool, u32)`.")]
    MissingColonInEnumTypeField,
    #[error("Expected storage key of type U256.")]
    ExpectedStorageKeyU256,
}

#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
#[error("{}", kind)]
pub struct ParseError {
    pub span: Span,
    pub kind: ParseErrorKind,
}