Skip to content

Commit c8eb6de

Browse files
committed
attempt to account for parenthesis pairs in particle names
1 parent 4e27b8c commit c8eb6de

2 files changed

Lines changed: 54 additions & 18 deletions

File tree

src/decaylanguage/data/descriptor.lark

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22
//
33
// Distributed under the 3-clause BSD license, see accompanying file LICENSE
44
// or https://github.com/scikit-hep/decaylanguage for details.
5+
%import common.WS_INLINE
56

6-
start: decay
7+
start: _ws decay _ws
78

8-
decay: particle ARROW daughters
9+
decay: particle _ws ARROW _ws daughters
910

10-
daughters: daughter+
11+
daughters: daughter (_sep daughter)* _ws
1112

1213
daughter: particle
1314
| sub_decay
1415

15-
particle: PARTICLE
16-
17-
sub_decay: LPAR decay RPAR
16+
particle: PHEAD psuffix*
17+
sub_decay: LPAR _ws decay RPAR
1818

1919
// Terminals
20-
ARROW: "->"
20+
ARROW.2: "->"
2121
LPAR: "("
2222
RPAR: ")"
2323

24-
// Particle names start with alphanumeric/underscore and can then include
25-
// common descriptor suffix symbols such as +, -, *, ', and ~.
26-
PARTICLE: /[A-Za-z0-9_][A-Za-z0-9_+*'~̄-]*/
24+
psuffix: PCHUNK | pgroup_paren
25+
pgroup_paren: "(" _ws PCHUNK _ws ")"
26+
27+
PHEAD: /[A-Za-z0-9~][A-Za-z0-9\/\-+*_.'~]*/
28+
PCHUNK: /[A-Za-z0-9\/\-+*_.'~]+/
2729

28-
%import common.WS
29-
%ignore WS
30+
_sep: WS_INLINE+
31+
_ws: WS_INLINE*

src/decaylanguage/decay/decay.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -772,16 +772,38 @@ def start(self, items: list[Any]) -> DecayChainDict:
772772
return typing.cast(DecayChainDict, items[0])
773773

774774
def particle(self, items: list[Any]) -> str:
775-
# particle: PARTICLE
776-
return str(items[0])
775+
# particle: PHEAD psuffix*
776+
# Rebuild full particle name from head + suffixes
777+
return "".join(self._item_text(i) for i in items if not self._is_ws(i))
778+
779+
def psuffix(self, items: list[Any]) -> str:
780+
# psuffix: PCHUNK | pgroup_paren
781+
# Return the only significant piece
782+
for item in items:
783+
if not self._is_ws(item):
784+
return self._item_text(item)
785+
return ""
786+
787+
def pgroup_paren(self, items: list[Any]) -> str:
788+
# pgroup_paren: "(" _ws PCHUNK _ws ")"
789+
inner = "".join(
790+
self._item_text(i)
791+
for i in items
792+
if not self._is_ws(i) and not self._is_paren_token(i)
793+
)
794+
return f"({inner})"
777795

778796
def daughter(self, items: list[Any]) -> str | DecayChainDict:
779797
# daughter: particle | sub_decay
780798
return typing.cast(str | DecayChainDict, items[0])
781799

782800
def daughters(self, items: list[Any]) -> list[str | DecayChainDict]:
783801
# daughters: daughter+
784-
return [item for item in items if isinstance(item, (str, dict))]
802+
return [
803+
item
804+
for item in items
805+
if not isinstance(item, Token) and isinstance(item, (str, dict))
806+
]
785807

786808
def sub_decay(self, items: list[Any]) -> DecayChainDict:
787809
# sub_decay: LPAR decay RPAR
@@ -796,13 +818,13 @@ def decay(self, items: list[Any]) -> DecayChainDict:
796818
daughters: list[str | DecayChainDict] | None = None
797819

798820
for item in items:
821+
if isinstance(item, Token):
822+
# Ignore punctuation and whitespace tokens.
823+
continue
799824
if isinstance(item, str) and mother is None:
800825
mother = item
801826
elif isinstance(item, list):
802827
daughters = item
803-
elif isinstance(item, Token):
804-
# Ignore punctuation tokens such as ARROW.
805-
continue
806828

807829
if not mother or not daughters:
808830
raise ValueError(
@@ -817,6 +839,18 @@ def decay(self, items: list[Any]) -> DecayChainDict:
817839
}
818840
return {mother: [mode]}
819841

842+
@staticmethod
843+
def _is_ws(item: Any) -> bool:
844+
return isinstance(item, Token) and item.type == "WS_INLINE"
845+
846+
@staticmethod
847+
def _is_paren_token(item: Any) -> bool:
848+
return isinstance(item, Token) and item.type in {"LPAR", "RPAR"}
849+
850+
@staticmethod
851+
def _item_text(item: Any) -> str:
852+
return str(item.value) if isinstance(item, Token) else str(item)
853+
820854

821855
class DecayChain:
822856
"""

0 commit comments

Comments
 (0)