50 lines
1.3 KiB
Plaintext
50 lines
1.3 KiB
Plaintext
use crate::ast::*;
|
|
use std::rc::Rc;
|
|
|
|
grammar<'s>();
|
|
|
|
match {
|
|
r"\s*" => { }, // The default whitespace skipping is disabled an `ignore pattern` is specified
|
|
r";[^\n\r]*[\n\r]*" => { }, // Skip `;` comments
|
|
_
|
|
}
|
|
|
|
|
|
pub(crate) File = {Lang? <Trees> }
|
|
|
|
Trees = Tree+;
|
|
|
|
pub(crate) Tree: Tree = {
|
|
"(" <Tree+> ")" => Tree::Branch(<>),
|
|
"(" "define" <Sym> <BTree> ")" => Tree::Define(<>),
|
|
"(" "define" "(" <name:Sym> <args:RcSlice<Sym>> ")" <body:RcSlice<Tree>> ")" => Tree::Define(name, Box::new(Tree::Lambda(Lambda(args, body)))),
|
|
"(" "if" <Tree> <Tree> <Tree> ")" => Tree::If(Box::new([<>])),
|
|
"(" "lambda (" <RcSlice<Sym>> ")" <RcSlice<Tree>> ")" => Tree::Lambda(Lambda(<>)),
|
|
Literal => Tree::Leaf(<>),
|
|
}
|
|
|
|
BTree = { Box<Tree> }
|
|
|
|
Literal: Literal = {
|
|
Sym => Literal::Sym(<>),
|
|
Num => Literal::Num(<>),
|
|
Bool => Literal::Bool(<>),
|
|
}
|
|
|
|
Box<T>: Box<T> = { T => Box::new(<>) }
|
|
RcSlice<T>: Rc<[T]> = { T* => <>.into() }
|
|
Rc<T>: Rc<T> = { T => Rc::new(<>) }
|
|
|
|
Sym: String = { r"[A-Za-z!$%&*+\-./:<=>?@^_~][A-Za-z!$%&*+\-./:<=>?@^_~0-9]*" => <>.to_owned() }
|
|
Num: f64 = { r"[0-9]+(\.[0-9]+)?" => <>.parse().unwrap() }
|
|
Bool: bool = {
|
|
"#t" => true,
|
|
"#f" => false,
|
|
}
|
|
|
|
Lang: () = { "#lang" LangName }
|
|
LangName = {
|
|
// TODO: What should these be?
|
|
"scheme",
|
|
"r7rs",
|
|
} |