yeet table and number nodes

This commit is contained in:
elkowar 2021-07-03 21:21:23 +02:00
parent 55e3d17f73
commit cc07d68c91
No known key found for this signature in database
GPG Key ID: E321AD71B1D1F27F
3 changed files with 43 additions and 43 deletions

View File

@ -1,18 +1,23 @@
use eww_config::{config::*, error, expr::*, parser};
use eww_config::{config::*, expr::*, parser};
fn main() {
let parser = parser::ExprParser::new();
let mut files = codespan_reporting::files::SimpleFiles::new();
let input = "(12 :bar 22 (foo) (baz))";
let input = "(12 :bar 22 (foo) (baz)";
let file_id = files.add("foo.eww", input);
let ast = parser.parse(file_id, input).unwrap();
let element: Result<Element<Expr, Expr>, _> = Element::from_expr(ast);
let err = element.unwrap_err();
let ast = parser.parse(file_id, input);
match ast {
Ok(ast) => {
let element: Result<Element<Expr, Expr>, _> = Element::from_expr(ast);
let err = element.unwrap_err();
let diag = err.pretty_diagnostic(&files);
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();
let diag = err.pretty_diagnostic(&files);
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();
}
Err(err) => eprintln!("{}", err),
}
}

View File

@ -22,11 +22,9 @@ impl std::fmt::Debug for Span {
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ExprType {
List,
Table,
Keyword,
Symbol,
Str,
Number,
Value,
Comment,
}
@ -39,11 +37,9 @@ impl Display for ExprType {
#[derive(PartialEq, Eq, Clone)]
pub enum Expr {
List(Span, Vec<Expr>),
Table(Span, Vec<(Expr, Expr)>),
Keyword(Span, String),
Symbol(Span, String),
Str(Span, String),
Number(Span, i32),
Value(Span, String),
Comment(Span),
}
@ -66,7 +62,7 @@ macro_rules! as_func {
}
impl Expr {
as_func!(ExprType::Str, as_str as_str_ref<String> = Expr::Str(_, x) => x);
as_func!(ExprType::Value, as_value as_value_ref<String> = Expr::Value(_, x) => x);
as_func!(ExprType::Symbol, as_symbol as_symbol_ref<String> = Expr::Symbol(_, x) => x);
@ -75,23 +71,19 @@ impl Expr {
pub fn expr_type(&self) -> ExprType {
match self {
Expr::List(..) => ExprType::List,
Expr::Table(..) => ExprType::Table,
Expr::Keyword(..) => ExprType::Keyword,
Expr::Symbol(..) => ExprType::Symbol,
Expr::Str(..) => ExprType::Str,
Expr::Number(..) => ExprType::Number,
Expr::Comment(_) => ExprType::Number,
Expr::Value(..) => ExprType::Value,
Expr::Comment(_) => ExprType::Comment,
}
}
pub fn span(&self) -> Span {
match self {
Expr::List(span, _) => *span,
Expr::Table(span, _) => *span,
Expr::Keyword(span, _) => *span,
Expr::Symbol(span, _) => *span,
Expr::Str(span, _) => *span,
Expr::Number(span, _) => *span,
Expr::Value(span, _) => *span,
Expr::Comment(span) => *span,
}
}
@ -108,12 +100,10 @@ impl std::fmt::Display for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Expr::*;
match self {
Number(_, x) => write!(f, "{}", x),
List(_, x) => write!(f, "({})", x.iter().map(|e| format!("{}", e)).join(" ")),
Table(_, x) => write!(f, "{{{}}}", x.iter().map(|(k, v)| format!("{} {}", k, v)).join(" ")),
Keyword(_, x) => write!(f, "{}", x),
Symbol(_, x) => write!(f, "{}", x),
Str(_, x) => write!(f, "{}", x),
Value(_, x) => write!(f, "{}", x),
Comment(_) => write!(f, ""),
}
}
@ -122,12 +112,10 @@ impl std::fmt::Debug for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Expr::*;
match self {
Number(span, x) => write!(f, "Number<{}>({})", span, x),
List(span, x) => f.debug_tuple(&format!("List<{}>", span)).field(x).finish(),
Table(span, x) => f.debug_tuple(&format!("Table<{}>", span)).field(x).finish(),
Keyword(span, x) => write!(f, "Number<{}>({})", span, x),
Symbol(span, x) => write!(f, "Symbol<{}>({})", span, x),
Str(span, x) => write!(f, "Str<{}>({})", span, x),
Value(span, x) => write!(f, "Value<{}>({})", span, x),
Comment(span) => write!(f, "Comment<{}>", span),
}
}
@ -158,9 +146,7 @@ macro_rules! return_or_put_back {
impl<I: Iterator<Item = Expr>> ExprIterator<I> {
return_or_put_back!(expect_symbol, ExprType::Symbol, (Span, String) = Expr::Symbol(span, x) => (span, x));
return_or_put_back!(expect_string, ExprType::Str, (Span, String) = Expr::Str(span, x) => (span, x));
return_or_put_back!(expect_number, ExprType::Number, (Span, i32) = Expr::Number(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_list, ExprType::List, (Span, Vec<Expr>) = Expr::List(span, x) => (span, x));

View File

@ -7,30 +7,39 @@ grammar(file_id: usize);
pub Expr: Expr = {
<l:@L> "(" <elems:(<Expr>)+> ")" <r:@R> => Expr::List(Span(l, r, file_id), elems),
<l:@L> "{" <elems:(<(<Expr>)> <(<Expr>)>)*> "}" <r:@R> => Expr::Table(Span(l, r, file_id), elems),
<x:Keyword> => x,
<x:Symbol> => x,
<l:@L> <x:StrLit> <r:@R> => Expr::Str(Span(l, r, file_id), x),
<l:@L> <x:Num> <r:@R> => Expr::Number(Span(l, r, file_id), x),
<l:@L> <x:Value> <r:@R> => Expr::Value(Span(l, r, file_id), x),
<l:@L> Comment <r:@R> => Expr::Comment(Span(l, r, file_id)),
};
Keyword: Expr = <l:@L> <x:r":[^\s]+"> <r:@R> => Expr::Keyword(Span(l, r, file_id), x.to_string());
Symbol: Expr = <l:@L> <x:r"[a-zA-Z_!\?<>/.*-+][^\s{}\(\)]*"> <r:@R> => Expr::Symbol(Span(l, r, file_id), x.to_string());
Symbol: Expr = <l:@L> <x:SymbolRegex> <r:@R> => Expr::Symbol(Span(l, r, file_id), x.to_string());
Value: String = {
<StrLit> => <>,
<Num> => <>,
<Bool> => <>,
};
StrLit: String = {
<x:r#""(?:[^"\\]|\\.)*""#> => {
x[1..x.len() - 1].to_owned()
},
};
Num: String = <x:r"[0-9]+"> => x.to_string();
Bool: String = <x:BoolRegex> => x.to_string();
match {
r"true|false" => BoolRegex,
} else {
r"[a-zA-Z_!\?<>/.*-+][^\s{}\(\)]*" => SymbolRegex,
_
}
Comment: () = r";[^\n\r]*";
Num: i32 = <r"[0-9]+"> => i32::from_str(<>).unwrap();
// vim:shiftwidth=4