Skip to content

Commit 025ba04

Browse files
committed
Only construct the parse table once
1 parent bf75ddf commit 025ba04

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

jsonpath_ng/parser.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ def __init__(self, debug=False, lexer_class=None):
3333
self.debug = debug
3434
self.lexer_class = lexer_class or JsonPathLexer # Crufty but works around statefulness in PLY
3535

36-
def parse(self, string, lexer = None):
37-
lexer = lexer or self.lexer_class()
38-
return self.parse_token_stream(lexer.tokenize(string))
39-
40-
def parse_token_stream(self, token_iterator, start_symbol='jsonpath'):
41-
4236
# Since PLY has some crufty aspects and dumps files, we try to keep them local
4337
# However, we need to derive the name of the output Python file :-/
4438
output_directory = os.path.dirname(__file__)
@@ -47,19 +41,24 @@ def parse_token_stream(self, token_iterator, start_symbol='jsonpath'):
4741
except:
4842
module_name = __name__
4943

44+
start_symbol = 'jsonpath'
5045
parsing_table_module = '_'.join([module_name, start_symbol, 'parsetab'])
5146

52-
# And we regenerate the parse table every time;
53-
# it doesn't actually take that long!
54-
new_parser = ply.yacc.yacc(module=self,
55-
debug=self.debug,
56-
tabmodule = parsing_table_module,
57-
outputdir = output_directory,
58-
write_tables=0,
59-
start = start_symbol,
60-
errorlog = logger)
61-
62-
return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
47+
# Generate the parse table
48+
self.parser = ply.yacc.yacc(module=self,
49+
debug=self.debug,
50+
tabmodule = parsing_table_module,
51+
outputdir = output_directory,
52+
write_tables=0,
53+
start = start_symbol,
54+
errorlog = logger)
55+
56+
def parse(self, string, lexer = None):
57+
lexer = lexer or self.lexer_class()
58+
return self.parse_token_stream(lexer.tokenize(string))
59+
60+
def parse_token_stream(self, token_iterator):
61+
return self.parser.parse(lexer = IteratorToTokenStream(token_iterator))
6362

6463
# ===================== PLY Parser specification =====================
6564

0 commit comments

Comments
 (0)