Make more fields public
This commit is contained in:
parent
b12b7e9977
commit
3a79d8650a
4 changed files with 22 additions and 6 deletions
|
@ -8,9 +8,9 @@ pub type Result<T> = std::result::Result<T, ConversionError>;
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
#[error("Failed to turn {value} into a {target_type}")]
|
#[error("Failed to turn {value} into a {target_type}")]
|
||||||
pub struct ConversionError {
|
pub struct ConversionError {
|
||||||
value: DynVal,
|
pub value: DynVal,
|
||||||
target_type: &'static str,
|
pub target_type: &'static str,
|
||||||
source: Option<Box<dyn std::error::Error>>,
|
pub source: Option<Box<dyn std::error::Error>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConversionError {
|
impl ConversionError {
|
||||||
|
|
12
src/error.rs
12
src/error.rs
|
@ -1,4 +1,8 @@
|
||||||
use crate::{ast::Span, dynval, parser::lexer};
|
use crate::{
|
||||||
|
ast::Span,
|
||||||
|
dynval,
|
||||||
|
parser::lexer::{self, LexicalError},
|
||||||
|
};
|
||||||
use codespan_reporting::diagnostic;
|
use codespan_reporting::diagnostic;
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
@ -25,6 +29,10 @@ impl Error {
|
||||||
Error::ParseError { source: err }
|
Error::ParseError { source: err }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn at(self, span: Span) -> Self {
|
||||||
|
Self::Spanned(span, Box::new(self))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_span(&self) -> Option<Span> {
|
pub fn get_span(&self) -> Option<Span> {
|
||||||
match self {
|
match self {
|
||||||
Self::ParseError { source } => get_parse_error_span(source),
|
Self::ParseError { source } => get_parse_error_span(source),
|
||||||
|
@ -68,7 +76,7 @@ fn get_parse_error_span(err: &lalrpop_util::ParseError<usize, lexer::Token, lexe
|
||||||
lalrpop_util::ParseError::UnrecognizedEOF { location, expected: _ } => Some(Span(*location, *location)),
|
lalrpop_util::ParseError::UnrecognizedEOF { location, expected: _ } => Some(Span(*location, *location)),
|
||||||
lalrpop_util::ParseError::UnrecognizedToken { token, expected: _ } => Some(Span(token.0, token.2)),
|
lalrpop_util::ParseError::UnrecognizedToken { token, expected: _ } => Some(Span(token.0, token.2)),
|
||||||
lalrpop_util::ParseError::ExtraToken { token } => Some(Span(token.0, token.2)),
|
lalrpop_util::ParseError::ExtraToken { token } => Some(Span(token.0, token.2)),
|
||||||
lalrpop_util::ParseError::User { error: _ } => None,
|
lalrpop_util::ParseError::User { error: LexicalError(l, r) } => Some(Span(*l, *r)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub enum Token {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
||||||
pub struct LexicalError(usize, usize);
|
pub struct LexicalError(pub usize, pub usize);
|
||||||
|
|
||||||
impl std::fmt::Display for LexicalError {
|
impl std::fmt::Display for LexicalError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use crate::ast::{SimplExpr::{self, *}, Span, BinOp::*, UnaryOp::*};
|
use crate::ast::{SimplExpr::{self, *}, Span, BinOp::*, UnaryOp::*};
|
||||||
use crate::parser::lexer::{Token, LexicalError};
|
use crate::parser::lexer::{Token, LexicalError};
|
||||||
use crate::parser::lalrpop_helpers::*;
|
use crate::parser::lalrpop_helpers::*;
|
||||||
|
use lalrpop_util::ParseError;
|
||||||
|
|
||||||
|
|
||||||
grammar;
|
grammar;
|
||||||
|
@ -42,6 +43,8 @@ extern {
|
||||||
"number" => Token::NumLit(<String>),
|
"number" => Token::NumLit(<String>),
|
||||||
"string" => Token::StrLit(<String>),
|
"string" => Token::StrLit(<String>),
|
||||||
|
|
||||||
|
"lexer_error" => Token::Error,
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +59,12 @@ Comma<T>: Vec<T> = {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub Expr: SimplExpr = {
|
pub Expr: SimplExpr = {
|
||||||
|
|
||||||
#[precedence(level="0")]
|
#[precedence(level="0")]
|
||||||
|
<l:@L> "lexer_error" <r:@R> =>? {
|
||||||
|
Err(ParseError::User { error: LexicalError(l, r) })
|
||||||
|
},
|
||||||
|
|
||||||
<Literal>,
|
<Literal>,
|
||||||
<l:@L> <ident:"identifier"> <r:@R> => VarRef(Span(l, r), ident.to_string()),
|
<l:@L> <ident:"identifier"> <r:@R> => VarRef(Span(l, r), ident.to_string()),
|
||||||
"(" <ExprReset> ")",
|
"(" <ExprReset> ")",
|
||||||
|
|
Loading…
Add table
Reference in a new issue