1
- from __future__ import print_function , absolute_import , division , generators , nested_scopes
1
+ from __future__ import (
2
+ print_function ,
3
+ absolute_import ,
4
+ division ,
5
+ generators ,
6
+ nested_scopes ,
7
+ )
2
8
import sys
3
9
import os .path
4
- import logging
5
10
6
11
import ply .yacc
7
12
13
+ from jsonpath_ng .exceptions import JsonPathParserError
8
14
from jsonpath_ng .jsonpath import *
9
15
from jsonpath_ng .lexer import JsonPathLexer
10
16
11
17
logger = logging .getLogger (__name__ )
12
18
19
+
13
20
def parse (string ):
14
21
return JsonPathParser ().parse (string )
15
22
23
+
16
24
class JsonPathParser (object ):
17
25
'''
18
26
An LALR-parser for JsonPath
@@ -21,8 +29,12 @@ class JsonPathParser(object):
21
29
tokens = JsonPathLexer .tokens
22
30
23
31
def __init__ (self , debug = False , lexer_class = None ):
24
- if self .__doc__ == None :
25
- raise Exception ('Docstrings have been removed! By design of PLY, jsonpath-rw requires docstrings. You must not use PYTHONOPTIMIZE=2 or python -OO.' )
32
+ if self .__doc__ is None :
33
+ raise JsonPathParserError (
34
+ 'Docstrings have been removed! By design of PLY, '
35
+ 'jsonpath-rw requires docstrings. You must not use '
36
+ 'PYTHONOPTIMIZE=2 or python -OO.'
37
+ )
26
38
27
39
self .debug = debug
28
40
self .lexer_class = lexer_class or JsonPathLexer # Crufty but works around statefulness in PLY
@@ -66,7 +78,8 @@ def parse_token_stream(self, token_iterator, start_symbol='jsonpath'):
66
78
]
67
79
68
80
def p_error (self , t ):
69
- raise Exception ('Parse error at %s:%s near token %s (%s)' % (t .lineno , t .col , t .value , t .type ))
81
+ raise JsonPathParserError ('Parse error at %s:%s near token %s (%s)'
82
+ % (t .lineno , t .col , t .value , t .type ))
70
83
71
84
def p_jsonpath_binop (self , p ):
72
85
"""jsonpath : jsonpath '.' jsonpath
@@ -98,7 +111,8 @@ def p_jsonpath_named_operator(self, p):
98
111
elif p [1 ] == 'parent' :
99
112
p [0 ] = Parent ()
100
113
else :
101
- raise Exception ('Unknown named operator `%s` at %s:%s' % (p [1 ], p .lineno (1 ), p .lexpos (1 )))
114
+ raise JsonPathParserError ('Unknown named operator `%s` at %s:%s'
115
+ % (p [1 ], p .lineno (1 ), p .lexpos (1 )))
102
116
103
117
def p_jsonpath_root (self , p ):
104
118
"jsonpath : '$'"
0 commit comments