From 703c19305370272f1ebc7ef1a44f2903f5a8a040 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 14 Feb 2023 18:16:49 -0300 Subject: [PATCH] Lists --- pestfoamtest/src/foam.pest | 5 ++++- pestfoamtest/src/lib.rs | 28 +++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/pestfoamtest/src/foam.pest b/pestfoamtest/src/foam.pest index fbeb306..aa4bdfd 100644 --- a/pestfoamtest/src/foam.pest +++ b/pestfoamtest/src/foam.pest @@ -2,14 +2,17 @@ whitespace = _{ " " | "\t" | "\r" | "\n" } multi_comment = { "/*" ~ (!"*/" ~ ANY)* ~ "*/" } single_comment = { "//" ~ (!NEWLINE ~ ANY)* } identifier = { ASCII_ALPHA+ ~ (ASCII_ALPHA | ASCII_DIGIT | "_")* } -rvalue = { (!";" ~ ANY)+ } +rvalue = { ('a'..'z' | 'A'..'Z' | '0'..'9' | "_" | ".")+ } attribution = { identifier ~ whitespace+ ~ rvalue ~ ";" } dictionary = { identifier ~ whitespace+ ~ "{" ~ (dictionary | attribution | whitespace)+ ~ "}" } +list_size = _{ ASCII_DIGIT+ ~ whitespace+ } +list = { identifier ~ whitespace+ ~ list_size? ~ "(" ~ (rvalue | whitespace)+ ~ ");" } file = { SOI ~ ( whitespace | multi_comment | single_comment | dictionary + | list | attribution)* ~ EOI } diff --git a/pestfoamtest/src/lib.rs b/pestfoamtest/src/lib.rs index a88819e..ed3c555 100644 --- a/pestfoamtest/src/lib.rs +++ b/pestfoamtest/src/lib.rs @@ -31,8 +31,7 @@ mod test { #[test] fn chained_comments() { - let text = "/* this is one comment */ -// And this is another"; + let text = "/* this is one comment */\n// And this is another"; let parse = Foam::parse(Rule::file, text); assert!(parse.is_ok(), "{:?}", parse); } @@ -69,14 +68,7 @@ mod test { #[test] fn dictionary() { - let text = "FoamFile -{ - version 2.0; - format ascii; - class dictionary; - location system; - object caseSetupDict; -}"; + let text = "FoamFile\n{\nversion 2.0;\nformat ascii;\nclass dictionary;\nlocation system;\nobject caseSetupDict;\n}"; let parse = Foam::parse(Rule::dictionary, text); assert!(parse.is_ok(), "{:#?}", parse); } @@ -85,6 +77,20 @@ mod test { fn dict_in_dict() { let text = "dict1 { dict2 { class bad; } }"; let parse = Foam::parse(Rule::dictionary, text); - assert!(parse.is_err(), "{:#?}", parse); + assert!(parse.is_ok(), "{:#?}", parse); + } + + #[test] + fn list() { + let text = "list_name ( 1 2 3 );"; + let parse = Foam::parse(Rule::list, text); + assert!(parse.is_ok(), "{:#?}", parse); + } + + #[test] + fn sized_list() { + let text = "list_name 3 ( 1 2 3 );"; + let parse = Foam::parse(Rule::list, text); + assert!(parse.is_ok(), "{:#?}", parse); } }