Skip to content

Commit

Permalink
Merge pull request #645 from idaholab/parser_fixes
Browse files Browse the repository at this point in the history
Fix various parsing errors
  • Loading branch information
MicahGale authored Jan 29, 2025
2 parents bce95f6 + 640db53 commit ce3bb20
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 50 deletions.
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
124 changes: 80 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,91 @@ class DataLexer(ParticleLexer):
ZAID,
}

_KEYWORDS = set(MCNP_Lexer._KEYWORDS) | {
# ssw
"sym",
"pty",
"cel",
# ssr
"old",
"new",
"col",
"wgt",
"tr",
"psc",
"poa",
"bcw",
# 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

0 comments on commit ce3bb20

Please sign in to comment.