Skip to content

Commit a05c72c

Browse files
committed
[lexical-scoping] Support root-node reference.
1 parent 7092885 commit a05c72c

File tree

5 files changed

+26
-252
lines changed

5 files changed

+26
-252
lines changed

jmespath.test

jmespath/lexer.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class Lexer(object):
1818
',': 'comma',
1919
':': 'colon',
2020
'@': 'current',
21-
'$': 'root',
2221
'(': 'lparen',
2322
')': 'rparen',
2423
'{': 'lbrace',
@@ -117,7 +116,13 @@ def tokenize(self, expression, options=None):
117116
elif self._current == '=':
118117
yield self._match_or_else('=', 'eq', 'assign')
119118
elif self._current == '$':
120-
yield self._consume_variable()
119+
if self._peek_may_be_valid_unquoted_identifier():
120+
yield self._consume_variable()
121+
else:
122+
yield {'type': 'root',
123+
'value': self._current,
124+
'start': self._position, 'end': self._position + 1}
125+
self._next()
121126
else:
122127
raise LexerError(lexer_position=self._position,
123128
lexer_value=self._current,
@@ -147,6 +152,13 @@ def _consume_variable(self):
147152
return {'type': 'variable', 'value': buff,
148153
'start': start, 'end': start + len(buff)}
149154

155+
def _peek_may_be_valid_unquoted_identifier(self):
156+
if (self._position == self._length - 1):
157+
return False
158+
else:
159+
next = self._chars[self._position + 1]
160+
return next in self.START_IDENTIFIER
161+
150162
def _peek_is_next_digit(self):
151163
if (self._position == self._length - 1):
152164
return False

tests/compliance/letexpr.json

Lines changed: 0 additions & 247 deletions
This file was deleted.

tests/test_lexer.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,17 @@ def test_root_reference(self):
210210
]
211211
)
212212

213+
def test_variable(self):
214+
tokens = list(self.lexer.tokenize('$foo'))
215+
self.assertEqual(
216+
tokens,
217+
[{'type': 'variable', 'value': '$foo',
218+
'start': 0, 'end': 4},
219+
{'type': 'eof', 'value': '',
220+
'start': 4, 'end': 4}
221+
]
222+
)
223+
213224
def test_unknown_character(self):
214225
with self.assertRaises(LexerError) as e:
215226
tokens = list(self.lexer.tokenize('foo[0^]'))

tests/test_parser.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def test_arithmetic_expressions(self):
9292
for sign in operations:
9393
operation = operations[sign]
9494
expression = 'foo {} bar'.format(sign)
95-
print(expression)
9695
self.assert_parsed_ast(
9796
expression,
9897
ast.arithmetic(
@@ -110,7 +109,6 @@ def test_arithmetic_unary(self):
110109
for sign in operations:
111110
operation = operations[sign]
112111
expression = '{} foo'.format(sign)
113-
print(expression)
114112
self.assert_parsed_ast(
115113
expression,
116114
ast.arithmetic_unary(

0 commit comments

Comments
 (0)