Skip to content

Commit e9eaf89

Browse files
committed
Move key_pattern to Lexer.
1 parent f83bdcb commit e9eaf89

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

‎jsonpath/env.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,12 @@ class JSONPathEnvironment:
4343
union_token = "|"
4444
filter_context_token = "#"
4545

46-
key_pattern = r"[\u0080-\uFFFFa-zA-Z_][\u0080-\uFFFFa-zA-Z0-9_-]*"
47-
4846
lexer_class = Lexer
4947
parser_class = Parser
5048

5149
def __init__(self) -> None:
5250
self.lexer = self.lexer_class(env=self)
5351
self.parser = self.parser_class(env=self)
54-
self.re_key = re.compile(self.key_pattern)
5552

5653
def compile(self, path: str) -> Union[JSONPath, CompoundJSONPath]:
5754
"""Prepare an internal representation of a JSONPath string."""

‎jsonpath/lex.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,18 @@ class Lexer:
7676
def __init__(self, *, env: JSONPathEnvironment) -> None:
7777
self.env = env
7878

79+
self.key_pattern = r"[\u0080-\uFFFFa-zA-Z_][\u0080-\uFFFFa-zA-Z0-9_-]*"
80+
7981
# 'thing' or "thing" in the right hand side of a filter expression or in a list
8082
# self.string_pattern = r"(?P<G_QUOTE>[\"'])(?P<G_QUOTED>.*?)(?P=G_QUOTE)"
8183
self.double_quote_pattern = r'"(?P<G_DQUOTE>(?:(?!(?<!\\)").)*)"'
8284
self.single_quote_pattern = r"'(?P<G_SQUOTE>(?:(?!(?<!\\)').)*)'"
8385

8486
# .thing
85-
self.dot_property_pattern = rf"\.(?P<G_PROP>{env.key_pattern})"
87+
self.dot_property_pattern = rf"\.(?P<G_PROP>{self.key_pattern})"
8688

8789
# [thing]
88-
self.bracketed_property_pattern = rf"\[\s*(?P<G_BPROP>{env.key_pattern})\s*]"
90+
self.bracketed_property_pattern = rf"\[\s*(?P<G_BPROP>{self.key_pattern})\s*]"
8991

9092
# .1
9193
# NOTE: `.1` can be a dot property where the key is "1".
@@ -171,7 +173,7 @@ def compile_rules(self) -> Pattern[str]:
171173
(TOKEN_LT, r"<"),
172174
(TOKEN_GT, r">"),
173175
(TOKEN_NOT, self.logical_not_pattern),
174-
(TOKEN_BARE_PROPERTY, self.env.key_pattern),
176+
(TOKEN_BARE_PROPERTY, self.key_pattern),
175177
(TOKEN_LPAREN, r"\("),
176178
(TOKEN_RPAREN, r"\)"),
177179
(TOKEN_SKIP, r"[ \n\t\r\.]+"),

0 commit comments

Comments
 (0)