Merge pull request #8987 from bluetech/k-backslash

mark/expression: allow backslash characters in identifiers
This commit is contained in:
Ran Benita 2021-08-08 17:15:53 +03:00 committed by GitHub
commit df033f3ab1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 3 deletions

View File

@ -0,0 +1,2 @@
The test selection options ``pytest -k`` and ``pytest -m`` now support matching names containing backslash (`\\`) characters.
Backslashes are treated literally, not as escape characters (the values being matched against are already escaped).

View File

@ -6,7 +6,7 @@ expression: expr? EOF
expr: and_expr ('or' and_expr)*
and_expr: not_expr ('and' not_expr)*
not_expr: 'not' not_expr | '(' expr ')' | ident
ident: (\w|:|\+|-|\.|\[|\])+
ident: (\w|:|\+|-|\.|\[|\]|\\)+
The semantics are:
@ -88,7 +88,7 @@ class Scanner:
yield Token(TokenType.RPAREN, ")", pos)
pos += 1
else:
match = re.match(r"(:?\w|:|\+|-|\.|\[|\])+", input[pos:])
match = re.match(r"(:?\w|:|\+|-|\.|\[|\]|\\)+", input[pos:])
if match:
value = match.group(0)
if value == "or":

View File

@ -66,6 +66,20 @@ def test_syntax_oddeties(expr: str, expected: bool) -> None:
assert evaluate(expr, matcher) is expected
def test_backslash_not_treated_specially() -> None:
r"""When generating nodeids, if the source name contains special characters
like a newline, they are escaped into two characters like \n. Therefore, a
user will never need to insert a literal newline, only \n (two chars). So
mark expressions themselves do not support escaping, instead they treat
backslashes as regular identifier characters."""
matcher = {r"\nfoo\n"}.__contains__
assert evaluate(r"\nfoo\n", matcher)
assert not evaluate(r"foo", matcher)
with pytest.raises(ParseError):
evaluate("\nfoo\n", matcher)
@pytest.mark.parametrize(
("expr", "column", "message"),
(
@ -129,6 +143,7 @@ def test_syntax_errors(expr: str, column: int, message: str) -> None:
":::",
"a:::c",
"a+-b",
r"\nhe\\l\lo\n\t\rbye",
"אבגד",
"aaאבגדcc",
"a[bcd]",
@ -156,7 +171,6 @@ def test_valid_idents(ident: str) -> None:
"ident",
(
"/",
"\\",
"^",
"*",
"=",