result alias

main
Ziyang Hu 2 years ago
parent 28e52242e0
commit 666507dd12

@ -1,4 +1,4 @@
use pest::iterators::{Pair, Pairs};
use pest::iterators::{Pair};
use pest::Parser as PestParser;
use pest::prec_climber::{Assoc, PrecClimber, Operator};
use crate::parser::Parser;
@ -6,7 +6,7 @@ use crate::parser::Rule;
use lazy_static::lazy_static;
use crate::ast::Expr::{Apply, Const};
use crate::error::CozoError;
use crate::error::CozoError::ReservedIdent;
use crate::error::Result;
use crate::value::Value;
@ -66,7 +66,7 @@ pub trait ExprVisitor<'a, T> {
}
fn build_expr_infix<'a>(lhs: Result<Expr<'a>, CozoError>, op: Pair<Rule>, rhs: Result<Expr<'a>, CozoError>) -> Result<Expr<'a>, CozoError> {
fn build_expr_infix<'a>(lhs: Result<Expr<'a>>, op: Pair<Rule>, rhs: Result<Expr<'a>>) -> Result<Expr<'a>> {
let lhs = lhs?;
let rhs = rhs?;
let op = match op.as_rule() {
@ -96,12 +96,12 @@ fn parse_int(s: &str, radix: u32) -> i64 {
}
#[inline]
fn parse_raw_string(pair: Pair<Rule>) -> Result<String, CozoError> {
fn parse_raw_string(pair: Pair<Rule>) -> Result<String> {
Ok(pair.into_inner().into_iter().next().unwrap().as_str().to_string())
}
#[inline]
fn parse_quoted_string(pair: Pair<Rule>) -> Result<String, CozoError> {
fn parse_quoted_string(pair: Pair<Rule>) -> Result<String> {
let pairs = pair.into_inner().next().unwrap().into_inner();
let mut ret = String::with_capacity(pairs.as_str().len());
for pair in pairs {
@ -129,7 +129,7 @@ fn parse_quoted_string(pair: Pair<Rule>) -> Result<String, CozoError> {
#[inline]
fn parse_s_quoted_string(pair: Pair<Rule>) -> Result<String, CozoError> {
fn parse_s_quoted_string(pair: Pair<Rule>) -> Result<String> {
let pairs = pair.into_inner().next().unwrap().into_inner();
let mut ret = String::with_capacity(pairs.as_str().len());
for pair in pairs {
@ -156,7 +156,7 @@ fn parse_s_quoted_string(pair: Pair<Rule>) -> Result<String, CozoError> {
}
#[inline]
pub fn parse_string(pair: Pair<Rule>) -> Result<String, CozoError> {
pub fn parse_string(pair: Pair<Rule>) -> Result<String> {
match pair.as_rule() {
Rule::quoted_string => Ok(parse_quoted_string(pair)?),
Rule::s_quoted_string => Ok(parse_s_quoted_string(pair)?),
@ -165,7 +165,7 @@ pub fn parse_string(pair: Pair<Rule>) -> Result<String, CozoError> {
}
}
fn build_expr_primary(pair: Pair<Rule>) -> Result<Expr, CozoError> {
fn build_expr_primary(pair: Pair<Rule>) -> Result<Expr> {
match pair.as_rule() {
Rule::expr => build_expr_primary(pair.into_inner().next().unwrap()),
Rule::term => build_expr_primary(pair.into_inner().next().unwrap()),
@ -189,7 +189,7 @@ fn build_expr_primary(pair: Pair<Rule>) -> Result<Expr, CozoError> {
Rule::dot_float | Rule::sci_float => Ok(Const(Value::Float(pair.as_str().replace('_', "").parse::<f64>()?))),
Rule::null => Ok(Const(Value::Null)),
Rule::boolean => Ok(Const(Value::Bool(pair.as_str() == "true"))),
Rule::quoted_string | Rule::s_quoted_string | Rule::s_quoted_string => Ok(
Rule::quoted_string | Rule::s_quoted_string | Rule::raw_string => Ok(
Const(Value::OwnString(Box::new(parse_string(pair)?)))),
_ => {
println!("{:#?}", pair);
@ -198,11 +198,11 @@ fn build_expr_primary(pair: Pair<Rule>) -> Result<Expr, CozoError> {
}
}
fn build_expr(pair: Pair<Rule>) -> Result<Expr, CozoError> {
fn build_expr(pair: Pair<Rule>) -> Result<Expr> {
PREC_CLIMBER.climb(pair.into_inner(), build_expr_primary, build_expr_infix)
}
pub fn parse_expr_from_str(inp: &str) -> Result<Expr, CozoError> {
pub fn parse_expr_from_str(inp: &str) -> Result<Expr> {
let expr_tree = Parser::parse(Rule::expr, inp)?.next().unwrap();
build_expr(expr_tree)
}

@ -1,11 +1,10 @@
use pest::Parser as PestParser;
use pest::iterators::{Pair, Pairs};
use crate::ast::parse_string;
use crate::env::Env;
use crate::error::CozoError;
use crate::error::Result;
use crate::error::CozoError::*;
use crate::parser::{Parser, Rule};
use crate::typing::{BaseType, Col, Edge, Node, Structured, StructuredEnv, StructuredEnvItem, TableId, Typing};
use crate::parser::{Rule};
use crate::typing::{Col, Edge, Node, Structured, StructuredEnv, StructuredEnvItem, TableId, Typing};
use crate::typing::Persistence::{Global, Local};
use crate::typing::StorageStatus::Planned;
use crate::value::Value;
@ -14,7 +13,7 @@ fn parse_ident(pair: Pair<Rule>) -> String {
pair.as_str().to_string()
}
fn build_name_in_def(pair: Pair<Rule>, forbid_underscore: bool) -> Result<String, CozoError> {
fn build_name_in_def(pair: Pair<Rule>, forbid_underscore: bool) -> Result<String> {
let inner = pair.into_inner().next().unwrap();
let name = match inner.as_rule() {
Rule::ident => parse_ident(inner),
@ -28,7 +27,7 @@ fn build_name_in_def(pair: Pair<Rule>, forbid_underscore: bool) -> Result<String
}
}
fn parse_col_name(pair: Pair<Rule>) -> Result<(String, bool), CozoError> {
fn parse_col_name(pair: Pair<Rule>) -> Result<(String, bool)> {
let mut pairs = pair.into_inner();
let mut is_key = false;
let mut nxt_pair = pairs.next().unwrap();
@ -42,7 +41,7 @@ fn parse_col_name(pair: Pair<Rule>) -> Result<(String, bool), CozoError> {
impl StructuredEnvItem {
pub fn build_edge_def(&mut self, pair: Pair<Rule>, table_id: TableId) -> Result<(), CozoError> {
pub fn build_edge_def(&mut self, pair: Pair<Rule>, table_id: TableId) -> Result<()> {
let mut inner = pair.into_inner();
let src_name = build_name_in_def(inner.next().unwrap(), true)?;
let src = self.resolve(&src_name).ok_or(UndefinedType)?;
@ -91,7 +90,7 @@ impl StructuredEnvItem {
Err(NameConflict)
}
}
pub fn build_node_def(&mut self, pair: Pair<Rule>, table_id: TableId) -> Result<(), CozoError> {
pub fn build_node_def(&mut self, pair: Pair<Rule>, table_id: TableId) -> Result<()> {
let mut inner = pair.into_inner();
let name = build_name_in_def(inner.next().unwrap(), true)?;
let (keys, cols) = self.build_col_defs(inner.next().unwrap())?;
@ -110,7 +109,7 @@ impl StructuredEnvItem {
}
}
fn build_type(&self, pair: Pair<Rule>) -> Result<Typing, CozoError> {
fn build_type(&self, pair: Pair<Rule>) -> Result<Typing> {
let mut pairs = pair.into_inner();
let mut inner = pairs.next().unwrap();
let nullable = if Rule::nullable_marker == inner.as_rule() {
@ -142,12 +141,12 @@ impl StructuredEnvItem {
})
}
fn build_default_value(&self, _pair: Pair<Rule>) -> Result<Value<'static>, CozoError> {
fn build_default_value(&self, _pair: Pair<Rule>) -> Result<Value<'static>> {
// TODO: _pair is an expression, parse it and evaluate it to a constant value
Ok(Value::Null)
}
fn build_col_entry(&self, pair: Pair<Rule>) -> Result<(Col, bool), CozoError> {
fn build_col_entry(&self, pair: Pair<Rule>) -> Result<(Col, bool)> {
let mut pairs = pair.into_inner();
let (name, is_key) = parse_col_name(pairs.next().unwrap())?;
let typ = self.build_type(pairs.next().unwrap())?;
@ -164,7 +163,7 @@ impl StructuredEnvItem {
}, is_key))
}
fn build_col_defs(&self, pair: Pair<Rule>) -> Result<(Vec<Col>, Vec<Col>), CozoError> {
fn build_col_defs(&self, pair: Pair<Rule>) -> Result<(Vec<Col>, Vec<Col>)> {
let mut keys = vec![];
let mut cols = vec![];
for pair in pair.into_inner() {
@ -181,7 +180,7 @@ impl StructuredEnvItem {
}
impl StructuredEnv {
pub fn build_table(&mut self, pairs: Pairs<Rule>) -> Result<(), CozoError> {
pub fn build_table(&mut self, pairs: Pairs<Rule>) -> Result<()> {
for pair in pairs {
match pair.as_rule() {
r @ (Rule::global_def | Rule::local_def) => {
@ -217,6 +216,8 @@ impl StructuredEnv {
#[cfg(test)]
mod tests {
use super::*;
use pest::Parser as PestParser;
use crate::parser::Parser;
#[test]
fn definitions() {

@ -1,3 +1,4 @@
use std::result;
use thiserror::Error;
use crate::parser::Rule;
@ -36,3 +37,5 @@ pub enum CozoError {
#[error(transparent)]
Parse(#[from] pest::error::Error<Rule>),
}
pub type Result<T> = result::Result<T, CozoError>;

@ -1,5 +1,6 @@
use crate::ast::Expr;
use crate::ast::Expr::*;
use crate::error::Result;
use crate::error::CozoError;
use crate::error::CozoError::*;
use crate::value::Value::*;
@ -22,8 +23,8 @@ impl Default for Evaluator {
}
}
impl<'a> ExprVisitor<'a, Result<Expr<'a>, CozoError>> for Evaluator {
fn visit_expr(&mut self, ex: &Expr<'a>) -> Result<Expr<'a>, CozoError> {
impl<'a> ExprVisitor<'a, Result<Expr<'a>>> for Evaluator {
fn visit_expr(&mut self, ex: &Expr<'a>) -> Result<Expr<'a>> {
match ex {
Apply(op, args) => {
match op {
@ -55,7 +56,7 @@ impl<'a> ExprVisitor<'a, Result<Expr<'a>, CozoError>> for Evaluator {
}
impl Evaluator {
fn add_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn add_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -84,7 +85,7 @@ impl Evaluator {
}
}
fn sub_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn sub_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -109,7 +110,7 @@ impl Evaluator {
}
}
fn mul_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn mul_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -135,7 +136,7 @@ impl Evaluator {
}
fn div_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn div_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -160,7 +161,7 @@ impl Evaluator {
}
}
fn mod_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn mod_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -183,7 +184,7 @@ impl Evaluator {
}
fn eq_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn eq_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -201,7 +202,7 @@ impl Evaluator {
}
fn ne_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn ne_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -219,7 +220,7 @@ impl Evaluator {
}
fn gt_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn gt_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -245,7 +246,7 @@ impl Evaluator {
}
fn ge_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn ge_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -271,7 +272,7 @@ impl Evaluator {
}
fn lt_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn lt_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -296,7 +297,7 @@ impl Evaluator {
}
}
fn le_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn le_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -322,7 +323,7 @@ impl Evaluator {
}
fn pow_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn pow_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -347,7 +348,7 @@ impl Evaluator {
}
}
fn coalesce_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn coalesce_exprs<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
match exprs {
[a, b] => {
let a = self.visit_expr(a)?;
@ -367,7 +368,7 @@ impl Evaluator {
}
}
fn negate_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn negate_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
Ok(match exprs {
[a] => {
match self.visit_expr(a)? {
@ -391,7 +392,7 @@ impl Evaluator {
}
fn minus_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn minus_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
Ok(match exprs {
[a] => {
match self.visit_expr(a)? {
@ -408,7 +409,7 @@ impl Evaluator {
}
fn test_null_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn test_null_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
Ok(match exprs {
[a] => {
match self.visit_expr(a)? {
@ -421,7 +422,7 @@ impl Evaluator {
})
}
fn not_null_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn not_null_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
Ok(match exprs {
[a] => {
match self.visit_expr(a)? {
@ -434,7 +435,7 @@ impl Evaluator {
})
}
fn or_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn or_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
let mut unevaluated = vec![];
let mut has_null = false;
for expr in exprs {
@ -467,7 +468,7 @@ impl Evaluator {
}
fn and_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>, CozoError> {
fn and_expr<'a>(&mut self, exprs: &[Expr<'a>]) -> Result<Expr<'a>> {
let mut unevaluated = vec![];
let mut no_null = true;
for expr in exprs {

@ -1,4 +1,3 @@
use pest::Parser as PestParser;
use pest_derive::Parser;
@ -10,6 +9,7 @@ pub struct Parser;
#[cfg(test)]
mod tests {
use super::*;
use pest::Parser as PestParser;
#[test]
fn db() {
@ -66,9 +66,7 @@ mod tests {
#[test]
fn numbers() {
assert_eq!(Parser::parse(Rule::number, "123").unwrap().as_str(), "123");
assert_eq!(Parser::parse(Rule::number, "-123").unwrap().as_str(), "-123");
assert_eq!(Parser::parse(Rule::number, "0").unwrap().as_str(), "0");
assert_eq!(Parser::parse(Rule::number, "-0").unwrap().as_str(), "-0");
assert_eq!(Parser::parse(Rule::number, "0123").unwrap().as_str(), "0123");
assert_eq!(Parser::parse(Rule::number, "000_1").unwrap().as_str(), "000_1");
assert!(Parser::parse(Rule::number, "_000_1").is_err());
@ -81,7 +79,6 @@ mod tests {
assert_eq!(Parser::parse(Rule::number, "123.45").unwrap().as_str(), "123.45");
assert_eq!(Parser::parse(Rule::number, "1_23.4_5_").unwrap().as_str(), "1_23.4_5_");
assert_ne!(Parser::parse(Rule::number, "123.").unwrap().as_str(), "123.");
assert_eq!(Parser::parse(Rule::number, "-123e-456").unwrap().as_str(), "-123e-456");
assert_eq!(Parser::parse(Rule::number, "123.333e456").unwrap().as_str(), "123.333e456");
assert_eq!(Parser::parse(Rule::number, "1_23.33_3e45_6").unwrap().as_str(), "1_23.33_3e45_6");
}

@ -1,5 +1,4 @@
use std::collections::BTreeMap;
use crate::ast::Op;
use crate::env::{Env, LayeredEnv};
use crate::value::Value;

Loading…
Cancel
Save