1+ import re
2+
3+ from . import _symbol
14from .. import errors
2- from .. import _symbol
3- from ..utils import _parser
45
56
6- class Card (_symbol .Nonterminal ):
7+ class Card (_symbol .InpNonterminal ):
78 """
89 Represents generic INP cards.
910 """
@@ -25,7 +26,7 @@ def from_mcnp(cls, source: str):
2526 InpError: SYNTAX_OPTION.
2627 """
2728
28- source , comments = _parser . preprocess_inp (source )
29+ source , comments = cls . _preprocess (source )
2930 tokens = cls ._REGEX .match (source )
3031
3132 if not tokens :
@@ -38,3 +39,71 @@ def from_mcnp(cls, source: str):
3839 card = cls (** attrs )
3940 card .comments = comments
4041 return card
42+
43+ @staticmethod
44+ def _preprocess (source : str ) -> tuple [str , tuple [str ]]:
45+ """
46+ Preprocess INP for ``from_mcnp``.
47+
48+ Parameters:
49+ source: INP to preprocess.
50+
51+ Returns:
52+ Preprocess INP.
53+ """
54+
55+ lines = []
56+ comments = []
57+
58+ for line in source .split ('\n ' ):
59+ split = line .split ('$' , maxsplit = 1 )
60+
61+ if len (split ) == 2 :
62+ lines .append (split [0 ])
63+ comments .append (split [1 ])
64+ else :
65+ lines .append (line )
66+
67+ source = '\n ' .join (lines )
68+ source = re .sub (r'\n +|& *\n *' , ' ' , source )
69+ source = re .sub (r' +' , ' ' , source )
70+ source = re .sub (r'[(] ' , '(' , source )
71+ source = re .sub (r' [)]' , ')' , source )
72+ source = re .sub (r'\n \n' , '\n \n ' , source )
73+ source = re .sub (r' = | =|= |=' , ' ' , source )
74+ source = re .sub (r'\t' , ' ' , source )
75+ source = source .strip ()
76+
77+ source = re .sub (r'((?: [jJ]){2,})(\s|\Z|\n)' , lambda match : f' { len (match [1 ].strip ().split ())} j' + match [2 ], source )
78+ source = re .sub (r'((?: [rR]){2,})(\s|\Z|\n)' , lambda match : f' { len (match [1 ].strip ().split ())} r' + match [2 ], source )
79+ source = re .sub (r'((?: [iI]){2,})(\s|\Z|\n)' , lambda match : f' { len (match [1 ].strip ().split ())} i' + match [2 ], source )
80+ source = re .sub (r' (\d+)[jJ](\s|\Z|\n)' , lambda match : int (match [1 ]) * ' j' + match [2 ], source )
81+ source = re .sub (r' (\d+)[rR](\s|\Z|\n)' , lambda match : int (match [1 ]) * ' r' + match [2 ], source )
82+ source = re .sub (r' (\d+)[iI](\s|\Z|\n)' , lambda match : int (match [1 ]) * ' I' + match [2 ], source )
83+
84+ return source , comments
85+
86+ @staticmethod
87+ def _postprocess (source : str ) -> str :
88+ """
89+ Postprocesses INP for ``to_mcnp``.
90+
91+ Parameters:
92+ source: INP to postprocess.
93+
94+ Returns:
95+ Postprocessed INP.
96+ """
97+
98+ lines = []
99+ length = 0
100+
101+ for word in source .split (' ' ):
102+ if len (word ) + length > 78 :
103+ lines .append ('&\n ' )
104+ length = 5
105+
106+ lines .append (word )
107+ length += len (word ) + 1
108+
109+ return ' ' .join (lines )
0 commit comments