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

Fix various parsing errors #645

Merged
merged 11 commits into from
Jan 29, 2025
8 changes: 8 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ MontePy Changelog
0.5 releases
============

#Next Release#
--------------

**Bug Fixes**

* Fixed parsing error with not being able to parse a blank ``sdef`` (:issue:`636`).
* Fixed parsing error with parsing ``SSW`` (:issue:`639`).

0.5.3
--------------

Expand Down
9 changes: 5 additions & 4 deletions montepy/input_parser/data_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def data_input(self, p):

@_(
"classifier_phrase",
"classifier_phrase KEYWORD padding",
"classifier_phrase MODIFIER padding",
"padding classifier_phrase",
"padding classifier_phrase KEYWORD padding",
"padding classifier_phrase MODIFIER padding",
)
def introduction(self, p):
ret = {}
Expand All @@ -49,8 +49,8 @@ def introduction(self, p):
else:
ret["start_pad"] = syntax_node.PaddingNode()
ret["classifier"] = p.classifier_phrase
if hasattr(p, "KEYWORD"):
ret["keyword"] = syntax_node.ValueNode(p.KEYWORD, str, padding=p[-1])
if hasattr(p, "MODIFIER"):
ret["keyword"] = syntax_node.ValueNode(p.MODIFIER, str, padding=p[-1])
else:
ret["keyword"] = syntax_node.ValueNode(None, str, padding=None)
return syntax_node.SyntaxNode("data intro", ret)
Expand Down Expand Up @@ -183,6 +183,7 @@ class ParamOnlyDataParser(DataParser):
debugfile = None

@_(
"param_introduction",
"param_introduction spec_parameters",
)
def param_data_input(self, p):
Expand Down
114 changes: 70 additions & 44 deletions montepy/input_parser/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MCNP_Lexer(Lexer):
LIBRARY_SUFFIX,
LOG_INTERPOLATE,
MESSAGE,
MODIFIER,
MULTIPLY,
NUM_INTERPOLATE,
NUM_JUMP,
Expand Down Expand Up @@ -75,50 +76,6 @@ class MCNP_Lexer(Lexer):
"cosy",
"bflcl",
"unc",
# materials
"gas",
"estep",
"hstep",
"nlib",
"plib",
"pnlib",
"elib",
"hlib",
"alib",
"slib",
"tlib",
"dlib",
"cond",
"refi",
"refc",
"refs",
# volume
"no",
# sdef
"cel",
"sur",
"erg",
"tme",
"dir",
"vec",
"nrm",
"pos",
"rad",
"ext",
"axs",
"x",
"y",
"z",
"ccc",
"ara",
"wgt",
"tr",
"eff",
"par",
"dat",
"loc",
"bem",
"bap",
}
"""
Defines allowed keywords in MCNP.
Expand Down Expand Up @@ -471,6 +428,7 @@ class DataLexer(ParticleLexer):
JUMP,
KEYWORD,
LOG_INTERPOLATE,
MODIFIER,
MESSAGE,
MULTIPLY,
NUMBER,
Expand All @@ -486,13 +444,81 @@ class DataLexer(ParticleLexer):
ZAID,
}

_KEYWORDS = set(MCNP_Lexer._KEYWORDS) | {
"sym", # SSW
"pty",
"cel",
# materials
"gas",
"estep",
"hstep",
"nlib",
"plib",
"pnlib",
"elib",
"hlib",
"alib",
"slib",
"tlib",
"dlib",
"cond",
"refi",
"refc",
"refs",
# volume
"no",
# sdef
"cel",
"sur",
"erg",
"tme",
"dir",
"vec",
"nrm",
"pos",
"rad",
"ext",
"axs",
"x",
"y",
"z",
"ccc",
"ara",
"wgt",
"tr",
"eff",
"par",
"dat",
"loc",
"bem",
"bap",
}

_MODIFIERS = {
"no", # VOLUME
}
"""
A keyword flag at the beginning of a input that modifies it's behavior.
"""

@_(r"([|+\-!<>/%^_~@\*\?\#]|\#\d*)+")
def PARTICLE_SPECIAL(self, t):
"""
Particle designators that are special characters.
"""
return t

@_(r"[+\-]?[0-9]*\.?[0-9]*E?[+\-]?[0-9]*[ijrml]+[a-z\./]*", r"[a-z]+[a-z\./]*")
def TEXT(self, t):
t = super().TEXT(t)
if t.value.lower() in self._KEYWORDS:
t.type = "KEYWORD"
if t.value.lower() in self._MODIFIERS:
t.type = "MODIFIER"
elif t.value.lower() in self._PARTICLES:
t.type = "PARTICLE"
return t


class SurfaceLexer(MCNP_Lexer):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/inputs/test_complement_edge.imcnp
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ m814 26000.55c 5.657-2
28000.50c 9.881-3
24000.50c 1.581-2
25055.51c 1.760-3

sdef cel=2 pos=0 0 0 rad=d3 ext=d3 axs=0 0 1

2 changes: 2 additions & 0 deletions tests/inputs/test_dos.imcnp
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ ksrc 0 0 0
kcode 100000 1.000 50 1050
phys:p j 1 2j 1
mode n p
ssw cel=5 99
sdef $ blank one from issue #636


2 changes: 1 addition & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_original_input(simple_problem):

def test_original_input_dos():
dos_problem = montepy.read_input(os.path.join("tests", "inputs", "test_dos.imcnp"))
cell_order = [Message, Title] + [Input] * 16
cell_order = [Message, Title] + [Input] * 18
for i, input_ob in enumerate(dos_problem.original_inputs):
assert isinstance(input_ob, cell_order[i])

Expand Down
Loading