Skip to content

Commit 5ec2dc5

Browse files
committed
[parser] Supports root node.
1 parent 5a87847 commit 5ec2dc5

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

jmespath/ast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ def current_node():
1010
return {'type': 'current', 'children': []}
1111

1212

13+
def root_node():
14+
return {'type': 'root', 'children': []}
15+
16+
1317
def expref(expression):
1418
return {'type': 'expref', 'children': [expression]}
1519

jmespath/parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Parser(object):
4646
'rbrace': 0,
4747
'number': 0,
4848
'current': 0,
49+
'root': 0,
4950
'expref': 0,
5051
'colon': 0,
5152
'pipe': 1,
@@ -239,6 +240,9 @@ def _parse_slice_expression(self):
239240
def _token_nud_current(self, token):
240241
return ast.current_node()
241242

243+
def _token_nud_root(self, token):
244+
return ast.root_node()
245+
242246
def _token_nud_expref(self, token):
243247
expression = self._expression(self.BINDING_POWER['expref'])
244248
return ast.expref(expression)

tests/test_parser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ def test_function_call_with_and_statement(self):
106106
'type': 'function_expression',
107107
'value': 'f'})
108108

109+
def test_root_node(self):
110+
self.assert_parsed_ast(
111+
'$[0]',
112+
{
113+
'type': 'index_expression',
114+
'children': [
115+
{'type': 'root', 'children': []},
116+
{'type': 'index', 'value': 0, 'children': []}
117+
]
118+
})
119+
109120

110121
class TestErrorMessages(unittest.TestCase):
111122

0 commit comments

Comments
 (0)