fix clippy lints
This commit is contained in:
parent
3a79d8650a
commit
a6fe813ed1
8 changed files with 27 additions and 103 deletions
|
@ -13,7 +13,6 @@ regex = "1"
|
||||||
itertools = "0.10"
|
itertools = "0.10"
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
maplit = "1.0"
|
maplit = "1.0"
|
||||||
codespan-reporting = "0.11"
|
|
||||||
logos = "0.12"
|
logos = "0.12"
|
||||||
|
|
||||||
serde = {version = "1.0", features = ["derive"]}
|
serde = {version = "1.0", features = ["derive"]}
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let mut files = codespan_reporting::files::SimpleFiles::new();
|
|
||||||
|
|
||||||
let input = "12 + foo * 2 < 2 ? bar == true : false";
|
|
||||||
|
|
||||||
let _ = files.add("foo.eww", input);
|
|
||||||
let ast = simplexpr::parser::parse_string(input);
|
|
||||||
|
|
||||||
let mut vars = HashMap::new();
|
|
||||||
vars.insert("foo".to_string(), "2".into());
|
|
||||||
|
|
||||||
match ast.and_then(|x| x.eval(&vars).map_err(|e| e.into())) {
|
|
||||||
Ok(ast) => {
|
|
||||||
println!("{:?}", ast);
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
let diag = err.pretty_diagnostic();
|
|
||||||
use codespan_reporting::term;
|
|
||||||
let mut writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Always);
|
|
||||||
term::emit(&mut writer, &term::Config::default(), &files, &diag).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
19
src/ast.rs
19
src/ast.rs
|
@ -5,20 +5,6 @@ use serde::{Deserialize, Serialize};
|
||||||
#[derive(Eq, PartialEq, Clone, Copy, Serialize, Deserialize)]
|
#[derive(Eq, PartialEq, Clone, Copy, Serialize, Deserialize)]
|
||||||
pub struct Span(pub usize, pub usize);
|
pub struct Span(pub usize, pub usize);
|
||||||
|
|
||||||
pub trait MaybeSpanned {
|
|
||||||
fn try_span(&self) -> Option<Span>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MaybeSpanned for Span {
|
|
||||||
fn try_span(&self) -> Option<Span> {
|
|
||||||
Some(*self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn span_between(a: impl MaybeSpanned, b: impl MaybeSpanned) -> Option<Span> {
|
|
||||||
Some(Span(a.try_span()?.0, b.try_span()?.1))
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for Span {
|
impl std::fmt::Display for Span {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "{}..{}", self.0, self.1)
|
write!(f, "{}..{}", self.0, self.1)
|
||||||
|
@ -65,11 +51,6 @@ pub enum SimplExpr {
|
||||||
JsonAccess(Span, Box<SimplExpr>, Box<SimplExpr>),
|
JsonAccess(Span, Box<SimplExpr>, Box<SimplExpr>),
|
||||||
FunctionCall(Span, String, Vec<SimplExpr>),
|
FunctionCall(Span, String, Vec<SimplExpr>),
|
||||||
}
|
}
|
||||||
impl MaybeSpanned for SimplExpr {
|
|
||||||
fn try_span(&self) -> Option<Span> {
|
|
||||||
Some(self.span())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for SimplExpr {
|
impl std::fmt::Display for SimplExpr {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::ast::{MaybeSpanned, Span};
|
use crate::ast::Span;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{convert::TryFrom, fmt, iter::FromIterator};
|
use std::{convert::TryFrom, fmt, iter::FromIterator};
|
||||||
|
@ -26,12 +26,6 @@ impl ConversionError {
|
||||||
#[derive(Clone, Deserialize, Serialize, Default, Eq)]
|
#[derive(Clone, Deserialize, Serialize, Default, Eq)]
|
||||||
pub struct DynVal(pub String, pub Option<Span>);
|
pub struct DynVal(pub String, pub Option<Span>);
|
||||||
|
|
||||||
impl MaybeSpanned for DynVal {
|
|
||||||
fn try_span(&self) -> Option<Span> {
|
|
||||||
self.1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<String> for DynVal {
|
impl From<String> for DynVal {
|
||||||
fn from(s: String) -> Self {
|
fn from(s: String) -> Self {
|
||||||
DynVal(s, None)
|
DynVal(s, None)
|
||||||
|
@ -98,7 +92,7 @@ impl_try_from!(impl From<DynVal> {
|
||||||
for f64 => |x| x.as_f64();
|
for f64 => |x| x.as_f64();
|
||||||
for i32 => |x| x.as_i32();
|
for i32 => |x| x.as_i32();
|
||||||
for bool => |x| x.as_bool();
|
for bool => |x| x.as_bool();
|
||||||
for Vec<String> => |x| x.as_vec();
|
//for Vec<String> => |x| x.as_vec();
|
||||||
});
|
});
|
||||||
|
|
||||||
impl_primval_from!(bool, i32, u32, f32, u8, f64, &str);
|
impl_primval_from!(bool, i32, u32, f32, u8, f64, &str);
|
||||||
|
@ -145,26 +139,24 @@ impl DynVal {
|
||||||
self.0.parse().map_err(|e| ConversionError::new(self.clone(), "bool", Box::new(e)))
|
self.0.parse().map_err(|e| ConversionError::new(self.clone(), "bool", Box::new(e)))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_vec(&self) -> Result<Vec<String>> {
|
// pub fn as_vec(&self) -> Result<Vec<String>> {
|
||||||
match self.0.strip_prefix('[').and_then(|x| x.strip_suffix(']')) {
|
// match self.0.strip_prefix('[').and_then(|x| x.strip_suffix(']')) {
|
||||||
Some(content) => {
|
// Some(content) => {
|
||||||
let mut items: Vec<String> = content.split(',').map(|x: &str| x.to_string()).collect();
|
// let mut items: Vec<String> = content.split(',').map(|x: &str| x.to_string()).collect();
|
||||||
let mut removed = 0;
|
// let mut removed = 0;
|
||||||
for times_ran in 0..items.len() {
|
// for times_ran in 0..items.len() {
|
||||||
// escapes `,` if there's a `\` before em
|
//// escapes `,` if there's a `\` before em
|
||||||
if items[times_ran - removed].ends_with('\\') {
|
// if items[times_ran - removed].ends_with('\\') {
|
||||||
items[times_ran - removed].pop();
|
// items[times_ran - removed].pop();
|
||||||
let it = items.remove((times_ran + 1) - removed);
|
// let it = items.remove((times_ran + 1) - removed);
|
||||||
items[times_ran - removed] += ",";
|
// items[times_ran - removed] += ",";
|
||||||
items[times_ran - removed] += ⁢
|
// items[times_ran - removed] += ⁢
|
||||||
removed += 1;
|
// removed += 1;
|
||||||
}
|
//}
|
||||||
}
|
// Ok(items)
|
||||||
Ok(items)
|
//}
|
||||||
}
|
// None => Err(ConversionError { value: self.clone(), target_type: "vec", source: None }),
|
||||||
None => Err(ConversionError { value: self.clone(), target_type: "vec", source: None }),
|
//}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_json_value(&self) -> Result<serde_json::Value> {
|
pub fn as_json_value(&self) -> Result<serde_json::Value> {
|
||||||
serde_json::from_str::<serde_json::Value>(&self.0)
|
serde_json::from_str::<serde_json::Value>(&self.0)
|
||||||
|
|
27
src/error.rs
27
src/error.rs
|
@ -3,7 +3,6 @@ use crate::{
|
||||||
dynval,
|
dynval,
|
||||||
parser::lexer::{self, LexicalError},
|
parser::lexer::{self, LexicalError},
|
||||||
};
|
};
|
||||||
use codespan_reporting::diagnostic;
|
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
|
@ -42,32 +41,6 @@ impl Error {
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pretty_diagnostic(&self) -> diagnostic::Diagnostic<usize> {
|
|
||||||
let diag = diagnostic::Diagnostic::error().with_message(format!("{}", self));
|
|
||||||
if let Some(span) = self.get_span() {
|
|
||||||
diag.with_labels(vec![diagnostic::Label::primary(0, span.0..span.1)])
|
|
||||||
} else {
|
|
||||||
diag
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait ErrorExt {
|
|
||||||
fn at(self, span: Span) -> Error;
|
|
||||||
}
|
|
||||||
impl ErrorExt for Box<dyn std::error::Error> {
|
|
||||||
fn at(self, span: Span) -> Error {
|
|
||||||
Error::Spanned(span, self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub trait ResultExt<T> {
|
|
||||||
fn at(self, span: Span) -> std::result::Result<T, Error>;
|
|
||||||
}
|
|
||||||
impl<T, E: std::error::Error + 'static> ResultExt<T> for std::result::Result<T, E> {
|
|
||||||
fn at(self, span: Span) -> std::result::Result<T, Error> {
|
|
||||||
self.map_err(|x| Error::Spanned(span, Box::new(x)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_parse_error_span(err: &lalrpop_util::ParseError<usize, lexer::Token, lexer::LexicalError>) -> Option<Span> {
|
fn get_parse_error_span(err: &lalrpop_util::ParseError<usize, lexer::Token, lexer::LexicalError>) -> Option<Span> {
|
||||||
|
|
|
@ -51,7 +51,7 @@ type VarName = String;
|
||||||
|
|
||||||
pub trait FunctionSource {
|
pub trait FunctionSource {
|
||||||
type Err;
|
type Err;
|
||||||
fn run_fn(&self, name: &str, args: &Vec<DynVal>) -> Result<DynVal, Self::Err>;
|
fn run_fn(&self, name: &str, args: &[DynVal]) -> Result<DynVal, Self::Err>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SimplExpr {
|
impl SimplExpr {
|
||||||
|
@ -139,6 +139,7 @@ impl SimplExpr {
|
||||||
BinOp::Mod => DynVal::from(a.as_f64()? % b.as_f64()?),
|
BinOp::Mod => DynVal::from(a.as_f64()? % b.as_f64()?),
|
||||||
BinOp::GT => DynVal::from(a.as_f64()? > b.as_f64()?),
|
BinOp::GT => DynVal::from(a.as_f64()? > b.as_f64()?),
|
||||||
BinOp::LT => DynVal::from(a.as_f64()? < b.as_f64()?),
|
BinOp::LT => DynVal::from(a.as_f64()? < b.as_f64()?),
|
||||||
|
#[allow(clippy::useless_conversion)]
|
||||||
BinOp::Elvis => DynVal::from(if a.0.is_empty() { b } else { a }),
|
BinOp::Elvis => DynVal::from(if a.0.is_empty() { b } else { a }),
|
||||||
BinOp::RegexMatch => {
|
BinOp::RegexMatch => {
|
||||||
let regex = regex::Regex::new(&b.as_string()?)?;
|
let regex = regex::Regex::new(&b.as_string()?)?;
|
||||||
|
|
|
@ -9,7 +9,10 @@ pub mod parser;
|
||||||
use ast::SimplExpr;
|
use ast::SimplExpr;
|
||||||
use lalrpop_util::lalrpop_mod;
|
use lalrpop_util::lalrpop_mod;
|
||||||
|
|
||||||
lalrpop_mod!(pub simplexpr_parser);
|
lalrpop_mod!(
|
||||||
|
#[allow(clippy::all)]
|
||||||
|
pub simplexpr_parser
|
||||||
|
);
|
||||||
|
|
||||||
pub fn parse_string(s: &str) -> Result<SimplExpr, error::Error> {
|
pub fn parse_string(s: &str) -> Result<SimplExpr, error::Error> {
|
||||||
parser::parse_string(s)
|
parser::parse_string(s)
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
pub fn parse_string(s: &str) -> Result<SimplExpr> {
|
pub fn parse_string(s: &str) -> Result<SimplExpr> {
|
||||||
let lexer = lexer::Lexer::new(s);
|
let lexer = lexer::Lexer::new(s);
|
||||||
let parser = crate::simplexpr_parser::ExprParser::new();
|
let parser = crate::simplexpr_parser::ExprParser::new();
|
||||||
Ok(parser.parse(lexer).map_err(|e| Error::from_parse_error(e))?)
|
parser.parse(lexer).map_err(Error::from_parse_error)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Add table
Reference in a new issue