diff --git a/mathics/eval/files_io/files.py b/mathics/eval/files_io/files.py index 03fb2d6c50..2aca568740 100644 --- a/mathics/eval/files_io/files.py +++ b/mathics/eval/files_io/files.py @@ -206,12 +206,17 @@ def eval_Get( with Mathics3Open(resolved_path, "r", encoding=encoding) as f: feeder = MathicsFileLineFeeder(f, trace_fn) while not feeder.empty(): + lineno = feeder.lineno try: # Note: we use mathics.core.parser.parse # so that tracing/debugging can intercept parse() query = mathics.core.parser.parse(definitions, feeder) except SyntaxError: - return SymbolNull + # As in WMA, report the error and go on reading the + # rest of the file, unless no input was consumed. + if feeder.lineno == lineno: + return SymbolNull + continue finally: feeder.send_messages(evaluation) if query is None: # blank line / comment diff --git a/test/builtin/files_io/test_files.py b/test/builtin/files_io/test_files.py index 25eb9a5660..74cecc68d2 100644 --- a/test/builtin/files_io/test_files.py +++ b/test/builtin/files_io/test_files.py @@ -67,6 +67,18 @@ def test_get_evaluates_queries_after_error(): check_evaluation(f'Get["{script_path}"]', "OK!", hold_expected=True) +def test_get_continues_after_syntax_error(): + # As in WMA, Get reports a syntax error and goes on reading the file. + script_path = canonic_filename( + osp.normpath( + osp.join(osp.dirname(__file__), "..", "..", "data", "syntax-error-bug.wl") + ) + ) + # The syntax message itself is not checked here: its text comes from + # Mathics3-scanner. + check_evaluation(f'Get["{script_path}"]', "{1, getSyntaxB, 3}") + + @pytest.mark.skipif( sys.platform in ("win32",), reason="$Path does not work on Windows?" ) diff --git a/test/data/syntax-error-bug.wl b/test/data/syntax-error-bug.wl new file mode 100644 index 0000000000..8b1c055924 --- /dev/null +++ b/test/data/syntax-error-bug.wl @@ -0,0 +1,5 @@ +(* Get must go on reading the file after a syntax error, as in WMA. *) +getSyntaxA = 1; +getSyntaxB = \[NoSuchCharacterName]; +getSyntaxC = 3; +{getSyntaxA, getSyntaxB, getSyntaxC}