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
use std::ops::Range;
use thiserror::Error;
/// Error containing information about an error encountered by the Fluent Parser.
///
/// Errors in Fluent Parser are non-fatal, and the syntax has been
/// designed to allow for strong recovery.
///
/// In result [`ParserError`] is designed to point at the slice of
/// the input that is most likely to be a complete fragment from after
/// the end of a valid entry, to the start of the next valid entry, with
/// the invalid syntax in the middle.
///
///
/// # Example
///
/// ```
/// use fluent_syntax::parser;
/// use fluent_syntax::ast;
///
/// let ftl = r#"
/// key1 = Value 1
///
/// g@Rb@ge = #2y ds
///
/// key2 = Value 2
///
/// "#;
///
/// let (resource, errors) = parser::parse_runtime(ftl)
/// .expect_err("Resource should contain errors.");
///
/// assert_eq!(
/// errors,
/// vec![
/// parser::ParserError {
/// pos: 18..19,
/// slice: Some(17..35),
/// kind: parser::ErrorKind::ExpectedToken('=')
/// }
/// ]
/// );
///
/// assert_eq!(
/// resource.body[0],
/// ast::Entry::Message(
/// ast::Message {
/// id: ast::Identifier {
/// name: "key1"
/// },
/// value: Some(ast::Pattern {
/// elements: vec![
/// ast::PatternElement::TextElement {
/// value: "Value 1"
/// },
/// ]
/// }),
/// attributes: vec![],
/// comment: None,
/// }
/// ),
/// );
///
/// assert_eq!(
/// resource.body[1],
/// ast::Entry::Junk {
/// content: "g@Rb@ge = #2y ds\n\n"
/// }
/// );
///
/// assert_eq!(
/// resource.body[2],
/// ast::Entry::Message(
/// ast::Message {
/// id: ast::Identifier {
/// name: "key2"
/// },
/// value: Some(ast::Pattern {
/// elements: vec![
/// ast::PatternElement::TextElement {
/// value: "Value 2"
/// },
/// ]
/// }),
/// attributes: vec![],
/// comment: None,
/// }
/// ),
/// );
/// ```
///
/// The information contained in the `ParserError` should allow the tooling
/// to display rich contextual annotations of the error slice, using
/// crates such as `annotate-snippers`.
#[derive(Error, Debug, PartialEq, Eq, Clone)]
#[error("{}", self.kind)]
pub struct ParserError {
/// Precise location of where the parser encountered the error.
pub pos: Range<usize>,
/// Slice of the input from the end of the last valid entry to the beginning
/// of the next valid entry with the invalid syntax in the middle.
pub slice: Option<Range<usize>>,
/// The type of the error that the parser encountered.
pub kind: ErrorKind,
}
macro_rules! error {
($kind:expr, $start:expr) => {{
Err(ParserError {
pos: $start..$start + 1,
slice: None,
kind: $kind,
})
}};
($kind:expr, $start:expr, $end:expr) => {{
Err(ParserError {
pos: $start..$end,
slice: None,
kind: $kind,
})
}};
}
/// Kind of an error associated with the [`ParserError`].
#[derive(Error, Debug, PartialEq, Eq, Clone)]
pub enum ErrorKind {
#[error("Expected a token starting with \"{0}\"")]
ExpectedToken(char),
#[error("Expected one of \"{range}\"")]
ExpectedCharRange { range: String },
#[error("Expected a message field for \"{entry_id}\"")]
ExpectedMessageField { entry_id: String },
#[error("Expected a term field for \"{entry_id}\"")]
ExpectedTermField { entry_id: String },
#[error("Callee is not allowed here")]
ForbiddenCallee,
#[error("The select expression must have a default variant")]
MissingDefaultVariant,
#[error("Expected a value")]
MissingValue,
#[error("A select expression can only have one default variant")]
MultipleDefaultVariants,
#[error("Message references can't be used as a selector")]
MessageReferenceAsSelector,
#[error("Term references can't be used as a selector")]
TermReferenceAsSelector,
#[error("Message attributes can't be used as a selector")]
MessageAttributeAsSelector,
#[error("Term attributes can't be used as a selector")]
TermAttributeAsPlaceable,
#[error("Unterminated string literal")]
UnterminatedStringLiteral,
#[error("Positional arguments must come before named arguments")]
PositionalArgumentFollowsNamed,
#[error("The \"{0}\" argument appears twice")]
DuplicatedNamedArgument(String),
#[error("Unknown escape sequence")]
UnknownEscapeSequence(String),
#[error("Invalid unicode escape sequence, \"{0}\"")]
InvalidUnicodeEscapeSequence(String),
#[error("Unbalanced closing brace")]
UnbalancedClosingBrace,
#[error("Expected an inline expression")]
ExpectedInlineExpression,
#[error("Expected a simple expression as selector")]
ExpectedSimpleExpressionAsSelector,
#[error("Expected a string or number literal")]
ExpectedLiteral,
}