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

Fixing parser error with read card #372

Merged
merged 17 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 1 addition & 14 deletions montepy/input_parser/input_syntax_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from montepy.input_parser.input_file import MCNP_InputFile
from montepy.input_parser.mcnp_input import Input, Message, ReadInput, Title
from montepy.input_parser.read_parser import ReadParser
from montepy.utilities import is_comment
import os
import warnings

Expand Down Expand Up @@ -88,20 +89,6 @@ def read_front_matters(fh, mcnp_version):
break


def is_comment(line):
""""""
upper_start = line[0 : BLANK_SPACE_CONTINUE + 1].upper()
non_blank_comment = upper_start and line.lstrip().upper().startswith("C ")
if non_blank_comment:
return True
blank_comment = (
"C\n" == upper_start.lstrip()
or "C\r\n" == upper_start.lstrip()
or ("C" == upper_start and "\n" not in line)
)
return blank_comment or non_blank_comment


def read_data(fh, mcnp_version, block_type=None, recursion=False):
"""
Reads the bulk of an MCNP file for all of the MCNP data.
Expand Down
22 changes: 17 additions & 5 deletions montepy/input_parser/mcnp_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,28 @@ class ReadInput(Input):

def __init__(self, input_lines, block_type, input_file=None, lineno=None):
super().__init__(input_lines, block_type, input_file, lineno)
if not self.is_read_input(input_lines):
raise ValueError("Not a valid Read Input")
parse_result = self._parser.parse(self.tokenize(), self)
first_word = input_lines[0].split()[0].lower()
if not parse_result:
if first_word != "read":
raise ValueError("Not a valid Read Input")
else:
raise ParsingError(self, "", self._parser.log.clear_queue())
raise ParsingError(self, "", self._parser.log.clear_queue())
self._tree = parse_result
self._parameters = self._tree["parameters"]

@staticmethod
def is_read_input(input_lines):
first_non_comment = ""
for line in input_lines:
if not is_comment(line):
first_non_comment = line
break
words = first_non_comment.split()
if len(words) > 0:
first_word = words[0].lower()
return first_word == "read"
# this is a fall through catch that only happens for a blank input
return False # pragma: no cover

@property
def file_name(self):
"""
Expand Down
1 change: 1 addition & 0 deletions montepy/input_parser/parser_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ def file_name(self, p):
"SURFACE_TYPE",
"THERMAL_LAW",
"ZAID",
"NUMBER_WORD",
)
def file_atom(self, p):
return p[0]
Expand Down
20 changes: 20 additions & 0 deletions montepy/utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
from montepy.constants import BLANK_SPACE_CONTINUE
import functools
import re

Expand Down Expand Up @@ -31,6 +32,25 @@ def fortran_float(number_string):
raise ValueError(f"Value Not parsable as float: {number_string}") from e


def is_comment(line):
"""
Determines if the line is a ``C comment`` style comment.

:param line: the line to analyze
:type line: str
:returns: True if the line is a comment
:rtype: bool
"""
upper_start = line[0 : BLANK_SPACE_CONTINUE + 1].upper()
non_blank_comment = upper_start and line.lstrip().upper().startswith("C ")
if non_blank_comment:
return True
blank_comment = ("C" == upper_start.strip() and "\n" in line) or (
"C" == upper_start and "\n" not in line
)
return blank_comment


def make_prop_val_node(
hidden_param, types=None, base_type=None, validator=None, deletable=False
):
Expand Down
1 change: 1 addition & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"testReadTarget.imcnp",
"bad_encoding.imcnp",
"unicode.imcnp",
"file2read.imcnp",
}

BAD_ENCODING_FILES = {
Expand Down
2 changes: 2 additions & 0 deletions tests/inputs/file2read.imcnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 0 -1
c
7 changes: 7 additions & 0 deletions tests/inputs/readEdgeCase.imcnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Testing read card
C this is a comment
read file=file2read.imcnp

1 so 0.5

mode n
5 changes: 5 additions & 0 deletions tests/test_edge_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,8 @@ def test_geometry_comments(self):
# this step caused an error for #163
cell.comments
cell._tree.format()

def test_bad_read(self):
problem = montepy.read_input(
os.path.join("tests", "inputs", "readEdgeCase.imcnp")
)
4 changes: 4 additions & 0 deletions tests/test_syntax_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,10 @@ def testReadCardConfusions(self):
input = ReadInput([f"Read FILE={file}"], BlockType.CELL)
self.assertEqual(input.file_name, file)

def testReadCardBadSyntax(self):
with self.assertRaises(ParsingError):
card = ReadInput(["Read 1"], BlockType.CELL)

def testTitleFinder(self):
test_title = "Richard Stallman writes GNU"
test_string = f"""{test_title}
Expand Down
Loading