mirror of
https://codeberg.org/icewind/php-literal-parser.git
synced 2026-06-03 18:44:07 +02:00
better trailing error
This commit is contained in:
parent
3db713b909
commit
d185883573
2 changed files with 26 additions and 5 deletions
27
src/error.rs
27
src/error.rs
|
|
@ -24,10 +24,10 @@ pub enum ParseError {
|
||||||
#[diagnostic(transparent)]
|
#[diagnostic(transparent)]
|
||||||
/// An array key was found that is invalid for this position
|
/// An array key was found that is invalid for this position
|
||||||
UnexpectedArrayKey(ArrayKeyError),
|
UnexpectedArrayKey(ArrayKeyError),
|
||||||
#[error("Trailing characters after parsing")]
|
#[error(transparent)]
|
||||||
#[diagnostic(code(php_object_parser::trailing))]
|
#[diagnostic(transparent)]
|
||||||
/// Trailing characters after parsing
|
/// Trailing characters after parsing
|
||||||
TrailingCharacters,
|
TrailingCharacters(#[from] TrailingError),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
#[diagnostic(code(php_object_parser::serde))]
|
#[diagnostic(code(php_object_parser::serde))]
|
||||||
/// Error while populating serde type
|
/// Error while populating serde type
|
||||||
|
|
@ -216,6 +216,27 @@ impl ArrayKeyError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Error, Diagnostic)]
|
||||||
|
#[diagnostic(code(php_object_parser::trailing))]
|
||||||
|
#[error("Trailing characters after parsing")]
|
||||||
|
pub struct TrailingError {
|
||||||
|
src: String,
|
||||||
|
#[snippet(src)]
|
||||||
|
snip: SourceSpan,
|
||||||
|
#[highlight(snip, label("end of parsed value"))]
|
||||||
|
err_span: SourceSpan,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TrailingError {
|
||||||
|
pub fn new(source: &str, err_span: Span) -> Self {
|
||||||
|
TrailingError {
|
||||||
|
src: source.into(),
|
||||||
|
snip: map_span(&(0..source.len())),
|
||||||
|
err_span: map_span(&err_span),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait ExpectToken<'source> {
|
pub trait ExpectToken<'source> {
|
||||||
fn expect_token(
|
fn expect_token(
|
||||||
self,
|
self,
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use serde::de::{
|
||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::error::{ArrayKeyError, ArrayKeyErrorKind, ExpectToken, ResultExt};
|
use crate::error::{ArrayKeyError, ArrayKeyErrorKind, ExpectToken, ResultExt, TrailingError};
|
||||||
use crate::lexer::{SpannedToken, Token};
|
use crate::lexer::{SpannedToken, Token};
|
||||||
use crate::num::ParseIntError;
|
use crate::num::ParseIntError;
|
||||||
use crate::parser::{ArraySyntax, Parser};
|
use crate::parser::{ArraySyntax, Parser};
|
||||||
|
|
@ -62,7 +62,7 @@ where
|
||||||
token: Token::SemiColon,
|
token: Token::SemiColon,
|
||||||
..
|
..
|
||||||
}) => Ok(t),
|
}) => Ok(t),
|
||||||
Some(_) => Err(ParseError::TrailingCharacters.into()),
|
Some(token) => Err(TrailingError::new(s, token.span.start..token.span.start).into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue