more stuff

This commit is contained in:
elkowar 2021-05-14 20:57:32 +02:00
parent 3b129957cd
commit 161de3dc0a
No known key found for this signature in database
GPG Key ID: E321AD71B1D1F27F
5 changed files with 424 additions and 534 deletions

1
Cargo.lock generated
View File

@ -266,6 +266,7 @@ checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
name = "nomwut"
version = "0.1.0"
dependencies = [
"itertools",
"lalrpop",
"lalrpop-util",
"regex",

View File

@ -11,6 +11,7 @@ build = "build.rs"
[dependencies]
lalrpop-util = "0.19.5"
regex = "1"
itertools = "0.10"
[build-dependencies]
lalrpop = "0.19.5"

View File

@ -1,38 +1,42 @@
use std::str::FromStr;
use crate::lexer;
//use crate::lexer;
use crate::Expr;
use crate::Sp;
grammar;
extern {
type Location = usize;
type Error = lexer::LexicalError;
enum lexer::Tok {
"(" => lexer::Tok::LPren,
")" => lexer::Tok::RPren,
Space => lexer::Tok::Space,
Int => lexer::Tok::Int(<i32>),
}
}
Sep<T, S>: Vec<T> = {
<mut v:(<T> S)*> <e:T?> => match e {
None => v,
Some(e) => {
v.push(e);
v
}
}
}
Span<T>: Sp<T> = {
<@L> <T> <@R> => Sp(<>)
};
pub Expr: Expr = {
"(" <elems:Sep<Expr, Space>> ")" => Expr::List(elems),
<n:Int> => Expr::Number(n),
"(" <elems:(<Span<(<Expr>)>>)+> ")" => Expr::List(elems),
"{" <elems:(<Span<(<Expr>)>> <Span<(<Expr>)>>)*> "}" => Expr::Table(elems),
<x:Keyword> => x,
<x:Symbol> => x,
<x:StrLit> => Expr::Str(x),
<x:Num> => Expr::Number(x),
Comment => Expr::Comment,
};
Keyword: Expr = <r":[^\s]+"> => Expr::Keyword(<>.to_string());
Symbol: Expr = <r"[a-zA-Z_!\?<>/.*-+][^\s{}\(\)]*"> => Expr::Symbol(<>.to_string());
StrLit: String = {
r#""(?:[^"\\]|\\.)*""# => {
let val = <>;
val[1..val.len() - 1].to_owned()
},
}
Comment: () = r";[^\n\r]*";
Num: i32 = <r"[0-9]+"> => i32::from_str(<>).unwrap();
// vim:shiftwidth=4

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,50 @@
#![allow(unused_imports)]
use itertools::Itertools;
use lalrpop_util::lalrpop_mod;
mod lexer;
//mod lexer;
lalrpop_mod!(pub calc);
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub struct Sp<T>(pub usize, pub T, pub usize);
impl<T: std::fmt::Display> std::fmt::Display for Sp<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<{}- {} -{}>", self.0, self.1, self.2)
}
}
#[derive(Debug)]
pub enum Expr {
List(Vec<Expr>),
List(Vec<Sp<Expr>>),
Table(Vec<(Sp<Expr>, Sp<Expr>)>),
Keyword(String),
Symbol(String),
Str(String),
Number(i32),
Comment,
}
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),
Comment => write!(f, ""),
}
}
}
fn main() {}
@ -19,22 +53,38 @@ fn main() {}
macro_rules! test_p {
($e:expr) => {
let e = $e;
let lex = crate::lexer::Lexer::new(e);
let p = calc::ExprParser::new();
match p.parse(lex) {
Ok(res) => println!("{:?}", res),
Err(e) => eprintln!("{:?}", e),
match p.parse(e) {
Ok(res) => println!("{}\n=> {}\n", e, res),
Err(e) => eprintln!("{}", e),
}
};
}
#[test]
fn calc() {
//assert!(calc::ExprParser::new().parse("(1 2 3)").is_ok());
test_p!("1");
test_p!("(12)");
test_p!("(1 2)");
test_p!("(1 :foo 1)");
test_p!("(:foo 1)");
test_p!("(:foo->: 1)");
test_p!("(foo 1)");
test_p!("(lol😄 1)");
test_p!(r#"(test "hi")"#);
test_p!(r#"(test "h\"i")"#);
test_p!(r#"(test " hi ")"#);
test_p!("(+ (1 2 (* 2 5)))");
test_p!(r#"{:key value 12 "hi" (test) (1 2 3)}"#);
test_p!(r#"; test"#);
test_p!(
r#"(f arg ; test
arg2)"#
);
println!("\n\n\n\n\n\n");