Skip to content

Commit

Permalink
Add grammar regex test for invalid escaped brace
Browse files Browse the repository at this point in the history
This new test is meant to answer the question "why doesn't `_arg`
include the backslash character while it is supported in the
`UNQUOTED_CHAR` parser rule?"

The answer to that question is that if we included the backslash
character, then this new test would fail because the
`SIMPLE_INTERPOLATION_PATTERN` regex would believe that `${foo:\}` is a
valid interpolation syntax, while it is not.

There is currently no other test that would fail if the backslash
character was added to `_arg`, motivating the need for such a test.

NB: this commit does *not* modify `_arg`, it just rewrites it in raw
string format to make it more readable.
  • Loading branch information
odelalleau authored and pixelb committed Nov 13, 2021
1 parent 243712c commit 87bd60a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion omegaconf/grammar_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
_node_inter = f"\\${{\\s*{_node_path}\\s*}}" # node interpolation ${foo.bar}
_id = "[a-zA-Z_]\\w*" # foo, foo_bar, abc123
_resolver_name = f"({_id}(\\.{_id})*)?" # foo, ns.bar3, ns_1.ns_2.b0z
_arg = "[a-zA-Z_0-9/\\-\\+.$%*@?|]+" # string representing a resolver argument
_arg = r"[a-zA-Z_0-9/\-\+.$%*@?|]+" # string representing a resolver argument
_args = f"{_arg}(\\s*,\\s*{_arg})*" # list of resolver arguments
_resolver_inter = f"\\${{\\s*{_resolver_name}\\s*:\\s*{_args}?\\s*}}" # ${foo:bar}
_inter = f"({_node_inter}|{_resolver_inter})" # any kind of interpolation
Expand Down
1 change: 1 addition & 0 deletions tests/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ def test_grammar_consistency(self, expression: str) -> None:
("${ns . f:var}", False),
("${$foo:bar}", False),
("${.foo:bar}", False),
(r"${foo:\}", False),
# Valid according to the grammar but not matched by the regex.
("${foo.${bar}}", True),
("${foo:${bar}}", True),
Expand Down

0 comments on commit 87bd60a

Please sign in to comment.