| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#; |
| 2 | use thiserrorrust-analyzer cargo thiserror 2.0.12 crate/::Errorrust-analyzer cargo thiserror-impl 2.0.12 Error:; |
| 3 | |
| 4 | /// Error containing information about an error encountered by the Fluent Parser. |
| 5 | /// |
| 6 | /// Errors in Fluent Parser are non-fatal, and the syntax has been |
| 7 | /// designed to allow for strong recovery. |
| 8 | /// |
| 9 | /// In result [`ParserError`] is designed to point at the slice of |
| 10 | /// the input that is most likely to be a complete fragment from after |
| 11 | /// the end of a valid entry, to the start of the next valid entry, with |
| 12 | /// the invalid syntax in the middle. |
| 13 | /// |
| 14 | /// |
| 15 | /// # Example |
| 16 | /// |
| 17 | /// ``` |
| 18 | /// use fluent_syntax::parser; |
| 19 | /// use fluent_syntax::ast; |
| 20 | /// |
| 21 | /// let ftl = r#" |
| 22 | /// key1 = Value 1 |
| 23 | /// |
| 24 | /// g@Rb@ge = #2y ds |
| 25 | /// |
| 26 | /// key2 = Value 2 |
| 27 | /// |
| 28 | /// "#; |
| 29 | /// |
| 30 | /// let (resource, errors) = parser::parse_runtime(ftl) |
| 31 | /// .expect_err("Resource should contain errors."); |
| 32 | /// |
| 33 | /// assert_eq!( |
| 34 | /// errors, |
| 35 | /// vec![ |
| 36 | /// parser::ParserError { |
| 37 | /// pos: 18..19, |
| 38 | /// slice: Some(17..35), |
| 39 | /// kind: parser::ErrorKind::ExpectedToken('=') |
| 40 | /// } |
| 41 | /// ] |
| 42 | /// ); |
| 43 | /// |
| 44 | /// assert_eq!( |
| 45 | /// resource.body[0], |
| 46 | /// ast::Entry::Message( |
| 47 | /// ast::Message { |
| 48 | /// id: ast::Identifier { |
| 49 | /// name: "key1" |
| 50 | /// }, |
| 51 | /// value: Some(ast::Pattern { |
| 52 | /// elements: vec![ |
| 53 | /// ast::PatternElement::TextElement { |
| 54 | /// value: "Value 1" |
| 55 | /// }, |
| 56 | /// ] |
| 57 | /// }), |
| 58 | /// attributes: vec![], |
| 59 | /// comment: None, |
| 60 | /// } |
| 61 | /// ), |
| 62 | /// ); |
| 63 | /// |
| 64 | /// assert_eq!( |
| 65 | /// resource.body[1], |
| 66 | /// ast::Entry::Junk { |
| 67 | /// content: "g@Rb@ge = #2y ds\n\n" |
| 68 | /// } |
| 69 | /// ); |
| 70 | /// |
| 71 | /// assert_eq!( |
| 72 | /// resource.body[2], |
| 73 | /// ast::Entry::Message( |
| 74 | /// ast::Message { |
| 75 | /// id: ast::Identifier { |
| 76 | /// name: "key2" |
| 77 | /// }, |
| 78 | /// value: Some(ast::Pattern { |
| 79 | /// elements: vec![ |
| 80 | /// ast::PatternElement::TextElement { |
| 81 | /// value: "Value 2" |
| 82 | /// }, |
| 83 | /// ] |
| 84 | /// }), |
| 85 | /// attributes: vec![], |
| 86 | /// comment: None, |
| 87 | /// } |
| 88 | /// ), |
| 89 | /// ); |
| 90 | /// ``` |
| 91 | /// |
| 92 | /// The information contained in the `ParserError` should allow the tooling |
| 93 | /// to display rich contextual annotations of the error slice, using |
| 94 | /// crates such as `annotate-snippers`. |
| 95 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Errorrust-analyzer cargo thiserror-impl 2.0.12 Error:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 96 | #[error("{}", self.kindrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ParserError#kind.)] |
| 97 | pub struct ParserErrorrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ParserError# { |
| 98 | /// Precise location of where the parser encountered the error. |
| 99 | pub posrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ParserError#pos.: Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>, |
| 100 | /// Slice of the input from the end of the last valid entry to the beginning |
| 101 | /// of the next valid entry with the invalid syntax in the middle. |
| 102 | pub slicerust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ParserError#slice.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<usize>>, |
| 103 | /// The type of the error that the parser encountered. |
| 104 | pub kindrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ParserError#kind.: ErrorKindrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#, |
| 105 | } |
| 106 | |
| 107 | macro_rules! errorrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/error! { |
| 108 | ($kind:expr, $start:expr) => {{ |
| 109 | Err(ParserError { |
| 110 | pos: $start..$start + 1, |
| 111 | slice: None, |
| 112 | kind: $kind, |
| 113 | }) |
| 114 | }}; |
| 115 | ($kind:expr, $start:expr, $end:expr) => {{ |
| 116 | Err(ParserError { |
| 117 | pos: $start..$end, |
| 118 | slice: None, |
| 119 | kind: $kind, |
| 120 | }) |
| 121 | }}; |
| 122 | } |
| 123 | |
| 124 | /// Kind of an error associated with the [`ParserError`]. |
| 125 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Errorrust-analyzer cargo thiserror-impl 2.0.12 Error:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 126 | pub enum ErrorKindrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind# { |
| 127 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Expected a token starting with \"{0}\"")] |
| 128 | ExpectedTokenrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedToken#(char), |
| 129 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Expected one of \"{range}\"")] |
| 130 | ExpectedCharRangerust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedCharRange# { rangerust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedCharRange#range.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# }, |
| 131 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Expected a message field for \"{entry_id}\"")] |
| 132 | ExpectedMessageFieldrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedMessageField# { entry_idrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedMessageField#entry_id.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# }, |
| 133 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Expected a term field for \"{entry_id}\"")] |
| 134 | ExpectedTermFieldrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedTermField# { entry_idrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedTermField#entry_id.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# }, |
| 135 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Callee is not allowed here")] |
| 136 | ForbiddenCalleerust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ForbiddenCallee#, |
| 137 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("The select expression must have a default variant")] |
| 138 | MissingDefaultVariantrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#MissingDefaultVariant#, |
| 139 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Expected a value")] |
| 140 | MissingValuerust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#MissingValue#, |
| 141 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("A select expression can only have one default variant")] |
| 142 | MultipleDefaultVariantsrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#MultipleDefaultVariants#, |
| 143 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Message references can't be used as a selector")] |
| 144 | MessageReferenceAsSelectorrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#MessageReferenceAsSelector#, |
| 145 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Term references can't be used as a selector")] |
| 146 | TermReferenceAsSelectorrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#TermReferenceAsSelector#, |
| 147 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Message attributes can't be used as a selector")] |
| 148 | MessageAttributeAsSelectorrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#MessageAttributeAsSelector#, |
| 149 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Term attributes can't be used as a selector")] |
| 150 | TermAttributeAsPlaceablerust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#TermAttributeAsPlaceable#, |
| 151 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Unterminated string literal")] |
| 152 | UnterminatedStringLiteralrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#UnterminatedStringLiteral#, |
| 153 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Positional arguments must come before named arguments")] |
| 154 | PositionalArgumentFollowsNamedrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#PositionalArgumentFollowsNamed#, |
| 155 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("The \"{0}\" argument appears twice")] |
| 156 | DuplicatedNamedArgumentrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#DuplicatedNamedArgument#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 157 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Unknown escape sequence")] |
| 158 | UnknownEscapeSequencerust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#UnknownEscapeSequence#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 159 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Invalid unicode escape sequence, \"{0}\"")] |
| 160 | InvalidUnicodeEscapeSequencerust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#InvalidUnicodeEscapeSequence#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), |
| 161 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Unbalanced closing brace")] |
| 162 | UnbalancedClosingBracerust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#UnbalancedClosingBrace#, |
| 163 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Expected an inline expression")] |
| 164 | ExpectedInlineExpressionrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedInlineExpression#, |
| 165 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Expected a simple expression as selector")] |
| 166 | ExpectedSimpleExpressionAsSelectorrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedSimpleExpressionAsSelector#, |
| 167 | #[errorrust-analyzer cargo thiserror-impl 2.0.12 error:("Expected a string or number literal")] |
| 168 | ExpectedLiteralrust-analyzer cargo fluent-syntax 0.12.0 parser/errors/ErrorKind#ExpectedLiteral#, |
| 169 | } |