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