From d7800df1682e16e540c88762802db910b2bbf38f Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Tue, 14 Jan 2025 12:57:37 +0100 Subject: [PATCH] Use lalr parser for main ert config This is for performance reasons. May cause ambiguous config files to no longer be parsed. --- src/ert/config/parsing/lark_parser.py | 17 +++++++---------- tests/ert/unit_tests/config/test_ert_config.py | 10 ++++++++++ .../ert/unit_tests/config/test_forward_model.py | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/ert/config/parsing/lark_parser.py b/src/ert/config/parsing/lark_parser.py index 3ad2fac8f4c..8f709e2b896 100644 --- a/src/ert/config/parsing/lark_parser.py +++ b/src/ert/config/parsing/lark_parser.py @@ -4,7 +4,7 @@ import os.path from typing import Self -from lark import Discard, Lark, Token, Transformer, Tree, UnexpectedCharacters +from lark import Discard, Lark, Token, Transformer, Tree, UnexpectedToken from .config_dict import ConfigDict from .config_errors import ConfigValidationError, ConfigWarning @@ -32,7 +32,7 @@ LETTER: UCASE_LETTER | LCASE_LETTER -COMMENT: "--" /[^\n]*/ +COMMENT.9: "--" /[^\n]*/ %ignore COMMENT UNQUOTED: (/[^\" \t\n]/)+ @@ -128,7 +128,7 @@ def NEWLINE(_token): return Discard -_parser = Lark(grammar, propagate_positions=True) +_parser = Lark(grammar, propagate_positions=True, parser="lalr") def _substitute_token( @@ -418,13 +418,10 @@ def _parse_contents(content: str, file: str) -> Tree[Instruction]: * ArgumentToStringTransformer() * InstructionTransformer() ).transform(tree) - except UnexpectedCharacters as e: - unexpected_char = e.char - allowed_chars = e.allowed - message = ( - f"Did not expect character: {unexpected_char}. " - f"Expected one of {allowed_chars}" - ) + except UnexpectedToken as e: + unexpected_token = e.token + allowed = e.expected + message = f"Did not expect token: {unexpected_token}. Expected one of {allowed}" raise ConfigValidationError.from_info( ErrorInfo( message=message, diff --git a/tests/ert/unit_tests/config/test_ert_config.py b/tests/ert/unit_tests/config/test_ert_config.py index 0c88a8f1e94..8bd4513cfea 100644 --- a/tests/ert/unit_tests/config/test_ert_config.py +++ b/tests/ert/unit_tests/config/test_ert_config.py @@ -402,6 +402,16 @@ def test_that_double_comments_are_handled(): assert ert_config.model_config.jobname_format_string == "&SUM$VAR@12@#£¤<" +def test_that_strings_escape_comments(): + ert_config = ErtConfig.from_file_contents( + """ + NUM_REALIZATIONS 1 + JOBNAME " -- " + """ + ) + assert ert_config.model_config.jobname_format_string == " -- " + + @pytest.mark.filterwarnings("ignore:.*Unknown keyword.*:ert.config.ConfigWarning") def test_bad_user_config_file_error_message(): with pytest.raises(ConfigValidationError, match="NUM_REALIZATIONS must be set"): diff --git a/tests/ert/unit_tests/config/test_forward_model.py b/tests/ert/unit_tests/config/test_forward_model.py index a76e54f7808..fe04b0ac5ab 100644 --- a/tests/ert/unit_tests/config/test_forward_model.py +++ b/tests/ert/unit_tests/config/test_forward_model.py @@ -414,7 +414,7 @@ def test_that_quotations_in_forward_model_arglist_are_handled_correctly(): def test_that_positional_forward_model_args_gives_config_validation_error(): - with pytest.raises(ConfigValidationError, match="Did not expect character: <"): + with pytest.raises(ConfigValidationError, match="Did not expect token: "): _ = ErtConfig.from_file_contents( """ NUM_REALIZATIONS 1