Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

daide string generation and tests #31

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ DAIDE parser using [parsimonious](https://github.com/erikrose/parsimonious). Par

## Use

Using the grammar in grammar.py, you can create a parse tree from a DAIDE press message or reply. The nodes of the parse tree can be visited to return something more useful. The visiting rules for each of the nodes of the parse tree are defined in node_visitor.py.
Using the grammar in `grammar.py`, you can create a parse tree from a DAIDE press message or reply. The nodes of the parse tree can be visited to return something more useful. The visiting rules for each of the nodes of the parse tree are defined in `node_visitor.py`.

Example:

Expand All @@ -31,6 +31,18 @@ Example:

If the DAIDE token is not in the grammar or if the message is malformed, the parser will just thrown an exception. We're currently working on returning a list of unrecognized tokens instead of just erroring out.


In addition, DAIDE strings can be constructed using the classes in `keywords.py`. Each class has type hints that indicate the parameters that should be used.

Example:

```python3
>>> from daidepp import AND, PRP, PCE
>>> str(AND(PRP(PCE("AUS")), PRP(PCE("AUS", "ENG"))))
`AND ( ( PRP ( PCE ( AUS ) ) ) ( PRP ( PCE ( AUS ENG ) ) ) ( PRP ( PCE ( AUS ENG FRA ) ) ) )`
```
Each class in `keywords.py` uses different parameters for instantiation, so it is recommended to carefully follow the type hints or checkout `tests/test_keywords.py`, which provices examples for each class.

## Pull Requests

Three files should be updated whenever making a PR:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = daidepp
version = 0.1.0
version = 0.2.0
author = Byung Oh
author_email = [email protected]
description = "DAIDE Parser"
Expand Down
1 change: 1 addition & 0 deletions src/daidepp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from daidepp.daide_visitor import daide_visitor
from daidepp.grammar import create_daide_grammar
from daidepp.keywords import *
170 changes: 170 additions & 0 deletions src/daidepp/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
from typing_extensions import Literal

POWER = Literal["AUS", "ENG", "FRA", "GER", "ITA", "RUS", "TUR"]
UNIT_TYPE = Literal["AMY", "FLT"]

PROVINCE_LAND_SEA = Literal[
"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",
]
PROVINCE_LANDLOCK = Literal[
"BOH",
"BUD",
"BUR",
"MOS",
"MUN",
"GAL",
"PAR",
"RUH",
"SER",
"SIL",
"TYR",
"UKR",
"VIE",
"WAR",
]
PROVINCE_SEA = Literal[
"ADR",
"AEG",
"BAL",
"BAR",
"BLA",
"BOT",
"EAS",
"ENG",
"HEL",
"ION",
"IRI",
"LYO",
"MAO",
"NAO",
"NTH",
"NWG",
"SKA",
"TYS",
"WES",
]
PROVINCE_COAST = Literal[
"STP NCS", "STP SCS", "SPA NCS", "SPA SCS", "BUL ECS", "BUL SCS"
]
PROVINCE = Literal[PROVINCE_LAND_SEA, PROVINCE_LANDLOCK, PROVINCE_SEA, PROVINCE_COAST]
PROVINCE_NO_COAST = Literal[PROVINCE_LAND_SEA, PROVINCE_LANDLOCK, PROVINCE_SEA]

SEASON = Literal["SPR", "SUM", "FAL", "AUT", "WIN"]

TRY_TOKENS = Literal[
"PRP",
"PCE",
"ALY",
"VSS",
"DRW",
"SLO",
"NOT",
"NAR",
"YES",
"REJ",
"BWX",
"FCT",
"XDO",
"DMZ",
"AND",
"ORR",
"SCD",
"OCC",
"CHO",
"INS",
"QRY",
"THK",
"IDK",
"SUG",
"HOW",
"WHT",
"EXP",
"SRY",
"FOR",
"IFF",
"XOY",
"YDO",
"SND",
"FWD",
"BCC",
"WHY",
"POB",
]

SUPPLY_CENTER = Literal[
"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",
]
2 changes: 2 additions & 0 deletions src/daidepp/daide_visitor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from parsimonious.nodes import Node, NodeVisitor

from daidepp.keywords import *


class DAIDEVisitor(NodeVisitor):
def visit_message(self, node, visited_children):
Expand Down
5 changes: 2 additions & 3 deletions src/daidepp/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def from_level(level: DAIDE_LEVEL, allow_just_arrangment: bool = False):
LEVEL_10: GRAMMAR_DICT = {
"pce": '"PCE" lpar power (ws power)* rpar',
"ccl": '"CCL" lpar press_message rpar',
"fct": '"FCT" lpar arrangement rpar',
"try": '"TRY" lpar try_tokens (ws try_tokens)* rpar',
"huh": '"HUH" lpar press_message rpar',
"prp": '"PRP" lpar arrangement rpar',
Expand Down Expand Up @@ -187,10 +186,10 @@ def from_level(level: DAIDE_LEVEL, allow_just_arrangment: bool = False):
# Explanations
LEVEL_130: GRAMMAR_DICT = {
"fct_thk_prp_ins": "fct / thk / prp / ins",
"qry_wht_prp_ins_sug": "qry / wht/ prp / ins / sug", # added sug because it looks like it's also supported at level 130
"qry_exp_wht_prp_ins_sug": "qry / exp / wht / prp / ins / sug", # added sug because it looks like it's also supported at level 130
"why": '"WHY" lpar fct_thk_prp_ins rpar',
"pob": '"POB" lpar why rpar',
"idk": '"IDK" lpar qry_wht_prp_ins_sug rpar',
"idk": '"IDK" lpar qry_exp_wht_prp_ins_sug rpar',
"reply": f"{TRAIL_TOKEN}why / pob / idk",
"try_tokens": f'{TRAIL_TOKEN}"WHY" / "POB"',
}
Expand Down
Loading