1use sway_types::ast::PunctKind;
2use sway_types::{Ident, Span};
3use thiserror::Error;
4
5#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
6pub enum ParseErrorKind {
7 #[error("Expected an import name, group of imports, or `*`.")]
8 ExpectedImportNameGroupOrGlob,
9 #[error("Expected an item.")]
10 ExpectedAnItem,
11 #[error("Expected {} element.",
12 if *is_only_documented {
13 "a documented"
14 } else {
15 "an annotated"
16 }
17 )]
18 ExpectedAnAnnotatedElement {
19 is_only_documented: bool,
23 },
24 #[error("Expected an inner doc comment (`//!`) to be at the top of the module file.")]
25 ExpectedInnerDocCommentAtTheTopOfFile,
26 #[error("Expected a comma or closing parenthesis in function arguments.")]
27 ExpectedCommaOrCloseParenInFnArgs,
28 #[error("Unknown assembly instruction.")]
29 UnrecognizedOpCode {
30 known_op_codes: &'static [&'static str],
31 },
32 #[error("Unexpected token in statement.")]
33 UnexpectedTokenInStatement,
34 #[error("This expression cannot be assigned to.")]
35 UnassignableExpression {
36 erroneous_expression_kind: &'static str,
40 erroneous_expression_span: Span,
45 },
46 #[error("Unexpected token after array index.")]
47 UnexpectedTokenAfterArrayIndex,
48 #[error("Invalid literal to use as a field name.")]
49 InvalidLiteralFieldName,
50 #[error("Invalid statement.")]
51 InvalidStatement,
52 #[error("Invalid item.")]
53 InvalidItem,
54 #[error("Integer field names cannot have type suffixes.")]
55 IntFieldWithTypeSuffix,
56 #[error("Expected a field name.")]
57 ExpectedFieldName,
58 #[error("Expected a comma or closing parenthesis in this tuple or parenthesized expression.")]
59 ExpectedCommaOrCloseParenInTupleOrParenExpression,
60 #[error("Expected an expression.")]
61 ExpectedExpression,
62 #[error("Unexpected token after array length.")]
63 UnexpectedTokenAfterArrayLength,
64 #[error("Expected a comma, semicolon or closing bracket when parsing this array.")]
65 ExpectedCommaSemicolonOrCloseBracketInArray,
66 #[error("Unexpected token after asm return type.")]
67 UnexpectedTokenAfterAsmReturnType,
68 #[error("Malformed asm immediate value.")]
69 MalformedAsmImmediate,
70 #[error("Expected an identifier.")]
71 ExpectedIdent,
72 #[error("Expected an pattern.")]
73 ExpectedPattern,
74 #[error("Unexpected token after str length.")]
75 UnexpectedTokenAfterStrLength,
76 #[error("Expected a type.")]
77 ExpectedType,
78 #[error("Unexpected token after array type length.")]
79 UnexpectedTokenAfterArrayTypeLength,
80 #[error("Expected an opening brace.")]
81 ExpectedOpenBrace,
82 #[error("Expected an opening parenthesis.")]
83 ExpectedOpenParen,
84 #[error("Expected an opening square bracket.")]
85 ExpectedOpenBracket,
86 #[error("Expected a literal.")]
87 ExpectedLiteral,
88 #[error("Expected a module kind (script, contract, predicate, or library).")]
89 ExpectedModuleKind,
90 #[error("Expected `{}`.", kinds.iter().map(PunctKind::as_char).collect::<String>())]
91 ExpectedPunct { kinds: Vec<PunctKind> },
92 #[error("Expected `{}`.", word)]
93 ExpectedKeyword { word: &'static str },
94 #[error("Unexpected token after abi address.")]
95 UnexpectedTokenAfterAbiAddress,
96 #[error("Expected an attribute.")]
97 ExpectedAnAttribute,
98 #[error("Unexpected token after an attribute.")]
99 UnexpectedTokenAfterAttribute,
100 #[error("Identifiers cannot begin with a double underscore, as that naming convention is reserved for compiler intrinsics.")]
101 InvalidDoubleUnderscore,
102 #[error("Unexpected rest token, must be at the end of pattern.")]
103 UnexpectedRestPattern,
104 #[error("Identifier cannot be a reserved keyword.")]
105 ReservedKeywordIdentifier,
106 #[error("Unnecessary visibility qualifier, `{}` is implied here.", visibility)]
107 UnnecessaryVisibilityQualifier { visibility: Ident },
108 #[error("Expected a doc comment.")]
109 ExpectedDocComment,
110 #[error("Use the `struct` keyword to define records, instead of `class`.")]
111 UnexpectedClass,
112 #[error("Field projections, e.g., `foo.bar` cannot have type arguments.")]
113 FieldProjectionWithGenericArgs,
114 #[error("Unexpected token after __ptr type.")]
115 UnexpectedTokenAfterPtrType,
116 #[error("Unexpected token after __slice type.")]
117 UnexpectedTokenAfterSliceType,
118 #[error("Expected a path type.")]
119 ExpectedPathType,
120 #[error("Expected ':'. Enum variants must be in the form `Variant: ()`, `Variant: <type>`, or `Variant: (<type1>, ..., <typeN>)`. E.g., `Foo: (), or `Bar: (bool, u32)`.")]
121 MissingColonInEnumTypeField,
122 #[error("Expected storage key of type U256.")]
123 ExpectedStorageKeyU256,
124}
125
126#[derive(Debug, Error, Clone, PartialEq, Eq, Hash)]
127#[error("{}", kind)]
128pub struct ParseError {
129 pub span: Span,
130 pub kind: ParseErrorKind,
131}