summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJJ2023-04-06 22:41:55 +0000
committerJJ2023-04-06 22:41:55 +0000
commita5c2add97c11237b3f0a224b1ec90dcf447cc2b5 (patch)
tree0b76e01c20a03d7e861016caa109aa792446afe5
parent4183e659af29571110dfff91b89b58de7c8dd449 (diff)
don't panic when parsing bad types (this is genuinely awful)
-rw-r--r--src/parser.rs20
-rw-r--r--tests/test_parser.rs2
2 files changed, 12 insertions, 10 deletions
diff --git a/src/parser.rs b/src/parser.rs
index 719ee3f..ef7e28d 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -29,16 +29,18 @@ pub fn parse_str(input: &str) -> Result<Expression, peg::error::ParseError<peg::
}
}
}
+ // fucking awful but i don't know another way
+ // k:("empty" / "unit" / etc) returns ()
+ // and i can't seem to match and raise a parse error
+ // so ¯\_(ツ)_/¯
+ rule empty() -> Type = k:"empty" {Type::Empty}
+ rule unit() -> Type = k:"unit" {Type::Unit}
+ rule boolean() -> Type = k:"bool" {Type::Boolean}
+ rule natural() -> Type = k:"nat" {Type::Natural}
+ rule integer() -> Type = k:"int" {Type::Integer}
rule kind() -> Type
- = k:identifier() {
- match k.as_str() {
- "empty" => Type::Empty,
- "unit" => Type::Unit,
- "bool" => Type::Boolean,
- "nat" => Type::Natural,
- "int" => Type::Integer,
- _ => panic!("invalid type"), // fixme: raise an error
- }
+ = k:(empty() / unit() / boolean() / natural() / integer()) {
+ k
}
rule annotation() -> Expression
= e:(conditional() / abstraction() / application() / constant() / variable()) " "* ":" " "* k:kind() {
diff --git a/tests/test_parser.rs b/tests/test_parser.rs
index 958fc37..54311b8 100644
--- a/tests/test_parser.rs
+++ b/tests/test_parser.rs
@@ -17,7 +17,7 @@ fn test_simple_phrases() {
fn test_simple_annotations() {
assert_eq!(parse_str("t: int"), Ok(Ann(Var("t"), Type::Integer)));
assert_eq!(parse_str("12: nat"), Ok(Ann(Const(12, Type::Empty), Type::Natural)));
- // assert!(parse_str("t: fake").is_err()); // fixme: currently panics
+ assert!(parse_str("t: fake").is_err());
}
#[test]