Skip to content

Commit

Permalink
Override grammar: allow question marks in unquoted strings (facebookr…
Browse files Browse the repository at this point in the history
…esearch#1598)

Override grammar: allow question marks in unquoted strings

Fixes facebookresearch#1597
  • Loading branch information
odelalleau authored May 4, 2021
1 parent c6397d2 commit 713d5ea
Show file tree
Hide file tree
Showing 5 changed files with 22 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 @@ -72,7 +72,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")
21 changes: 16 additions & 5 deletions tests/test_overrides_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
)
from hydra.errors import HydraException

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

parser = OverridesParser(create_functions())


Expand Down Expand Up @@ -277,7 +279,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 @@ -513,7 +519,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 @@ -955,8 +961,9 @@ def test_get_key_element(override: str, expected: str) -> None:
param("key={a:10,b:20}", "{a: 10, b: 20}", True, id="dict"),
param("key={a:10,b:[1,2,3]}", "{a: 10, b: [1, 2, 3]}", True, id="dict"),
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 @@ -1001,7 +1008,11 @@ def test_override_get_value_element_method(
param("key={a:10,b:20}", {"a": 10, "b": 20}, id="dict"),
param("key={a:10,b:[1,2,3]}", {"a": 10, "b": [1, 2, 3]}, id="dict"),
param("key={123id: 0}", {"123id": 0}, id="dict_key_int_plus_id"),
param("key={a/-\\+.$%*@: 0}", {"a/-\\+.$%*@": 0}, id="dict_key_noquote"),
param(
"key={%s: 0}" % UNQUOTED_SPECIAL,
{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 713d5ea

Please sign in to comment.