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

Fixed Parser bug with Source Definition #431

Merged
merged 32 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
292dcef
Made test case to replicate #396.
MicahGale Jul 2, 2024
928562d
Made simpler mechanism to swap parsers without making blank classes.
MicahGale Jul 2, 2024
44092c6
Started an sdef style parser.
MicahGale Jul 2, 2024
235edf4
Made a useful debug tool.
MicahGale Jul 2, 2024
3e11f99
Made parser more reliable and forbid partial success.
MicahGale Jul 2, 2024
8e04d25
Added doc strings.
MicahGale Jul 2, 2024
caf4356
Added sdef keywords.
MicahGale Jul 2, 2024
8f17a7b
Removed tally special classes thanks to machinery.
MicahGale Jul 2, 2024
1bd79b9
Cleaned up a few issues.
MicahGale Jul 2, 2024
2c3c3cb
Made progress on making an sdef parser.
MicahGale Jul 2, 2024
b6b417e
Added sdef bug to changelog.
MicahGale Jul 2, 2024
6963bb2
Switched to montepy.org email.
MicahGale Jul 8, 2024
2b0b20c
Fixed syntax error.
MicahGale Jul 8, 2024
3356e9b
Removed tallies from documentation site.
MicahGale Jul 8, 2024
908c907
Merge branch 'develop' into 396-silent-parsing-error-with-sdef
MicahGale Jul 8, 2024
355a8d7
Fixed sdef parser by making it its mostly own thing.
MicahGale Jul 8, 2024
1c7c19f
Made state by state bebugger.
MicahGale Jul 8, 2024
873456e
Tried to make ListNode update no padding if values are rammed together.
MicahGale Jul 8, 2024
33098e1
Add never_pad test to list_node format.
MicahGale Jul 9, 2024
cf56a48
Documented final step of deploy process.
MicahGale Jul 9, 2024
8d35edb
Created system to only flag never_pad during parsing.
MicahGale Jul 9, 2024
4be7cf1
Merge branch 'develop' into 396-silent-parsing-error-with-sdef
MicahGale Jul 9, 2024
9947726
Merge branch 'develop' into 396-silent-parsing-error-with-sdef
MicahGale Jul 9, 2024
b6d3598
Tried new logic for clearing never_pad. Failed.
MicahGale Jul 9, 2024
64052ae
Revert "Tried new logic for clearing never_pad. Failed."
MicahGale Jul 10, 2024
dfc762e
Don't label jump as never pad as the logic is handled elsewhere.
MicahGale Jul 10, 2024
3ae1bfa
relaxed tests with trailing spaces.
MicahGale Jul 10, 2024
ce11882
Removed crufty infinite recursion line.
MicahGale Jul 10, 2024
1c99dc8
Merge branch 'develop' into 396-silent-parsing-error-with-sdef
MicahGale Jul 15, 2024
dacd639
Merge branch 'develop' into 396-silent-parsing-error-with-sdef
MicahGale Jul 15, 2024
2008997
Added test for has space.
MicahGale Jul 10, 2024
0d27e03
correct next version release number.
MicahGale Jul 17, 2024
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
2 changes: 0 additions & 2 deletions doc/source/api/montepy.data_inputs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ montepy.data\_inputs package
montepy.data_inputs.material_component
montepy.data_inputs.mode
montepy.data_inputs.thermal_scattering
montepy.data_inputs.tally
montepy.data_inputs.tally_multiplier
montepy.data_inputs.transform
montepy.data_inputs.universe_input
montepy.data_inputs.volume
9 changes: 0 additions & 9 deletions doc/source/api/montepy.data_inputs.tally.rst

This file was deleted.

9 changes: 0 additions & 9 deletions doc/source/api/montepy.data_inputs.tally_multiplier.rst

This file was deleted.

7 changes: 6 additions & 1 deletion doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ MontePy Changelog
=================

#Next Version#
---------------------
-------------------

**Features Added**

* ``overwrite`` argument added to `MCNP_Problem.write_to_file` to ensure files are only overwritten if the user really wants to do so (:pull:`443`).

**Bug fixes**

* Fixed bug with ``SDEF`` input, and made parser more robust (:issue:`396`).


0.2.10
----------------------

Expand Down
2 changes: 1 addition & 1 deletion doc/source/developing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ For a deployment you need to:
#. Run the deploy script : ``.github/scripts/deploy.sh``
#. Manually merge onto main without creating a new commit.
This is necessary because there's no way to do a github PR that will not create a new commit, which will break setuptools_scm.

#. Update the release notes on the draft release, and finalize it on GitHub.

Package Structure
-----------------
Expand Down
8 changes: 4 additions & 4 deletions montepy/data_inputs/cell_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ def format_for_mcnp_input(self, mcnp_version):
C = is_worth_pring

Logic:
A!BC + !ABC
C *(A!B + !AB)
C * (A xor B)
C * (A != B)
1. A!BC + !ABC
2. C *(A!B + !AB)
3. C * (A xor B)
4. C * (A != B)
"""
# print in either block
if (self.in_cell_block != print_in_data_block) and self._is_worth_printing:
Expand Down
33 changes: 32 additions & 1 deletion montepy/data_inputs/data_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import montepy
from montepy.errors import *
from montepy.input_parser.data_parser import ClassifierParser, DataParser
from montepy.input_parser.data_parser import (
ClassifierParser,
DataParser,
ParamOnlyDataParser,
)
from montepy.input_parser.mcnp_input import Input
from montepy.particle import Particle
from montepy.mcnp_object import MCNP_Object
Expand Down Expand Up @@ -322,8 +326,17 @@ class DataInput(DataInputAbstract):

:param input: the Input object representing this data input
:type input: Input
:param fast_parse: Whether or not to only parse the first word for the type of data.
:type fast_parse: bool
:param prefix: The input prefix found during parsing (internal use only)
:type prefix: str
"""

def __init__(self, input=None, fast_parse=False, prefix=None):
if prefix:
self._load_correct_parser(prefix)
super().__init__(input, fast_parse)

@property
def _class_prefix(self):
return None
Expand All @@ -335,3 +348,21 @@ def _has_number(self): # pragma: no cover
@property
def _has_classifier(self): # pragma: no cover
return None

def _load_correct_parser(self, prefix):
"""
Decides if a specialized parser needs to be loaded for barebone
special cases.

.. versionadded:: 0.2.11
MicahGale marked this conversation as resolved.
Show resolved Hide resolved
"""
PARAM_PARSER = ParamOnlyDataParser
TALLY_PARSER = montepy.input_parser.tally_parser.TallyParser
PARSER_PREFIX_MAP = {
"f": TALLY_PARSER,
"fm": TALLY_PARSER,
"fs": montepy.input_parser.tally_seg_parser.TallySegmentParser,
"sdef": PARAM_PARSER,
}
if prefix.lower() in PARSER_PREFIX_MAP:
self._parser = PARSER_PREFIX_MAP[prefix.lower()]()
9 changes: 1 addition & 8 deletions montepy/data_inputs/data_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
lattice_input,
material,
mode,
tally,
tally_segment,
tally_multiplier,
thermal_scattering,
universe_input,
volume,
Expand All @@ -22,9 +19,6 @@
lattice_input.LatticeInput,
material.Material,
mode.Mode,
tally.Tally,
tally_multiplier.TallyMultiplier,
tally_segment.TallySegment,
thermal_scattering.ThermalScatteringLaw,
transform.Transform,
volume.Volume,
Expand All @@ -47,8 +41,7 @@ def parse_data(input):

base_input = data_input.DataInput(input, fast_parse=True)
prefix = base_input.prefix

for data_class in PREFIX_MATCHES:
if prefix == data_class._class_prefix():
return data_class(input)
return data_input.DataInput(input)
return data_input.DataInput(input, prefix=prefix)
22 changes: 0 additions & 22 deletions montepy/data_inputs/tally.py

This file was deleted.

19 changes: 0 additions & 19 deletions montepy/data_inputs/tally_multiplier.py

This file was deleted.

22 changes: 0 additions & 22 deletions montepy/data_inputs/tally_segment.py

This file was deleted.

2 changes: 2 additions & 0 deletions montepy/input_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
from . import read_parser
from . import shortcuts
from . import surface_parser
from . import tally_parser
from . import tally_seg_parser
from . import tokens
114 changes: 109 additions & 5 deletions montepy/input_parser/data_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ def zaid_phrase(self, p):
def particle_sequence(self, p):
if len(p) == 1:
sequence = syntax_node.ListNode("particle sequence")
sequence.append(p[0])
sequence.append(p[0], True)
else:
sequence = p[0]
sequence.append(p[1])
sequence.append(p[1], True)
return sequence

@_("PARTICLE", "SURFACE_TYPE", "PARTICLE_SPECIAL")
Expand All @@ -114,18 +114,18 @@ def text_phrase(self, p):
def text_sequence(self, p):
if len(p) == 1:
sequence = syntax_node.ListNode("text sequence")
sequence.append(p[0])
sequence.append(p[0], True)
else:
sequence = p[0]
sequence.append(p[1])
sequence.append(p[1], True)
return sequence

@_("kitchen_junk", "kitchen_sink kitchen_junk")
def kitchen_sink(self, p):
sequence = p[0]
if len(p) != 1:
for node in p[1].nodes:
sequence.append(node)
sequence.append(node, True)
return sequence

@_("number_sequence", "text_sequence", "particle_sequence")
Expand Down Expand Up @@ -166,3 +166,107 @@ def data_classifier(self, p):
return syntax_node.SyntaxNode(
"data input classifier", {"start_pad": padding, "classifier": p.classifier}
)


class ParamOnlyDataParser(DataParser):
"""
A parser for parsing parameter (key-value pair) only data inputs.

.e.g., SDEF

.. versionadded:: 0.2.11
MicahGale marked this conversation as resolved.
Show resolved Hide resolved

:returns: a syntax tree for the data input.
:rtype: SyntaxNode
"""

debugfile = None

@_(
"param_introduction spec_parameters",
)
def param_data_input(self, p):
ret = {}
for key, node in p.param_introduction.nodes.items():
ret[key] = node
if hasattr(p, "spec_parameters"):
ret["parameters"] = p.spec_parameters
return syntax_node.SyntaxNode("data", ret)

@_(
"classifier_phrase",
"padding classifier_phrase",
)
def param_introduction(self, p):
ret = {}
if isinstance(p[0], syntax_node.PaddingNode):
ret["start_pad"] = p[0]
else:
ret["start_pad"] = syntax_node.PaddingNode()
ret["classifier"] = p.classifier_phrase
ret["keyword"] = syntax_node.ValueNode(None, str, padding=None)
return syntax_node.SyntaxNode("data intro", ret)

@_("spec_parameter", "spec_parameters spec_parameter")
def spec_parameters(self, p):
"""
A list of the parameters (key, value pairs) for this input.

:returns: all parameters
:rtype: ParametersNode
"""
if len(p) == 1:
params = syntax_node.ParametersNode()
param = p[0]
else:
params = p[0]
param = p[1]
params.append(param)
MicahGale marked this conversation as resolved.
Show resolved Hide resolved
return params

@_("spec_classifier param_seperator data")
def spec_parameter(self, p):
return syntax_node.SyntaxNode(
p.spec_classifier.prefix.value,
{
"classifier": p.spec_classifier,
"seperator": p.param_seperator,
"data": p.data,
},
)

@_(
"KEYWORD",
)
def spec_data_prefix(self, p):
return syntax_node.ValueNode(p[0], str)

@_(
"modifier spec_data_prefix",
"spec_data_prefix",
"spec_classifier NUMBER",
"spec_classifier particle_type",
)
def spec_classifier(self, p):
"""
The classifier of a data input.

This represents the first word of the data input.
E.g.: ``M4``, `IMP:N`, ``F104:p``

:rtype: ClassifierNode
"""
if hasattr(p, "spec_classifier"):
classifier = p.spec_classifier
else:
classifier = syntax_node.ClassifierNode()

if hasattr(p, "modifier"):
classifier.modifier = syntax_node.ValueNode(p.modifier, str)
if hasattr(p, "spec_data_prefix"):
classifier.prefix = p.spec_data_prefix
if hasattr(p, "NUMBER"):
classifier.number = syntax_node.ValueNode(p.NUMBER, int)
if hasattr(p, "particle_type"):
classifier.particles = p.particle_type
return classifier
Loading
Loading