From 589948925091100bfca6497662eb0fa424bd007e Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Mon, 12 Jul 2021 11:57:02 +0200 Subject: [PATCH] rename expr to ast --- examples/errors.rs | 4 +- src/{expr.rs => ast.rs} | 81 +++++++++++++++++++++-------------------- src/config.rs | 22 +++++------ src/error.rs | 10 ++--- src/lib.rs | 10 ++--- src/parser.lalrpop | 14 +++---- 6 files changed, 71 insertions(+), 70 deletions(-) rename src/{expr.rs => ast.rs} (63%) diff --git a/examples/errors.rs b/examples/errors.rs index 48e394b..7f156a3 100644 --- a/examples/errors.rs +++ b/examples/errors.rs @@ -1,4 +1,4 @@ -use eww_config::{config::*, expr::*}; +use eww_config::{ast::*, config::*}; fn main() { let mut files = codespan_reporting::files::SimpleFiles::new(); @@ -7,7 +7,7 @@ fn main() { let file_id = files.add("foo.eww", input); let ast = eww_config::parse_string(file_id, input); - match ast.and_then(Element::::from_expr) { + match ast.and_then(Element::::from_ast) { Ok(ast) => { println!("{:?}", ast); } diff --git a/src/expr.rs b/src/ast.rs similarity index 63% rename from src/expr.rs rename to src/ast.rs index ca67df1..81033fb 100644 --- a/src/expr.rs +++ b/src/ast.rs @@ -1,7 +1,7 @@ use itertools::Itertools; use std::collections::HashMap; -use crate::{config::FromExpr, error::*}; +use crate::{config::FromAst, error::*}; use std::fmt::Display; #[derive(Eq, PartialEq, Clone, Copy)] @@ -20,7 +20,7 @@ impl std::fmt::Debug for Span { } #[derive(Debug, PartialEq, Eq, Copy, Clone)] -pub enum ExprType { +pub enum AstType { List, Keyword, Symbol, @@ -28,15 +28,16 @@ pub enum ExprType { Comment, } -impl Display for ExprType { +impl Display for AstType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self) } } #[derive(PartialEq, Eq, Clone)] -pub enum Expr { - List(Span, Vec), +pub enum Ast { + List(Span, Vec), + // ArgList(Span, Vec), Keyword(Span, String), Symbol(Span, String), Value(Span, String), @@ -61,46 +62,46 @@ macro_rules! as_func { }; } -impl Expr { - as_func!(ExprType::Value, as_value as_value_ref = Expr::Value(_, x) => x); +impl Ast { + as_func!(AstType::Value, as_value as_value_ref = Ast::Value(_, x) => x); - as_func!(ExprType::Symbol, as_symbol as_symbol_ref = Expr::Symbol(_, x) => x); + as_func!(AstType::Symbol, as_symbol as_symbol_ref = Ast::Symbol(_, x) => x); - as_func!(ExprType::Keyword, as_keyword as_keyword_ref = Expr::Keyword(_, x) => x); + as_func!(AstType::Keyword, as_keyword as_keyword_ref = Ast::Keyword(_, x) => x); - as_func!(ExprType::List, as_list as_list_ref> = Expr::List(_, x) => x); + as_func!(AstType::List, as_list as_list_ref> = Ast::List(_, x) => x); - pub fn expr_type(&self) -> ExprType { + pub fn expr_type(&self) -> AstType { match self { - Expr::List(..) => ExprType::List, - Expr::Keyword(..) => ExprType::Keyword, - Expr::Symbol(..) => ExprType::Symbol, - Expr::Value(..) => ExprType::Value, - Expr::Comment(_) => ExprType::Comment, + Ast::List(..) => AstType::List, + Ast::Keyword(..) => AstType::Keyword, + Ast::Symbol(..) => AstType::Symbol, + Ast::Value(..) => AstType::Value, + Ast::Comment(_) => AstType::Comment, } } pub fn span(&self) -> Span { match self { - Expr::List(span, _) => *span, - Expr::Keyword(span, _) => *span, - Expr::Symbol(span, _) => *span, - Expr::Value(span, _) => *span, - Expr::Comment(span) => *span, + Ast::List(span, _) => *span, + Ast::Keyword(span, _) => *span, + Ast::Symbol(span, _) => *span, + Ast::Value(span, _) => *span, + Ast::Comment(span) => *span, } } - pub fn first_list_elem(&self) -> Option<&Expr> { + pub fn first_list_elem(&self) -> Option<&Ast> { match self { - Expr::List(_, list) => list.first(), + Ast::List(_, list) => list.first(), _ => None, } } } -impl std::fmt::Display for Expr { +impl std::fmt::Display for Ast { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - use Expr::*; + use Ast::*; match self { List(_, x) => write!(f, "({})", x.iter().map(|e| format!("{}", e)).join(" ")), Keyword(_, x) => write!(f, "{}", x), @@ -110,9 +111,9 @@ impl std::fmt::Display for Expr { } } } -impl std::fmt::Debug for Expr { +impl std::fmt::Debug for Ast { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - use Expr::*; + use Ast::*; match self { List(span, x) => f.debug_tuple(&format!("List<{}>", span)).field(x).finish(), Keyword(span, x) => write!(f, "Number<{}>({})", span, x), @@ -123,7 +124,7 @@ impl std::fmt::Debug for Expr { } } -pub struct ExprIterator> { +pub struct AstIterator> { iter: itertools::PutBack, } @@ -145,24 +146,24 @@ macro_rules! return_or_put_back { }; } -impl> ExprIterator { - return_or_put_back!(expect_symbol, ExprType::Symbol, (Span, String) = Expr::Symbol(span, x) => (span, x)); +impl> AstIterator { + return_or_put_back!(expect_symbol, AstType::Symbol, (Span, String) = Ast::Symbol(span, x) => (span, x)); - return_or_put_back!(expect_string, ExprType::Value, (Span, String) = Expr::Value(span, x) => (span, x)); + return_or_put_back!(expect_string, AstType::Value, (Span, String) = Ast::Value(span, x) => (span, x)); - return_or_put_back!(expect_list, ExprType::List, (Span, Vec) = Expr::List(span, x) => (span, x)); + return_or_put_back!(expect_list, AstType::List, (Span, Vec) = Ast::List(span, x) => (span, x)); pub fn new(iter: I) -> Self { - ExprIterator { iter: itertools::put_back(iter) } + AstIterator { iter: itertools::put_back(iter) } } - pub fn expect_key_values(&mut self) -> AstResult> { + pub fn expect_key_values(&mut self) -> AstResult> { parse_key_values(&mut self.iter) } } -impl> Iterator for ExprIterator { - type Item = Expr; +impl> Iterator for AstIterator { + type Item = Ast; fn next(&mut self) -> Option { self.iter.next() @@ -170,16 +171,16 @@ impl> Iterator for ExprIterator { } /// Parse consecutive `:keyword value` pairs from an expression iterator into a HashMap. Transforms the keys using the FromExpr trait. -fn parse_key_values>(iter: &mut itertools::PutBack) -> AstResult> { +fn parse_key_values>(iter: &mut itertools::PutBack) -> AstResult> { let mut data = HashMap::new(); loop { match iter.next() { - Some(Expr::Keyword(span, kw)) => match iter.next() { + Some(Ast::Keyword(span, kw)) => match iter.next() { Some(value) => { - data.insert(kw, T::from_expr(value)?); + data.insert(kw, T::from_ast(value)?); } None => { - iter.put_back(Expr::Keyword(span, kw)); + iter.put_back(Ast::Keyword(span, kw)); return Ok(data); } }, diff --git a/src/config.rs b/src/config.rs index 5fd1f11..20d1558 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use crate::{ + ast::{Ast, AstIterator, AstType, Span}, error::*, - expr::{Expr, ExprIterator, ExprType, Span}, parser, spanned, }; use itertools::Itertools; @@ -14,12 +14,12 @@ type VarName = String; type AttrValue = String; type AttrName = String; -pub trait FromExpr: Sized { - fn from_expr(e: Expr) -> AstResult; +pub trait FromAst: Sized { + fn from_ast(e: Ast) -> AstResult; } -impl FromExpr for Expr { - fn from_expr(e: Expr) -> AstResult { +impl FromAst for Ast { + fn from_ast(e: Ast) -> AstResult { Ok(e) } } @@ -32,15 +32,15 @@ pub struct Element { span: Span, } -impl FromExpr for Element { - fn from_expr(e: Expr) -> AstResult { +impl FromAst for Element { + fn from_ast(e: Ast) -> AstResult { let span = e.span(); spanned!(e.span(), { let list = e.as_list()?; - let mut iter = ExprIterator::new(list.into_iter()); + let mut iter = AstIterator::new(list.into_iter()); let (_, name) = iter.expect_symbol()?; let attrs = iter.expect_key_values()?; - let children = iter.map(C::from_expr).collect::>>()?; + let children = iter.map(C::from_ast).collect::>>()?; Element { span, name, attrs, children } }) } @@ -55,11 +55,11 @@ mod test { #[test] fn test() { - let parser = parser::ExprParser::new(); + let parser = parser::AstParser::new(); insta::with_settings!({sort_maps => true}, { let lexer = lexer::Lexer::new("(box :bar 12 :baz \"hi\" foo (bar))"); insta::assert_debug_snapshot!( - Element::::from_expr(parser.parse(0, lexer).unwrap()).unwrap() + Element::::from_ast(parser.parse(0, lexer).unwrap()).unwrap() ); }); } diff --git a/src/error.rs b/src/error.rs index 384df0a..f89e709 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,5 @@ use crate::{ - expr::{Expr, ExprType, Span}, + ast::{Ast, AstType, Span}, lexer, }; use codespan_reporting::{diagnostic, files}; @@ -12,9 +12,9 @@ pub enum AstError { #[error("Definition invalid")] InvalidDefinition(Option), #[error("Expected a {1}, but got nothing")] - MissingNode(Option, ExprType), + MissingNode(Option, AstType), #[error("Wrong type of expression: Expected {1} but got {2}")] - WrongExprType(Option, ExprType, ExprType), + WrongExprType(Option, AstType, AstType), #[error("Parse error: {source}")] ParseError { file_id: Option, source: lalrpop_util::ParseError }, @@ -68,10 +68,10 @@ pub fn spanned(span: Span, err: impl Into) -> AstError { } pub trait OptionAstErrorExt { - fn or_missing(self, t: ExprType) -> Result; + fn or_missing(self, t: AstType) -> Result; } impl OptionAstErrorExt for Option { - fn or_missing(self, t: ExprType) -> Result { + fn or_missing(self, t: AstType) -> Result { self.ok_or(AstError::MissingNode(None, t)) } } diff --git a/src/lib.rs b/src/lib.rs index dbd1316..f21dad6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,12 +2,12 @@ #![allow(unused)] #![feature(try_blocks)] +pub mod ast; pub mod config; pub mod error; -pub mod expr; mod lexer; +use ast::Ast; use error::{AstError, AstResult}; -use expr::Expr; use std::{fmt::Display, ops::Deref}; @@ -17,15 +17,15 @@ use lalrpop_util::lalrpop_mod; lalrpop_mod!(pub parser); -pub fn parse_string(file_id: usize, s: &str) -> AstResult { +pub fn parse_string(file_id: usize, s: &str) -> AstResult { let lexer = lexer::Lexer::new(s); - let parser = parser::ExprParser::new(); + let parser = parser::AstParser::new(); Ok(parser.parse(file_id, lexer).map_err(|e| AstError::from_parse_error(file_id, e))?) } macro_rules! test_parser { ($($text:literal),*) => {{ - let p = crate::parser::ExprParser::new(); + let p = crate::parser::AstParser::new(); use crate::lexer::Lexer; ::insta::with_settings!({sort_maps => true}, { diff --git a/src/parser.lalrpop b/src/parser.lalrpop index 4070cef..c434c48 100644 --- a/src/parser.lalrpop +++ b/src/parser.lalrpop @@ -1,6 +1,6 @@ use std::str::FromStr; use crate::lexer::{Token, LexicalError}; -use crate::expr::{Expr, Span}; +use crate::ast::{Ast, Span}; grammar(file_id: usize); @@ -22,16 +22,16 @@ extern { } -pub Expr: Expr = { - "(" )+> ")" => Expr::List(Span(l, r, file_id), elems), +pub Ast: Ast = { + "(" )+> ")" => Ast::List(Span(l, r, file_id), elems), => x, => x, - => Expr::Value(Span(l, r, file_id), x), - "comment" => Expr::Comment(Span(l, r, file_id)), + => Ast::Value(Span(l, r, file_id), x), + "comment" => Ast::Comment(Span(l, r, file_id)), }; -Keyword: Expr = => Expr::Keyword(Span(l, r, file_id), x.to_string()); -Symbol: Expr = => Expr::Symbol(Span(l, r, file_id), x.to_string()); +Keyword: Ast = => Ast::Keyword(Span(l, r, file_id), x.to_string()); +Symbol: Ast = => Ast::Symbol(Span(l, r, file_id), x.to_string()); Value: String = { => <>,