Skip to content

Commit 5de95df

Browse files
committed
Merge branch 'jep-12' into develop
2 parents 5c9625a + e7049a9 commit 5de95df

File tree

8 files changed

+200
-100
lines changed

8 files changed

+200
-100
lines changed

jmespath/lexer.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import warnings
23
from json import loads
34

45
from jmespath.exceptions import LexerError, EmptyExpressionError
@@ -9,6 +10,7 @@ class Lexer(object):
910
r'(?P<number>-?\d+)|'
1011
r'(?P<unquoted_identifier>([a-zA-Z_][a-zA-Z_0-9]*))|'
1112
r'(?P<quoted_identifier>("(?:\\\\|\\"|[^"])*"))|'
13+
r'(?P<string_literal>(\'(?:\\\\|\\\'|[^\'])*\'))|'
1214
r'(?P<literal>(`(?:\\\\|\\`|[^`])*`))|'
1315
r'(?P<filter>\[\?)|'
1416
r'(?P<or>\|\|)|'
@@ -93,6 +95,9 @@ def _token_quoted_identifier(self, value, start, end):
9395
lexer_value=value,
9496
message=error_message)
9597

98+
def _token_string_literal(self, value, start, end):
99+
return value[1:-1]
100+
96101
def _token_literal(self, value, start, end):
97102
actual_value = value[1:-1]
98103
actual_value = actual_value.replace('\\`', '`').lstrip()
@@ -113,7 +118,10 @@ def _token_literal(self, value, start, end):
113118
# don't have to be quoted. This is only true if the
114119
# string doesn't start with chars that could start a valid
115120
# JSON value.
116-
return loads(potential_value)
121+
value = loads(potential_value)
122+
warnings.warn("deprecated string literal syntax",
123+
PendingDeprecationWarning)
124+
return value
117125
except ValueError:
118126
raise LexerError(lexer_position=start,
119127
lexer_value=value,

jmespath/parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ def _expression(self, binding_power=0):
127127
current_token = self._current_token()
128128
return left
129129

130+
def _token_nud_string_literal(self, token):
131+
return ast.literal(token['value'])
132+
130133
def _token_nud_literal(self, token):
131134
return ast.literal(token['value'])
132135

tests/compliance/filters.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cases": [
55
{
66
"comment": "Matching a literal",
7-
"expression": "foo[?name == `a`]",
7+
"expression": "foo[?name == 'a']",
88
"result": [{"name": "a"}]
99
}
1010
]
@@ -86,7 +86,7 @@
8686
"cases": [
8787
{
8888
"comment": "Filter with subexpression",
89-
"expression": "foo[?top.name == `a`]",
89+
"expression": "foo[?top.name == 'a']",
9090
"result": [{"top": {"name": "a"}}]
9191
}
9292
]

tests/compliance/functions.json

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"error": "invalid-type"
6969
},
7070
{
71-
"expression": "avg(`abc`)",
71+
"expression": "avg('abc')",
7272
"error": "invalid-type"
7373
},
7474
{
@@ -100,23 +100,23 @@
100100
"result": -1
101101
},
102102
{
103-
"expression": "ceil(`string`)",
103+
"expression": "ceil('string')",
104104
"error": "invalid-type"
105105
},
106106
{
107-
"expression": "contains(`abc`, `a`)",
107+
"expression": "contains('abc', 'a')",
108108
"result": true
109109
},
110110
{
111-
"expression": "contains(`abc`, `d`)",
111+
"expression": "contains('abc', 'd')",
112112
"result": false
113113
},
114114
{
115-
"expression": "contains(`false`, `d`)",
115+
"expression": "contains(`false`, 'd')",
116116
"error": "invalid-type"
117117
},
118118
{
119-
"expression": "contains(strings, `a`)",
119+
"expression": "contains(strings, 'a')",
120120
"result": true
121121
},
122122
{
@@ -128,23 +128,23 @@
128128
"result": false
129129
},
130130
{
131-
"expression": "ends_with(str, `r`)",
131+
"expression": "ends_with(str, 'r')",
132132
"result": true
133133
},
134134
{
135-
"expression": "ends_with(str, `tr`)",
135+
"expression": "ends_with(str, 'tr')",
136136
"result": true
137137
},
138138
{
139-
"expression": "ends_with(str, `Str`)",
139+
"expression": "ends_with(str, 'Str')",
140140
"result": true
141141
},
142142
{
143-
"expression": "ends_with(str, `SStr`)",
143+
"expression": "ends_with(str, 'SStr')",
144144
"result": false
145145
},
146146
{
147-
"expression": "ends_with(str, `foo`)",
147+
"expression": "ends_with(str, 'foo')",
148148
"result": false
149149
},
150150
{
@@ -156,7 +156,7 @@
156156
"result": 1
157157
},
158158
{
159-
"expression": "floor(`string`)",
159+
"expression": "floor('string')",
160160
"error": "invalid-type"
161161
},
162162
{
@@ -172,11 +172,11 @@
172172
"error": "invalid-type"
173173
},
174174
{
175-
"expression": "length(`abc`)",
175+
"expression": "length('abc')",
176176
"result": 3
177177
},
178178
{
179-
"expression": "length(`\"\"`)",
179+
"expression": "length('')",
180180
"result": 0
181181
},
182182
{
@@ -288,7 +288,7 @@
288288
"result": "a"
289289
},
290290
{
291-
"expression": "type(`abc`)",
291+
"expression": "type('abc')",
292292
"result": "string"
293293
},
294294
{
@@ -352,43 +352,43 @@
352352
"error": "invalid-type"
353353
},
354354
{
355-
"expression": "join(`\", \"`, strings)",
355+
"expression": "join(', ', strings)",
356356
"result": "a, b, c"
357357
},
358358
{
359-
"expression": "join(`, `, strings)",
359+
"expression": "join(', ', strings)",
360360
"result": "a, b, c"
361361
},
362362
{
363-
"expression": "join(`,`, `[\"a\", \"b\"]`)",
363+
"expression": "join(',', `[\"a\", \"b\"]`)",
364364
"result": "a,b"
365365
},
366366
{
367-
"expression": "join(`,`, `[\"a\", 0]`)",
367+
"expression": "join(',', `[\"a\", 0]`)",
368368
"error": "invalid-type"
369369
},
370370
{
371-
"expression": "join(`, `, str)",
371+
"expression": "join(', ', str)",
372372
"error": "invalid-type"
373373
},
374374
{
375-
"expression": "join(`|`, strings)",
375+
"expression": "join('|', strings)",
376376
"result": "a|b|c"
377377
},
378378
{
379379
"expression": "join(`2`, strings)",
380380
"error": "invalid-type"
381381
},
382382
{
383-
"expression": "join(`|`, decimals)",
383+
"expression": "join('|', decimals)",
384384
"error": "invalid-type"
385385
},
386386
{
387-
"expression": "join(`|`, decimals[].to_string(@))",
387+
"expression": "join('|', decimals[].to_string(@))",
388388
"result": "1.01|1.2|-1.5"
389389
},
390390
{
391-
"expression": "join(`|`, empty_list)",
391+
"expression": "join('|', empty_list)",
392392
"result": ""
393393
},
394394
{
@@ -404,27 +404,27 @@
404404
"result": []
405405
},
406406
{
407-
"expression": "reverse(``)",
407+
"expression": "reverse('')",
408408
"result": ""
409409
},
410410
{
411-
"expression": "reverse(`hello world`)",
411+
"expression": "reverse('hello world')",
412412
"result": "dlrow olleh"
413413
},
414414
{
415-
"expression": "starts_with(str, `S`)",
415+
"expression": "starts_with(str, 'S')",
416416
"result": true
417417
},
418418
{
419-
"expression": "starts_with(str, `St`)",
419+
"expression": "starts_with(str, 'St')",
420420
"result": true
421421
},
422422
{
423-
"expression": "starts_with(str, `Str`)",
423+
"expression": "starts_with(str, 'Str')",
424424
"result": true
425425
},
426426
{
427-
"expression": "starts_with(str, `String`)",
427+
"expression": "starts_with(str, 'String')",
428428
"result": false
429429
},
430430
{
@@ -452,7 +452,7 @@
452452
"result": 0
453453
},
454454
{
455-
"expression": "to_array(`foo`)",
455+
"expression": "to_array('foo')",
456456
"result": ["foo"]
457457
},
458458
{
@@ -472,7 +472,7 @@
472472
"result": [false]
473473
},
474474
{
475-
"expression": "to_string(`foo`)",
475+
"expression": "to_string('foo')",
476476
"result": "foo"
477477
},
478478
{
@@ -484,19 +484,19 @@
484484
"result": "[0,1]"
485485
},
486486
{
487-
"expression": "to_number(`\"1.0\"`)",
487+
"expression": "to_number('1.0')",
488488
"result": 1.0
489489
},
490490
{
491-
"expression": "to_number(`\"1.1\"`)",
491+
"expression": "to_number('1.1')",
492492
"result": 1.1
493493
},
494494
{
495-
"expression": "to_number(`\"4\"`)",
495+
"expression": "to_number('4')",
496496
"result": 4
497497
},
498498
{
499-
"expression": "to_number(`\"notanumber\"`)",
499+
"expression": "to_number('notanumber')",
500500
"result": null
501501
},
502502
{

0 commit comments

Comments
 (0)