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

Use lalr parser for main ert config #9735

Merged
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
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
Loading