Skip to content

Commit

Permalink
Only construct the parse table once
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmior committed Feb 13, 2024
1 parent bf75ddf commit 0e20f3d
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions jsonpath_ng/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ def __init__(self, debug=False, lexer_class=None):
self.debug = debug
self.lexer_class = lexer_class or JsonPathLexer # Crufty but works around statefulness in PLY

def parse(self, string, lexer = None):
lexer = lexer or self.lexer_class()
return self.parse_token_stream(lexer.tokenize(string))

def parse_token_stream(self, token_iterator, start_symbol='jsonpath'):

# Since PLY has some crufty aspects and dumps files, we try to keep them local
# However, we need to derive the name of the output Python file :-/
output_directory = os.path.dirname(__file__)
Expand All @@ -47,19 +41,24 @@ def parse_token_stream(self, token_iterator, start_symbol='jsonpath'):
except:
module_name = __name__

start_symbol = 'jsonpath'
parsing_table_module = '_'.join([module_name, start_symbol, 'parsetab'])

# And we regenerate the parse table every time;
# it doesn't actually take that long!
new_parser = ply.yacc.yacc(module=self,
debug=self.debug,
tabmodule = parsing_table_module,
outputdir = output_directory,
write_tables=0,
start = start_symbol,
errorlog = logger)

return new_parser.parse(lexer = IteratorToTokenStream(token_iterator))
# Generate the parse table
self.parser = ply.yacc.yacc(module=self,
debug=self.debug,
tabmodule = parsing_table_module,
outputdir = output_directory,
write_tables=0,
start = start_symbol,
errorlog = logger)

def parse(self, string, lexer = None):
lexer = lexer or self.lexer_class()
return self.parse_token_stream(lexer.tokenize(string))

def parse_token_stream(self, token_iterator):
return self.parser.parse(lexer = IteratorToTokenStream(token_iterator))

# ===================== PLY Parser specification =====================

Expand Down

0 comments on commit 0e20f3d

Please sign in to comment.