Skip to content

Commit

Permalink
Use lalr parser for main ert config
Browse files Browse the repository at this point in the history
This is for performance reasons. May cause ambiguous config files
to no longer be parsed.
  • Loading branch information
eivindjahren committed Jan 14, 2025
1 parent 60fc8f8 commit d7800df
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/ert/config/parsing/lark_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,7 +32,7 @@
LETTER: UCASE_LETTER | LCASE_LETTER
COMMENT: "--" /[^\n]*/
COMMENT.9: "--" /[^\n]*/
%ignore COMMENT
UNQUOTED: (/[^\" \t\n]/)+
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions tests/ert/unit_tests/config/test_ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
2 changes: 1 addition & 1 deletion tests/ert/unit_tests/config/test_forward_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d7800df

Please sign in to comment.