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

Refactored grammar to remove need for second transformer #385

Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 2 additions & 6 deletions src/decaylanguage/data/decfile.lark
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,13 @@ model_label : LABEL

model_alias : "ModelAlias" model_label model

model : (model_label _SEMICOLON | MODEL_NAME_AND_SC | MODEL_NAME_AND_WS model_options _SEMICOLON+ | MODEL_NAME_MULTILINE model_options _SEMICOLON+)
model : (model_label | MODEL_NAME model_options?) _SEMICOLON+
sognetic marked this conversation as resolved.
Show resolved Hide resolved
model_options : (value | LABEL | _NEWLINE | _COMMA)+

// Model names must either be followed by at least one whitespace (when they have model parameters) or a semicolon (without model parameters)
// We must set priorities here to use lalr - match model name above label, and label above something else

MODEL_NAME_AND_WS.2: MODEL_NAME _WS+
MODEL_NAME_AND_SC.2: MODEL_NAME _WS* _SEMICOLON+
MODEL_NAME_MULTILINE.2: MODEL_NAME NEWLINE _WS*

MODEL_NAME : "BaryonPCR"|"BC_SMN"|"BC_TMN"|"BC_VHAD"|"BC_VMN"|"BCL"|"BGL"|"BLLNUL"|"BNOCB0TO4PICP"|"BNOCBPTO3HPI0"|"BNOCBPTOKSHHH"|"BS_MUMUKK"|"BSTOGLLISRFSR"|"BSTOGLLMNT"|"BT02PI_CP_ISO"|"BTO3PI_CP"|"BTODDALITZCPK"|"BToDiBaryonlnupQCD"|"BTOSLLALI"|"BTOSLLBALL"|"BTOSLLMS"|"BTOSLLMSEXT"|"BTOVLNUBALL"|"BTOXSGAMMA"|"BTOXELNU"|"BTOXSLL"|"BQTOLLLLHYPERCP"|"BQTOLLLL"|"CB3PI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"TAUOLA"|"TAUSCALARNU"|"TAUVECTORNU"|"THREEBODYPHSP"|"TSS"|"TVP"|"TVS_PWAVE"|"VLL"|"VSP_PWAVE"|"VSS_BMIX"|"VSS_MIX"|"VSS"|"VTOSLL"|"VUB"|"VVPIPI"|"VVP"|"VVS_PWAVE"|"XLL"|"YMSTOYNSPIPICLEO"|"YMSTOYNSPIPICLEOBOOST")/\b/

// model : model_generic
// model_helamp : "HELAMP" (SIGNED_NUMBER SIGNED_NUMBER)+
Expand Down
21 changes: 0 additions & 21 deletions src/decaylanguage/dec/dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,6 @@ def parse(self, include_ccdecays: bool = True) -> None:
)
self._parsed_dec_file = parser.parse(self._dec_file)

# Strip whitespace and semicolons (necessary for LALR(1) grammar) from model names
self._parsed_dec_file = ModelNameCleanup().transform(self._parsed_dec_file)

# At last, find all particle decays defined in the .dec decay file ...
self._find_parsed_decays()
# Replace model aliases with the actual models and model parameters. Deepcopy to avoid modification of dict by
Expand Down Expand Up @@ -1035,24 +1032,6 @@ def __str__(self) -> str:
return repr(self)


class ModelNameCleanup(Transformer): # type: ignore[misc]
"""
Lark Transformer removing whitespace and semicolons from decay model parameter terminals
(MODEL_NAME_AND_WS and MODEL_NAME_AND_SC). These exist to distinguish model names from
strings containing them as substrings and have to be terminals to allow LALR(1) parsing.

"""

def MODEL_NAME_AND_WS(self, t: Token) -> Token:
return t.update(value=t.strip())

def MODEL_NAME_AND_SC(self, t: Token) -> Token:
return t.update(value=t.strip(";").strip())

def MODEL_NAME_MULTILINE(self, t: Token) -> Token:
return t.update(value=t.strip())


class DecayModelAliasReplacement(Transformer): # type: ignore[misc]
"""
Lark Transformer implementing the replacement of decay model aliases
Expand Down
83 changes: 7 additions & 76 deletions tests/dec/test_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
DecayNotFound,
DecFileNotParsed,
DecFileParser,
ModelNameCleanup,
get_decay_mother_name,
get_final_state_particle_names,
get_model_name,
Expand Down Expand Up @@ -467,7 +466,7 @@ def test_decay_model_parsing_with_model_alias():
p.parse()
assert p._dict_raw_model_aliases() == {
"SLBKPOLE_DtoKlnu": [
Token("MODEL_NAME_AND_WS", "SLBKPOLE"),
Token("MODEL_NAME", "SLBKPOLE"),
Tree(
"model_options",
[
Expand Down Expand Up @@ -654,74 +653,6 @@ def test_expand_decay_chains():
assert set(all_decays) == set(output[particle])


def test_Lark_ModelNameCleanup_Transformer_no_params():
t = Tree(
"decay",
[
Tree("particle", [Token("LABEL", "D0")]),
Tree(
"decayline",
[
Tree("value", [Token("SIGNED_NUMBER", "1.0")]),
Tree("particle", [Token("LABEL", "K-")]),
Tree("particle", [Token("LABEL", "pi+")]),
Tree("model", [Token("MODEL_NAME_AND_SC", "PHSP;")]),
],
),
],
)

cleaned_tree = ModelNameCleanup().transform(t)
tree_decayline = next(iter(cleaned_tree.find_data("decayline")))
assert get_model_name(tree_decayline) == "PHSP"


def test_Lark_ModelNameCleanup_Transformer_with_params():
t = Tree(
"decay",
[
Tree("particle", [Token("LABEL", "B0sig")]),
Tree(
"decayline",
[
Tree("value", [Token("SIGNED_NUMBER", "1.000")]),
Tree("particle", [Token("LABEL", "MyFirstD*-")]),
Tree("particle", [Token("LABEL", "MySecondD*+")]),
Tree(
"model",
[
Token("MODEL_NAME_AND_WS", "SVV_HELAMP "),
Tree(
"model_options",
[
Tree("value", [Token("SIGNED_NUMBER", "0.0")]),
Tree("value", [Token("SIGNED_NUMBER", "0.0")]),
Tree("value", [Token("SIGNED_NUMBER", "0.0")]),
Tree("value", [Token("SIGNED_NUMBER", "0.0")]),
Tree("value", [Token("SIGNED_NUMBER", "1.0")]),
Tree("value", [Token("SIGNED_NUMBER", "0.0")]),
],
),
],
),
],
),
],
)

cleaned_tree = ModelNameCleanup().transform(t)
tree_decayline = next(iter(cleaned_tree.find_data("decayline")))
assert get_model_name(tree_decayline) == "SVV_HELAMP"
assert get_model_parameters(tree_decayline) == [
"0.0",
"0.0",
"0.0",
"0.0",
"1.0",
"0.0",
]


def test_Lark_DecayModelAliasReplacement_Transformer():
t = Tree(
"decay",
Expand All @@ -744,7 +675,7 @@ def test_Lark_DecayModelAliasReplacement_Transformer():
)
dict_model_aliases = {
"SLBKPOLE_DtoKlnu": [
Token("MODEL_NAME_AND_WS", "SLBKPOLE"),
Token("MODEL_NAME", "SLBKPOLE"),
Tree(
"model_options",
[
Expand Down Expand Up @@ -777,7 +708,7 @@ def test_Lark_DecayModelParamValueReplacement_Visitor_no_params():
Tree("value", [Token("SIGNED_NUMBER", "1.0")]),
Tree("particle", [Token("LABEL", "K-")]),
Tree("particle", [Token("LABEL", "pi+")]),
Tree("model", [Token("MODEL_NAME_AND_SC", "PHSP")]),
Tree("model", [Token("MODEL_NAME", "PHSP")]),
],
),
],
Expand Down Expand Up @@ -805,7 +736,7 @@ def test_Lark_DecayModelParamValueReplacement_Visitor_single_value():
Tree(
"model",
[
Token("MODEL_NAME_AND_WS", "VSS_BMIX"),
Token("MODEL_NAME", "VSS_BMIX"),
Tree("model_options", [Token("LABEL", "dm")]),
],
),
Expand Down Expand Up @@ -845,7 +776,7 @@ def test_Lark_DecayModelParamValueReplacement_Visitor_list():
Tree(
"model",
[
Token("MODEL_NAME_AND_WS", "SVV_HELAMP"),
Token("MODEL_NAME", "SVV_HELAMP"),
Tree(
"model_options",
[
Expand Down Expand Up @@ -888,7 +819,7 @@ def test_Lark_ChargeConjugateReplacement_Visitor():
Tree("value", [Token("SIGNED_NUMBER", "1.0")]),
Tree("particle", [Token("LABEL", "K-")]),
Tree("particle", [Token("LABEL", "pi+")]),
Tree("model", [Token("MODEL_NAME_AND_SC", "PHSP")]),
Tree("model", [Token("MODEL_NAME", "PHSP")]),
],
),
],
Expand Down Expand Up @@ -920,7 +851,7 @@ def test_Lark_ChargeConjugateReplacement_Visitor_with_aliases():
Tree("value", [Token("SIGNED_NUMBER", "1.0")]),
Tree("particle", [Token("LABEL", "K-")]),
Tree("particle", [Token("LABEL", "pi+")]),
Tree("model", [Token("MODEL_NAME_AND_SC", "PHSP")]),
Tree("model", [Token("MODEL_NAME", "PHSP")]),
],
),
],
Expand Down
6 changes: 4 additions & 2 deletions tests/dec/test_known_decay_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ def test_lark_file_model_list_consistency():
with Path(filename).open(encoding="utf-8") as lark_file:
lines = lark_file.readlines()
for line in lines:
if "MODEL_NAME :" in line:
if "MODEL_NAME.2 :" in line:
break
models = line.split(":")[1].strip(" ").strip("\n").split('"|"')
models = (
line.split(":")[1][: -len(r"//\b/")].strip(" ()").strip("\n").split('"|"')
)
models = [m.strip('"') for m in models]

assert models == list(known_decay_models)
Expand Down