Skip to content

Commit

Permalink
Override grammar: allow question marks in unquoted strings
Browse files Browse the repository at this point in the history
Fixes #1597
  • Loading branch information
odelalleau committed May 3, 2021
1 parent 69924a3 commit 837227a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion hydra/grammar/OverrideLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ BOOL:

NULL: [Nn][Uu][Ll][Ll];

UNQUOTED_CHAR: [/\-\\+.$%*@]; // other characters allowed in unquoted strings
UNQUOTED_CHAR: [/\-\\+.$%*@?]; // other characters allowed in unquoted strings
ID: (CHAR|'_') (CHAR|DIGIT|'_')*;
// Note: when adding more characters to the ESC rule below, also add them to
// the `_ESC` string in `_internal/grammar/utils.py`.
Expand Down
4 changes: 2 additions & 2 deletions hydra/grammar/OverrideParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ primitive:
| FLOAT // 3.14, -20.0, 1e-1, -10e3
| BOOL // true, TrUe, false, False
| INTERPOLATION // ${foo.bar}, ${oc.env:USER,me}
| UNQUOTED_CHAR // /, -, \, +, ., $, %, *, @
| UNQUOTED_CHAR // /, -, \, +, ., $, %, *, @, ?
| COLON // :
| ESC // \\, \(, \), \[, \], \{, \}, \:, \=, \ , \\t, \,
| WS // whitespaces
Expand All @@ -73,7 +73,7 @@ dictKey:
| INT // 0, 10, -20, 1_000_000
| FLOAT // 3.14, -20.0, 1e-1, -10e3
| BOOL // true, TrUe, false, False
| UNQUOTED_CHAR // /, -, \, +, ., $, %, *, @
| UNQUOTED_CHAR // /, -, \, +, ., $, %, *, @, ?
| ESC // \\, \(, \), \[, \], \{, \}, \:, \=, \ , \\t, \,
| WS // whitespaces
)+;
1 change: 1 addition & 0 deletions news/1597.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Override grammar: question marks are now allowed in unquoted strings (e.g. "key=abc?def")
22 changes: 17 additions & 5 deletions tests/test_overrides_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
)
from hydra.errors import HydraException


UNQUOTED_SPECIAL = r"/-\+.$%*@?" # special characters allowed in unquoted strings

parser = OverridesParser(create_functions())


Expand Down Expand Up @@ -285,7 +288,11 @@ def test_shuffle_sequence(value: str, expected: Any) -> None:
param("{123: 1, 0: 2, -1: 3}", {123: 1, 0: 2, -1: 3}, id="dict_int_key"),
param("{3.14: 0, 1e3: 1}", {3.14: 0, 1000.0: 1}, id="dict_float_key"),
param("{true: 1, fAlSe: 0}", {True: 1, False: 0}, id="dict_bool_key"),
param("{/-\\+.$%*@: 1}", {"/-\\+.$%*@": 1}, id="dict_unquoted_char_key"),
param(
"{%s: 1}" % UNQUOTED_SPECIAL,
{UNQUOTED_SPECIAL: 1},
id="dict_unquoted_char_key",
),
param(
"{\\\\\\(\\)\\[\\]\\{\\}\\:\\=\\ \\\t\\,: 1}",
{"\\()[]{}:= \t,": 1},
Expand Down Expand Up @@ -497,7 +504,7 @@ def test_key(value: str, expected: Any) -> None:
"value,expected",
[
param("a", "a", id="a"),
param("/:-\\+.$%*@", "/:-\\+.$%*@", id="accepted_specials"),
param(UNQUOTED_SPECIAL, UNQUOTED_SPECIAL, id="accepted_specials"),
param("abc10", "abc10", id="abc10"),
param("a.b.c", "a.b.c", id="a.b.c"),
param("list.0.bar", "list.0.bar", id="list.0.bar"),
Expand Down Expand Up @@ -945,8 +952,9 @@ def test_get_key_element(override: str, expected: str) -> None:
id="dict_quoted_key",
),
param(
"key={/-\\+.$%*@: 1}",
"{/-\\\\+.$%*@: 1}", # note that \ gets escaped
"key={%s: 1}" % UNQUOTED_SPECIAL,
# Note that \ gets escaped.
"{%s: 1}" % UNQUOTED_SPECIAL.replace("\\", "\\\\"),
True,
id="dict_unquoted_key_special",
),
Expand Down Expand Up @@ -993,7 +1001,11 @@ def test_override_get_value_element_method(
param("key={123id: 0}", {"123id": 0}, id="dict_key_int_plus_id"),
param("key={' abc ': 0}", {" abc ": 0}, id="dict_key_quoted_single"),
param('key={" abc ": 0}', {" abc ": 0}, id="dict_key_quoted_double"),
param("key={a/-\\+.$%*@: 0}", {"a/-\\+.$%*@": 0}, id="dict_key_noquote"),
param(
"key={a%s: 0}" % UNQUOTED_SPECIAL,
{f"a{UNQUOTED_SPECIAL}": 0},
id="dict_key_noquote",
),
param("key={w s: 0}", {"w s": 0}, id="dict_key_ws"),
param(
"key={\\\\\\(\\)\\[\\]\\{\\}\\:\\=\\ \\\t\\,: 0}",
Expand Down
4 changes: 2 additions & 2 deletions website/docs/advanced/override_grammar/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ primitive:
| FLOAT // 3.14, -20.0, 1e-1, -10e3
| BOOL // true, TrUe, false, False
| INTERPOLATION // ${foo.bar}, ${oc.env:USER,me}
| UNQUOTED_CHAR // /, -, \, +, ., $, %, *, @
| UNQUOTED_CHAR // /, -, \, +, ., $, %, *, @, ?
| COLON // :
| ESC // \\, \(, \), \[, \], \{, \}, \:, \=, \ , \\t, \,
| WS // whitespaces
Expand All @@ -99,7 +99,7 @@ dictKey:
| INT // 0, 10, -20, 1_000_000
| FLOAT // 3.14, -20.0, 1e-1, -10e3
| BOOL // true, TrUe, false, False
| UNQUOTED_CHAR // /, -, \, +, ., $, %, *, @
| UNQUOTED_CHAR // /, -, \, +, ., $, %, *, @, ?
| ESC // \\, \(, \), \[, \], \{, \}, \:, \=, \ , \\t, \,
| WS // whitespaces
)+;
Expand Down

0 comments on commit 837227a

Please sign in to comment.