Skip to content

Commit

Permalink
Merge e002df6 into 673f5be
Browse files Browse the repository at this point in the history
  • Loading branch information
morenol committed Sep 6, 2020
2 parents 673f5be + e002df6 commit 3ad3de4
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 149 deletions.
4 changes: 2 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::{Error, ErrorKind, Result};
use crate::error::{Error, ParseErrorKind, Result};
use crate::source::SourceLocationInfo;
use crate::value::{Value, ValuesMap};
use crate::TemplateEnv;
Expand Down Expand Up @@ -41,7 +41,7 @@ impl<'a> Context<'a> {
} else if let Some(value) = self.global_scope.read().unwrap().get(key) {
Ok(value.clone())
} else {
Err(Error::from(ErrorKind::UndefinedValue(
Err(Error::from(ParseErrorKind::UndefinedValue(
key.to_string(),
SourceLocationInfo::new(0, 0),
)))
Expand Down
129 changes: 76 additions & 53 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@ use std::error;
use std::fmt;
use std::io;

#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
Io(io::Error),
ParseRender(ErrorKind),
ParseError(ParseErrorKind),
RenderError(RenderErrorKind),
}

#[non_exhaustive]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ErrorKind {
pub enum ParseErrorKind {
Unspecified,
YetUnsupported,
FileNotFound,
ExtensionDisabled,
TemplateEnvAbsent,
TemplateNotFound(String),
TemplateNotParsed,
InvalidValueType,
InvalidTemplateName,
InvalidOperation,
UndefinedValue(String, SourceLocationInfo),
ExpectedStringLiteral(SourceLocationInfo),
ExpectedIdentifier(SourceLocationInfo),
ExpectedSquareBracket(SourceLocationInfo),
ExpectedRoundBracket(SourceLocationInfo),
ExpectedCurlyBracket(SourceLocationInfo),
ExpectedBracket(&'static str, SourceLocationInfo),
ExpectedToken(&'static str, SourceLocationInfo),
ExpectedExpression(SourceLocationInfo),
ExpectedEndOfStatement(SourceLocationInfo),
Expand All @@ -44,84 +41,74 @@ pub enum ErrorKind {
UnexpectedRawEnd(SourceLocationInfo),
}

impl ErrorKind {
impl ParseErrorKind {
fn as_cow_str(&self) -> Cow<'_, str> {
match &*self {
ErrorKind::Unspecified => "Unspecified error".into(),
ErrorKind::YetUnsupported => "Jinja feature not yet supported".into(),
ErrorKind::FileNotFound => "File not found".into(),
ErrorKind::ExtensionDisabled => "Extension disabled".into(),
ErrorKind::TemplateEnvAbsent => "Expected template environment".into(),
ErrorKind::TemplateNotFound(tmp) => format!("Template {} not found", tmp).into(),
ErrorKind::TemplateNotParsed => "Template not parsed".into(),
ErrorKind::InvalidValueType => {
"Invalid type of the value in the particular context".into()
}
ErrorKind::InvalidTemplateName => "Invalid name of the template".into(),
ErrorKind::InvalidOperation => "Invalid operation".into(),
// ErrorKind::MetadataParseError => "Metadata Parse Error ", // TODO: Solve in jinja2cpp
ErrorKind::UndefinedValue(value, location) => format!(
ParseErrorKind::Unspecified => "Unspecified error".into(),
ParseErrorKind::YetUnsupported => "Jinja feature not yet supported".into(),
ParseErrorKind::ExtensionDisabled => "Extension disabled".into(),
ParseErrorKind::TemplateEnvAbsent => "Expected template environment".into(),
ParseErrorKind::TemplateNotFound(tmp) => format!("Template {} not found", tmp).into(),
ParseErrorKind::InvalidTemplateName => "Invalid name of the template".into(),
// ParseErrorKind::MetadataParseError => "Metadata Parse Error ", // TODO: Solve in jinja2cpp
ParseErrorKind::UndefinedValue(value, location) => format!(
"{} error: {} is not defined",
location.position_log(),
value
)
.into(),
ErrorKind::ExpectedStringLiteral(_location) => "String literal expected".into(),
ErrorKind::ExpectedIdentifier(_location) => "Identifier expected".into(),
ErrorKind::ExpectedSquareBracket(location) => {
format!("{} error: ']' expected", location.position_log()).into()
ParseErrorKind::ExpectedStringLiteral(_location) => "String literal expected".into(),
ParseErrorKind::ExpectedIdentifier(_location) => "Identifier expected".into(),
ParseErrorKind::ExpectedBracket(bracket, location) => {
format!("{} error: '{}' expected", location.position_log(), bracket).into()
}
ErrorKind::ExpectedRoundBracket(location) => {
format!("{} error: ')' expected", location.position_log()).into()
}
ErrorKind::ExpectedCurlyBracket(_location) => "'}}' expected".into(),
ErrorKind::ExpectedToken(s, location) => format!(
ParseErrorKind::ExpectedToken(s, location) => format!(
"{} error: Specific token expected ({})",
location.position_log(),
s
)
.into(),
ErrorKind::ExpectedExpression(location) => {
ParseErrorKind::ExpectedExpression(location) => {
format!("{} error: Expression expected", location.position_log()).into()
}
ErrorKind::ExpectedEndOfStatement(_location) => "End of statement expected".into(),
ErrorKind::ExpectedRawEnd(location) => {
ParseErrorKind::ExpectedEndOfStatement(_location) => "End of statement expected".into(),
ParseErrorKind::ExpectedRawEnd(location) => {
format!("{} error: {{% endraw %}} expected", location.position_log()).into()
}
ErrorKind::UnexpectedToken(location) => {
ParseErrorKind::UnexpectedToken(location) => {
format!("{} error: Unexpected token", location.position_log()).into()
}
ErrorKind::UnexpectedStatement(_location) => "Unexpected statement".into(),
ErrorKind::UnexpectedCommentBegin(_location) => {
ParseErrorKind::UnexpectedStatement(_location) => "Unexpected statement".into(),
ParseErrorKind::UnexpectedCommentBegin(_location) => {
"Unexpected comment block begin ('{{#')".into()
}
ErrorKind::UnexpectedCommentEnd(location) => format!(
ParseErrorKind::UnexpectedCommentEnd(location) => format!(
"{} error: Unexpected comment block end ('#}}')",
location.position_log()
)
.into(),
ErrorKind::UnexpectedExprBegin(_location) => {
ParseErrorKind::UnexpectedExprBegin(_location) => {
"Unexpected expression block begin ('{{{{}}')".into()
}
ErrorKind::UnexpectedExprEnd(location) => format!(
ParseErrorKind::UnexpectedExprEnd(location) => format!(
"{} error: Unexpected expression block end ('}}}}')",
location.position_log()
)
.into(),
ErrorKind::UnexpectedStmtBegin(_location) => {
ParseErrorKind::UnexpectedStmtBegin(_location) => {
"Unexpected statement block begin ('{{%')".into()
}
ErrorKind::UnexpectedStmtEnd(location) => format!(
ParseErrorKind::UnexpectedStmtEnd(location) => format!(
"{} error: Unexpected statement block end ('%}}')",
location.position_log()
)
.into(),
ErrorKind::UnexpectedRawBegin(location) => format!(
ParseErrorKind::UnexpectedRawBegin(location) => format!(
"{} error: Unexpected raw block begin ('{{% raw %}}')",
location.position_log()
)
.into(),
ErrorKind::UnexpectedRawEnd(location) => format!(
ParseErrorKind::UnexpectedRawEnd(location) => format!(
"{} error: Unexpected raw block end {{% endraw %}}",
location.position_log()
)
Expand All @@ -130,26 +117,56 @@ impl ErrorKind {
}
}

#[non_exhaustive]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum RenderErrorKind {
FileNotFound,
TemplateNotParsed,
InvalidOperation,
InvalidValueType,
}

impl RenderErrorKind {
fn as_cow_str(&self) -> Cow<'_, str> {
match &*self {
RenderErrorKind::FileNotFound => "File not found".into(),
RenderErrorKind::TemplateNotParsed => "Template not parsed".into(),
RenderErrorKind::InvalidOperation => "Invalid operation".into(),
RenderErrorKind::InvalidValueType => {
"Invalid type of the value in the particular context".into()
}
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Io(ref err) => err.fmt(f),
Error::ParseRender(ref err) => err.fmt(f),
Error::ParseError(ref err) => err.fmt(f),
Error::RenderError(ref err) => err.fmt(f),
}
}
}
impl error::Error for ErrorKind {}
impl error::Error for ParseErrorKind {}
impl error::Error for RenderErrorKind {}

impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Error::Io(ref err) => err.source(),
Error::ParseRender(ref err) => Some(err),
Error::ParseError(ref err) => Some(err),
Error::RenderError(ref err) => Some(err),
}
}
}

impl fmt::Display for ErrorKind {
impl fmt::Display for ParseErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_cow_str())
}
}
impl fmt::Display for RenderErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_cow_str())
}
Expand All @@ -161,9 +178,15 @@ impl From<io::Error> for Error {
}
}

impl From<ErrorKind> for Error {
fn from(err: ErrorKind) -> Error {
Error::ParseRender(err)
impl From<ParseErrorKind> for Error {
fn from(err: ParseErrorKind) -> Error {
Error::ParseError(err)
}
}

impl From<RenderErrorKind> for Error {
fn from(err: RenderErrorKind) -> Error {
Error::RenderError(err)
}
}

Expand Down
47 changes: 28 additions & 19 deletions src/expression_parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::{Error, ErrorKind, Result};
use crate::error::{Error, ParseErrorKind, Result};
use crate::expression_evaluator::{
BinaryOperation, CallParams, DictionaryExpression, Expression, FilteredExpression,
FullExpressionEvaluator, SubscriptExpression, TupleExpression, UnaryOperation,
Expand Down Expand Up @@ -219,8 +219,9 @@ impl ExpressionParser {
}
result = Some(filter);
} else {
return Err(Error::from(ErrorKind::ExpectedIdentifier(
SourceLocationInfo::new(1, 2),
let range = lexer.span();
return Err(Error::from(ParseErrorKind::ExpectedIdentifier(
SourceLocationInfo::new(range.start, range.end),
)));
}
if let Some(Token::Pipe) = lexer.peek() {
Expand All @@ -230,8 +231,9 @@ impl ExpressionParser {
}
}
None => {
return Err(Error::from(ErrorKind::ExpectedIdentifier(
SourceLocationInfo::new(1, 2),
let range = lexer.span();
return Err(Error::from(ParseErrorKind::ExpectedIdentifier(
SourceLocationInfo::new(range.start, range.end),
)));
}
}
Expand Down Expand Up @@ -272,7 +274,8 @@ impl ExpressionParser {
Ok(Some(params))
} else {
let range = lexer.span();
Err(Error::from(ErrorKind::ExpectedCurlyBracket(
Err(Error::from(ParseErrorKind::ExpectedBracket(
"}",
SourceLocationInfo::new_with_range(range.start, range.end),
)))
}
Expand All @@ -297,14 +300,16 @@ impl ExpressionParser {
Token::LCrlBracket => ExpressionParser::parse_dict(&mut lexer)?,

_ => {
return Err(Error::from(ErrorKind::ExpectedExpression(
SourceLocationInfo::new(1, 2), // TODO: Use actual source locations
let range = lexer.span();
return Err(Error::from(ParseErrorKind::ExpectedExpression(
SourceLocationInfo::new_with_range(range.start, range.end), // TODO: Use actual source locations
)));
}
}
} else {
return Err(Error::from(ErrorKind::ExpectedExpression(
SourceLocationInfo::new(1, 2), // TODO: Use actual source locations
let range = lexer.span();
return Err(Error::from(ParseErrorKind::ExpectedExpression(
SourceLocationInfo::new_with_range(range.start, range.end), // TODO: Use actual source locations
)));
};

Expand Down Expand Up @@ -336,8 +341,9 @@ impl ExpressionParser {
Ok(expr) => exprs.push(expr),
Err(err) => {
if !exprs.is_empty() {
return Err(Error::from(ErrorKind::ExpectedRoundBracket(
SourceLocationInfo::new(1, 2),
return Err(Error::from(ParseErrorKind::ExpectedBracket(
")",
SourceLocationInfo::new_at_the_end(),
)));
} else {
return Err(err);
Expand Down Expand Up @@ -374,7 +380,8 @@ impl ExpressionParser {
} else {
let range = lexer.span();

return Err(Error::from(ErrorKind::ExpectedSquareBracket(
return Err(Error::from(ParseErrorKind::ExpectedBracket(
"]",
SourceLocationInfo::new_with_range(range.start, range.end),
)));
}
Expand All @@ -388,7 +395,7 @@ impl ExpressionParser {
))));
} else {
let range = lexer.span();
return Err(Error::from(ErrorKind::ExpectedIdentifier(
return Err(Error::from(ParseErrorKind::ExpectedIdentifier(
SourceLocationInfo::new_with_range(range.start, range.end),
)));
}
Expand Down Expand Up @@ -421,7 +428,8 @@ impl ExpressionParser {
} else {
let range = lexer.span();

Err(Error::from(ErrorKind::ExpectedSquareBracket(
Err(Error::from(ParseErrorKind::ExpectedBracket(
"]",
SourceLocationInfo::new_with_range(range.start, range.end),
)))
}
Expand All @@ -447,15 +455,15 @@ impl ExpressionParser {
} else {
let range = lexer.span();

return Err(Error::from(ErrorKind::ExpectedToken(
return Err(Error::from(ParseErrorKind::ExpectedToken(
":",
SourceLocationInfo::new_with_range(range.start, range.end),
)));
}
} else {
let range = lexer.span();
return Err(Error::from(ErrorKind::ExpectedStringLiteral(
SourceLocationInfo::new(range.start, range.end),
return Err(Error::from(ParseErrorKind::ExpectedStringLiteral(
SourceLocationInfo::new_with_range(range.start, range.end),
)));
}
}
Expand All @@ -464,7 +472,8 @@ impl ExpressionParser {
} else {
let range = lexer.span();

Err(Error::from(ErrorKind::ExpectedCurlyBracket(
Err(Error::from(ParseErrorKind::ExpectedBracket(
"}",
SourceLocationInfo::new_with_range(range.start, range.end),
)))
}
Expand Down
3 changes: 3 additions & 0 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl SourceLocationInfo {
}
}
}
pub fn set_filename(&mut self, filename: String) {
self.filename = filename;
}
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
Loading

0 comments on commit 3ad3de4

Please sign in to comment.