From b04a7b2e84caf68eb4b1b3e47d433e3475d22534 Mon Sep 17 00:00:00 2001
From: Eivind Jahren <ejah@equinor.com>
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 +++++++----------
 .../ert/unit_tests/config/test_forward_model.py |  2 +-
 2 files changed, 8 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_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: <IENS>"):
         _ = ErtConfig.from_file_contents(
             """
             NUM_REALIZATIONS  1