Skip to content

Commit

Permalink
Add support for @ character in key names found in interpolations
Browse files Browse the repository at this point in the history
  • Loading branch information
odelalleau committed Dec 7, 2020
1 parent af3870d commit 3af2f85
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions omegaconf/grammar/OmegaConfGrammarLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,8 @@ INTER_CLOSE: '}' -> popMode;

DOT: '.';
INTER_ID: ID -> type(ID);
// A special type of `ID` that contains the "@" character. It has to be a different
// lexer token because resolver names must not contain this character.
ID_WITH_AT: (ID|'@')+;
LIST_INDEX: INT_UNSIGNED;
INTER_WS: WS -> skip;
2 changes: 1 addition & 1 deletion omegaconf/grammar/OmegaConfGrammarParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sequence: element (COMMA element)*;
interpolation: interpolationNode | interpolationResolver;
interpolationNode: INTER_OPEN DOT* configKey (DOT configKey)* INTER_CLOSE;
interpolationResolver: INTER_OPEN (interpolation | ID) COLON sequence? BRACE_CLOSE;
configKey: interpolation | ID | LIST_INDEX;
configKey: interpolation | ID | ID_WITH_AT | LIST_INDEX;

// Primitive types.

Expand Down
2 changes: 1 addition & 1 deletion omegaconf/grammar_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def defaultResult(self) -> List[Any]:
def visitConfigKey(self, ctx: OmegaConfGrammarParser.ConfigKeyContext) -> str:
from ._utils import _get_value

# interpolation | ID | LIST_INDEX
# interpolation | ID | ID_WITH_AT | LIST_INDEX
assert ctx.getChildCount() == 1
child = ctx.getChild(0)
if isinstance(child, OmegaConfGrammarParser.InterpolationContext):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ def _maybe_create(definition: str) -> Any:
("None", {"True": 1}, ...), # used to test keys with null-like names
("0", 42, ...), # used to test keys with int names
("1", {"2": 1337}, ...), # used to test dot-path with int keys
("x@y", 123, ...),
# Special keywords.
("null", "${test:null}", None),
("true", "${test:TrUe}", True),
Expand Down Expand Up @@ -706,6 +707,7 @@ def _maybe_create(definition: str) -> Any:
("null_like_key_quoted_1", "${'None'.'True'}", GrammarParseError),
("null_like_key_quoted_2", "${'None.True'}", GrammarParseError),
("dotpath_bad_type", "${prim_dict.${float}}", GrammarParseError),
("at_in_key", "${x@y}", 123),
# Resolver interpolations.
("no_args", "${test:}", []),
("space_in_args", "${test:a, b c}", ["a", "b c"]),
Expand All @@ -716,6 +718,7 @@ def _maybe_create(definition: str) -> Any:
("dict_list_as_key", "${test:{[0]: 1}}", GrammarParseError),
("missing_resolver", "${MiSsInG_ReSoLvEr:0}", UnsupportedInterpolationType),
("non_str_resolver", "${${bool}:}", GrammarParseError),
("at_in_resolver", "${y@z:}", GrammarParseError),
# Env resolver (limited: more tests in `test_env_values_are_typed()`).
("env_int", "${env:OMEGACONF_TEST_ENV_INT}", 123),
("env_missing_str", "${env:OMEGACONF_TEST_MISSING,miss}", "miss"),
Expand Down

0 comments on commit 3af2f85

Please sign in to comment.