Refactor
This commit is contained in:
parent
00abe27c13
commit
497f781d0d
15 changed files with 51 additions and 65 deletions
|
@ -1,6 +1,6 @@
|
|||
use eww_config::{
|
||||
format_diagnostic::ToDiagnostic,
|
||||
parser::{ast::*, element::FromAst},
|
||||
parser::{ast::*, from_ast::FromAst},
|
||||
};
|
||||
|
||||
fn main() {
|
||||
|
@ -15,17 +15,16 @@ fn main() {
|
|||
|
||||
let file_id = files.add("foo.eww", input);
|
||||
let ast = eww_config::parser::parse_string(file_id, input);
|
||||
match ast.and_then(eww_config::parser::element::Element::<Ast, Ast>::from_ast) {
|
||||
Ok(ast) => {
|
||||
println!("{:?}", ast);
|
||||
}
|
||||
Err(err) => {
|
||||
dbg!(&err);
|
||||
let diag = err.to_diagnostic();
|
||||
use codespan_reporting::term;
|
||||
let config = term::Config::default();
|
||||
let mut writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Always);
|
||||
term::emit(&mut writer, &config, &files, &diag).unwrap();
|
||||
}
|
||||
}
|
||||
// match ast.and_then(eww_config::parser::from_ast::Element::<Ast, Ast>::from_ast) {
|
||||
// Ok(ast) => {
|
||||
// println!("{:?}", ast);
|
||||
//}
|
||||
// Err(err) => {
|
||||
// dbg!(&err);
|
||||
// let diag = err.to_diagnostic();
|
||||
// use codespan_reporting::term;
|
||||
// let config = term::Config::default();
|
||||
// let mut writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Always);
|
||||
// term::emit(&mut writer, &config, &files, &diag).unwrap();
|
||||
//}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use eww_config::{
|
|||
config::{widget_definition::WidgetDefinition, widget_use::WidgetUse, *},
|
||||
error::AstError,
|
||||
format_diagnostic::ToDiagnostic,
|
||||
parser::element::FromAst,
|
||||
parser::from_ast::FromAst,
|
||||
};
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -11,7 +11,7 @@ use crate::{
|
|||
error::{AstError, AstResult, OptionAstErrorExt},
|
||||
parser::{
|
||||
ast::{Ast, AstIterator, Span},
|
||||
element::{Element, FromAst, FromAstElementContent},
|
||||
from_ast::{FromAst, FromAstElementContent},
|
||||
},
|
||||
spanned,
|
||||
value::{AttrName, VarName},
|
||||
|
|
2
src/config/config_parse_error.rs
Normal file
2
src/config/config_parse_error.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ConfigParseError {}
|
|
@ -1,4 +1,5 @@
|
|||
mod config;
|
||||
pub mod config_parse_error;
|
||||
pub mod script_var_definition;
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
error::{AstError, AstResult},
|
||||
parser::{
|
||||
ast::{Ast, AstIterator, Span},
|
||||
element::{Element, FromAst, FromAstElementContent},
|
||||
from_ast::{FromAst, FromAstElementContent},
|
||||
},
|
||||
spanned,
|
||||
value::{AttrName, VarName},
|
||||
|
@ -51,7 +51,7 @@ impl FromAstElementContent for PollScriptVar {
|
|||
let attrs: HashMap<String, String> = iter.expect_key_values()?;
|
||||
let interval = attrs.get("interval").unwrap();
|
||||
let interval = crate::util::parse_duration(interval).map_err(|e| AstError::Other(Some(span), e.into()))?;
|
||||
let (_, script) = iter.expect_value()?;
|
||||
let (_, script) = iter.expect_literal()?;
|
||||
Ok(Self { name: VarName(name), command: VarSource::Shell(script.to_string()), interval })
|
||||
}
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ impl FromAstElementContent for TailScriptVar {
|
|||
|
||||
fn from_tail<I: Iterator<Item = Ast>>(span: Span, mut iter: AstIterator<I>) -> AstResult<Self> {
|
||||
let (_, name) = iter.expect_symbol()?;
|
||||
let (_, script) = iter.expect_value()?;
|
||||
let (_, script) = iter.expect_literal()?;
|
||||
Ok(Self { name: VarName(name), command: script.to_string() })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::{
|
|||
parser::{
|
||||
self,
|
||||
ast::{Ast, Span},
|
||||
element::FromAst,
|
||||
from_ast::FromAst,
|
||||
lexer::Lexer,
|
||||
},
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
error::AstResult,
|
||||
parser::{
|
||||
ast::{Ast, AstIterator, Span},
|
||||
element::{Element, FromAst},
|
||||
from_ast::FromAst,
|
||||
},
|
||||
spanned,
|
||||
value::{AttrName, VarName},
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
error::AstResult,
|
||||
parser::{
|
||||
ast::{Ast, AstIterator, Span},
|
||||
element::{Element, FromAst, FromAstElementContent},
|
||||
from_ast::{FromAst, FromAstElementContent},
|
||||
},
|
||||
spanned,
|
||||
value::{AttrName, VarName},
|
||||
|
@ -26,7 +26,7 @@ impl FromAstElementContent for VarDefinition {
|
|||
|
||||
fn from_tail<I: Iterator<Item = Ast>>(span: Span, mut iter: AstIterator<I>) -> AstResult<Self> {
|
||||
let (_, name) = iter.expect_symbol()?;
|
||||
let (_, initial_value) = iter.expect_value()?;
|
||||
let (_, initial_value) = iter.expect_literal()?;
|
||||
Ok(Self { name: VarName(name), initial_value, span })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
error::AstResult,
|
||||
parser::{
|
||||
ast::{Ast, AstIterator, Span},
|
||||
element::{Element, FromAst, FromAstElementContent},
|
||||
from_ast::{FromAst, FromAstElementContent},
|
||||
},
|
||||
spanned,
|
||||
value::{AttrName, VarName},
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
error::AstResult,
|
||||
parser::{
|
||||
ast::{Ast, AstIterator, Span},
|
||||
element::{Element, FromAst},
|
||||
from_ast::FromAst,
|
||||
},
|
||||
spanned,
|
||||
value::AttrName,
|
||||
|
@ -23,7 +23,7 @@ impl FromAst for WidgetUse {
|
|||
fn from_ast(e: Ast) -> AstResult<Self> {
|
||||
let span = e.span();
|
||||
spanned!(e.span(), {
|
||||
if let Ok(text) = e.as_value_ref() {
|
||||
if let Ok(text) = e.as_literal_ref() {
|
||||
Self {
|
||||
name: "text".to_string(),
|
||||
attrs: maplit::hashmap! { AttrName("text".to_string()) => SimplExpr::Literal(span.into(), text.clone()) },
|
||||
|
|
|
@ -4,7 +4,7 @@ use std::collections::HashMap;
|
|||
|
||||
use std::fmt::Display;
|
||||
|
||||
use super::element::FromAst;
|
||||
use super::from_ast::FromAst;
|
||||
use crate::error::{AstError, AstResult, OptionAstErrorExt};
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Copy, serde::Serialize)]
|
||||
|
@ -34,9 +34,11 @@ pub enum AstType {
|
|||
Array,
|
||||
Keyword,
|
||||
Symbol,
|
||||
Value,
|
||||
Literal,
|
||||
SimplExpr,
|
||||
Comment,
|
||||
// A value that could be used as a [SimplExpr]
|
||||
IntoPrimitive,
|
||||
}
|
||||
|
||||
impl Display for AstType {
|
||||
|
@ -51,7 +53,7 @@ pub enum Ast {
|
|||
Array(Span, Vec<Ast>),
|
||||
Keyword(Span, String),
|
||||
Symbol(Span, String),
|
||||
Value(Span, DynVal),
|
||||
Literal(Span, DynVal),
|
||||
SimplExpr(Span, SimplExpr),
|
||||
Comment(Span),
|
||||
}
|
||||
|
@ -75,7 +77,7 @@ macro_rules! as_func {
|
|||
}
|
||||
|
||||
impl Ast {
|
||||
as_func!(AstType::Value, as_value as_value_ref<DynVal> = Ast::Value(_, x) => x);
|
||||
as_func!(AstType::Literal, as_literal as_literal_ref<DynVal> = Ast::Literal(_, x) => x);
|
||||
|
||||
as_func!(AstType::Symbol, as_symbol as_symbol_ref<String> = Ast::Symbol(_, x) => x);
|
||||
|
||||
|
@ -89,7 +91,7 @@ impl Ast {
|
|||
Ast::Array(..) => AstType::Array,
|
||||
Ast::Keyword(..) => AstType::Keyword,
|
||||
Ast::Symbol(..) => AstType::Symbol,
|
||||
Ast::Value(..) => AstType::Value,
|
||||
Ast::Literal(..) => AstType::Literal,
|
||||
Ast::SimplExpr(..) => AstType::SimplExpr,
|
||||
Ast::Comment(_) => AstType::Comment,
|
||||
}
|
||||
|
@ -101,16 +103,20 @@ impl Ast {
|
|||
Ast::Array(span, _) => *span,
|
||||
Ast::Keyword(span, _) => *span,
|
||||
Ast::Symbol(span, _) => *span,
|
||||
Ast::Value(span, _) => *span,
|
||||
Ast::Literal(span, _) => *span,
|
||||
Ast::SimplExpr(span, _) => *span,
|
||||
Ast::Comment(span) => *span,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn first_list_elem(&self) -> Option<&Ast> {
|
||||
pub fn as_simplexpr(self) -> AstResult<SimplExpr> {
|
||||
match self {
|
||||
Ast::List(_, list) => list.first(),
|
||||
_ => None,
|
||||
// do I do this?
|
||||
// Ast::Array(_, _) => todo!(),
|
||||
// Ast::Symbol(_, _) => todo!(),
|
||||
Ast::Literal(span, x) => Ok(SimplExpr::Literal(span.into(), x)),
|
||||
Ast::SimplExpr(span, x) => Ok(x),
|
||||
_ => Err(AstError::WrongExprType(Some(self.span()), AstType::IntoPrimitive, self.expr_type())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -123,7 +129,7 @@ impl std::fmt::Display for Ast {
|
|||
Array(_, x) => write!(f, "({})", x.iter().map(|e| format!("{}", e)).join(" ")),
|
||||
Keyword(_, x) => write!(f, ":{}", x),
|
||||
Symbol(_, x) => write!(f, "{}", x),
|
||||
Value(_, x) => write!(f, "\"{}\"", x),
|
||||
Literal(_, x) => write!(f, "\"{}\"", x),
|
||||
SimplExpr(_, x) => write!(f, "{{{}}}", x),
|
||||
Comment(_) => write!(f, ""),
|
||||
}
|
||||
|
@ -170,7 +176,7 @@ macro_rules! return_or_put_back {
|
|||
impl<I: Iterator<Item = Ast>> AstIterator<I> {
|
||||
return_or_put_back!(expect_symbol, AstType::Symbol, (Span, String) = Ast::Symbol(span, x) => (span, x));
|
||||
|
||||
return_or_put_back!(expect_value, AstType::Value, (Span, DynVal) = Ast::Value(span, x) => (span, x));
|
||||
return_or_put_back!(expect_literal, AstType::Literal, (Span, DynVal) = Ast::Literal(span, x) => (span, x));
|
||||
|
||||
return_or_put_back!(expect_list, AstType::List, (Span, Vec<Ast>) = Ast::List(span, x) => (span, x));
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use super::ast::{Ast, AstIterator, AstType, Span};
|
||||
use crate::{error::*, parser, spanned, value::AttrName};
|
||||
use crate::{error::*, parser, spanned, util, value::AttrName};
|
||||
use itertools::Itertools;
|
||||
use simplexpr::{ast::SimplExpr, dynval::DynVal};
|
||||
use std::{
|
||||
|
@ -20,7 +20,7 @@ impl FromAst for Ast {
|
|||
|
||||
impl FromAst for String {
|
||||
fn from_ast(e: Ast) -> AstResult<Self> {
|
||||
Ok(e.as_value()?.as_string().unwrap())
|
||||
Ok(e.as_literal()?.as_string().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,31 +50,9 @@ impl FromAst for SimplExpr {
|
|||
fn from_ast(e: Ast) -> AstResult<Self> {
|
||||
match e {
|
||||
Ast::Symbol(span, x) => Ok(SimplExpr::VarRef(span.into(), x)),
|
||||
Ast::Value(span, x) => Ok(SimplExpr::Literal(span.into(), x)),
|
||||
Ast::Literal(span, x) => Ok(SimplExpr::Literal(span.into(), x)),
|
||||
Ast::SimplExpr(span, x) => Ok(x),
|
||||
_ => Err(AstError::NotAValue(Some(e.span()), e.expr_type())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Element<C, A> {
|
||||
name: String,
|
||||
attrs: HashMap<AttrName, A>,
|
||||
children: Vec<C>,
|
||||
span: Span,
|
||||
}
|
||||
|
||||
impl<C: FromAst, A: FromAst> FromAst for Element<C, A> {
|
||||
fn from_ast(e: Ast) -> AstResult<Self> {
|
||||
let span = e.span();
|
||||
spanned!(e.span(), {
|
||||
let list = e.as_list()?;
|
||||
let mut iter = AstIterator::new(list.into_iter());
|
||||
let (_, name) = iter.expect_symbol()?;
|
||||
let attrs = iter.expect_key_values()?.into_iter().map(|(k, v)| (AttrName(k), v)).collect();
|
||||
let children = iter.map(C::from_ast).collect::<AstResult<Vec<_>>>()?;
|
||||
Element { span, name, attrs, children }
|
||||
})
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ use std::{fmt::Display, ops::Deref};
|
|||
use itertools::Itertools;
|
||||
|
||||
pub mod ast;
|
||||
pub mod element;
|
||||
pub mod from_ast;
|
||||
pub(crate) mod lexer;
|
||||
pub(crate) mod parse_error;
|
||||
|
||||
|
|
|
@ -36,14 +36,14 @@ pub Ast: Ast = {
|
|||
<l:@L> <expr:SimplExpr> <r:@R> => Ast::SimplExpr(Span(l, r, file_id), expr),
|
||||
<x:Keyword> => x,
|
||||
<x:Symbol> => x,
|
||||
<l:@L> <x:Value> <r:@R> => Ast::Value(Span(l, r, file_id), x.into()),
|
||||
<l:@L> <x:Literal> <r:@R> => Ast::Literal(Span(l, r, file_id), x.into()),
|
||||
<l:@L> "comment" <r:@R> => Ast::Comment(Span(l, r, file_id)),
|
||||
};
|
||||
|
||||
Keyword: Ast = <l:@L> <x:"keyword"> <r:@R> => Ast::Keyword(Span(l, r, file_id), x[1..].to_string());
|
||||
Symbol: Ast = <l:@L> <x:"symbol"> <r:@R> => Ast::Symbol(Span(l, r, file_id), x.to_string());
|
||||
|
||||
Value: String = {
|
||||
Literal: String = {
|
||||
<StrLit> => <>,
|
||||
<Num> => <>,
|
||||
<Bool> => <>,
|
||||
|
|
Loading…
Add table
Reference in a new issue