Fixup whitespace

This commit is contained in:
Dane Johnson 2022-02-01 10:15:07 -06:00
parent 35e7c66f68
commit 356905316e

View File

@ -18,48 +18,48 @@ pub enum Expr<'a> {
peg::parser! { peg::parser! {
grammar deelang_parser() for str { grammar deelang_parser() for str {
pub rule program() -> Vec<Stmt<'input>> pub rule program() -> Vec<Stmt<'input>>
= __* s:stmt()* { s } = __* s:stmt()* { s }
pub rule stmt() -> Stmt<'input> pub rule stmt() -> Stmt<'input>
= i:id() "<-" _ e:expr() stop() { Stmt::Assignment(i, e) } / = i:id() "<-" _ e:expr() stop() { Stmt::Assignment(i, e) } /
f:funcall() stop() { Stmt::Funcall(f) } f:funcall() stop() { Stmt::Funcall(f) }
rule expr() -> Expr<'input> = precedence! { rule expr() -> Expr<'input> = precedence! {
e1:(@) "+" _ e2:@ { Expr::Plus(Box::new(e1), Box::new(e2)) } e1:(@) "+" _ e2:@ { Expr::Plus(Box::new(e1), Box::new(e2)) }
e1:(@) "-" _ e2:@ { Expr::Minus(Box::new(e1), Box::new(e2)) } e1:(@) "-" _ e2:@ { Expr::Minus(Box::new(e1), Box::new(e2)) }
-- --
e1:(@) "*" _ e2:@ { Expr::Mult(Box::new(e1), Box::new(e2)) } e1:(@) "*" _ e2:@ { Expr::Mult(Box::new(e1), Box::new(e2)) }
e1:(@) "/" _ e2:@ { Expr::Div(Box::new(e1), Box::new(e2)) } e1:(@) "/" _ e2:@ { Expr::Div(Box::new(e1), Box::new(e2)) }
-- --
"(" _ e:expr() ")" _ { e } "(" _ e:expr() ")" _ { e }
f:funcall() _ { f } f:funcall() _ { f }
i:id()? "->" _ s:stmt() { Expr::Funcdef(i, vec![s]) } i:id()? "->" _ s:stmt() { Expr::Funcdef(i, vec![s]) }
i:id() _ { Expr::Id(i) } i:id() _ { Expr::Id(i) }
n:num() _ { Expr::Num(n) } n:num() _ { Expr::Num(n) }
} }
rule id() -> &'input str rule id() -> &'input str
= i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i } = i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i }
rule num() -> f64 rule num() -> f64
= n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() } = n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() }
rule funcall() -> Expr<'input> rule funcall() -> Expr<'input>
= i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) } = i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) }
rule letter() rule letter()
= ['A'..='Z'] / ['a'..='z'] = ['A'..='Z'] / ['a'..='z']
rule digit() rule digit()
= ['0'..='9'] = ['0'..='9']
rule stop() rule stop()
= __+ / eof() = __+ / eof()
rule _ // Non-meaningful whitespace rule _ // Non-meaningful whitespace
= ['\t'|' ']* = ['\t'|' ']*
rule __ // End Of Statement (comment, newline, eof, TODO semicolon) rule __ // End Of Statement (comment, newline, eof, TODO semicolon)
= comment()? newline() / comment() &eof() = comment()? newline() / comment() &eof()
rule comment() rule comment()
= "#" (!newline() [_])* &(newline() / eof()) = "#" (!newline() [_])* &(newline() / eof())
rule newline() rule newline()
= "\r\n" / "\r" / "\n" = "\r\n" / "\r" / "\n"
rule eof() rule eof()
= ![_] = ![_]
} }
} }
@ -67,104 +67,105 @@ peg::parser! {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
#[test] #[test]
fn test_comments() { fn test_comments() {
let prgm = "## This is a comment let prgm = "## This is a comment
apple <- 1 ## This is too apple <- 1 ## This is too
## This comment ends the file"; ## This comment ends the file";
let expected = vec![Stmt::Assignment("apple", Expr::Num(1.0))]; let expected = vec![Stmt::Assignment("apple", Expr::Num(1.0))];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected); assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
} }
#[test] #[test]
fn test_funcall() { fn test_funcall() {
let expected = vec![ let expected = vec![
Stmt::Funcall(Expr::Funcall("pear", vec![])), Stmt::Funcall(Expr::Funcall("pear", vec![])),
Stmt::Funcall(Expr::Funcall("pear", Stmt::Funcall(Expr::Funcall("pear",
vec![Expr::Id("x"), Expr::Id("y")], vec![Expr::Id("x"), Expr::Id("y")],
)) ))
]; ];
let prgm = r"pear() let prgm = r"pear()
pear(x, y)"; pear(x, y)";
assert_eq!(deelang_parser::program(prgm).unwrap(), expected); assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
} }
#[test] #[test]
fn test_assignment() { fn test_assignment() {
let prgm = r"apple <- 1 let prgm = r"apple <- 1
apple <- pear(x, y)"; apple <- pear(x, y)";
let expected = vec![ let expected = vec![
Stmt::Assignment("apple", Stmt::Assignment("apple",
Expr::Num(1.0)), Expr::Num(1.0)),
Stmt::Assignment("apple", Stmt::Assignment("apple",
Expr::Funcall("pear", Expr::Funcall("pear",
vec![ vec![
Expr::Id("x"), Expr::Id("x"),
Expr::Id("y"), Expr::Id("y"),
])), ])),
]; ];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected); assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
} }
#[test] #[test]
fn test_operators() { fn test_operators() {
let prgm = r"three <- 1 + 2 let prgm = r"three <- 1 + 2
one <- 3 - 2 one <- 3 - 2
four <- (3 - 1) * 2"; four <- (3 - 1) * 2";
let expected = vec![ let expected = vec![
Stmt::Assignment("three", Stmt::Assignment("three",
Expr::Plus( Expr::Plus(
Box::new(Expr::Num(1.0)), Box::new(Expr::Num(1.0)),
Box::new(Expr::Num(2.0)) Box::new(Expr::Num(2.0))
)), )),
Stmt::Assignment("one", Stmt::Assignment("one",
Expr::Minus( Expr::Minus(
Box::new(Expr::Num(3.0)), Box::new(Expr::Num(3.0)),
Box::new(Expr::Num(2.0)) Box::new(Expr::Num(2.0))
)), )),
Stmt::Assignment("four", Stmt::Assignment("four",
Expr::Mult( Expr::Mult(
Box::new(Expr::Minus( Box::new(Expr::Minus(
Box::new(Expr::Num(3.0)), Box::new(Expr::Num(3.0)),
Box::new(Expr::Num(1.0)), Box::new(Expr::Num(1.0)),
)), )),
Box::new(Expr::Num(2.0)), Box::new(Expr::Num(2.0)),
)) ))
]; ];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected); assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
} }
#[test] #[test]
fn test_compound_expression() { fn test_compound_expression() {
let prgm = "apple <- pear(x, y) + z"; let prgm = "apple <- pear(x, y) + z";
let expected = vec![ let expected = vec![
Stmt::Assignment("apple", Stmt::Assignment("apple",
Expr::Plus( Expr::Plus(
Box::new(Expr::Funcall( Box::new(Expr::Funcall(
"pear", "pear",
vec![Expr::Id("x"), Expr::Id("y")], vec![Expr::Id("x"), Expr::Id("y")],
)), )),
Box::new(Expr::Id("z")) Box::new(Expr::Id("z"))
) )
), ),
]; ];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected); assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
} }
#[test] #[test]
fn test_funcdef() { fn test_funcdef() {
let prgm = r"foo <- -> bar()"; let prgm = r"foo <- -> bar()
let expected = vec![ ";
Stmt::Assignment("foo", let expected = vec![
Expr::Funcdef( Stmt::Assignment("foo",
None, Expr::Funcdef(
vec![ None,
Stmt::Funcall(Expr::Funcall("bar", vec![])), vec![
]) Stmt::Funcall(Expr::Funcall("bar", vec![])),
) ])
]; )
];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected); assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
} }
} }