Add enums

main
trivernis 4 months ago
parent d71b92c431
commit 520fa3780d
Signed by: Trivernis
GPG Key ID: 7E6D18B61C8D2F4B

@ -6,11 +6,17 @@ statement = { (decl | type_decl | expr) ~ TERMINATE }
decl = { "let" ~ #name = ident ~ #args = (ident)* ~ "=" ~ expr } decl = { "let" ~ #name = ident ~ #args = (ident)* ~ "=" ~ expr }
type_decl = { "type" ~ #name = type_ident ~ "=" ~ type_expr } type_decl = { "type" ~ #name = type_ident ~ "=" ~ type_expr }
type_expr = { tuple | rec | type_ident } type_expr = { tuple | rec | enum | type_ident }
tuple = { "#(" ~ MLF ~ tuple_entry* ~ MLF ~ ")" } tuple = { "#(" ~ MLF ~ tuple_entry* ~ MLF ~ ")" }
tuple_entry = _{ type_expr ~ MLF } tuple_entry = _{ type_expr ~ MLF }
rec = { "#{" ~ MLF ~ (rec_field ~ TERMINATE ~ MLF)* ~ rec_field? ~ MLF ~ "}" } rec = { "#{" ~ MLF ~ (rec_field ~ TERMINATE ~ MLF)* ~ rec_field? ~ MLF ~ "}" }
rec_field = { #field_name = ident ~ ":" ~ #field_type = type_expr* } 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_ident = ${ #name = ident ~ #args = type_args? }
type_args = { "<" ~ MLF ~ type_ident* ~ MLF ~ ">" } type_args = { "<" ~ MLF ~ type_ident* ~ MLF ~ ">" }

@ -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] #[test]
fn it_parses_generics() { fn it_parses_generics() {
parses_to!(parser: SnekParser, input: "type MyNum<a> = Num<a>", rule: Rule::statement, tokens: [ parses_to!(parser: SnekParser, input: "type MyNum<a> = Num<a>", rule: Rule::statement, tokens: [

Loading…
Cancel
Save