From d8362da80f1591b091cab1172637f7ab53062095 Mon Sep 17 00:00:00 2001 From: Gabriel Gerlero Date: Mon, 1 Apr 2024 01:53:54 -0300 Subject: [PATCH] Update parsing --- foamlib/_dictionaries.py | 2 +- tests/test_dictionaries.py | 48 +++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/foamlib/_dictionaries.py b/foamlib/_dictionaries.py index 63561bb..90c3a59 100644 --- a/foamlib/_dictionaries.py +++ b/foamlib/_dictionaries.py @@ -353,7 +353,7 @@ def _list_of(elem: ParserElement) -> ParserElement: Opt(common.integer).suppress() + ( Literal("(").suppress() - + Group((elem)[...]).set_parse_action(lambda tks: tks.as_list()) + + Group((elem)[...], aslist=True) + Literal(")").suppress() ) ) diff --git a/tests/test_dictionaries.py b/tests/test_dictionaries.py index f2f630e..2172f2a 100644 --- a/tests/test_dictionaries.py +++ b/tests/test_dictionaries.py @@ -11,51 +11,51 @@ def test_parse_value() -> None: - assert _VALUE.parse_string("1").as_list()[0] == 1 - assert _VALUE.parse_string("1.0").as_list()[0] == 1.0 - assert _VALUE.parse_string("1.0e-3").as_list()[0] == 1.0e-3 - assert _VALUE.parse_string("yes").as_list()[0] is True - assert _VALUE.parse_string("no").as_list()[0] is False - assert _VALUE.parse_string("word").as_list()[0] == "word" - assert _VALUE.parse_string("word word").as_list()[0] == "word word" - assert _VALUE.parse_string('"a string"').as_list()[0] == '"a string"' - assert _VALUE.parse_string("uniform 1").as_list()[0] == 1 - assert _VALUE.parse_string("uniform 1.0").as_list()[0] == 1.0 - assert _VALUE.parse_string("uniform 1.0e-3").as_list()[0] == 1.0e-3 - assert _VALUE.parse_string("(1.0 2.0 3.0)").as_list()[0] == [1.0, 2.0, 3.0] - assert _VALUE.parse_string("uniform (1 2 3)").as_list()[0] == [1, 2, 3] - assert _VALUE.parse_string("nonuniform List 2(1 2)").as_list()[0] == [1, 2] - assert _VALUE.parse_string("nonuniform List 2{1}").as_list()[0] == [1, 1] - assert _VALUE.parse_string("3(1 2 3)").as_list()[0] == [1, 2, 3] - assert _VALUE.parse_string("2((1 2 3) (4 5 6))").as_list()[0] == [ + assert _VALUE.parse_string("1")[0] == 1 + assert _VALUE.parse_string("1.0")[0] == 1.0 + assert _VALUE.parse_string("1.0e-3")[0] == 1.0e-3 + assert _VALUE.parse_string("yes")[0] is True + assert _VALUE.parse_string("no")[0] is False + assert _VALUE.parse_string("word")[0] == "word" + assert _VALUE.parse_string("word word")[0] == "word word" + assert _VALUE.parse_string('"a string"')[0] == '"a string"' + assert _VALUE.parse_string("uniform 1")[0] == 1 + assert _VALUE.parse_string("uniform 1.0")[0] == 1.0 + assert _VALUE.parse_string("uniform 1.0e-3")[0] == 1.0e-3 + assert _VALUE.parse_string("(1.0 2.0 3.0)")[0] == [1.0, 2.0, 3.0] + assert _VALUE.parse_string("uniform (1 2 3)")[0] == [1, 2, 3] + assert _VALUE.parse_string("nonuniform List 2(1 2)")[0] == [1, 2] + assert _VALUE.parse_string("nonuniform List 2{1}")[0] == [1, 1] + assert _VALUE.parse_string("3(1 2 3)")[0] == [1, 2, 3] + assert _VALUE.parse_string("2((1 2 3) (4 5 6))")[0] == [ [1, 2, 3], [4, 5, 6], ] - assert _VALUE.parse_string("2{(1 2 3)}").as_list()[0] == [ + assert _VALUE.parse_string("2{(1 2 3)}")[0] == [ [1, 2, 3], [1, 2, 3], ] - assert _VALUE.parse_string("nonuniform List 2((1 2 3) (4 5 6))").as_list()[ + assert _VALUE.parse_string("nonuniform List 2((1 2 3) (4 5 6))")[ 0 ] == [ [1, 2, 3], [4, 5, 6], ] - assert _VALUE.parse_string("nonuniform List 2{(1 2 3)}").as_list()[0] == [ + assert _VALUE.parse_string("nonuniform List 2{(1 2 3)}")[0] == [ [1, 2, 3], [1, 2, 3], ] - assert _VALUE.parse_string("[1 1 -2 0 0 0 0]").as_list()[ + assert _VALUE.parse_string("[1 1 -2 0 0 0 0]")[ 0 ] == FoamFile.DimensionSet(mass=1, length=1, time=-2) - assert _VALUE.parse_string("g [1 1 -2 0 0 0 0] (0 0 -9.81)").as_list()[ + assert _VALUE.parse_string("g [1 1 -2 0 0 0 0] (0 0 -9.81)")[ 0 ] == FoamFile.Dimensioned( name="g", dimensions=FoamFile.DimensionSet(mass=1, length=1, time=-2), value=[0, 0, -9.81], ) - assert _VALUE.parse_string("[1 1 -2 0 0 0 0] 9.81").as_list()[ + assert _VALUE.parse_string("[1 1 -2 0 0 0 0] 9.81")[ 0 ] == FoamFile.Dimensioned( dimensions=FoamFile.DimensionSet(mass=1, length=1, time=-2), value=9.81 @@ -63,7 +63,7 @@ def test_parse_value() -> None: assert ( _VALUE.parse_string( "hex (0 1 2 3 4 5 6 7) (1 1 1) simpleGrading (1 1 1)" - ).as_list()[0] + )[0] == "hex (0 1 2 3 4 5 6 7) (1 1 1) simpleGrading (1 1 1)" )