From a71af581e3e884a15954263f676f047a107208e6 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 21 Jul 2024 18:19:13 +0200 Subject: [PATCH] Add corpus tests --- corpus/types.sk | 24 ++++++++++++++++++++++++ src/grammar.pest | 22 +++++++++++++--------- src/test/mod.rs | 8 ++++++-- 3 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 corpus/types.sk diff --git a/corpus/types.sk b/corpus/types.sk new file mode 100644 index 0000000..ec7b900 --- /dev/null +++ b/corpus/types.sk @@ -0,0 +1,24 @@ + +type Food = #[ + Apple Num + Banana Num + Cereal Num + Milk #(String Num) +] + +type Utils = #[ + Paper Num + Detergent Num + Pencils Num +] + +type Packaging = #[ + Bag + PlasticBag +] + +type ShoppingList = #{ + food: Food + utils: Utils + packaging: Packaging +} diff --git a/src/grammar.pest b/src/grammar.pest index ba706f3..f0d9c13 100644 --- a/src/grammar.pest +++ b/src/grammar.pest @@ -1,9 +1,11 @@ -file = { "hello world" } +file = { + SOI ~ MLF ~ (statement ~ MLF)* ~ EOI +} -block = { "{" ~ MLF ~ block_line ~ MLF ~ "}" } -block_line = _{ expr | (statement ~ MLF)* } statement = { (decl | type_decl | expr) ~ TERMINATE } decl = { "let" ~ #name = ident ~ #args = (ident)* ~ "=" ~ expr } +block = { "{" ~ MLF ~ block_line ~ MLF ~ "}" } +block_line = _{ expr | (statement ~ MLF)* } type_decl = { "type" ~ #name = type_ident ~ "=" ~ type_expr } type_expr = { tuple | rec | enum | type_ident } @@ -14,8 +16,8 @@ tuple_entry = _{ type_expr ~ MLF } rec = { "#{" ~ MLF ~ (rec_field ~ TERMINATE ~ MLF)* ~ rec_field? ~ MLF ~ "}" } rec_field = { #field_name = ident ~ ":" ~ #field_type = type_expr* } -enum = { "#[" ~ (enum_vrt ~ TERMINATE ~ MLF)* ~ enum_vrt? ~ "]" } -enum_vrt = { #vrt_name = ident ~ type_expr } +enum = { "#[" ~ MLF ~ (enum_vrt ~ TERMINATE ~ MLF)* ~ enum_vrt? ~ "]" } +enum_vrt = { #vrt_name = ident ~ type_expr? } type_ident = ${ #name = ident ~ #args = type_args? } type_args = { "<" ~ MLF ~ type_ident* ~ MLF ~ ">" } @@ -46,8 +48,10 @@ float = @{ byte = @{ "0x" ~ ASCII_HEX_DIGIT{1, 2} | "0b" ~ ASCII_BIN_DIGIT{0, 8} } boolean = @{ "true" | "false" } +KEYWORD = _{ "let" } +TERMINATE = _{ LF | EOI | ";" } +MLF = _{ (LF)* } +LF = _{ "\n" | "\r" } + WHITESPACE = _{ " " | "\t" } -KEYWORD = _{ "let" } -TERMINATE = _{ LF | EOI | ";" } -MLF = _{ (LF)* } -LF = _{ "\n" | "\r" } +COMMENT = _{ "/*" ~ (!"*/" ~ ANY)* ~ "*/" } diff --git a/src/test/mod.rs b/src/test/mod.rs index ae20ded..f6c6b58 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -6,6 +6,10 @@ mod types; use crate::parse; #[test] -fn it_parses() { - parse("hello world").unwrap(); +fn it_parse_types() { + let result = parse(include_str!("../../corpus/types.sk")); + + if let Err(e) = result { + panic!("{e}") + } }