more stuff
This commit is contained in:
parent
3b129957cd
commit
161de3dc0a
5 changed files with 424 additions and 534 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -266,6 +266,7 @@ checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
|
||||||
name = "nomwut"
|
name = "nomwut"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
"lalrpop",
|
"lalrpop",
|
||||||
"lalrpop-util",
|
"lalrpop-util",
|
||||||
"regex",
|
"regex",
|
||||||
|
|
|
@ -11,6 +11,7 @@ build = "build.rs"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
lalrpop-util = "0.19.5"
|
lalrpop-util = "0.19.5"
|
||||||
regex = "1"
|
regex = "1"
|
||||||
|
itertools = "0.10"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
lalrpop = "0.19.5"
|
lalrpop = "0.19.5"
|
||||||
|
|
|
@ -1,38 +1,42 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use crate::lexer;
|
//use crate::lexer;
|
||||||
use crate::Expr;
|
use crate::Expr;
|
||||||
|
use crate::Sp;
|
||||||
|
|
||||||
grammar;
|
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 = {
|
pub Expr: Expr = {
|
||||||
"(" <elems:Sep<Expr, Space>> ")" => Expr::List(elems),
|
"(" <elems:(<Span<(<Expr>)>>)+> ")" => Expr::List(elems),
|
||||||
<n:Int> => Expr::Number(n),
|
|
||||||
|
"{" <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
|
// vim:shiftwidth=4
|
||||||
|
|
836
src/calc.rs
836
src/calc.rs
File diff suppressed because it is too large
Load diff
66
src/main.rs
66
src/main.rs
|
@ -1,16 +1,50 @@
|
||||||
#![allow(unused_imports)]
|
#![allow(unused_imports)]
|
||||||
|
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
use lalrpop_util::lalrpop_mod;
|
use lalrpop_util::lalrpop_mod;
|
||||||
|
|
||||||
mod lexer;
|
//mod lexer;
|
||||||
|
|
||||||
lalrpop_mod!(pub calc);
|
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)]
|
#[derive(Debug)]
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
List(Vec<Expr>),
|
List(Vec<Sp<Expr>>),
|
||||||
|
Table(Vec<(Sp<Expr>, Sp<Expr>)>),
|
||||||
|
Keyword(String),
|
||||||
Symbol(String),
|
Symbol(String),
|
||||||
|
Str(String),
|
||||||
Number(i32),
|
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() {}
|
fn main() {}
|
||||||
|
@ -19,22 +53,38 @@ fn main() {}
|
||||||
macro_rules! test_p {
|
macro_rules! test_p {
|
||||||
($e:expr) => {
|
($e:expr) => {
|
||||||
let e = $e;
|
let e = $e;
|
||||||
let lex = crate::lexer::Lexer::new(e);
|
|
||||||
let p = calc::ExprParser::new();
|
let p = calc::ExprParser::new();
|
||||||
match p.parse(lex) {
|
match p.parse(e) {
|
||||||
Ok(res) => println!("{:?}", res),
|
Ok(res) => println!("{}\n=> {}\n", e, res),
|
||||||
Err(e) => eprintln!("{:?}", e),
|
Err(e) => eprintln!("{}", e),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn calc() {
|
fn calc() {
|
||||||
//assert!(calc::ExprParser::new().parse("(1 2 3)").is_ok());
|
|
||||||
|
|
||||||
test_p!("1");
|
test_p!("1");
|
||||||
test_p!("(12)");
|
test_p!("(12)");
|
||||||
test_p!("(1 2)");
|
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");
|
println!("\n\n\n\n\n\n");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue