From 8f4ef023527aafe13fa4e51e6f3fb1364d238917 Mon Sep 17 00:00:00 2001 From: Matt Speck <20689127+mjspeck@users.noreply.github.com> Date: Tue, 4 Oct 2022 21:54:06 -0400 Subject: [PATCH 1/2] fix: fixed issue with provinces and prov_no_coast I'm not 100% sure that this is the best solution or that it doesn't cause other errors. @byung-o double check the changes I made both in src/daidepp/grammar.py and src/daidepp/daide_visitor.py Closes: #24 --- src/daidepp/__init__.py | 2 +- .../{node_visitor.py => daide_visitor.py} | 16 +++++++------- src/daidepp/grammar.py | 22 ++++++++++++++----- tests/conftest.py | 9 ++++++++ tests/test_daide_visitor.py | 12 ++++++++++ 5 files changed, 47 insertions(+), 14 deletions(-) rename src/daidepp/{node_visitor.py => daide_visitor.py} (99%) create mode 100644 tests/conftest.py create mode 100644 tests/test_daide_visitor.py diff --git a/src/daidepp/__init__.py b/src/daidepp/__init__.py index 734318c..d17191d 100644 --- a/src/daidepp/__init__.py +++ b/src/daidepp/__init__.py @@ -1,2 +1,2 @@ +from daidepp.daide_visitor import daide_visitor from daidepp.grammar import create_daide_grammar -from daidepp.node_visitor import daide_visitor diff --git a/src/daidepp/node_visitor.py b/src/daidepp/daide_visitor.py similarity index 99% rename from src/daidepp/node_visitor.py rename to src/daidepp/daide_visitor.py index 1880ea5..3863258 100644 --- a/src/daidepp/node_visitor.py +++ b/src/daidepp/daide_visitor.py @@ -418,14 +418,6 @@ def visit_power(self, node, visited_children): return node.text def visit_province(self, node, visited_children): - if isinstance(visited_children[0], str): - return node.text - - _, province, _, coast, _ = visited_children[0] - - return province + " " + coast - - def visit_coast(self, node, visited_children): return node.text def visit_prov_coast(self, node, visited_children): @@ -447,6 +439,14 @@ def visit_unit(self, node, visited_children): def visit_unit_type(self, node, visited_children): return node.text + def visit_province(self, node, visited_children): + if isinstance(visited_children[0], str): + return node.text + return visited_children[0][0].text + + def visit_coast(self, node, visited_children): + return node.text + def visit_turn(self, node, visited_children): season, _, year = visited_children return season + " " + year.text diff --git a/src/daidepp/grammar.py b/src/daidepp/grammar.py index 087e59b..7bd0bed 100644 --- a/src/daidepp/grammar.py +++ b/src/daidepp/grammar.py @@ -21,8 +21,8 @@ def _set_try_tokens(self): self.try_tokens: List[str] = try_tokens_strings @staticmethod - def from_level(level: DAIDE_LEVEL): - return create_daide_grammar(level) + def from_level(level: DAIDE_LEVEL, allow_just_arrangment: bool = False): + return create_daide_grammar(level, allow_just_arrangment) DAIDE_LEVEL = Literal[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130] @@ -58,6 +58,9 @@ def from_level(level: DAIDE_LEVEL): "try_tokens": '"PRP" / "PCE" / "ALY" / "VSS" / "DRW" / "SLO" / "NOT" / "YES" / "REJ" / "BWX" / "FCT"', } +# prov_no_coast: all province tokens without coasts +# province: all provinces including coasts + # Order Proposals LEVEL_20: GRAMMAR_DICT = { "xdo": '"XDO" lpar order rpar', @@ -77,10 +80,19 @@ def from_level(level: DAIDE_LEVEL): "wve": 'power ws "WVE"', "unit_type": '"AMY" / "FLT"', "unit": "power ws unit_type ws province", - "province": "prov_coast / prov_no_coast / prov_sea / (lpar prov_coast ws coast rpar)", + "prov_no_coast": "prov_land_sea / prov_landlock / prov_sea", + "stp_ncs": 'lpar "STP" ws "SCS" rpar', + "stp_scs": 'lpar "STP" ws "SCS" rpar', + "spa_ncs": 'lpar "SPA" ws "NCS" rpar', + "spa_scs": 'lpar "SPA" ws "SCS" rpar', + "bul_ecs": 'lpar "BUL" ws "ECS" rpar', + "bul_scs": 'lpar "BUL" ws "SCS" rpar', + "prov_coast": "stp_ncs / stp_scs / spa_ncs / spa_scs / bul_ecs / bul_scs", + # "prov_coast": '"(STP NCS)" / "(STP SCS)" / "(SPA NCS)" / "(SPA SCS)" / "(BUL ECS)" / "(BUL SCS)"', + "province": "prov_land_sea / prov_landlock / prov_sea / prov_coast", "coast": '"NCS" / "ECS" / "SCS" / "WCS"', - "prov_coast": '"ALB" / "ANK" / "APU" / "ARM" / "BEL" / "BER" / "BRE" / "BUL" / "CLY" / "CON" / "DEN" / "EDI" / "FIN" / "GAS" / "GRE" / "HOL" / "KIE" / "LON" / "LVN" / "LVP" / "MAR" / "NAF" / "NAP" / "NWY" / "PIC" / "PIE" / "POR" / "PRU" / "ROM" / "RUM" / "SEV" / "SMY" / "SPA" / "STP" / "SWE" / "SYR" / "TRI" / "TUN" / "TUS" / "VEN" / "YOR" / "WAL"', - "prov_no_coast": '"BOH" / "BUD" / "BUR" / "MOS" / "MUN" / "GAL" / "PAR" / "RUH" / "SER" / "SIL" / "TYR" / "UKR" / "VIE" / "WAR" ', + "prov_land_sea": '"ALB" / "ANK" / "APU" / "ARM" / "BEL" / "BER" / "BRE" / "BUL" / "CLY" / "CON" / "DEN" / "EDI" / "FIN" / "GAS" / "GRE" / "HOL" / "KIE" / "LON" / "LVN" / "LVP" / "MAR" / "NAF" / "NAP" / "NWY" / "PIC" / "PIE" / "POR" / "PRU" / "ROM" / "RUM" / "SEV" / "SMY" / "SPA" / "STP" / "SWE" / "SYR" / "TRI" / "TUN" / "TUS" / "VEN" / "YOR" / "WAL"', + "prov_landlock": '"BOH" / "BUD" / "BUR" / "MOS" / "MUN" / "GAL" / "PAR" / "RUH" / "SER" / "SIL" / "TYR" / "UKR" / "VIE" / "WAR" ', "prov_sea": '"ADR" / "AEG" / "BAL" / "BAR" / "BLA" / "BOT" / "EAS" / "ENG" / "HEL" / "ION" / "IRI" / "LYO" / "MAO" / "NAO" / "NTH" / "NWG" / "SKA" / "TYS" / "WES"', "supply_center": '"ANK" / "BEL" / "BER" / "BRE" / "BUD" / "BUL" / "CON" / "DEN" / "EDI" / "GRE" / "HOL" / "KIE" / "LON" / "LVP" / "MAR" / "MOS" / "MUN" / "NAP" / "NWY" / "PAR" / "POR" / "ROM" / "RUM" / "SER" / "SEV" / "SMY" / "SPA" / "STP" / "SWE" / "TRI" / "TUN" / "VEN" / "VIE" / "WAR"', "arrangement": f"{TRAIL_TOKEN}mto / xdo / dmz", diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..ea5b7c3 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,9 @@ +import pytest + + +@pytest.fixture(scope="session") +def sample_daide_messages(): + return [ + "PRP(XDO((ENG FLT EDI) SUP (ENG AMY LVP) MTO CLY))", + "PRP(XDO((ENG FLT (STP SCS)) SUP (ENG AMY LVP) MTO CLY))", + ] diff --git a/tests/test_daide_visitor.py b/tests/test_daide_visitor.py new file mode 100644 index 0000000..98bbcb5 --- /dev/null +++ b/tests/test_daide_visitor.py @@ -0,0 +1,12 @@ +from typing import List + + +def test_basic_visitor(sample_daide_messages: List[str]): + from daidepp.daide_visitor import daide_visitor + from daidepp.grammar import create_daide_grammar + + grammar = create_daide_grammar(level=130) + for message in sample_daide_messages: + tree = grammar.parse(message) + daide_visitor.visit(tree) + assert True From b706e51492548d014781c6634e5cba2ec0161fd2 Mon Sep 17 00:00:00 2001 From: Matt Speck <20689127+mjspeck@users.noreply.github.com> Date: Wed, 5 Oct 2022 11:10:16 -0400 Subject: [PATCH 2/2] chore: updated daide_visitor and level 20 grammar with @byung-o --- src/daidepp/daide_visitor.py | 17 ++++++++++++----- src/daidepp/grammar.py | 9 +-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/daidepp/daide_visitor.py b/src/daidepp/daide_visitor.py index 3863258..3df807d 100644 --- a/src/daidepp/daide_visitor.py +++ b/src/daidepp/daide_visitor.py @@ -417,9 +417,6 @@ def visit_wve(self, node, visited_children): def visit_power(self, node, visited_children): return node.text - def visit_province(self, node, visited_children): - return node.text - def visit_prov_coast(self, node, visited_children): return node.text @@ -440,9 +437,19 @@ def visit_unit_type(self, node, visited_children): return node.text def visit_province(self, node, visited_children): - if isinstance(visited_children[0], str): + if isinstance(visited_children, str): return node.text - return visited_children[0][0].text + return visited_children[0] + + def visit_prov_landlock(self, node, visited_children): + return node.text + + def visit_prov_land_sea(self, node, visited_children): + return node.text + + def visit_prov_coast(self, node, visited_children): + _, province, _, coast, _ = visited_children[0] + return province.text + " " + coast.text def visit_coast(self, node, visited_children): return node.text diff --git a/src/daidepp/grammar.py b/src/daidepp/grammar.py index 7bd0bed..dc80497 100644 --- a/src/daidepp/grammar.py +++ b/src/daidepp/grammar.py @@ -81,14 +81,7 @@ def from_level(level: DAIDE_LEVEL, allow_just_arrangment: bool = False): "unit_type": '"AMY" / "FLT"', "unit": "power ws unit_type ws province", "prov_no_coast": "prov_land_sea / prov_landlock / prov_sea", - "stp_ncs": 'lpar "STP" ws "SCS" rpar', - "stp_scs": 'lpar "STP" ws "SCS" rpar', - "spa_ncs": 'lpar "SPA" ws "NCS" rpar', - "spa_scs": 'lpar "SPA" ws "SCS" rpar', - "bul_ecs": 'lpar "BUL" ws "ECS" rpar', - "bul_scs": 'lpar "BUL" ws "SCS" rpar', - "prov_coast": "stp_ncs / stp_scs / spa_ncs / spa_scs / bul_ecs / bul_scs", - # "prov_coast": '"(STP NCS)" / "(STP SCS)" / "(SPA NCS)" / "(SPA SCS)" / "(BUL ECS)" / "(BUL SCS)"', + "prov_coast": '(lpar "STP" ws "SCS" rpar) / (lpar "STP" ws "SCS" rpar) / (lpar "SPA" ws "NCS" rpar) / (lpar "SPA" ws "SCS" rpar) / (lpar "BUL" ws "ECS" rpar) / (lpar "BUL" ws "SCS" rpar)', "province": "prov_land_sea / prov_landlock / prov_sea / prov_coast", "coast": '"NCS" / "ECS" / "SCS" / "WCS"', "prov_land_sea": '"ALB" / "ANK" / "APU" / "ARM" / "BEL" / "BER" / "BRE" / "BUL" / "CLY" / "CON" / "DEN" / "EDI" / "FIN" / "GAS" / "GRE" / "HOL" / "KIE" / "LON" / "LVN" / "LVP" / "MAR" / "NAF" / "NAP" / "NWY" / "PIC" / "PIE" / "POR" / "PRU" / "ROM" / "RUM" / "SEV" / "SMY" / "SPA" / "STP" / "SWE" / "SYR" / "TRI" / "TUN" / "TUS" / "VEN" / "YOR" / "WAL"',