From 520fa3780dcdd94e3d6010b267b86516bc561320 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 21 Jul 2024 18:01:34 +0200 Subject: [PATCH] Add enums --- src/grammar.pest | 18 ++++++++++++------ src/test/types.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/grammar.pest b/src/grammar.pest index 7c87493..ba706f3 100644 --- a/src/grammar.pest +++ b/src/grammar.pest @@ -5,14 +5,20 @@ block_line = _{ expr | (statement ~ MLF)* } statement = { (decl | type_decl | expr) ~ TERMINATE } decl = { "let" ~ #name = ident ~ #args = (ident)* ~ "=" ~ expr } -type_decl = { "type" ~ #name = type_ident ~ "=" ~ type_expr } -type_expr = { tuple | rec | type_ident } +type_decl = { "type" ~ #name = type_ident ~ "=" ~ type_expr } +type_expr = { tuple | rec | enum | type_ident } + tuple = { "#(" ~ MLF ~ tuple_entry* ~ MLF ~ ")" } tuple_entry = _{ type_expr ~ MLF } -rec = { "#{" ~ MLF ~ (rec_field ~ TERMINATE ~ MLF)* ~ rec_field? ~ MLF ~ "}" } -rec_field = { #field_name = ident ~ ":" ~ #field_type = type_expr* } -type_ident = ${ #name = ident ~ #args = type_args? } -type_args = { "<" ~ MLF ~ type_ident* ~ 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 } + +type_ident = ${ #name = ident ~ #args = type_args? } +type_args = { "<" ~ MLF ~ type_ident* ~ MLF ~ ">" } expr = { infix_expr | call_expr | term } term = _{ ident | literal | "(" ~ MLF ~ expr ~ MLF ~ ")" | block } diff --git a/src/test/types.rs b/src/test/types.rs index 58af932..54c91f6 100644 --- a/src/test/types.rs +++ b/src/test/types.rs @@ -83,6 +83,40 @@ fn it_parses_recs() { ]); } +#[test] +fn it_parses_enums() { + parses_to!(parser: SnekParser, input: "type MyEnum = #[Var1 Num\nVar2 Type\n]", rule: Rule::statement, tokens: [ + statement(0, 36, [ + type_decl(0, 36, [ + type_ident(5, 11, [ + ident(5, 11) + ]), + type_expr(14, 36, [ + r#enum(14, 36, [ + enum_vrt(16, 24, [ + ident(16, 20), + type_expr(21, 24, [ + type_ident(21, 24, [ + ident(21, 24) + ]) + ]), + ]), + enum_vrt(25, 34, [ + ident(25, 29), + type_expr(30, 34, [ + type_ident(30, 34, [ + ident(30, 34) + ]) + ]), + ]), + ]) + ]) + ]), + EOI(36, 36) + ]) + ]); +} + #[test] fn it_parses_generics() { parses_to!(parser: SnekParser, input: "type MyNum = Num", rule: Rule::statement, tokens: [