yeet table and number nodes
This commit is contained in:
parent
55e3d17f73
commit
cc07d68c91
3 changed files with 43 additions and 43 deletions
|
@ -1,18 +1,23 @@
|
||||||
use eww_config::{config::*, error, expr::*, parser};
|
use eww_config::{config::*, expr::*, parser};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let parser = parser::ExprParser::new();
|
let parser = parser::ExprParser::new();
|
||||||
let mut files = codespan_reporting::files::SimpleFiles::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 file_id = files.add("foo.eww", input);
|
||||||
let ast = parser.parse(file_id, input).unwrap();
|
let ast = parser.parse(file_id, input);
|
||||||
let element: Result<Element<Expr, Expr>, _> = Element::from_expr(ast);
|
match ast {
|
||||||
let err = element.unwrap_err();
|
Ok(ast) => {
|
||||||
|
let element: Result<Element<Expr, Expr>, _> = Element::from_expr(ast);
|
||||||
|
let err = element.unwrap_err();
|
||||||
|
|
||||||
let diag = err.pretty_diagnostic(&files);
|
let diag = err.pretty_diagnostic(&files);
|
||||||
use codespan_reporting::term;
|
use codespan_reporting::term;
|
||||||
let mut writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Always);
|
let mut writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Always);
|
||||||
term::emit(&mut writer, &term::Config::default(), &files, &diag).unwrap();
|
term::emit(&mut writer, &term::Config::default(), &files, &diag).unwrap();
|
||||||
|
}
|
||||||
|
Err(err) => eprintln!("{}", err),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
32
src/expr.rs
32
src/expr.rs
|
@ -22,11 +22,9 @@ impl std::fmt::Debug for Span {
|
||||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||||
pub enum ExprType {
|
pub enum ExprType {
|
||||||
List,
|
List,
|
||||||
Table,
|
|
||||||
Keyword,
|
Keyword,
|
||||||
Symbol,
|
Symbol,
|
||||||
Str,
|
Value,
|
||||||
Number,
|
|
||||||
Comment,
|
Comment,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,11 +37,9 @@ impl Display for ExprType {
|
||||||
#[derive(PartialEq, Eq, Clone)]
|
#[derive(PartialEq, Eq, Clone)]
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
List(Span, Vec<Expr>),
|
List(Span, Vec<Expr>),
|
||||||
Table(Span, Vec<(Expr, Expr)>),
|
|
||||||
Keyword(Span, String),
|
Keyword(Span, String),
|
||||||
Symbol(Span, String),
|
Symbol(Span, String),
|
||||||
Str(Span, String),
|
Value(Span, String),
|
||||||
Number(Span, i32),
|
|
||||||
Comment(Span),
|
Comment(Span),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +62,7 @@ macro_rules! as_func {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Expr {
|
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);
|
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 {
|
pub fn expr_type(&self) -> ExprType {
|
||||||
match self {
|
match self {
|
||||||
Expr::List(..) => ExprType::List,
|
Expr::List(..) => ExprType::List,
|
||||||
Expr::Table(..) => ExprType::Table,
|
|
||||||
Expr::Keyword(..) => ExprType::Keyword,
|
Expr::Keyword(..) => ExprType::Keyword,
|
||||||
Expr::Symbol(..) => ExprType::Symbol,
|
Expr::Symbol(..) => ExprType::Symbol,
|
||||||
Expr::Str(..) => ExprType::Str,
|
Expr::Value(..) => ExprType::Value,
|
||||||
Expr::Number(..) => ExprType::Number,
|
Expr::Comment(_) => ExprType::Comment,
|
||||||
Expr::Comment(_) => ExprType::Number,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn span(&self) -> Span {
|
pub fn span(&self) -> Span {
|
||||||
match self {
|
match self {
|
||||||
Expr::List(span, _) => *span,
|
Expr::List(span, _) => *span,
|
||||||
Expr::Table(span, _) => *span,
|
|
||||||
Expr::Keyword(span, _) => *span,
|
Expr::Keyword(span, _) => *span,
|
||||||
Expr::Symbol(span, _) => *span,
|
Expr::Symbol(span, _) => *span,
|
||||||
Expr::Str(span, _) => *span,
|
Expr::Value(span, _) => *span,
|
||||||
Expr::Number(span, _) => *span,
|
|
||||||
Expr::Comment(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 {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
use Expr::*;
|
use Expr::*;
|
||||||
match self {
|
match self {
|
||||||
Number(_, x) => write!(f, "{}", x),
|
|
||||||
List(_, x) => write!(f, "({})", x.iter().map(|e| format!("{}", e)).join(" ")),
|
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),
|
Keyword(_, x) => write!(f, "{}", x),
|
||||||
Symbol(_, x) => write!(f, "{}", x),
|
Symbol(_, x) => write!(f, "{}", x),
|
||||||
Str(_, x) => write!(f, "{}", x),
|
Value(_, x) => write!(f, "{}", x),
|
||||||
Comment(_) => write!(f, ""),
|
Comment(_) => write!(f, ""),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,12 +112,10 @@ impl std::fmt::Debug for Expr {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
use Expr::*;
|
use Expr::*;
|
||||||
match self {
|
match self {
|
||||||
Number(span, x) => write!(f, "Number<{}>({})", span, x),
|
|
||||||
List(span, x) => f.debug_tuple(&format!("List<{}>", span)).field(x).finish(),
|
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),
|
Keyword(span, x) => write!(f, "Number<{}>({})", span, x),
|
||||||
Symbol(span, x) => write!(f, "Symbol<{}>({})", 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),
|
Comment(span) => write!(f, "Comment<{}>", span),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -158,9 +146,7 @@ macro_rules! return_or_put_back {
|
||||||
impl<I: Iterator<Item = Expr>> ExprIterator<I> {
|
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_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_string, ExprType::Value, (Span, String) = Expr::Value(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_list, ExprType::List, (Span, Vec<Expr>) = Expr::List(span, x) => (span, x));
|
return_or_put_back!(expect_list, ExprType::List, (Span, Vec<Expr>) = Expr::List(span, x) => (span, x));
|
||||||
|
|
||||||
|
|
|
@ -7,30 +7,39 @@ grammar(file_id: usize);
|
||||||
|
|
||||||
pub Expr: Expr = {
|
pub Expr: Expr = {
|
||||||
<l:@L> "(" <elems:(<Expr>)+> ")" <r:@R> => Expr::List(Span(l, r, file_id), elems),
|
<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:Keyword> => x,
|
||||||
<x:Symbol> => x,
|
<x:Symbol> => x,
|
||||||
<l:@L> <x:StrLit> <r:@R> => Expr::Str(Span(l, r, file_id), x),
|
<l:@L> <x:Value> <r:@R> => Expr::Value(Span(l, r, file_id), x),
|
||||||
<l:@L> <x:Num> <r:@R> => Expr::Number(Span(l, r, file_id), x),
|
|
||||||
<l:@L> Comment <r:@R> => Expr::Comment(Span(l, r, file_id)),
|
<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());
|
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 = {
|
StrLit: String = {
|
||||||
<x:r#""(?:[^"\\]|\\.)*""#> => {
|
<x:r#""(?:[^"\\]|\\.)*""#> => {
|
||||||
x[1..x.len() - 1].to_owned()
|
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]*";
|
Comment: () = r";[^\n\r]*";
|
||||||
|
|
||||||
|
|
||||||
Num: i32 = <r"[0-9]+"> => i32::from_str(<>).unwrap();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// vim:shiftwidth=4
|
// vim:shiftwidth=4
|
||||||
|
|
Loading…
Add table
Reference in a new issue